Created
January 24, 2018 21:26
-
-
Save SeanROlszewski/a37b614471f8b474f2f6f28199d74d05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Coordinator { | |
func start() | |
} | |
class AppCoordinator: Coordinator { | |
let dataStore: DataStore | |
let childCoordinators: [Coordinator] | |
var userFinishedOnboarding: Bool = { | |
return dataStore.bool(forKey: "userHasAnsweredMicPermission") | |
&& dataStore.enteredData.count > 0 | |
} | |
func start() { | |
if userFinishedOnboarding { | |
// continue to main flow | |
} else { | |
let onboardingCoordinator = OnboardingCoordinator() | |
onboardingCoordinator.completionHandler = { | |
self.childCoordinators.dropLast() | |
self.start() | |
} | |
childCoordinators.append(onboardingCoordinator) | |
onboardingCoordinator.start() | |
} | |
} | |
} | |
class OnboardingCoordinator: Coordinator { | |
let childCoordinators: [Coordinator] | |
let completionHandler: ()->() | |
var userFinishedAnsweringPermissions: Bool = { | |
return dataStore.bool(forKey: "userHasAnsweredMicPermission") | |
} | |
func start() { | |
if userFinishedAnsweringPermissions { | |
let dataEntryCoordinator = DataEntryCoordinator() | |
childCoordinators.append(dataEntryCoordinator) | |
dataEntryCoordinator.completionHandler = { | |
childCoordinators.dropLast() | |
self.start() | |
} | |
dataEntryCoordinator.start() | |
} else { | |
let appPermissionsCoordinator = AppPermissionsCoordinator() | |
childCoordinators.append(appPermissionsCoordinator) | |
appPermissionsCoordinator.completionHandler = { | |
childCoordinators.dropLast() | |
completionHandler() | |
} | |
appPermissionsCoordinator.start() | |
} | |
} | |
} | |
class AppPermissionsCoordinator: Coordinator { | |
let completionHandler: ()->() | |
let rootViewController: UIViewController | |
let dataStore: DataStore | |
var userAnsweredCameraPermissions: Bool = { | |
return dataStore.bool(forKey: "userAnsweredCameraPermissions") | |
} | |
var userAnsweredPhotoAccessPermissions: Bool = { | |
return dataStore.bool(forKey: "userAnsweredPhotoAccessPermissions") | |
} | |
func start() { | |
var viewController: PermissionsViewController | |
if !userAnsweredCameraPermissions { | |
viewController = CameraPermissionsViewController() | |
} else if !userAnsweredPhotoAccessPermissions { | |
viewController = PhotoPermissionsViewController() | |
} else { | |
viewController = MicPermissionsViewController() | |
} | |
viewController.permissionsChangeHandler = permissionChanged | |
rootViewController.present(viewController, animated: true) | |
} | |
private func permissionChanged(forResource resource: Resource, to value: Bool) { | |
switch resource { | |
case .camera: | |
dataStore.setBool(forKey: "userAnsweredCameraPermissions", to: value) | |
start() | |
case .photos: | |
//etc. | |
start() | |
case .mic: | |
//etc. | |
rootViewController.popToRootViewController() | |
completionHandler() | |
} | |
} | |
} | |
class DataEntryCoordinator: Coordinator { | |
let completionHandler: ()->() | |
let rootViewController: UIViewController | |
let dataStore: DataStore | |
func start() { | |
var viewController = DataEntryViewController() | |
viewController.dataEntryCompletionHandler = { newDataEntry in | |
dataStore.addDataEntry(newDataEntry) | |
completionHandler() | |
} | |
rootViewController.present(viewController, animated: true) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment