Created
April 26, 2017 20:46
-
-
Save dkw5877/8c9e0e0450c63c7ea779ed4c3c087003 to your computer and use it in GitHub Desktop.
Example of AppCoordinator Object
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
import UIKit | |
class Coordinator { } | |
class AppCoordinator { | |
fileprivate var isLoggedIn = false | |
fileprivate let navigationController:UINavigationController | |
fileprivate var childCoordinators = [Coordinator]() | |
init(with navigationController:UINavigationController) { | |
self.navigationController = navigationController | |
} | |
deinit { | |
print("deallocing \(self)") | |
} | |
func start() { | |
if isLoggedIn { | |
showProfile() | |
} else { | |
showAuthentication() | |
} | |
} | |
fileprivate func showProfile() { | |
let profileFlowCoordinator = ProfileFlowCoordinator(navigationController: navigationController) | |
profileFlowCoordinator.delegate = self | |
profileFlowCoordinator.start() | |
childCoordinators.append(profileFlowCoordinator) | |
} | |
fileprivate func showAuthentication() { | |
let authenticationCoordinator = AuthenticationCoordinator(navigationController: navigationController) | |
authenticationCoordinator.delegate = self | |
authenticationCoordinator.start() | |
childCoordinators.append(authenticationCoordinator) | |
} | |
} | |
extension AppCoordinator : AuthenticationCoordinatorDelegate { | |
func coordinatorDidAuthenticate(coordinator:AuthenticationCoordinator) { | |
removeCoordinator(coordinator: coordinator) | |
showProfile() | |
} | |
//we need a better way to find coordinators | |
fileprivate func removeCoordinator(coordinator:Coordinator) { | |
var idx:Int? | |
for (index,value) in childCoordinators.enumerated() { | |
if value === coordinator { | |
idx = index | |
break | |
} | |
} | |
if let index = idx { | |
childCoordinators.remove(at: index) | |
} | |
} | |
} | |
extension AppCoordinator : ProfileFlowCoordinatorDelegate { | |
//TODO: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment