Created
September 2, 2019 01:07
-
-
Save liuzhida33/67ae091180a3629cc9ac8a6754634d90 to your computer and use it in GitHub Desktop.
IGListKit Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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