summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/SettingsDataSource.swift
blob: f4a2267492acfc3e7a224340d6d93abf733d617d (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
//  SettingsDataSource.swift
//  MullvadVPN
//
//  Created by pronebird on 19/10/2021.
//  Copyright © 2021 Mullvad VPN AB. All rights reserved.
//

import UIKit

class SettingsDataSource: NSObject, AccountObserver, UITableViewDataSource, UITableViewDelegate {
    private enum CellReuseIdentifiers: String, CaseIterable {
        case accountCell
        case basicCell

        var reusableViewClass: AnyClass {
            switch self {
            case .accountCell:
                return SettingsAccountCell.self
            case .basicCell:
                return SettingsCell.self
            }
        }
    }

    private enum HeaderFooterReuseIdentifier: String, CaseIterable {
        case spacer

        var reusableViewClass: AnyClass {
            switch self {
            case .spacer:
                return EmptyTableViewHeaderFooterView.self
            }
        }
    }

    enum Section: String {
        case main
        case version
        case problemReport
    }

    enum Item: String {
        case account
        case preferences
        case wireguardKey
        case version
        case problemReport
    }

    private var snapshot = DataSourceSnapshot<Section, Item>()

    weak var delegate: SettingsDataSourceDelegate?

    weak var tableView: UITableView? {
        didSet {
            tableView?.delegate = self
            tableView?.dataSource = self

            registerClasses()
        }
    }

    override init() {
        super.init()

        Account.shared.addObserver(self)
        updateDataSnapshot()
    }

    private func registerClasses() {
        CellReuseIdentifiers.allCases.forEach { cellIdentifier in
            tableView?.register(cellIdentifier.reusableViewClass, forCellReuseIdentifier: cellIdentifier.rawValue)
        }

        HeaderFooterReuseIdentifier.allCases.forEach { reuseIdentifier in
            tableView?.register(reuseIdentifier.reusableViewClass, forHeaderFooterViewReuseIdentifier: reuseIdentifier.rawValue)
        }
    }

    private func updateDataSnapshot() {
        var newSnapshot = DataSourceSnapshot<Section, Item>()

        if Account.shared.isLoggedIn {
            newSnapshot.appendSections([.main])
            newSnapshot.appendItems([.account, .preferences, .wireguardKey], in: .main)
        }

        newSnapshot.appendSections([.version, .problemReport])
        newSnapshot.appendItems([.version], in: .version)
        newSnapshot.appendItems([.problemReport], in: .problemReport)

        snapshot = newSnapshot
    }

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let sectionIdentifier = snapshot.section(at: section)!

        return snapshot.numberOfItems(in: sectionIdentifier) ?? 0
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return snapshot.numberOfSections()
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = snapshot.itemForIndexPath(indexPath)!

        switch item {
        case .account:
            let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.accountCell.rawValue, for: indexPath) as! SettingsAccountCell
            cell.titleLabel.text = NSLocalizedString("ACCOUNT_CELL_LABEL", tableName: "Settings", value: "Account", comment: "")
            cell.accountExpiryDate = Account.shared.expiry
            cell.accessibilityIdentifier = "AccountCell"
            cell.setCustomDisclosureIndicator()

            return cell

        case .preferences:
            let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.basicCell.rawValue, for: indexPath) as! SettingsCell
            cell.titleLabel.text = NSLocalizedString("PREFERENCES_CELL_LABEL", tableName: "Settings", value: "Preferences", comment: "")
            cell.detailTitleLabel.text = nil
            cell.accessibilityIdentifier = nil
            cell.setCustomDisclosureIndicator()

            return cell

        case .wireguardKey:
            let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.basicCell.rawValue, for: indexPath) as! SettingsCell
            cell.titleLabel.text = NSLocalizedString("WIREGUARD_KEY_CELL_LABEL", tableName: "Settings", value: "WireGuard key", comment: "")
            cell.detailTitleLabel.text = nil
            cell.accessibilityIdentifier = "WireGuardKeyCell"
            cell.setCustomDisclosureIndicator()

            return cell

        case .version:
            let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.basicCell.rawValue, for: indexPath) as! SettingsCell
            cell.titleLabel.text = NSLocalizedString("APP_VERSION_CELL_LABEL", tableName: "Settings", value: "App version", comment: "")
            cell.detailTitleLabel.text = Bundle.main.productVersion
            cell.accessibilityIdentifier = nil
            cell.unsetCustomDisclosureIndicator()

            return cell

        case .problemReport:
            let cell = tableView.dequeueReusableCell(withIdentifier: CellReuseIdentifiers.basicCell.rawValue, for: indexPath) as! SettingsCell
            cell.titleLabel.text = NSLocalizedString("REPORT_PROBLEM_CELL_LABEL", tableName: "Settings", value: "Report a problem", comment: "")
            cell.detailTitleLabel.text = nil
            cell.accessibilityIdentifier = nil
            cell.setCustomDisclosureIndicator()

            return cell
        }
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        if case .version = snapshot.itemForIndexPath(indexPath) {
            return false
        } else {
            return true
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let item = snapshot.itemForIndexPath(indexPath) else { return }

        delegate?.settingsDataSource(self, didSelectItem: item)
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return tableView.dequeueReusableHeaderFooterView(withIdentifier: HeaderFooterReuseIdentifier.spacer.rawValue)
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return UIMetrics.sectionSpacing
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }

    // MARK: - AccountObserver

    func account(_ account: Account, didUpdateExpiry expiry: Date) {
        tableView?.performBatchUpdates {
            if let indexPath = snapshot.indexPathForItem(.version) {
                tableView?.reloadRows(at: [indexPath], with: .none)
            }
        }
    }

    func account(_ account: Account, didLoginWithToken token: String, expiry: Date) {
        updateDataSnapshot()
        tableView?.reloadData()
    }

    func accountDidLogout(_ account: Account) {
        updateDataSnapshot()
        tableView?.reloadData()
    }
}