-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathListCustomListViewController.swift
152 lines (124 loc) · 4.7 KB
/
ListCustomListViewController.swift
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
//
// ListCustomListViewController.swift
// MullvadVPN
//
// Created by Jon Petersson on 2024-03-06.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import UIKit
private enum SectionIdentifier: Hashable {
case `default`
}
private struct ItemIdentifier: Hashable {
var id: UUID
}
private enum CellReuseIdentifier: String, CaseIterable, CellIdentifierProtocol {
case `default`
var cellClass: AnyClass {
switch self {
default: BasicCell.self
}
}
}
class ListCustomListViewController: UIViewController {
private typealias DataSource = UITableViewDiffableDataSource<SectionIdentifier, ItemIdentifier>
private let interactor: CustomListInteractorProtocol
private var dataSource: DataSource?
private var fetchedItems: [CustomList] = []
private var tableView = UITableView(frame: .zero, style: .plain)
var didSelectItem: ((CustomList) -> Void)?
var didFinish: (() -> Void)?
init(interactor: CustomListInteractorProtocol) {
self.interactor = interactor
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondaryColor
view.accessibilityIdentifier = .listCustomListsView
addSubviews()
configureNavigationItem()
configureDataSource()
configureTableView()
}
func updateDataSource(reloadExisting: Bool, animated: Bool = true) {
fetchedItems = interactor.fetchAll()
var snapshot = NSDiffableDataSourceSnapshot<SectionIdentifier, ItemIdentifier>()
snapshot.appendSections([.default])
let itemIdentifiers = fetchedItems.map { ItemIdentifier(id: $0.id) }
snapshot.appendItems(itemIdentifiers, toSection: .default)
if reloadExisting {
for item in fetchedItems {
snapshot.reconfigureOrReloadItems([ItemIdentifier(id: item.id)])
}
}
dataSource?.apply(snapshot, animatingDifferences: animated)
}
private func addSubviews() {
view.addConstrainedSubviews([tableView]) {
tableView.pinEdgesToSuperview()
}
}
private func configureTableView() {
tableView.delegate = self
tableView.backgroundColor = .secondaryColor
tableView.separatorColor = .secondaryColor
tableView.separatorInset = .zero
tableView.separatorStyle = .singleLine
tableView.rowHeight = UIMetrics.SettingsCell.customListsCellHeight
tableView.registerReusableViews(from: CellReuseIdentifier.self)
tableView.accessibilityIdentifier = .listCustomListsTableView
}
private func configureNavigationItem() {
navigationItem.title = NSLocalizedString(
"LIST_CUSTOM_LIST_NAVIGATION_TITLE",
tableName: "CustomList",
value: "Edit custom list",
comment: ""
)
navigationItem.rightBarButtonItem = UIBarButtonItem(
systemItem: .done,
primaryAction: UIAction(handler: { [weak self] _ in
self?.didFinish?()
})
)
navigationItem.rightBarButtonItem?.accessibilityIdentifier = .listCustomListDoneButton
}
private func configureDataSource() {
dataSource = DataSource(
tableView: tableView,
cellProvider: { [weak self] _, indexPath, itemIdentifier in
self?.dequeueCell(at: indexPath, itemIdentifier: itemIdentifier)
}
)
updateDataSource(reloadExisting: false, animated: false)
}
private func dequeueCell(
at indexPath: IndexPath,
itemIdentifier: ItemIdentifier
) -> UITableViewCell {
let cell = tableView.dequeueReusableView(withIdentifier: CellReuseIdentifier.default, for: indexPath)
let item = fetchedItems[indexPath.row]
var contentConfiguration = ListCellContentConfiguration()
contentConfiguration.text = item.name
cell.contentConfiguration = contentConfiguration
if let cell = cell as? DynamicBackgroundConfiguration {
cell.setAutoAdaptingBackgroundConfiguration(.mullvadListPlainCell(), selectionType: .dimmed)
}
if let cell = cell as? CustomCellDisclosureHandling {
cell.disclosureType = .chevron
}
return cell
}
}
extension ListCustomListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
let item = fetchedItems[indexPath.row]
didSelectItem?(item)
}
}