Skip to content
This repository was archived by the owner on Feb 27, 2019. It is now read-only.

Commit cf7648d

Browse files
author
bre7
committed
Refactored switch inside viewWillLayoutSubviews
1 parent b270f3a commit cf7648d

File tree

1 file changed

+45
-61
lines changed

1 file changed

+45
-61
lines changed

PermissionScope/PermissionScope.swift

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public enum PermissionType: String {
2020
case Microphone = "Microphone"
2121
case Camera = "Camera"
2222
case Photos = "Photos"
23+
24+
var prettyName: String {
25+
switch self {
26+
case .LocationAlways, .LocationInUse:
27+
return "Location"
28+
default:
29+
return self.rawValue
30+
}
31+
}
2332
}
2433

2534
public enum PermissionStatus: String {
@@ -208,66 +217,19 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
208217
button.frame.offset(dx: -contentView.frame.origin.x, dy: -contentView.frame.origin.y)
209218
button.frame.offset(dx: 0, dy: -80 + CGFloat(index * baseOffset))
210219

211-
// TODO: New func to setUnauthorizedStyle ? new tintColor also ?
212-
// Question: Use (XXX, YYY) tuple instead of case XXX: if status() = YYY ? => Each Permission should know how to get it's status
213220
let type = configuredPermissions[index].type
214-
switch type {
215-
case .LocationAlways:
216-
if statusLocationAlways() == .Authorized {
217-
setButtonAuthorizedStyle(button)
218-
button.setTitle("Got Location".localized.uppercaseString, forState: UIControlState.Normal)
219-
} else if statusNotifications() == .Unauthorized {
220-
setButtonUnauthorizedStyle(button)
221-
button.setTitle("Denied Location".localized.uppercaseString, forState: UIControlState.Normal)
222-
}
223-
case .LocationInUse:
224-
if statusLocationInUse() == .Authorized {
225-
setButtonAuthorizedStyle(button)
226-
button.setTitle("Got Location".localized.uppercaseString, forState: UIControlState.Normal)
227-
} else if statusLocationInUse() == .Unauthorized {
228-
setButtonUnauthorizedStyle(button)
229-
button.setTitle("Denied Location".localized.uppercaseString, forState: UIControlState.Normal)
230-
}
231-
case .Contacts:
232-
if statusContacts() == .Authorized {
233-
setButtonAuthorizedStyle(button)
234-
button.setTitle("Allowed Contacts".localized.uppercaseString, forState: UIControlState.Normal)
235-
} else if statusContacts() == .Unauthorized {
236-
setButtonUnauthorizedStyle(button)
237-
button.setTitle("Denied \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
238-
}
239-
case .Notifications:
240-
if statusNotifications() == .Authorized {
241-
setButtonAuthorizedStyle(button)
242-
button.setTitle("Allowed Notifications".localized.uppercaseString, forState: UIControlState.Normal)
243-
} else if statusNotifications() == .Unauthorized {
244-
setButtonUnauthorizedStyle(button)
245-
button.setTitle("Denied \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
246-
}
247-
case .Microphone:
248-
if statusMicrophone() == .Authorized {
249-
setButtonAuthorizedStyle(button)
250-
button.setTitle("Allowed \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
251-
} else if statusMicrophone() == .Unauthorized {
252-
setButtonUnauthorizedStyle(button)
253-
button.setTitle("Denied \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
254-
}
255-
case .Camera:
256-
if statusCamera() == .Authorized {
257-
setButtonAuthorizedStyle(button)
258-
button.setTitle("Allowed \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
259-
} else if statusCamera() == .Unauthorized {
260-
setButtonUnauthorizedStyle(button)
261-
button.setTitle("Denied \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
262-
}
263-
case .Photos:
264-
if statusPhotos() == .Authorized {
265-
setButtonAuthorizedStyle(button)
266-
button.setTitle("Allowed \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
267-
} else if statusPhotos() == .Unauthorized {
268-
setButtonUnauthorizedStyle(button)
269-
button.setTitle("Denied \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
270-
}
221+
222+
let currentStatus = statusForPermission(type)
223+
let prettyName = type.prettyName
224+
if currentStatus == .Authorized {
225+
setButtonAuthorizedStyle(button)
226+
button.setTitle("Allowed \(prettyName)".localized.uppercaseString, forState: .Normal)
227+
} else if currentStatus == .Unauthorized {
228+
setButtonUnauthorizedStyle(button)
229+
button.setTitle("Denied \(prettyName)".localized.uppercaseString, forState: .Normal)
230+
} else if currentStatus == .Disabled {
231+
// setButtonDisabledStyle(button)
232+
button.setTitle("\(prettyName) Disabled".localized.uppercaseString, forState: .Normal)
271233
}
272234

273235
let label = permissionLabels[index]
@@ -304,7 +266,7 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
304266
// this is a bit of a mess, eh?
305267
switch type {
306268
case .LocationAlways, .LocationInUse:
307-
button.setTitle("Enable Location".localized.uppercaseString, forState: UIControlState.Normal)
269+
button.setTitle("Enable \(type.prettyName)".localized.uppercaseString, forState: UIControlState.Normal)
308270
default:
309271
button.setTitle("Allow \(type.rawValue)".localized.uppercaseString, forState: UIControlState.Normal)
310272
}
@@ -345,7 +307,7 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
345307
if !CLLocationManager.locationServicesEnabled() {
346308
return .Disabled
347309
}
348-
310+
349311
let status = CLLocationManager.authorizationStatus()
350312
switch status {
351313
case .AuthorizedAlways:
@@ -732,4 +694,26 @@ public class PermissionScope: UIViewController, CLLocationManagerDelegate, UIGes
732694

733695
detectAndCallback()
734696
}
697+
698+
// MARK: Helpers
699+
700+
func statusForPermission(type: PermissionType) -> PermissionStatus {
701+
// :(
702+
switch type {
703+
case .LocationAlways:
704+
return statusLocationAlways()
705+
case .LocationInUse:
706+
return statusLocationInUse()
707+
case .Contacts:
708+
return statusContacts()
709+
case .Notifications:
710+
return statusNotifications()
711+
case .Microphone:
712+
return statusMicrophone()
713+
case .Camera:
714+
return statusCamera()
715+
case .Photos:
716+
return statusPhotos()
717+
}
718+
}
735719
}

0 commit comments

Comments
 (0)