Files
Pulse/Sources/PulseUI/Extensions/UITableView+Extensions.swift
2022-07-25 17:20:15 -04:00

37 lines
1022 B
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The MIT License (MIT)
//
// Copyright (c) 2020–2022 Alexander Grebenyuk (github.com/kean).
#if os(iOS)
import UIKit
extension UITableView {
func apply<T: Hashable>(diff: CollectionDifference<T>, _ closure: () -> Void) {
var deletes: [IndexPath] = []
var inserts: [IndexPath] = []
for update in diff.inferringMoves() {
switch update {
case .remove(let offset, _, let move):
if move == nil {
deletes.append(IndexPath(row: offset, section: 0))
}
case .insert(let offset, _, let move):
// If there's no move, it's a true insertion and not the result of a move.
if move == nil {
inserts.append(IndexPath(row: offset, section: 0))
}
}
}
performBatchUpdates {
closure()
insertRows(at: inserts, with: .right)
deleteRows(at: deletes, with: .left)
}
}
}
#endif