Skip to content

Delta is a Swift Library focused on replacing reloadData() calls with animated insert, delete and move operations.

License

Notifications You must be signed in to change notification settings

ello/DeltaCalculator

 
 

Repository files navigation

Version License Platform

DeltaCalculator

DeltaCalculator

DeltaCalculator is a Swift Library focused on replacing reloadData() calls with animated insert, delete and move operations.

DeltaCalculator tries to optimize the number of iterations to calculate all the changes, making sure the UI thread doesn't block.

This framework is based on BKDeltaCalculator Objective-C library.

Installation

DeltaCalculator is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'DeltaCalculator'

Usage

DeltaCalculator compares two arrays and computes the delta between them, represented by a Delta object. DeltaCalculator is uses generics, therefor, you need to supply the class in the initializer.

In case your generic class is Equatable you can initialize the calculator without an equalityTest.

let deltaCalculator = DeltaCalculator<String>()
let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)

Otherwise you are required to supply an equalityTest in the initializer.

let deltaCalculator = DeltaCalculator<NSDate>() { (lhs, rhs) -> Bool in
  return lhs.compare(rhs) == .OrderedSame
}
let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)

UITableViews

To apply the Delta to a UITableView you need to use a batch update such as the following:

let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)
tableView.beginUpdates()
dataModel = newDataModel
delta.applyUpdatesToTableView(tableView, inSection: 0, withRowAnimation: UITableViewRowAnimation.Right)
tableView.endUpdates()

UICollectionViews

To apply the Delta to a UICollectionView you also need to perform it in a batch update:

let delta = deltaCalculator.deltaFromOldArray(dataModel, toNewArray: newDataModel)
collectionView.performBatchUpdates({
  self.dataModel = newDataModel
  delta.applyUpdatesToCollectionView(collectionView, inSection: 0)
}, completion: nil)

Sample

There is a sample project in the Example directory.

Author

Ivan Bruel, @ivanbruel

License

Delta is available under the MIT license. See the LICENSE file for more info.

About

Delta is a Swift Library focused on replacing reloadData() calls with animated insert, delete and move operations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 80.8%
  • Ruby 19.2%