Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sections diff improvements #8

Open
piv199 opened this issue Nov 5, 2017 · 6 comments
Open

Sections diff improvements #8

piv199 opened this issue Nov 5, 2017 · 6 comments

Comments

@piv199
Copy link

piv199 commented Nov 5, 2017

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, ...)

@tonyarnold
Copy link
Owner

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.

@wokalski
Copy link

My Swift foo is not good anymore, but I think this isn't a solution. IMO you should make such object conform to Collection.

@bryan1anderson
Copy link

Your Section object just needs to conform to Collection and you'll be good to go

@OrAzr
Copy link

OrAzr commented Jan 24, 2019

@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?

@gerchicov-bp
Copy link

gerchicov-bp commented Oct 8, 2021

@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?

@tonyarnold
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants