onmyway133 / DeepDiff

🦀Amazingly incredible extraordinary lightning fast diffing in Swift
https://onmyway133.com/apps/
Other
2.05k stars 145 forks source link

reload changes #7

Closed Yurlagin closed 6 years ago

Yurlagin commented 6 years ago

I need make custom reload cell animation. How can i get reloading indices instead of insertion and deletion pairs?

Yurlagin commented 6 years ago

// Perhaps there is a way to be more elegant than this

let changes = diff(old: items, new: viewModel. items)

items = viewModel.items

let insertionsSet = Set(changes.flatMap{$0.insert?.index})
let deletionsSet = Set(changes.flatMap{$0.delete?.index})
let reloadsSet = deletionsSet.intersection(insertionsSet)

let insertions = Array(insertionsSet.filter{!reloadsSet.contains($0)}.map{IndexPath(row: $0, section: 0)})
let deletions = Array(deletionsSet.filter{!reloadsSet.contains($0)}.map{IndexPath(row: $0, section: 0)})

reloadsSet.forEach {
  if let cell = tableView.cellForRow(at: IndexPath(row: $0, section: 0)) as? MyCell {
    cell.renderUI(event: items[$0])
  }
}

tableView.beginUpdates()
tableView.deleteRows(at: deletions, with: .automatic)
tableView.insertRows(at: insertions, with: .automatic)
tableView.endUpdates()
onmyway133 commented 6 years ago

@Yurlagin Hi, sorry for late reply, you can see my response at this issue https://github.com/onmyway133/DeepDiff/issues/16