SettingsViewController.swift 3.58 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
104
105
106
107
108
109
110
111
112
//
//  SettingsViewController.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 18.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class SettingsViewController: UITableViewController {
    
    // MARK: - View Variables
    
    // MARK: - Variables
    
    private var model: SettingsModel
    
    public var delegate: SettingsViewControllerDelegate?
    
    fileprivate var cellIdentifier = "cell"
    
    // MARK: - Initializers
    
    public init(model: SettingsModel) {
        self.model = model
        super.init(style: .grouped)
        
        self.title = "Settings"
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Creation
    
    // MARK: - View Setup
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationItem.largeTitleDisplayMode = .always
    }
}

// MARK: - SettingsViewController TableView Extension

extension SettingsViewController {
    
    public override func numberOfSections(in tableView: UITableView) -> Int {
        return self.model.sectionModels.count
    }
    
    public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.model.sectionModels[section].cellModels.count
    }
    
    public override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.model.sectionModels[section].headerText
    }
    
    public override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        return self.model.sectionModels[section].footerText
    }
    
    public override func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
        if let footer = view as? UITableViewHeaderFooterView {
            footer.textLabel?.textAlignment = self.model.sectionModels[section].footerTextAlignment
        }
    }
    
    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellModel = self.model.sectionModels[indexPath.section].cellModels[indexPath.row]
        
        let cell: UITableViewCell = {
            guard let cell = tableView.dequeueReusableCell(withIdentifier: cellModel.cellType.reuseIdentifier()) else {
                return UITableViewCell(style: cellModel.cellType.style(), reuseIdentifier: cellModel.cellType.reuseIdentifier())
            }
            return cell
        }()
        
        cell.textLabel?.text = cellModel.text
        cell.detailTextLabel?.text = cellModel.detailText
        cell.textLabel?.numberOfLines = 0
        cell.accessoryType = cellModel.action.indicator()
        cell.tintColor = UIColor(named: "lightBlue")
        cell.selectionStyle = cellModel.action.selectionStyle()
        
        return cell
    }
    
    public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cellModel = self.model.sectionModels[indexPath.section].cellModels[indexPath.row]
        
        let cell = tableView.cellForRow(at: indexPath)
        cell?.isSelected = false
        
        switch cellModel.action {
        case .none:
            break
        case .toggleLogin:
            self.delegate?.toggleLogin()
        case .changeValue(let value):
            self.delegate?.changeValue(for: value)
        }
    }
    
}