Skip to content

Commit

Permalink
feat: recreate model to avoid instance replacement (useful when model…
Browse files Browse the repository at this point in the history
…s carry reactive streams)
  • Loading branch information
Oni-zerone committed Apr 3, 2020
1 parent efd4930 commit ab4646f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions PowerTools/CollectionVM/Classes/DataStructure/ModelUpdate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ internal struct ModelUpdate {

init(from oldModel: [SectionViewModel], to newModel: [SectionViewModel], forceReload: Bool = false) {

self.model = newModel

if forceReload || newModel.shouldReload(from: oldModel) {
self.change = nil
self.model = newModel
return
}

Expand All @@ -43,6 +42,17 @@ internal struct ModelUpdate {
let deletions = oldLookupTable.calculateDeletions(countTable: countTable)
changes.append(contentsOf: deletions)
self.change = changes
self.model = newModel.map { section in

var section = section
section.items = section.items.map { item in
guard let oldIndexPath = oldLookupTable[item.hashValue]?.first else {
return item
}
return oldModel[oldIndexPath.section].items[oldIndexPath.item]
}
return section
}
}
}

Expand Down

0 comments on commit ab4646f

Please sign in to comment.