EventsViewController.swift 3.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
//  EventsViewController.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 EventsViewController: UITableViewController {
    
    // MARK: - Variables
    
domenicw's avatar
domenicw committed
16
17
    public var delegate: EventsViewControllerDelegate?
    
domenicw's avatar
domenicw committed
18
    public var model: EventViewModel {
domenicw's avatar
domenicw committed
19
        didSet {
domenicw's avatar
domenicw committed
20
            self.updateView()
domenicw's avatar
domenicw committed
21
22
23
        }
    }
    
24
25
    // MARK: - Initializers
    
domenicw's avatar
domenicw committed
26
    public init(model: EventViewModel) {
domenicw's avatar
domenicw committed
27
        self.model = model
28
        super.init(style: .plain)
domenicw's avatar
domenicw committed
29
        self.title = model.title
30
31
32
33
34
35
36
37
38
39
40
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Setup
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
domenicw's avatar
domenicw committed
41
42
43
44
45
46
47
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(self.refreshData), for: .primaryActionTriggered)
        self.tableView.refreshControl = refreshControl
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
48
49
50
51
52
        
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationItem.largeTitleDisplayMode = .automatic
    }
    
domenicw's avatar
domenicw committed
53
54
55
56
57
58
    public override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        self.refreshControl?.endRefreshing()
    }
    
domenicw's avatar
domenicw committed
59
60
61
62
63
64
65
66
    private func updateView() {
        DispatchQueue.main.async {
            self.title = self.model.title
            self.tableView.refreshControl?.endRefreshing()
            self.tableView.reloadData()
        }
    }
    
67
68
    // MARK: - View Interaction
    
domenicw's avatar
domenicw committed
69
    @objc private func refreshData() {
domenicw's avatar
domenicw committed
70
        self.delegate?.refreshData(self)
domenicw's avatar
domenicw committed
71
72
    }
    
73
74
75
76
77
78
79
80
81
82
83
}

// MARK: - Table View Data Source

extension EventsViewController {
    
    public override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
domenicw's avatar
domenicw committed
84
        if let model = self.model.eventResponse {
domenicw's avatar
domenicw committed
85
86
87
            return model.events.count
        }
        return 0
88
89
90
    }
    
    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
domenicw's avatar
domenicw committed
91
92
93
94
95
96
97
        let cell: UITableViewCell = {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
                return UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
            }
            return cell
        }()
        
domenicw's avatar
domenicw committed
98
        guard let model = self.model.eventResponse else {
domenicw's avatar
domenicw committed
99
100
101
102
103
104
105
            return cell
        }
        
        cell.textLabel?.text = model.events[indexPath.row].title
        cell.detailTextLabel?.text = model.events[indexPath.row].catchPhrase
        
        return cell
106
107
108
    }
    
    public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
domenicw's avatar
domenicw committed
109
        return "Events"
110
111
112
    }
    
}
domenicw's avatar
domenicw committed
113
114
115
116
117
118

// MARK: - Table View Delegate

extension EventsViewController {
    
    public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
domenicw's avatar
domenicw committed
119
        self.delegate?.didSelectEvent(self, section: indexPath.section, index: indexPath.row)
domenicw's avatar
domenicw committed
120
121
122
123
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
}