Last active
September 8, 2021 22:33
-
-
Save dzmitry-antonenka/75ceb704148772c8b228b39776ae515d to your computer and use it in GitHub Desktop.
SwiftListTreeDataSource delete to return indices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Deletes the array of`items`. | |
/// - Parameter items: The array of items to delete. | |
public func delete(_ items: [ItemIdentifierType]) -> [Int] { | |
let deleteItemSet = Set(items) | |
let filterPredicate: (TreeItem<ItemIdentifierType>) -> Bool = { !deleteItemSet.contains($0.value) } | |
/// Depth first traversal to get include all related deleted expanded children | |
let deletedItemsWithRelatedExpandedChildren = Set( | |
items | |
.compactMap { lookup($0) } | |
.flatMap { itemToDelete in | |
depthFirstFlattened(items: [itemToDelete], itemChildren: { $0.isExpanded ? $0.subitems : [] }) | |
}) | |
/// Traverse `shownFlatItems` and add idx if it's in deletion set. | |
var deletedIndices = [Int]() | |
for idx in shownFlatItems.indices { | |
if deletedExpandedItemSet.contains(shownFlatItems[idx]) { | |
deletedIndices.append(idx) | |
} | |
} | |
// filter backing store and all subitems | |
backingStore = backingStore.filter(filterPredicate) | |
let theDepthFirstFlattened = depthFirstFlattened(items: backingStore) | |
for item in theDepthFirstFlattened { | |
item.subitems = item.subitems.filter(filterPredicate) | |
} | |
// delete from lookupTable | |
deleteItemSet.forEach { lookupTable[$0] = nil } | |
return deletedIndices | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment