AmivMicroAppsViewController.swift 2.45 KB
Newer Older
domenicw's avatar
domenicw committed
1
//
domenicw's avatar
domenicw committed
2
//  AmivMicroAppsViewController.swift
domenicw's avatar
domenicw committed
3
4
5
6
7
8
9
10
11
//  Amiv
//
//  Created by Domenic Wüthrich on 18.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

domenicw's avatar
domenicw committed
12
public class AmivMicroAppsViewController: UITableViewController {
domenicw's avatar
domenicw committed
13
14
15
16
17
    
    // MARK: - Variables
    
    fileprivate let reuseIdentifer = "amivApplicationCell"
    
domenicw's avatar
domenicw committed
18
    public var delegate: AmivMicroAppsViewControllerDelegate?
domenicw's avatar
domenicw committed
19
    
domenicw's avatar
domenicw committed
20
    public var model: [AmivMicroAppModel]
domenicw's avatar
domenicw committed
21
22
23
    
    // MARK: - Initializers
    
domenicw's avatar
domenicw committed
24
    public init(model: [AmivMicroAppModel]) {
domenicw's avatar
domenicw committed
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
        self.model = model
        
        super.init(style: .plain)
        
        self.title = "Apps"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Setup
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.separatorStyle = .none
        self.view.backgroundColor = .white
        self.navigationItem.largeTitleDisplayMode = .always
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.prefersLargeTitles = true
    }
    
    // MARK: - View Interaction
    
}

// MARK: - Collection View Data Source Extension

domenicw's avatar
domenicw committed
58
extension AmivMicroAppsViewController {
domenicw's avatar
domenicw committed
59
60
61
62
63
64
65
66
67
68
    
    public override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.model.count
    }
    
    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
domenicw's avatar
domenicw committed
69
70
71
        let cell: AmivMicroAppsCell = {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseIdentifer) as? AmivMicroAppsCell else {
                return AmivMicroAppsCell(reuseIdentifier: self.reuseIdentifer)
domenicw's avatar
domenicw committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
            }
            return cell
        }()
        
        let model = self.model[indexPath.row]
        cell.prepareCell(model: model)
        
        return cell
    }
    
}

// MARK: - Collection View Delegate

domenicw's avatar
domenicw committed
86
extension AmivMicroAppsViewController {
domenicw's avatar
domenicw committed
87
88
89
90
91
92
93
    
    public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.delegate?.start(app: self.model[indexPath.row].app)
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
}