-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathLocationDataSource.swift
305 lines (258 loc) · 10.7 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
//
// LocationDataSource.swift
// MullvadVPN
//
// Created by pronebird on 11/03/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import Combine
import MullvadREST
import MullvadTypes
import UIKit
final class LocationDataSource: UITableViewDiffableDataSource<SelectLocationSection, LocationCellViewModel> {
private var currentSearchString = ""
private let tableView: UITableView
private let locationCellFactory: LocationCellFactory
private var dataSources: [LocationDataSourceProtocol] = []
var selectedRelayLocation: LocationCellViewModel?
var didSelectRelayLocation: ((RelayLocation) -> Void)?
init(
tableView: UITableView,
allLocations: LocationDataSourceProtocol,
customLists: LocationDataSourceProtocol
) {
self.tableView = tableView
#if DEBUG
self.dataSources.append(customLists)
#endif
self.dataSources.append(allLocations)
let locationCellFactory = LocationCellFactory(
tableView: tableView,
reuseIdentifier: SelectLocationSection.Cell.locationCell.reuseIdentifier
)
self.locationCellFactory = locationCellFactory
super.init(tableView: tableView) { _, indexPath, itemIdentifier in
locationCellFactory.makeCell(for: itemIdentifier, indexPath: indexPath)
}
tableView.delegate = self
locationCellFactory.delegate = self
defaultRowAnimation = .fade
registerClasses()
}
func setRelays(_ response: REST.ServerRelaysResponse, filter: RelayFilter) {
let relays = response.wireguard.relays.filter { relay in
return RelaySelector.relayMatchesFilter(relay, filter: filter)
}
var list: [[LocationCellViewModel]] = []
for section in 0 ..< dataSources.count {
list.append(
dataSources[section]
.reload(response, relays: relays)
.map { LocationCellViewModel(group: SelectLocationSection.allCases[section], location: $0) }
)
}
filterRelays(by: currentSearchString)
}
func indexPathForSelectedRelay() -> IndexPath? {
selectedRelayLocation.flatMap {
indexPath(for: $0)
}
}
func filterRelays(by searchString: String) {
currentSearchString = searchString
let list = SelectLocationSection.allCases.enumerated().map { section, group in
dataSources[section]
.search(by: searchString)
.map { LocationCellViewModel(group: group, location: $0) }
}
updateDataSnapshot(with: list, reloadExisting: !searchString.isEmpty) {
DispatchQueue.main.async {
if searchString.isEmpty {
self.setSelectedRelayLocation(self.selectedRelayLocation, animated: false, completion: {
self.scrollToSelectedRelay()
})
} else {
self.scrollToTop(animated: false)
}
}
}
}
private func updateDataSnapshot(
with list: [[LocationCellViewModel]],
reloadExisting: Bool = false,
animated: Bool = false,
completion: (() -> Void)? = nil
) {
var snapshot = NSDiffableDataSourceSnapshot<SelectLocationSection, LocationCellViewModel>()
let sections = SelectLocationSection.allCases
snapshot.appendSections(sections)
for (index, section) in sections.enumerated() {
snapshot.appendItems(list[index], toSection: section)
}
if reloadExisting {
snapshot.reloadSections(SelectLocationSection.allCases)
}
apply(snapshot, animatingDifferences: animated, completion: completion)
}
private func registerClasses() {
SelectLocationSection.allCases.forEach {
tableView.register(
$0.cell.reusableViewClass,
forCellReuseIdentifier: $0.cell.reuseIdentifier
)
}
}
private func setSelectedRelayLocation(
_ relayLocation: LocationCellViewModel?,
animated: Bool,
completion: (() -> Void)? = nil
) {
selectedRelayLocation = relayLocation
guard let selectedRelayLocation else { return }
let group = selectedRelayLocation.group
var locationList = snapshot().itemIdentifiers(inSection: group)
guard !locationList.contains(selectedRelayLocation) else {
completion?()
return
}
let selectedLocationTree = selectedRelayLocation.location.ancestors + [selectedRelayLocation.location]
guard let first = selectedLocationTree.first else { return }
let topLocation = LocationCellViewModel(group: group, location: first)
guard let indexPath = indexPath(for: topLocation),
let topNode = node(for: topLocation) else {
return
}
selectedLocationTree.forEach { location in
node(for: LocationCellViewModel(group: group, location: location))?.showsChildren = true
}
locationList.addLocations(
topNode.flatRelayLocationList().map { LocationCellViewModel(group: group, location: $0) },
at: indexPath.row + 1
)
var list: [[LocationCellViewModel]] = Array(repeating: [], count: dataSources.count)
for index in 0 ..< list.count {
list[index] = (index == indexPath.section)
? locationList
: snapshot().itemIdentifiers(inSection: SelectLocationSection.allCases[index])
}
updateDataSnapshot(
with: list,
reloadExisting: true,
animated: animated,
completion: completion
)
}
}
extension LocationDataSource: UITableViewDelegate {
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
guard let item = itemIdentifier(for: indexPath) else { return false }
return node(for: item)?.isActive ?? false
}
func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
guard let item = itemIdentifier(for: indexPath) else { return 0 }
return node(for: item)?.indentationLevel ?? 0
}
func tableView(
_ tableView: UITableView,
willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath
) {
if let item = itemIdentifier(for: indexPath),
item == selectedRelayLocation {
cell.setSelected(true, animated: false)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
itemIdentifier(for: indexPath)
.flatMap { item in
guard item.location != selectedRelayLocation?.location else { return }
didSelectRelayLocation?(item.location)
setSelectedRelayLocation(item, animated: false)
indexPathForSelectedRelay().flatMap {
let cell = tableView.cellForRow(at: $0)
cell?.setSelected(false, animated: false)
}
}
}
}
extension LocationDataSource: LocationCellEventHandler {
func toggleCell(for item: LocationCellViewModel) {
indexPath(for: item).flatMap { indexPath in
guard let node = node(for: item), let cell = tableView.cellForRow(at: indexPath) else { return }
let isExpanded = node.showsChildren
let group = SelectLocationSection.allCases[indexPath.section]
node.showsChildren = !isExpanded
locationCellFactory.configureCell(
cell,
item: LocationCellViewModel(group: group, location: node.location),
indexPath: indexPath
)
var locationList = snapshot().itemIdentifiers(inSection: group)
let locationsToEdit = node.flatRelayLocationList().map { LocationCellViewModel(group: group, location: $0) }
if !isExpanded {
locationList.addLocations(locationsToEdit, at: indexPath.row + 1)
} else {
locationsToEdit.forEach { self.node(for: $0)?.showsChildren = false }
locationList.removeLocations(locationsToEdit)
}
var list: [[LocationCellViewModel]] = Array(repeating: [], count: dataSources.count)
for index in 0 ..< list.count {
list[index] = (index == indexPath.section)
? locationList
: snapshot().itemIdentifiers(inSection: SelectLocationSection.allCases[index])
}
updateDataSnapshot(with: list, completion: {
self.scroll(to: item, animated: true)
})
}
}
func node(for item: LocationCellViewModel) -> SelectLocationNode? {
guard let sectionIndex = SelectLocationSection.allCases.firstIndex(of: item.group) else {
return nil
}
return dataSources[sectionIndex].nodeByLocation[item.location]
}
}
extension LocationDataSource {
private func scroll(to location: LocationCellViewModel, animated: Bool) {
guard let visibleIndexPaths = tableView.indexPathsForVisibleRows,
let indexPath = indexPath(for: location),
let node = node(for: location) else { return }
if node.children.count > visibleIndexPaths.count {
tableView.scrollToRow(at: indexPath, at: .top, animated: animated)
} else {
node.children.last.flatMap { last in
if let lastInsertedIndexPath = self.indexPath(for: LocationCellViewModel(
group: SelectLocationSection.allCases[indexPath.section],
location: last.location
)),
let lastVisibleIndexPath = visibleIndexPaths.last,
lastInsertedIndexPath >= lastVisibleIndexPath {
tableView.scrollToRow(at: lastInsertedIndexPath, at: .bottom, animated: animated)
}
}
}
}
private func scrollToTop(animated: Bool) {
tableView.setContentOffset(.zero, animated: animated)
}
private func scrollToSelectedRelay() {
indexPathForSelectedRelay().flatMap {
tableView.scrollToRow(at: $0, at: .middle, animated: false)
}
}
}
private extension [LocationCellViewModel] {
mutating func addLocations(_ locations: [LocationCellViewModel], at index: Int) {
if index < count {
insert(contentsOf: locations, at: index)
} else {
append(contentsOf: locations)
}
}
mutating func removeLocations(_ locations: [LocationCellViewModel]) {
removeAll(where: { location in
locations.contains(location)
})
}
}