EventsNavigator 2.swift 3.17 KB
Newer Older
domenicw's avatar
domenicw committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
//  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
    
    public let networkManager = NetworkManager<AMIVApiEvents>()
    
    public var rootViewController: UIViewController {
        return self.navigationController
    }
    
    private var navigationController: UINavigationController
    
    // MARK: - Initializers
    
    public init() {
        let events = EventsViewController(model: .empty())
        self.navigationController = UINavigationController(rootViewController: events)
        self.navigationController.navigationBar.tintColor = .amivRed
        events.delegate = self
        
        self.refreshData(events)
        
        self.networkManager.getEventSignups { (events, error) in
            guard let event = events?.first else {
                return
            }
            
            self.networkManager.signupTo(event, { (success, error) in
                debugPrint(success)
                debugPrint(error)
            })
        }
    }
    
    // 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(_ viewController: EventsViewController, event: AMIVEvent) {
        
        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)
        }
    }
    
    public func refreshData(_ viewController: EventsViewController) {
        self.networkManager.getEvents { (response, error) in
            guard let response = response else {
                DispatchQueue.main.async {
                    viewController.tableView.refreshControl?.endRefreshing()
                }
                return
            }
            DispatchQueue.main.async {
                viewController.model = EventViewModel(response: response)
            }
        }
    }
    
}

extension EventsNavigator: GenericInfoViewControllerDelegate {
    
    public func buttonTapped(_ viewController: GenericInfoViewController, action: GenericInfoViewControllerAction) {
        debugPrint("Info View button tapped")
        
        // TODO: - Sign up for event
    }
    
}