Skip to content

Commit

Permalink
Use preprocessor flags within switch cases
Browse files Browse the repository at this point in the history
  • Loading branch information
delba committed Aug 17, 2019
1 parent e48d20f commit 53042b7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 105 deletions.
114 changes: 34 additions & 80 deletions Source/Permission.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,64 +137,64 @@ open class Permission: NSObject {

/// The permission status.
open var status: PermissionStatus {
switch type {
#if PERMISSION_CONTACTS
if case .contacts = type { return statusContacts }
case .contacts: return statusContacts
#endif

#if PERMISSION_ADDRESS_BOOK
if case .addressBook = type { return statusAddressBook }
case .addressBook: return statusAddressBook
#endif

#if PERMISSION_LOCATION
if case .locationAlways = type { return statusLocationAlways }
if case .locationWhenInUse = type { return statusLocationWhenInUse }
case .locationAlways: return statusLocationAlways
case .locationWhenInUse: return statusLocationWhenInUse
#endif

#if PERMISSION_NOTIFICATIONS
if case .notifications = type { return statusNotifications }
case .notifications: return statusNotifications
#endif

#if PERMISSION_MICROPHONE
if case .microphone = type { return statusMicrophone }
case .microphone: return statusMicrophone
#endif

#if PERMISSION_CAMERA
if case .camera = type { return statusCamera }
case .camera: return statusCamera
#endif

#if PERMISSION_PHOTOS
if case .photos = type { return statusPhotos }
case .photos: return statusPhotos
#endif

#if PERMISSION_REMINDERS
if case .reminders = type { return statusReminders }
case .reminders: return statusReminders
#endif

#if PERMISSION_EVENTS
if case .events = type { return statusEvents }
case .events: return statusEvents
#endif

#if PERMISSION_BLUETOOTH
if case .bluetooth = type { return statusBluetooth }
case .bluetooth: return statusBluetooth
#endif

#if PERMISSION_MOTION
if case .motion = type { return statusMotion }
case .motion: return statusMotion
#endif

#if PERMISSION_SPEECH_RECOGNIZER
if case .speechRecognizer = type { return statusSpeechRecognizer }
case .speechRecognizer: return statusSpeechRecognizer
#endif

#if PERMISSION_MEDIA_LIBRARY
if case .mediaLibrary = type { return statusMediaLibrary }
case .mediaLibrary: return statusMediaLibrary
#endif

#if PERMISSION_SIRI
if case .siri = type { return statusSiri }
case .siri: return statusSiri
#endif

fatalError()
}
}

/// Determines whether to present the pre-permission alert.
Expand Down Expand Up @@ -259,110 +259,64 @@ open class Permission: NSObject {
}

internal func requestAuthorization(_ callback: @escaping Callback) {
switch type {
#if PERMISSION_CONTACTS
if case .contacts = type {
requestContacts(callback)
return
}
case .contacts: requestContacts(callback)
#endif

#if PERMISSION_ADDRESS_BOOK
if case .addressBook = type {
requestAddressBook(callback)
return
}
case .addressBook: requestAddressBook(callback)
#endif

#if PERMISSION_LOCATION
if case .locationAlways = type {
requestLocationAlways(callback)
return
}

if case .locationWhenInUse = type {
requestLocationWhenInUse(callback)
return
}
case .locationAlways: requestLocationAlways(callback)
case .locationWhenInUse: requestLocationWhenInUse(callback)
#endif

#if PERMISSION_NOTIFICATIONS
if case .notifications = type {
requestNotifications(callback)
return
}
case .notifications: requestNotifications(callback)
#endif

#if PERMISSION_MICROPHONE
if case .microphone = type {
requestMicrophone(callback)
return
}
case .microphone: requestMicrophone(callback)
#endif

#if PERMISSION_CAMERA
if case .camera = type {
requestCamera(callback)
return
}
case .camera: requestCamera(callback)
#endif

#if PERMISSION_PHOTOS
if case .photos = type {
requestPhotos(callback)
return
}
case .photos: requestPhotos(callback)
#endif

#if PERMISSION_REMINDERS
if case .reminders = type {
requestReminders(callback)
return
}
case .reminders: requestReminders(callback)
#endif

#if PERMISSION_EVENTS
if case .events = type {
requestEvents(callback)
return
}
case .events: requestEvents(callback)
#endif

#if PERMISSION_BLUETOOTH
if case .bluetooth = type {
requestBluetooth(self.callback)
return
}
case .bluetooth: requestBluetooth(self.callback)
#endif

#if PERMISSION_MOTION
if case .motion = type {
requestMotion(self.callback)
return
}
case .motion: requestMotion(self.callback)
#endif

#if PERMISSION_SPEECH_RECOGNIZER
if case .speechRecognizer = type {
requestSpeechRecognizer(callback)
return
}
case .speechRecognizer: requestSpeechRecognizer(callback)
#endif

#if PERMISSION_MEDIA_LIBRARY
if case .mediaLibrary = type {
requestMediaLibrary(callback)
return
}
case .mediaLibrary: requestMediaLibrary(callback)
#endif

#if PERMISSION_SIRI
if case .siri = type {
requestSiri(callback)
return
}
case .siri: requestSiri(callback)
#endif

fatalError()
}
}

internal func callbacks(_ with: PermissionStatus) {
Expand Down
34 changes: 17 additions & 17 deletions Source/PermissionType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,63 +83,63 @@ public enum PermissionType {

extension PermissionType: CustomStringConvertible {
public var description: String {
switch self {
#if PERMISSION_CONTACTS
if case .contacts = self { return "Contacts" }
case .contacts: return "Contacts"
#endif

#if PERMISSION_ADDRESS_BOOK
if case .addressBook = self { return "Address Book" }
case .addressBook: return "Address Book"
#endif

#if PERMISSION_LOCATION
if case .locationAlways = self { return "Location" }
if case .locationWhenInUse = self { return "Location" }
case .locationAlways: return "Location"
case .locationWhenInUse: return "Location"
#endif

#if PERMISSION_NOTIFICATIONS
if case .notifications = self { return "Notifications" }
case .notifications: return "Notifications"
#endif

#if PERMISSION_MICROPHONE
if case .microphone = self { return "Microphone" }
case .microphone: return "Microphone"
#endif

#if PERMISSION_CAMERA
if case .camera = self { return "Camera" }
case .camera: return "Camera"
#endif

#if PERMISSION_PHOTOS
if case .photos = self { return "Photos" }
case .photos: return "Photos"
#endif

#if PERMISSION_REMINDERS
if case .reminders = self { return "Reminders" }
case .reminders: return "Reminders"
#endif

#if PERMISSION_EVENTS
if case .events = self { return "Events" }
case .events: return "Events"
#endif

#if PERMISSION_BLUETOOTH
if case .bluetooth = self { return "Bluetooth" }
case .bluetooth: return "Bluetooth"
#endif

#if PERMISSION_MOTION
if case .motion = self { return "Motion" }
case .motion: return "Motion"
#endif

#if PERMISSION_SPEECH_RECOGNIZER
if case .speechRecognizer = self { return "Speech Recognizer" }
case .speechRecognizer: return "Speech Recognizer"
#endif

#if PERMISSION_SIRI
if case .siri = self { return "SiriKit" }
case .siri: return "SiriKit"
#endif

#if PERMISSION_MEDIA_LIBRARY
if case .mediaLibrary = self { return "Media Library" }
case .mediaLibrary: return "Media Library"
#endif

fatalError()
}
}
}
12 changes: 4 additions & 8 deletions Source/PermissionTypes/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ extension CLLocationManager {

requestedLocation = true

if case .locationAlways = permission.type {
requestAlwaysAuthorization()
return
}

if case .locationWhenInUse = permission.type {
requestWhenInUseAuthorization()
return
switch permission.type {
case .locationAlways: requestAlwaysAuthorization()
case .locationWhenInUse: requestWhenInUseAuthorization()
default: break
}
}
}
Expand Down

0 comments on commit 53042b7

Please sign in to comment.