ra1028 / DiffableDataSources

💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.
https://ra1028.github.io/DiffableDataSources
Apache License 2.0
853 stars 69 forks source link

Add update and move support #18

Open winstondu opened 4 years ago

winstondu commented 4 years ago

Checklist

Description

The current code was not correctly leveraging DifferenceKit to address moves and updates. In particular, it was not distinguishing changes in content versus changes in the identifier.

This code change ensures that the difference identifier is no longer of the whole Item, but instead just the hashValue of the Item (as determined by the implemention of Item.)

For a visualization of the difference, please see the below gifs (the example iOS code in this repo was slightly modified to illustrate this).

For a clear conceptual example of the correction that this pull request sets out to accomplish, consider if Item was the following:

struct person: Hashable {
    var birthName : String  // Assume this is our unique identifier for a person.
    var age : Int
    func hash(into hasher: inout Hasher) {
            hasher.combine(birthName)
   }
}

And we had to compare the datasource diff of the following lists (being used as datasource updates for a collectionview): Source: [(birthName: "Andy", age: 15), (birthName: "Kevin", age: 16)]
Target: [(birthName: "Kevin", age: 16)]

Without the change in this pull request, we get 2 deletions and 1 insertion (animated as such)

With this change in this PR , we get 1 deletion and 1 update (correctly animated).

Related Issue

N/A

Motivation and Context

The motivation was for a better, and more correct, diff animation.

Impact on Existing Code

None beyond what is described.

Screenshots

Notice the "move" animations that occur in after that do not happen in before

Before: Before

After: after

winstondu commented 4 years ago

@ra1028

ollitapa commented 4 years ago

Seems really welcome fix in my opinion! 👍

Just for clarification, do I understand correctly:

I have item with automatic hash and equality:

struct MyText: Hashable, Equatable {
    var id: Int
    var text: String
}

Change [ {id: 1, text: "a"} ] -> [ {id: 1, text: "b"} ] will result in one deletion and one insert.

but

struct MyText: Hashable, Equatable {
    var id: Int
    var text: String

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

[ {id: 1, text: "a"} ] -> [ {id: 1, text: "b"} ] will result in one update.

winstondu commented 4 years ago

@ollitapa , that is correct.

winstondu commented 4 years ago

@ra1028 , awaiting your approval to merge :)

ollitapa commented 4 years ago

@ra1028 ping. Would be nice to get this merged 😄

ra1028 commented 4 years ago

Thanks @winstondu and reviewers.

Sorry for my late reply.

I think it's difficult to judge this specification change. I agree with its behavior, but it works differently than the original version UICollectionViewDiffableDataSource. Because, DiffableDataSources is only a backport, differences between the original version and the specification can adversely affect future replacements.