summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/DataSourceSnapshot.swift
blob: cb97945b65477e3d168c3d13f654fab3e77a4fdd (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
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
//
//  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 .insert(let indexPath):
                return "insert \(indexPath)"
            case .delete(let indexPath):
                return "delete \(indexPath)"
            case .move(let source, let target):
                return "move from \(source) to \(target)"
            case .reload(let indexPath):
                return "reload \(indexPath)"
            case .reconfigure(let indexPath):
                return "reconfigure \(indexPath)"
            }
        }

        func breakMoveOntoInsertionDeletion() -> [Change] {
            if case .move(let fromIndexPath, let 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 .move(let fromIndexPath, let 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 .move(let sourceIndexPath, let 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 .insert(let insertedIndexPath) where insertedIndexPath != targetIndexPath:
                    if inferredIndexPath.row >= insertedIndexPath.row, inferredIndexPath.section == insertedIndexPath.section {
                        inferredIndexPath.row += 1
                    }

                case .delete(let 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 (.insert(let lhsIndexPath), .insert(let rhsIndexPath)):
            return lhsIndexPath < rhsIndexPath

        case (.delete(let lhsIndexPath), .delete(let rhsIndexPath)):
            return lhsIndexPath > rhsIndexPath

        case (.reload(let lhsIndexPath), .reload(let rhsIndexPath)):
            return lhsIndexPath < rhsIndexPath

        case (.reconfigure(let lhsIndexPath), .reconfigure(let rhsIndexPath)):
            return lhsIndexPath < rhsIndexPath

        case (let lhs, let 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 .insert(let indexPath):
                indexPathsToInsert.append(indexPath)

            case .delete(let indexPath):
                indexPathsToDelete.append(indexPath)

            case .move:
                // Moves are broken down onto insert and delete changes at this point.
                break

            case .reload(let indexPath):
                indexPathsToReload.append(indexPath)

            case .reconfigure(let indexPath):
                indexPathsToReconfigure.append(indexPath)
            }
        }

        return DataSnapshotDifference(
            indexPathsToInsert: indexPathsToInsert,
            indexPathsToDelete: indexPathsToDelete,
            indexPathsToReload: indexPathsToReload,
            indexPathsToReconfigure: indexPathsToReconfigure
        )
    }
}

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)
    }
}