EventsViewController.swift 3.09 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
19
20
21
22
23
    public var model: EventsResponse? {
        didSet {
            self.tableView.reloadData()
        }
    }
    
24
25
    // MARK: - Initializers
    
domenicw's avatar
domenicw committed
26
27
    public init(model: EventsResponse?) {
        self.model = model
28
29
30
31
32
33
34
35
36
37
38
39
40
        super.init(style: .plain)
        self.title = "Events"
    }
    
    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()
    }
    
59
60
    // MARK: - View Interaction
    
domenicw's avatar
domenicw committed
61
62
63
64
    @objc private func refreshData() {
        self.delegate?.refreshData()
    }
    
65
66
67
68
69
70
71
72
73
74
75
}

// 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
76
77
78
79
        if let model = self.model {
            return model.events.count
        }
        return 0
80
81
82
    }
    
    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
domenicw's avatar
domenicw committed
83
84
85
86
87
88
89
90
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
        }()
        
        guard let model = self.model else {
            return cell
        }
        
        cell.textLabel?.text = model.events[indexPath.row].title
        cell.detailTextLabel?.text = model.events[indexPath.row].catchPhrase
        
        return cell
98
99
100
    }
    
    public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
domenicw's avatar
domenicw committed
101
        return "Events"
102
103
104
    }
    
}
domenicw's avatar
domenicw committed
105
106
107
108
109
110
111
112
113
114
115

// MARK: - Table View Delegate

extension EventsViewController {
    
    public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.delegate?.didSelectEvent(section: indexPath.section, index: indexPath.row)
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
}