OnboardingNavigator.swift 2.3 KB
Newer Older
domenicw's avatar
domenicw committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
//  OnboardingNavigator.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 18.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class OnboardingNavigator: Navigator {
    
    // MARK: - Variables
    
16
17
    let networkManager = NetworkManager<AMIVApiSession>()
    
domenicw's avatar
domenicw committed
18
19
    public var delegate: OnboardingNavigatorDelegate?
    
domenicw's avatar
domenicw committed
20
21
22
23
24
25
26
27
28
    public var rootViewController: UIViewController {
        return self.navigationController
    }
    
    private var navigationController: UINavigationController
    
    // MARK: - Initializers
    
    public init() {
domenicw's avatar
domenicw committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        let info = InfoViewController(model: .create())
        self.navigationController = UINavigationController(rootViewController: info)
        info.delegate = self
    }
    
    // MARK: - Navigation
    
    private func presentLogin() {
        let login = LoginViewController(model: .createOnboarding())
        login.delegate = self
        self.navigationController.present(login, animated: true, completion: nil)
    }
    
}

extension OnboardingNavigator: InfoViewControllerDelegate {
    
    public func buttonPressed() {
        self.presentLogin()
    }
    
    public func smallButtonPressed() {
        self.delegate?.onboardingFinished()
    }
    
}

extension OnboardingNavigator: LoginViewControllerDelegate {
    
58
    public func login(_ viewController: LoginViewController, username: String, password: String) {
domenicw's avatar
domenicw committed
59
        // TODO: - Check for valid credentials
domenicw's avatar
domenicw committed
60
        debugPrint("Logging in with username: \(username) and password: \(password)")
61
62
63
64
65
66
67
68
69
70
71
        
        
        self.networkManager.authenticate(username: username, password: password) { (response, error) in
            guard let response = response else {
                DispatchQueue.main.async {
                    viewController.loginFailed(with: "Incorrect Username or Password.\nPlease try again.")
                }
                return
            }
            
            // Save token into secure and encrypted keychain
72
            SessionManager.save(response)
73
74
75
76
77
            
            DispatchQueue.main.async {
                self.delegate?.onboardingFinished()
            }
        }
domenicw's avatar
domenicw committed
78
79
    }
    
80
    public func smallButtonTapped(_ viewController: LoginViewController) {
domenicw's avatar
domenicw committed
81
        self.delegate?.onboardingFinished()
domenicw's avatar
domenicw committed
82
83
84
    }
    
}