Git Product home page Git Product logo

arraydiff's Introduction

ArrayDiff Carthage compatible Pods Pod platforms

An efficient Swift utility to compute the difference between two arrays. Get the removedIndexes and insertedIndexes and pass them directly along to UITableView or UICollectionView when you update your data! The diffing algorithm is the same that powers the diff utility – it's robust and quick.

Basic Example

let old = [ "a", "b", "c", "d" ]
let new = [ "a", "c", "e", "f", "d" ]

let diff = old.diff(new)
// diff.removedIndexes = { 1 }
// diff.insertedIndexes = { 2, 3 }

// To update rows in section 0:
tableView.beginUpdates()
self.data = new
diff.applyToRowsInTableView(tableView, section: 0, rowAnimation: .Automatic)
tableView.endUpdates()

// Or to update sections:
tableView.beginUpdates()
self.data = new
diff.applyToSectionsInTableView(tableView, rowAnimation: .Automatic)
tableView.endUpdates()

Nested Diff

You can use types that conform to SectionType to perform nested row- and section-level diffs simultaneously:

let old = [
  BasicSection(name: "Alpha", items: ["a", "b", "c", "d", "e"]),
  BasicSection(name: "Bravo", items: ["f", "g", "h", "i", "j"]),
  BasicSection(name: "Charlie", items: ["k", "l", "m", "n", "o"])
]
let new = [
  BasicSection(name: "Alpha", items: ["a", "b", "d", "e", "x"]),
  BasicSection(name: "Charlie", items: ["f", "g", "h", "i", "j"]),
  BasicSection(name: "Delta", items: ["f", "g", "h", "i", "j"])
]

let nestedDiff = old.diffNested(new)
// nestedDiff.sectionsDiff.removedIndexes == {1}
// nestedDiff.sectionsDiff.insertedIndexes == {2}
// nestedDiff.itemDiffs[0].removedIndexes == {2}
// nestedDiff.itemDiffs[0].insertedIndexes == {5}
// etc.

tableView.beginUpdates()
self.data = new
nestedDiff.applyToTableView(tableView, rowAnimation: .Automatic)
tableView.endUpdates()

Limitations

Item moves are treated as remove/insert, so when they are animated the cell will "teleport" to its new position, rather than sliding there. If you would like this feature, let me know in the Issues!

Example Project

Check out the iOS app in the Example folder to see this framework pushed to its limits to drive a UITableView. In it we have a table view with 20 sections of strings. When you tap update, the data is randomly updated and we assert that the changes we made are equal to the changes that the framework recovers by comparing the two arrays.

Attribution

Thanks to https://github.com/khanlou/NSArray-LongestCommonSubsequence which I took inspiration totally copied from.

arraydiff's People

Contributors

adlai-holler avatar raheelahmad avatar sudeepsidhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

arraydiff's Issues

Thrash Testing

We should move ThrashingDataSource from the Example project into the framework itself and use it to power unit tests.

Migrate to Swift 2.3 and Swift 3

In order to migrate and test our app to Swift 2.3/3.0 prior to the release of Xcode 8 and iOS 10, all of our dependencies need to be migrated first.

This can be solved by pointing our podfile to those respective Swift 2.3/3.0 branches, but this repo does not appear to have either of those branches.

This blog post contains helpful tips for migrating open source libraries.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.