summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/StaticTableViewDataSource.swift
blob: 36cef2adaa67e5c957f7597d53e77bf680b93c9a (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
//
//  StaticTableViewDataSource.swift
//  MullvadVPN
//
//  Created by pronebird on 24/05/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import UIKit

class StaticTableViewRow {
    typealias ConfigurationBlock = (IndexPath, UITableViewCell) -> Void
    typealias ActionBlock = (IndexPath) -> Void

    let reuseIdentifier: String
    let configurationBlock: ConfigurationBlock

    var isSelectable = true
    var isHidden = false
    var actionBlock: ActionBlock?

    init(reuseIdentifier: String, configurationBlock: @escaping ConfigurationBlock) {
        self.reuseIdentifier = reuseIdentifier
        self.configurationBlock = configurationBlock
    }
}

class StaticTableViewSection {
    private(set) var rows = [StaticTableViewRow]()

    var isHidden: Bool {
        return rows.allSatisfy({ $0.isHidden })
    }

    func addRows(_ rows: [StaticTableViewRow]) {
        self.rows.append(contentsOf: rows)
    }
}

class StaticTableViewDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView?

    private(set) var sections = [StaticTableViewSection]()

    func addSections(_ sections: [StaticTableViewSection]) {
        self.sections.append(contentsOf: sections)
    }

    func reloadRows(_ rows: [StaticTableViewRow], with animation: UITableView.RowAnimation) {
        let indexPaths = rows.compactMap { indexPathForRow($0) }

        tableView?.reloadRows(at: indexPaths, with: animation)
    }

    func indexPathForRow(_ searchRow: StaticTableViewRow) -> IndexPath? {
        var sectionIndex = 0

        for section in sections {
            let visibleRows = section.rows.filter { !$0.isHidden }

            // skip incrementing the section index since invisible sections are normally collapsed
            guard visibleRows.count > 0 else {
                continue
            }

            if let rowIndex = visibleRows.firstIndex(where: { $0 === searchRow }) {
                return IndexPath(row: rowIndex, section: sectionIndex)
            }

            sectionIndex += 1
        }

        return nil
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        let row = self.row(for: indexPath)

        return row.isSelectable
    }

    // MARK: - UITableViewDataSource

    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.reduce(0, { $1.isHidden ? $0 : $0 + 1 })
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].rows.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let row = self.row(for: indexPath)
        let reuseIdentifier = row.reuseIdentifier

        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)

        row.configurationBlock(indexPath, cell)

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let row = self.row(for: indexPath)

        row.actionBlock?(indexPath)
    }

    // MARK: - Private

    private func row(for indexPath: IndexPath) -> StaticTableViewRow {
        let section = self.section(for: indexPath)
        let row = section.rows.compactMap({ $0.isHidden ? nil : $0 })

        return row[indexPath.row]
    }

    private func section(for indexPath: IndexPath) -> StaticTableViewSection {
        let visibleSections = sections.compactMap({ $0.isHidden ? nil : $0 })

        return visibleSections[indexPath.section]
    }

}