-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathLocationDataSource.swift
368 lines (302 loc) · 13.3 KB
/
LocationDataSource.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// LocationDataSource.swift
// MullvadVPN
//
// Created by pronebird on 11/03/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import Combine
import MullvadREST
import MullvadSettings
import MullvadTypes
import UIKit
final class LocationDataSource:
UITableViewDiffableDataSource<LocationSection, LocationCellViewModel>,
LocationDiffableDataSourceProtocol {
private var currentSearchString = ""
private var dataSources: [LocationDataSourceProtocol] = []
// The selected location.
private var selectedLocation: LocationCellViewModel?
// When multihop is enabled, this is the "inverted" selected location, ie. entry
// if in exit mode and exit if in entry mode.
private var excludedLocation: LocationCellViewModel?
let tableView: UITableView
let sections: [LocationSection]
var didSelectRelayLocations: ((UserSelectedRelays) -> Void)?
var didTapEditCustomLists: (() -> Void)?
init(
tableView: UITableView,
allLocations: LocationDataSourceProtocol,
customLists: LocationDataSourceProtocol
) {
self.tableView = tableView
let sections: [LocationSection] = LocationSection.allCases
self.sections = sections
self.dataSources.append(contentsOf: [customLists, allLocations])
super.init(tableView: tableView) { _, indexPath, itemIdentifier in
let cell = tableView.dequeueReusableView(
withIdentifier: sections[indexPath.section],
for: indexPath
) as! LocationCell // swiftlint:disable:this force_cast
cell.configure(item: itemIdentifier, behavior: .select)
return cell
}
tableView.delegate = self
tableView.registerReusableViews(from: LocationSection.self)
defaultRowAnimation = .fade
}
func setRelays(_ cachedRelays: LocationRelays, selectedRelays: RelaySelection) {
let allLocationsDataSource =
dataSources.first(where: { $0 is AllLocationDataSource }) as? AllLocationDataSource
let customListsDataSource =
dataSources.first(where: { $0 is CustomListsDataSource }) as? CustomListsDataSource
allLocationsDataSource?.reload(cachedRelays)
customListsDataSource?.reload(allLocationNodes: allLocationsDataSource?.nodes ?? [])
setSelectedRelays(selectedRelays)
filterRelays(by: currentSearchString)
}
func filterRelays(by searchString: String) {
currentSearchString = searchString
let list = sections.enumerated().map { index, section in
dataSources[index]
.search(by: searchString)
.flatMap { node in
let rootNode = RootLocationNode(children: [node])
return recursivelyCreateCellViewModelTree(for: rootNode, in: section, indentationLevel: 0)
}
}
updateDataSnapshot(with: list, reloadExisting: !searchString.isEmpty) {
self.tableView.reloadData()
if searchString.isEmpty {
self.updateSelection(self.selectedLocation, animated: false, completion: {
self.scrollToSelectedRelay()
})
} else {
self.scrollToTop(animated: false)
}
}
}
/// Refreshes the custom list section and keeps all modifications intact (selection and expanded states).
func refreshCustomLists(selectedRelays: RelaySelection) {
guard let allLocationsDataSource =
dataSources.first(where: { $0 is AllLocationDataSource }) as? AllLocationDataSource,
let customListsDataSource =
dataSources.first(where: { $0 is CustomListsDataSource }) as? CustomListsDataSource
else {
return
}
// Take a "snapshot" of the currently expanded nodes.
let expandedNodes = customListsDataSource.nodes
.flatMap { [$0] + $0.flattened }
.filter { $0.showsChildren }
// Reload data source with (possibly) updated custom lists.
customListsDataSource.reload(allLocationNodes: allLocationsDataSource.nodes)
// Reapply current selection.
setSelectedRelays(selectedRelays)
// Reapply current search filter.
let searchResultNodes = dataSources[0].search(by: currentSearchString)
// Reapply expanded status and override nodes being hidden by search filter.
RootLocationNode(children: searchResultNodes).forEachDescendant { node in
node.showsChildren = expandedNodes.contains(node)
node.isHiddenFromSearch = false
}
// Construct node tree.
let list = searchResultNodes.flatMap { node in
let rootNode = RootLocationNode(children: [node])
return recursivelyCreateCellViewModelTree(for: rootNode, in: .customLists, indentationLevel: 0)
}
updateDataSnapshot(with: [
list,
snapshot().itemIdentifiers(inSection: .allLocations),
], reloadExisting: true)
}
func setSelectedRelays(_ selectedRelays: RelaySelection) {
selectedLocation = mapSelection(from: selectedRelays.selected)
excludedLocation = mapSelection(from: selectedRelays.excluded)
excludedLocation?.excludedRelayTitle = selectedRelays.excludedTitle
tableView.reloadData()
}
func scrollToSelectedRelay() {
indexPathForSelectedRelay().flatMap {
tableView.scrollToRow(at: $0, at: .middle, animated: false)
}
}
private func indexPathForSelectedRelay() -> IndexPath? {
selectedLocation.flatMap { indexPath(for: $0) }
}
private func mapSelection(from selectedRelays: UserSelectedRelays?) -> LocationCellViewModel? {
let allLocationsDataSource =
dataSources.first(where: { $0 is AllLocationDataSource }) as? AllLocationDataSource
let customListsDataSource =
dataSources.first(where: { $0 is CustomListsDataSource }) as? CustomListsDataSource
if let selectedRelays {
// Look for a matching custom list node.
if let customListSelection = selectedRelays.customListSelection,
let customList = customListsDataSource?.customList(by: customListSelection.listId),
let selectedNode = customListsDataSource?.node(by: selectedRelays, for: customList) {
return LocationCellViewModel(
section: .customLists,
node: selectedNode,
indentationLevel: selectedNode.hierarchyLevel
)
// Look for a matching all locations node.
} else if let location = selectedRelays.locations.first,
let selectedNode = allLocationsDataSource?.node(by: location) {
return LocationCellViewModel(
section: .allLocations,
node: selectedNode,
indentationLevel: selectedNode.hierarchyLevel
)
}
}
return nil
}
private func updateSelection(_ item: LocationCellViewModel?, animated: Bool, completion: (() -> Void)? = nil) {
selectedLocation = item
guard let selectedLocation else { return }
let rootNode = selectedLocation.node.root
// Exit early if no changes to the node tree should be made.
guard selectedLocation.node != rootNode else {
completion?()
return
}
// Make sure we have an index path for the selected item.
guard let indexPath = indexPath(for: LocationCellViewModel(
section: selectedLocation.section,
node: rootNode
)) else { return }
// Walk tree backwards to determine which nodes should be expanded.
selectedLocation.node.forEachAncestor { node in
node.showsChildren = true
}
// Construct node tree.
let nodesToAdd = recursivelyCreateCellViewModelTree(
for: rootNode,
in: selectedLocation.section,
indentationLevel: 1
)
// Insert the new node tree below the selected item.
var snapshotItems = snapshot().itemIdentifiers(inSection: selectedLocation.section)
snapshotItems.insert(contentsOf: nodesToAdd, at: indexPath.row + 1)
let list = sections.enumerated().map { index, section in
index == indexPath.section
? snapshotItems
: snapshot().itemIdentifiers(inSection: section)
}
updateDataSnapshot(
with: list,
reloadExisting: true,
animated: animated,
completion: completion
)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
guard let cell = cell as? LocationCell, let item = itemIdentifier(for: indexPath) else {
return cell
}
cell.delegate = self
if item.shouldExcludeLocation(excludedLocation) {
// Only host locations should have an excluded title. Since custom list nodes contain
// all locations of all child nodes, its first location could possibly be a host.
// Therefore we need to check for that as well.
if case .hostname = item.node.locations.first, !(item.node is CustomListLocationNode) {
cell.setExcluded(relayTitle: excludedLocation?.excludedRelayTitle)
} else {
cell.setExcluded()
}
}
return cell
}
}
// MARK: - Called from LocationDiffableDataSourceProtocol
extension LocationDataSource {
func nodeShowsChildren(_ node: LocationNode) -> Bool {
node.showsChildren
}
func nodeShouldBeSelected(_ node: LocationNode) -> Bool {
false // N/A
}
}
extension LocationDataSource: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
switch sections[section] {
case .allLocations:
return LocationSectionHeaderView(
configuration: LocationSectionHeaderView.Configuration(name: LocationSection.allLocations.description)
)
case .customLists:
return LocationSectionHeaderView(configuration: LocationSectionHeaderView.Configuration(
name: LocationSection.customLists.description,
primaryAction: UIAction(
handler: { [weak self] _ in
self?.didTapEditCustomLists?()
}
)
))
}
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
nil
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
switch sections[section] {
case .allLocations:
return .zero
case .customLists:
return 24
}
}
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
guard let item = itemIdentifier(for: indexPath) else { return false }
return !item.shouldExcludeLocation(excludedLocation) && item.node.isActive
}
func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
itemIdentifier(for: indexPath)?.indentationLevel ?? 0
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let item = itemIdentifier(for: indexPath), item == selectedLocation {
cell.setSelected(true, animated: false)
}
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let indexPath = indexPathForSelectedRelay() {
if let cell = tableView.cellForRow(at: indexPath) {
cell.setSelected(false, animated: false)
}
}
return indexPath
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = itemIdentifier(for: indexPath) else { return }
selectedLocation = item
var customListSelection: UserSelectedRelays.CustomListSelection?
if let topmostNode = item.node.root as? CustomListLocationNode {
customListSelection = UserSelectedRelays.CustomListSelection(
listId: topmostNode.customList.id,
isList: topmostNode == item.node
)
}
let relayLocations = UserSelectedRelays(
locations: item.node.locations,
customListSelection: customListSelection
)
didSelectRelayLocations?(relayLocations)
}
private func scrollToTop(animated: Bool) {
tableView.setContentOffset(.zero, animated: animated)
}
}
extension LocationDataSource: LocationCellDelegate {
func toggleExpanding(cell: LocationCell) {
guard let indexPath = tableView.indexPath(for: cell),
let item = itemIdentifier(for: indexPath) else { return }
let items = toggledItems(for: cell)
updateDataSnapshot(with: items, reloadExisting: true, completion: {
self.scroll(to: item, animated: true)
})
}
func toggleSelecting(cell: LocationCell) {
// No op.
}
}