TableDiff is a small library that takes two collections and generates
a diff that is suitable for feeding into
UICollectionView to animate the changes. It additionally tracks
which items have been updated so that UI code can adjust individual
rows or cells.
We created it to animate between distinct collection states in a Redux-like architecture like ReSwift.
Table of Contents
Please see the demo app for a working example.
Initially you will want to make the elements in your datasource to conform to
SequenceDiffable. This means you need to
- Conform to
- Have a
identifiervariable that is
identifier is used to determine if two items are the same, while equality
checks to see if the same item has been updated.
When you want to update the datasource you will need to calculate the diff:
let (diff, updates) = originalData.tableDiff(newData)
Then you can choose to either perform all the updates yourself or you can use the convenience extensions to handle the moves/inserts/deletes:
Updates still need to be managed manually, as this is app specific code. You can change the animation styles with additional parameters on the helpers.
We originally approached this problem with a
Longest Common Subsequence
algorithm, based on the work of Dwifft.
However, that algorithm only speaks in inserts and deletes, while for some use
cases we would prefer to use
UITableView's move capabilities.
We provide three different implementation options:
.lcs: This is the original LCS based algorithm, which only returns inserts and deletes.
.lcsWithMoves: This is the same algorithm with a post-processing step included to turn some delete/insert pairs into moves. This does not always line up with what you may perceive as the "intuitive moves".
.allMoves: This algorithm looks for all possible moves first, and then layers in inserts and deletes. It creates the nicest effect for moves, but has more extraneous instructions in the diff.
You can choose which implementation to use via a parameter. The default value is
tableView.applyDiff(diff, implementation: .lcs)
Updates to an individual item are tracked while the diff is created. But since
an update implies the same item is in both collections, you have the choice of
which collection's indices you'd like to use. You can choose to have the first
collection's indices with
.pre, or the second collection's with
tableView.applyDiff(diff, updateStyle: .post)
- Calculate diff between 2
- Translate diff into moves/inserts/deletes/updates
- Adapt algorithm to be used with sections
Please read through our contributing guidelines. Included are directions for opening issues, coding standards, and notes on development.