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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
//
// DataSourceSnapshot.swift
// MullvadVPN
//
// Created by pronebird on 11/10/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import Foundation
import UIKit
/// `NSDiffableDataSourceSnapshot` replica.
struct DataSourceSnapshot<Section: Hashable, Item: Hashable> {
/// Ordered set of section identifiers.
private var orderedSections = NSMutableOrderedSet()
/// Ordered set of item identifiers.
private var orderedItems = NSMutableOrderedSet()
/// Item identifier ranges by section.
private var sectionToItemMapping = [Range<Int>]()
/// Items to reload.
private var itemsToReload = NSMutableOrderedSet()
/// Items to reconfigure.
private var itemsToReconfigure = NSMutableOrderedSet()
/// Ordered array of section identifiers.
var sectionIdentifiers: [Section] {
return orderedSections.array as! [Section]
}
/// Ordered array of item identifiers.
var itemIdentifiers: [Item] {
return orderedItems.array as! [Item]
}
mutating func appendItems(_ itemsToAppend: [Item], in section: Section) {
assert(orderedSections.contains(section))
let sectionIndex = indexOfSection(section)!
let uniqueItemsToAppend = NSOrderedSet(array: itemsToAppend)
let itemRange = sectionToItemMapping[sectionIndex]
let oldEndIndex = itemRange.endIndex
let newEndIndex = itemRange.endIndex.advanced(by: uniqueItemsToAppend.count)
let newItemRange = (itemRange.startIndex ..< newEndIndex)
sectionToItemMapping[sectionIndex] = newItemRange
orderedItems.insert(
uniqueItemsToAppend.array,
at: IndexSet(integersIn: oldEndIndex ..< newEndIndex)
)
offsetItemRange(inSectionsAfter: sectionIndex, by: uniqueItemsToAppend.count)
}
mutating func appendSections(_ newSections: [Section]) {
let lastSectionRange = sectionToItemMapping.last
let emptyRange = lastSectionRange.flatMap { range in
return (range.upperBound ..< range.upperBound)
} ?? (0 ..< 0)
let uniqueNewSections = NSOrderedSet(array: newSections)
for newSection in uniqueNewSections {
orderedSections.add(newSection)
sectionToItemMapping.append(emptyRange)
}
}
func section(at index: Int) -> Section? {
if index < orderedSections.count {
return orderedSections.object(at: index) as? Section
} else {
return nil
}
}
func indexOfSection(_ section: Section) -> Int? {
let index = orderedSections.index(of: section)
if index == NSNotFound {
return nil
} else {
return index
}
}
func numberOfSections() -> Int {
return orderedSections.count
}
func numberOfItems(in section: Section) -> Int? {
guard let sectionIndex = indexOfSection(section) else { return nil }
return sectionToItemMapping[sectionIndex].count
}
func items(in section: Section) -> [Item] {
guard let sectionIndex = indexOfSection(section) else { return [] }
let range = sectionToItemMapping[sectionIndex]
let indexSet = IndexSet(integersIn: range)
return orderedItems.objects(at: indexSet) as! [Item]
}
func itemForIndexPath(_ indexPath: IndexPath) -> Item? {
guard indexPath.section < orderedSections.count else { return nil }
let itemRange = sectionToItemMapping[indexPath.section]
let itemIndex = itemRange.startIndex + indexPath.row
if itemRange.contains(itemIndex) {
return orderedItems.object(at: itemIndex) as? Item
} else {
return nil
}
}
func indexPathForItem(_ item: Item) -> IndexPath? {
let itemIndex = orderedItems.index(of: item)
guard itemIndex != NSNotFound else { return nil }
guard let sectionIdentifier = section(containingItem: item) else { return nil }
let sectionIndex = orderedSections.index(of: sectionIdentifier)
guard sectionIndex != NSNotFound else { return nil }
let range = sectionToItemMapping[sectionIndex]
let rowIndex = itemIndex - range.startIndex
return IndexPath(row: rowIndex, section: sectionIndex)
}
func section(containingItem item: Item) -> Section? {
let itemIndex = orderedItems.index(of: item)
guard itemIndex != NSNotFound else { return nil }
for (sectionIndex, sectionObject) in orderedSections.enumerated() {
let sectionIdentifier = sectionObject as! Section
let range = sectionToItemMapping[sectionIndex]
if range.contains(itemIndex) {
return sectionIdentifier
}
}
return nil
}
mutating func reloadItems(_ items: [Item]) {
itemsToReload.addObjects(from: items)
}
mutating func reconfigureItems(_ items: [Item]) {
itemsToReconfigure.addObjects(from: items)
}
private mutating func offsetItemRange(inSectionsAfter sectionIndex: Int, by offset: Int) {
let startIndex = sectionIndex + 1
let sectionRange = (startIndex ..< orderedSections.count)
for sectionIndex in sectionRange {
let range = sectionToItemMapping[sectionIndex]
let offsetRange = (range.startIndex + offset ..< range.endIndex + offset)
sectionToItemMapping[sectionIndex] = offsetRange
}
}
}
extension DataSourceSnapshot {
enum Change: CustomDebugStringConvertible, Hashable {
case insert(IndexPath)
case delete(IndexPath)
case move(_ source: IndexPath, _ target: IndexPath)
case reload(IndexPath)
case reconfigure(IndexPath)
var sortOrder: Int {
switch self {
case .delete:
return 0
case .insert:
return 1
case .move:
return 2
case .reload:
return 3
case .reconfigure:
return 4
}
}
var debugDescription: String {
switch self {
case let .insert(indexPath):
return "insert \(indexPath)"
case let .delete(indexPath):
return "delete \(indexPath)"
case let .move(source, target):
return "move from \(source) to \(target)"
case let .reload(indexPath):
return "reload \(indexPath)"
case let .reconfigure(indexPath):
return "reconfigure \(indexPath)"
}
}
func breakMoveOntoInsertionDeletion() -> [Change] {
if case let .move(fromIndexPath, toIndexPath) = self {
return [.delete(fromIndexPath), .insert(toIndexPath)]
} else {
return [self]
}
}
}
func difference(_ other: DataSourceSnapshot<Section, Item>) -> DataSnapshotDifference {
var changes = [Change]()
let oldItems = itemIdentifiers
let newItems = other.itemIdentifiers
for item in oldItems {
let oldIndexPath = indexPathForItem(item)
let newIndexPath = other.indexPathForItem(item)
if let oldIndexPath = oldIndexPath, oldIndexPath != newIndexPath {
guard let newIndexPath = newIndexPath else {
changes.append(.delete(oldIndexPath))
continue
}
// Guard against recording the `.move` twice when exchanging two adjacent items.
let isSwappingTwoAdjacentItems = changes.contains { otherChange in
if case let .move(fromIndexPath, toIndexPath) = otherChange {
let itemDistance = abs(oldIndexPath.row - fromIndexPath.row)
return oldIndexPath == toIndexPath && newIndexPath == fromIndexPath &&
oldIndexPath.section == newIndexPath.section &&
itemDistance == 1
} else {
return false
}
}
if !isSwappingTwoAdjacentItems {
changes.append(.move(oldIndexPath, newIndexPath))
}
}
}
for item in newItems {
if let indexPath = other.indexPathForItem(item), !oldItems.contains(item) {
changes.append(.insert(indexPath))
}
}
changes = Self.inferMoves(changes: changes)
for itemObject in other.itemsToReload {
let itemIdentifier = itemObject as! Item
if let indexPath = other.indexPathForItem(itemIdentifier) {
changes.append(.reload(indexPath))
}
}
for itemObject in other.itemsToReconfigure {
let itemIdentifier = itemObject as! Item
if let indexPath = other.indexPathForItem(itemIdentifier) {
changes.append(.reconfigure(indexPath))
}
}
changes.sort(by: Self.changeSortPredicate)
return Self.changeSetToDifference(changes)
}
/// Infer and discard unnecessary moves that occur due to items shifting back or forth based on
/// insertions and deletions of other items.
private static func inferMoves(changes: [Change]) -> [Change] {
var newChanges = [Change]()
// Expand .move onto .insert + .delete pair and sort changes.
let sortedChangesWithoutMoves = changes
.flatMap { change in
return change.breakMoveOntoInsertionDeletion()
}
.sorted(by: Self.changeSortPredicate)
for sourceChange in changes {
guard case let .move(sourceIndexPath, targetIndexPath) = sourceChange else {
newChanges.append(sourceChange)
continue
}
// Replay all changes to compute the item's index path, ignoring the changes
// associated with the current change.
let inferredIndexPath = sortedChangesWithoutMoves
.reduce(into: sourceIndexPath) { inferredIndexPath, otherChange in
switch otherChange {
case let .insert(insertedIndexPath) where insertedIndexPath != targetIndexPath:
if inferredIndexPath.row >= insertedIndexPath.row,
inferredIndexPath.section == insertedIndexPath.section
{
inferredIndexPath.row += 1
}
case let .delete(deletedIndexPath) where deletedIndexPath != sourceIndexPath:
if inferredIndexPath.row > deletedIndexPath.row,
inferredIndexPath.section == deletedIndexPath.section
{
inferredIndexPath.row -= 1
}
default:
break
}
}
// Discard the change if the index path, produced after replaying other changes,
// matches the target index path.
if inferredIndexPath != targetIndexPath {
newChanges.append(contentsOf: sourceChange.breakMoveOntoInsertionDeletion())
}
}
return newChanges
}
/// Sort predicate used for sorting a collection of `Change`.
///
/// Sort order by kind and index path:
/// Deletion: descending
/// Insertion: ascending
/// Reload, reconfigure: ascending
private static func changeSortPredicate(_ lhs: Change, _ rhs: Change) -> Bool {
switch (lhs, rhs) {
case let (.insert(lhsIndexPath), .insert(rhsIndexPath)):
return lhsIndexPath < rhsIndexPath
case let (.delete(lhsIndexPath), .delete(rhsIndexPath)):
return lhsIndexPath > rhsIndexPath
case let (.reload(lhsIndexPath), .reload(rhsIndexPath)):
return lhsIndexPath < rhsIndexPath
case let (.reconfigure(lhsIndexPath), .reconfigure(rhsIndexPath)):
return lhsIndexPath < rhsIndexPath
case let (lhs, rhs):
return lhs.sortOrder < rhs.sortOrder
}
}
private static func changeSetToDifference(_ changes: [Change]) -> DataSnapshotDifference {
var indexPathsToInsert = [IndexPath]()
var indexPathsToDelete = [IndexPath]()
var indexPathsToReload = [IndexPath]()
var indexPathsToReconfigure = [IndexPath]()
for change in changes {
switch change {
case let .insert(indexPath):
indexPathsToInsert.append(indexPath)
case let .delete(indexPath):
indexPathsToDelete.append(indexPath)
case .move:
// Moves are broken down onto insert and delete changes at this point.
break
case let .reload(indexPath):
indexPathsToReload.append(indexPath)
case let .reconfigure(indexPath):
indexPathsToReconfigure.append(indexPath)
}
}
return DataSnapshotDifference(
indexPathsToInsert: indexPathsToInsert,
indexPathsToDelete: indexPathsToDelete,
indexPathsToReload: indexPathsToReload,
indexPathsToReconfigure: indexPathsToReconfigure
)
}
}
struct StackViewApplyDataSnapshotConfiguration {
var animationDuration: TimeInterval = 0.25
var animationOptions: UIView.AnimationOptions = [.curveEaseInOut]
var makeView: (IndexPath) -> UIView
}
struct DataSnapshotDifference: CustomDebugStringConvertible {
var indexPathsToInsert = [IndexPath]()
var indexPathsToDelete = [IndexPath]()
var indexPathsToReload = [IndexPath]()
var indexPathsToReconfigure = [IndexPath]()
var debugDescription: String {
var s = "DataSnapshotDifference {\n"
s += " insert: \n"
for indexPath in indexPathsToInsert {
s += " \(indexPath),\n"
}
s += " delete: \n"
for indexPath in indexPathsToDelete {
s += " \(indexPath),\n"
}
s += " reload: \n"
for indexPath in indexPathsToReload {
s += " \(indexPath),\n"
}
s += " reconfigure: \n"
for indexPath in indexPathsToReconfigure {
s += " \(indexPath),\n"
}
s += "}"
return s
}
func apply(
to tableView: UITableView,
animateDifferences: Bool,
completion: ((Bool) -> Void)? = nil
) {
let animation: UITableView.RowAnimation = animateDifferences ? .automatic : .none
tableView.performBatchUpdates({
if !indexPathsToDelete.isEmpty {
tableView.deleteRows(at: indexPathsToDelete, with: animation)
}
if !indexPathsToInsert.isEmpty {
tableView.insertRows(at: indexPathsToInsert, with: animation)
}
if !indexPathsToReload.isEmpty {
tableView.reloadRows(at: indexPathsToReload, with: animation)
}
if !indexPathsToReconfigure.isEmpty {
if #available(iOS 15.0, *) {
tableView.reconfigureRows(at: indexPathsToReconfigure)
} else {
tableView.reloadRows(at: indexPathsToReconfigure, with: .none)
}
}
}, completion: completion)
}
func apply(
to stackView: UIStackView,
configuration: StackViewApplyDataSnapshotConfiguration,
animateDifferences: Bool,
completion: ((Bool) -> Void)? = nil
) {
let viewsToRemove = indexPathsToDelete.map { indexPath in
return stackView.arrangedSubviews[indexPath.row]
}
let viewsToAdd = indexPathsToInsert.map { indexPath -> UIView in
let view = configuration.makeView(indexPath)
view.isHidden = true
view.alpha = 0
var viewIndex = indexPath.row
// Adjust insertion index since views are not removed from stack view during animation.
for view in stackView.arrangedSubviews[..<indexPath.row] {
if viewsToRemove.contains(view) {
viewIndex += 1
}
}
stackView.insertArrangedSubview(view, at: viewIndex)
return view
}
// Layout inserted subviews before running animations to achieve a folding effect.
if animateDifferences {
UIView.performWithoutAnimation {
stackView.layoutIfNeeded()
}
}
let showHideViews = {
for view in viewsToRemove {
view.alpha = 0
view.isHidden = true
}
for view in viewsToAdd {
view.alpha = 1
view.isHidden = false
}
}
let removeViews = {
for view in viewsToRemove {
view.removeFromSuperview()
}
}
if animateDifferences {
UIView.animate(
withDuration: configuration.animationDuration,
delay: 0,
options: configuration.animationOptions,
animations: {
showHideViews()
stackView.layoutIfNeeded()
},
completion: { isComplete in
removeViews()
completion?(isComplete)
}
)
} else {
showHideViews()
removeViews()
completion?(true)
}
}
}
|