Skip to content

Instantly share code, notes, and snippets.

@liuzhida33
Created September 2, 2019 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuzhida33/67ae091180a3629cc9ac8a6754634d90 to your computer and use it in GitHub Desktop.
Save liuzhida33/67ae091180a3629cc9ac8a6754634d90 to your computer and use it in GitHub Desktop.
IGListKit Extension
import IGListKit
public protocol Diffable: Equatable {
var diffIdentifier: String { get }
}
final class DiffableBox<T: Diffable>: ListDiffable {
let value: T
let identifier: NSObjectProtocol
let equal: (T, T) -> Bool
init(value: T, identifier: NSObjectProtocol, equal: @escaping (T, T) -> Bool) {
self.value = value
self.identifier = identifier
self.equal = equal
}
func diffIdentifier() -> NSObjectProtocol {
return identifier as NSObjectProtocol
}
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
guard let other = object as? DiffableBox<T> else { return false }
return equal(value, other.value)
}
}
extension String: Diffable {
public var diffIdentifier: String { return self }
}
extension Int: Diffable {
public var diffIdentifier: String { return String(self) }
}
extension Sequence where Iterator.Element: Diffable {
func diffable() -> [ListDiffable] {
return map { DiffableBox(value: $0, identifier: $0.diffIdentifier as NSObjectProtocol, equal: ==) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment