Sections diff improvements
Hi, currently Differ support calculating extended diffs of two dimensional arrays.
But what can I do if I have an array of some objects where internal object has array and some additional data:
Section - header: String - items: [Item]
Would it be possible to add new KeyPath feature, for example:
sections.nestedDiff(to: newSections, item: \.items, ...)
This sounds like a great idea - I don't have time at present to do this, however you're more than welcome to have a go at implementing it, if you'd like to.
My Swift foo is not good anymore, but I think this isn't a solution. IMO you should make such object conform to Collection.
Your Section object just needs to conform to Collection and you'll be good to go
@bryan1anderson with 2 section where the Iterator Element are the same there is no problem, how can you add 2 sections into an array with different Element?
@piv199 @tonyarnold Found a solution of this problem:
extension Section: Collection {
var startIndex: Int {
self.items.startIndex
}
var endIndex: Int {
self.items.endIndex
}
func index(after i: Int) -> Int {
self.items.index(after: i)
}
subscript(index: Int) -> Item {
self.items[index]
}
}
BUT: nestedDiff returns the same result as usual diff. For example:
//Source:
[
Section(header: "header0"),
Section(header: "header1", items: [Item(0), Item(1)]),
Section(header: "header2"),
Section(header: "header3", items: [Item(0), Item(1)])
]
//Destination:
[
Section(header: "header0"),
Section(header: "header1"),
Section(header: "header2"),
Section(header: "header3", items: [Item(0), Item(1)])
]
Instead of deleting rows this library wants to delete and insert the whole section at index 1.
Should I open a separate bug about it?
I'd need to check the code, but my understanding is that how this works would be dependent on the implementation of the Equatable conformance for your Section type. The following two things would need to be considered equal:
Section(header: "header1")
Section(header: "header1", items: [Item(0), Item(1)])
I think there is potential to use Identifiable here to alleviate some of these unnecessary changes.