Skip to content

Commit

Permalink
Merge pull request #21 from leoMehlig/master
Browse files Browse the repository at this point in the history
Adds parameter to UIKit method to skip the reload of updated cells.
  • Loading branch information
mcudich authored Oct 17, 2017
2 parents 7cbb39a + 29238c8 commit 25964ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
21 changes: 12 additions & 9 deletions Source/UICollectionView+Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import UIKit
public extension UICollectionView {
/// Applies a batch update to the receiver, efficiently reporting changes between old and new.
///
/// - parameter old: The previous state of the collection view.
/// - parameter new: The current state of the collection view.
/// - parameter section: The section where these changes took place.
func applyDiff<T: Collection>(_ old: T, _ new: T, inSection section: Int, completion: ((Bool) -> Void)?) where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
/// - parameter old: The previous state of the collection view.
/// - parameter new: The current state of the collection view.
/// - parameter section: The section where these changes took place.
/// - parameter reloadUpdated: Whether or not updated cells should be reloaded (default: true)
func applyDiff<T: Collection>(_ old: T, _ new: T, inSection section: Int, reloadUpdated: Bool = true, completion: ((Bool) -> Void)?) where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
let update = ListUpdate(diff(old, new), section)

performBatchUpdates({
Expand All @@ -25,13 +26,15 @@ public extension UICollectionView {
for move in update.moves {
self.moveItem(at: move.from, to: move.to)
}
}, completion: nil)
}, completion: reloadUpdated ? nil : completion)

// reloadItems is done separately as the update indexes returne by diff() are in respect to the
// "after" state, but the collectionView.reloadItems() call wants the "before" indexPaths.
performBatchUpdates({
if reloadUpdated {
// reloadItems is done separately as the update indexes returne by diff() are in respect to the
// "after" state, but the collectionView.reloadItems() call wants the "before" indexPaths.
performBatchUpdates({
self.reloadItems(at: update.updates)
}, completion: completion)
}, completion: completion)
}
}
}
#endif
13 changes: 7 additions & 6 deletions Source/UITableView+Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import UIKit
public extension UITableView {
/// Applies a batch update to the receiver, efficiently reporting changes between old and new.
///
/// - parameter old: The previous state of the table view.
/// - parameter new: The current state of the table view.
/// - parameter section: The section where these changes took place.
/// - parameter animation: The animation type.
func applyDiff<T: Collection>(_ old: T, _ new: T, inSection section: Int, withAnimation animation: UITableViewRowAnimation) where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
/// - parameter old: The previous state of the table view.
/// - parameter new: The current state of the table view.
/// - parameter section: The section where these changes took place.
/// - parameter animation: The animation type.
/// - parameter reloadUpdated: Whether or not updated cells should be reloaded (default: true)
func applyDiff<T: Collection>(_ old: T, _ new: T, inSection section: Int, withAnimation animation: UITableViewRowAnimation, reloadUpdated: Bool = true) where T.Iterator.Element: Hashable, T.IndexDistance == Int, T.Index == Int {
let update = ListUpdate(diff(old, new), section)

beginUpdates()
Expand All @@ -31,7 +32,7 @@ public extension UITableView {

// reloadItems is done separately as the update indexes returne by diff() are in respect to the
// "after" state, but the collectionView.reloadItems() call wants the "before" indexPaths.
if update.updates.count > 0 {
if reloadUpdated && update.updates.count > 0 {
beginUpdates()
reloadRows(at: update.updates, with: animation)
endUpdates()
Expand Down

0 comments on commit 25964ab

Please sign in to comment.