-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDiffable.swift
95 lines (69 loc) · 1.97 KB
/
Diffable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// Diffable.swift
// DataSource
//
// Created by Matthias Buchetics on 17/03/2017.
// Copyright © 2017 aaa - all about apps GmbH. All rights reserved.
//
// MARK: - Diffable
public protocol Diffable {
var diffIdentifier: String { get }
func isEqualToDiffable(_ other: Diffable?) -> Bool
}
public extension Diffable where Self: Hashable {
var diffIdentifier: String {
return String(hashValue)
}
}
public extension Diffable where Self: Equatable {
func isEqualToDiffable(_ other: Diffable?) -> Bool {
guard let other = other as? Self else { return false }
return self == other
}
}
extension String: Diffable { }
// MARK: - DiffableSection
public struct DiffableSection {
let identifier: String
let content: Any?
let rowCount: Int
let rowClosure: (Int) -> RowType
}
extension DiffableSection: Diffable {
public var diffIdentifier: String {
return identifier
}
public func isEqualToDiffable(_ other: Diffable?) -> Bool {
guard let other = other as? DiffableSection else { return false }
return self == other
}
}
extension DiffableSection: Equatable {
public static func ==(lhs: DiffableSection, rhs: DiffableSection) -> Bool {
guard lhs.identifier == rhs.identifier else {
return false
}
if lhs.content == nil && rhs.content == nil {
return true
}
if let a = lhs.content as? Diffable, let b = rhs.content as? Diffable {
return a.isEqualToDiffable(b)
}
return false
}
}
extension DiffableSection: Collection {
public typealias Index = Int
public var startIndex: Int {
return 0
}
public var endIndex: Int {
return rowCount
}
public subscript(i: Int) -> RowType {
return rowClosure(i)
}
public func index(after i: Int) -> Int {
return i + 1
}
}