EventsNavigator.swift 2.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
//  EventsNavigator.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 20.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class EventsNavigator: Navigator {
    
    // MARK: - Variables
    
domenicw's avatar
domenicw committed
16
17
    public let networkManager = NetworkManager()
    
18
19
20
21
22
23
24
25
26
    public var rootViewController: UIViewController {
        return self.navigationController
    }
    
    private var navigationController: UINavigationController
    
    // MARK: - Initializers
    
    public init() {
domenicw's avatar
domenicw committed
27
        let home = EventsViewController(model: nil)
28
        self.navigationController = UINavigationController(rootViewController: home)
domenicw's avatar
domenicw committed
29
30
        self.navigationController.navigationBar.tintColor = .amivRed
        home.delegate = self
domenicw's avatar
domenicw committed
31
32
33
34
35
36
37
38
39
40
        
        self.networkManager.getEvents { (response, error) in
            if let response = response {
                DispatchQueue.main.async {
                    if let top = self.navigationController.topViewController as? EventsViewController {
                        top.model = response
                    }
                }
            }
        }
domenicw's avatar
domenicw committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    }
    
    // MARK: - Navigation
    
    private func goToEventDetailView(model: GenericInfoViewControllerModel) {
        let info = GenericInfoViewController(model: model)
        info.delegate = self
        self.navigationController.pushViewController(info, animated: true)
    }
    
}

extension EventsNavigator: EventsViewControllerDelegate {
    
    public func didSelectEvent(section: Int, index: Int) {
        debugPrint("didSelect section: \(section) and index: \(index)")
        
        // TODO: - Retrieve event, convert into GenericInfoViewControllerModel and show Detail View
        // let model = ...
        self.goToEventDetailView(model: .createTestModel())
    }
    
    public func refreshData() {
        debugPrint("Refreshing Events Data")
domenicw's avatar
domenicw committed
65
66
67
68
69
70
71
72
73
74
75
        
        self.networkManager.getEvents { (response, error) in
            if let response = response {
                DispatchQueue.main.async {
                    if let top = self.navigationController.topViewController as? EventsViewController {
                        top.model = response
                        top.tableView.refreshControl?.endRefreshing()
                    }
                }
            }
        }
domenicw's avatar
domenicw committed
76
77
78
79
80
81
82
83
84
85
    }
    
}

extension EventsNavigator: GenericInfoViewControllerDelegate {
    
    public func buttonTapped() {
        debugPrint("Info View button tapped")
        
        // TODO: - Sign up for event
86
87
88
    }
    
}