Saik0s
4/26/2017 - 8:48 PM

Example of Authentication Coordinator Object

Example of Authentication Coordinator Object

import UIKit

protocol AuthenticationCoordinatorDelegate:class {
    func coordinatorDidAuthenticate(coordinator:AuthenticationCoordinator)
}

class AuthenticationCoordinator:Coordinator {

    weak var delegate:AuthenticationCoordinatorDelegate?
    let navigationController:UINavigationController
    let loginViewController:LoginViewController

    init(navigationController:UINavigationController) {
        self.navigationController = navigationController
        self.loginViewController = LoginViewController()
    }

    deinit {
        print("deallocing \(self)")
    }

    func start() {
        showLoginViewController()
    }

    func showLoginViewController() {
        loginViewController.delegate = self
        navigationController.show(loginViewController, sender: self)
    }

    func showSignupViewController(){
        let signup = SignupViewController()
        signup.delegate = self
        navigationController.show(signup, sender: self)
    }

    func showPasswordViewController(){
        let password = PasswordViewController()
        password.delegate = self
        navigationController.show(password, sender: self)
    }
}

extension AuthenticationCoordinator : LoginViewControllerDelegate {

    func didSuccessfullyLogin() {
        print(navigationController.childViewControllers)
        delegate?.coordinatorDidAuthenticate(coordinator: self)
    }

    func didChooseSignup() {
        showSignupViewController()
    }
}

extension AuthenticationCoordinator : SignupViewControllerDelegate {
    func didCompleteSignup() {
        showPasswordViewController()
    }
}

extension AuthenticationCoordinator : PasswordViewControllerDelegate {
    func didSignupWithEmailAndPassword(email: String, passowrd: String) {
        delegate?.coordinatorDidAuthenticate(coordinator: self)
    }
}