EventsNavigator.swift 2.84 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
    
16
    public let networkManager = NetworkManager<AMIVApiEvents>()
domenicw's avatar
domenicw committed
17
    
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 events = EventsViewController(model: .empty())
domenicw's avatar
domenicw committed
28
        self.navigationController = UINavigationController(rootViewController: events)
domenicw's avatar
domenicw committed
29
        self.navigationController.navigationBar.tintColor = .amivRed
domenicw's avatar
domenicw committed
30
        events.delegate = self
domenicw's avatar
domenicw committed
31
        
domenicw's avatar
domenicw committed
32
        self.refreshData(events)
domenicw's avatar
domenicw committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    }
    
    // MARK: - Navigation
    
    private func goToEventDetailView(model: GenericInfoViewControllerModel) {
        let info = GenericInfoViewController(model: model)
        info.delegate = self
        self.navigationController.pushViewController(info, animated: true)
    }
    
}

extension EventsNavigator: EventsViewControllerDelegate {
    
domenicw's avatar
domenicw committed
47
    public func didSelectEvent(_ viewController: EventsViewController, event: AMIVEvent) {
domenicw's avatar
domenicw committed
48
        
domenicw's avatar
domenicw committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        if let eventImage = event.image {
            self.networkManager.getImage(for: eventImage.filePath) { (data, error) in
                let model: GenericInfoViewControllerModel
                if error == nil, let data = data {
                    let image = UIImage(data: data)
                    model = GenericInfoViewControllerModel(event: event, image: image)
                } else {
                    model = GenericInfoViewControllerModel(event: event, image: nil)
                }
                DispatchQueue.main.async {
                    self.goToEventDetailView(model: model)
                }
            }
        } else {
            let model = GenericInfoViewControllerModel(event: event, image: nil)
            self.goToEventDetailView(model: model)
        }
domenicw's avatar
domenicw committed
66
67
    }
    
domenicw's avatar
domenicw committed
68
    public func refreshData(_ viewController: EventsViewController) {
domenicw's avatar
domenicw committed
69
        self.networkManager.getEvents { (response, error) in
domenicw's avatar
domenicw committed
70
            guard let response = response else {
domenicw's avatar
domenicw committed
71
                DispatchQueue.main.async {
domenicw's avatar
domenicw committed
72
                    viewController.tableView.refreshControl?.endRefreshing()
domenicw's avatar
domenicw committed
73
                }
domenicw's avatar
domenicw committed
74
75
76
77
                return
            }
            DispatchQueue.main.async {
                viewController.model = EventViewModel(response: response)
domenicw's avatar
domenicw committed
78
79
            }
        }
domenicw's avatar
domenicw committed
80
81
82
83
84
85
    }
    
}

extension EventsNavigator: GenericInfoViewControllerDelegate {
    
domenicw's avatar
domenicw committed
86
    public func buttonTapped(_ viewController: GenericInfoViewController, action: GenericInfoViewControllerAction) {
domenicw's avatar
domenicw committed
87
88
89
        debugPrint("Info View button tapped")
        
        // TODO: - Sign up for event
90
91
92
    }
    
}