Skip to content

Commit

Permalink
Add helper methods to update visible items
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
Ian Terrell committed Jan 13, 2017
1 parent 3c879a6 commit 0b4f73a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 24 deletions.
18 changes: 7 additions & 11 deletions Example/Example/CollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CollectionViewController: UICollectionViewController {
Emoji(identifier: "6", name: "Cool", image: UIImage(named: "cool")!, color: .white),
Emoji(identifier: "7", name: "Dog", image: UIImage(named: "dog")!, color: .white),
Emoji(identifier: "8", name: "Heart", image: UIImage(named: "heart")!, color: .white),
Emoji(identifier: "9", name: "Monkey", image: UIImage(named: "monkey")!, color: .white),
Emoji(identifier: "9", name: "Monkey", image: UIImage(named: "monkey" )!, color: .white),
Emoji(identifier: "10", name: "Nerd", image: UIImage(named: "nerd")!, color: .white),
Emoji(identifier: "11", name: "Wink", image: UIImage(named: "wink")!, color: .white),
Emoji(identifier: "12", name: "Parrot", image: UIImage.gifWithName("parrot")!, color: .white),
Expand All @@ -45,19 +45,15 @@ class CollectionViewController: UICollectionViewController {

func refresh(_ control: UIRefreshControl) {
let newArray = randomize(possibleEmojis)
let (diff, updates) = emojis.tableDiff(newArray)

let visibleIndices = Set((self.collectionView?.indexPathsForVisibleItems ?? []).map { $0.item })
for index in updates.intersection(visibleIndices) {
let cell = self.collectionView?.cellForItem(at: IndexPath(item: index, section: 0)) as? EmojiCell
cell?.update(self.emojis[index])
}

let diff = emojis.tableDiff(newArray)
emojis = newArray
collectionView?.applyDiff(diff, completion: {(finished) -> Void in

collectionView?.apply(diff: diff, completion: { _ in
control.endRefreshing()
}, updateVisibleItem: { item in
let cell = self.collectionView?.cellForItem(at: IndexPath(item: item, section: 0)) as? EmojiCell
cell?.update(self.emojis[item])
})

}

func randomize(_ array: [Emoji]) -> [Emoji] {
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/NumbersViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ class NumbersViewController: UITableViewController {
let new = numbers[index]

let (diff, _) = old.tableDiff(new)
tableView.applyDiff(diff)
tableView.apply(diff: diff)
}
}
85 changes: 73 additions & 12 deletions Sources/UIKit+Diff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import UIKit

public extension UITableView {
public func applyDiff(_ diff: Set<DiffStep<Int>>,
section: Int = 0,
insertRowAnimation: UITableViewRowAnimation = .automatic,
deleteRowAnimation: UITableViewRowAnimation = .automatic)
public func apply(diff: Set<DiffStep<Int>>,
section: Int = 0,
insertRowAnimation: UITableViewRowAnimation = .automatic,
deleteRowAnimation: UITableViewRowAnimation = .automatic)
{
beginUpdates()
var insertionIndexPaths: [IndexPath] = []
Expand All @@ -32,10 +32,27 @@ public extension UITableView {
deleteRows(at: deletionIndexPaths, with: deleteRowAnimation)
endUpdates()
}

public func updateVisible(rows updates: Set<Int>, _ callback: (Int) -> Void) {
guard let visibleRows = indexPathsForVisibleRows?.map({ $0.row }) else {
return
}
updates.intersection(Set(visibleRows)).forEach(callback)
}

public func apply(diff: (diff: Set<DiffStep<Int>>, updates: Set<Int>),
section: Int = 0,
insertRowAnimation insert: UITableViewRowAnimation = .automatic,
deleteRowAnimation delete: UITableViewRowAnimation = .automatic,
updateVisibleRow: (Int) -> Void)
{
apply(diff: diff.diff, section: section, insertRowAnimation: insert, deleteRowAnimation: delete)
updateVisible(rows: diff.updates, updateVisibleRow)
}

public func applyDiff(_ diff: Set<DiffStep<IndexPath>>,
insertRowAnimation: UITableViewRowAnimation = .automatic,
deleteRowAnimation: UITableViewRowAnimation = .automatic)
public func apply(diff: Set<DiffStep<IndexPath>>,
insertRowAnimation: UITableViewRowAnimation = .automatic,
deleteRowAnimation: UITableViewRowAnimation = .automatic)
{
beginUpdates()
var insertionIndexPaths: [IndexPath] = []
Expand All @@ -54,12 +71,29 @@ public extension UITableView {
deleteRows(at: deletionIndexPaths, with: deleteRowAnimation)
endUpdates()
}

@objc(updateVisibleRowByIndexPaths::)
public func updateVisible(rows updates: Set<IndexPath>, _ callback: (IndexPath) -> Void) {
guard let visibleRows = indexPathsForVisibleRows else {
return
}
updates.intersection(Set(visibleRows)).forEach(callback)
}

public func apply(diff: (diff: Set<DiffStep<IndexPath>>, updates: Set<IndexPath>),
insertRowAnimation insert: UITableViewRowAnimation = .automatic,
deleteRowAnimation delete: UITableViewRowAnimation = .automatic,
updateVisibleRow: (IndexPath) -> Void)
{
apply(diff: diff.diff, insertRowAnimation: insert, deleteRowAnimation: delete)
updateVisible(rows: diff.updates, updateVisibleRow)
}
}

public extension UICollectionView {
public func applyDiff(_ diff: Set<DiffStep<Int>>,
section: Int = 0,
completion: ((Bool) -> Void)? = nil)
public func apply(diff: Set<DiffStep<Int>>,
section: Int = 0,
completion: ((Bool) -> Void)? = nil)
{
performBatchUpdates({
var insertionIndexPaths: [IndexPath] = []
Expand All @@ -80,8 +114,22 @@ public extension UICollectionView {
}, completion: completion)

}

public func applyDiff(_ diff: Set<DiffStep<IndexPath>>, completion: ((Bool) -> Void)? = nil) {

public func updateVisible(items updates: Set<Int>, _ callback: (Int) -> Void) {
let visibleItems = indexPathsForVisibleItems.map({ $0.row })
updates.intersection(Set(visibleItems)).forEach(callback)
}

public func apply(diff: (diff: Set<DiffStep<Int>>, updates: Set<Int>),
section: Int = 0,
completion: ((Bool) -> Void)? = nil,
updateVisibleItem: (Int) -> Void)
{
apply(diff: diff.diff, section: section, completion: completion)
updateVisible(items: diff.updates, updateVisibleItem)
}

public func apply(diff: Set<DiffStep<IndexPath>>, completion: ((Bool) -> Void)? = nil) {
performBatchUpdates({
var insertionIndexPaths: [IndexPath] = []
var deletionIndexPaths: [IndexPath] = []
Expand All @@ -99,4 +147,17 @@ public extension UICollectionView {
self.deleteItems(at: deletionIndexPaths)
}, completion: completion)
}

@objc(updateVisibleItemsByIndexPath::)
public func updateVisible(items updates: Set<IndexPath>, _ callback: (IndexPath) -> Void) {
updates.intersection(Set(indexPathsForVisibleItems)).forEach(callback)
}

public func apply(diff: (diff: Set<DiffStep<IndexPath>>, updates: Set<IndexPath>),
completion: ((Bool) -> Void)? = nil,
updateVisibleItem: (IndexPath) -> Void)
{
apply(diff: diff.diff, completion: completion)
updateVisible(items: diff.updates, updateVisibleItem)
}
}

0 comments on commit 0b4f73a

Please sign in to comment.