-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathDiffableDataSourceSnapshot.swift
More file actions
208 lines (183 loc) · 8.1 KB
/
DiffableDataSourceSnapshot.swift
File metadata and controls
208 lines (183 loc) · 8.1 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/// A class for backporting `NSDiffableDataSourceSnapshot` introduced in iOS 13.0+, macOS 10.15+, tvOS 13.0+.
/// Represents the mutable state of diffable data source of UI.
public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIdentifierType: Hashable> {
internal var structure = SnapshotStructure<SectionIdentifierType, ItemIdentifierType>()
/// Creates a new empty snapshot object.
public init() {}
/// The number of item identifiers in the snapshot.
public var numberOfItems: Int {
var count = 0
for section in structure.sections {
count += numberOfItems(inSection: section.differenceIdentifier)
}
return count
}
/// The number of section identifiers in the snapshot.
public var numberOfSections: Int {
return structure.sections.count
}
/// All section identifiers in the snapshot.
public var sectionIdentifiers: [SectionIdentifierType] {
return structure.allSectionIDs
}
/// All item identifiers in the snapshot.
public var itemIdentifiers: [ItemIdentifierType] {
return structure.allItemIDs
}
/// Returns the number of item identifiers in the specified section.
///
/// - Parameters:
/// - identifier: An identifier of section.
///
/// - Returns: The number of item identifiers in the specified section.
public func numberOfItems(inSection identifier: SectionIdentifierType) -> Int {
return structure.numberOfItems(in: identifier)
}
/// Returns the item identifiers in the specified section.
///
/// - Parameters:
/// - identifier: An identifier of section.
///
/// - Returns: The item identifiers in the specified section.
public func itemIdentifiers(inSection identifier: SectionIdentifierType) -> [ItemIdentifierType] {
return structure.items(in: identifier)
}
/// Returns a section identifier containing the specified item.
///
/// - Parameters:
/// - identifier: An identifier of item.
///
/// - Returns: A section identifier containing the specified item.
public func sectionIdentifier(containingItem identifier: ItemIdentifierType) -> SectionIdentifierType? {
return structure.section(containing: identifier)
}
/// Returns an index of the specified item.
///
/// - Parameters:
/// - identifier: An identifier of item.
///
/// - Returns: An index of the specified item.
public func indexOfItem(_ identifier: ItemIdentifierType) -> Int? {
return itemIdentifiers.firstIndex { $0.isEqualHash(to: identifier) }
}
/// Returns an index of the specified section.
///
/// - Parameters:
/// - identifier: An identifier of section.
///
/// - Returns: An index of the specified section.
public func indexOfSection(_ identifier: SectionIdentifierType) -> Int? {
return sectionIdentifiers.firstIndex { $0.isEqualHash(to: identifier) }
}
/// Appends the given item identifiers to the specified section or last section.
///
/// - Parameters:
/// - identifiers: The item identifiers to be appended.
/// - sectionIdentifier: An identifier of section to append the given identiciers.
public mutating func appendItems(_ identifiers: [ItemIdentifierType], toSection sectionIdentifier: SectionIdentifierType? = nil) {
structure.append(itemIDs: identifiers, to: sectionIdentifier)
}
/// Inserts the given item identifiers before the specified item.
///
/// - Parameters:
/// - identifiers: The item identifiers to be inserted.
/// - beforeIdentifier: An identifier of item.
public mutating func insertItems(_ identifiers: [ItemIdentifierType], beforeItem beforeIdentifier: ItemIdentifierType) {
structure.insert(itemIDs: identifiers, before: beforeIdentifier)
}
/// Inserts the given item identifiers after the specified item.
///
/// - Parameters:
/// - identifiers: The item identifiers to be inserted.
/// - afterIdentifier: An identifier of item.
public mutating func insertItems(_ identifiers: [ItemIdentifierType], afterItem afterIdentifier: ItemIdentifierType) {
structure.insert(itemIDs: identifiers, after: afterIdentifier)
}
/// Deletes the specified items.
///
/// - Parameters:
/// - identifiers: The item identifiers to be deleted.
public mutating func deleteItems(_ identifiers: [ItemIdentifierType]) {
structure.remove(itemIDs: identifiers)
}
/// Deletes the all items in the snapshot.
public mutating func deleteAllItems() {
structure.removeAllItems()
}
/// Moves the given item identifier before the specified item.
///
/// - Parameters:
/// - identifier: An item identifier to be moved.
/// - toIdentifier: An identifier of item.
public mutating func moveItem(_ identifier: ItemIdentifierType, beforeItem toIdentifier: ItemIdentifierType) {
structure.move(itemID: identifier, before: toIdentifier)
}
/// Moves the given item identifier after the specified item.
///
/// - Parameters:
/// - identifier: An item identifier to be moved.
/// - toIdentifier: An identifier of item.
public mutating func moveItem(_ identifier: ItemIdentifierType, afterItem toIdentifier: ItemIdentifierType) {
structure.move(itemID: identifier, after: toIdentifier)
}
/// Reloads the specified items.
///
/// - Parameters:
/// - identifiers: The item identifiers to be reloaded.
public mutating func reloadItems(_ identifiers: [ItemIdentifierType]) {
structure.update(itemIDs: identifiers)
}
/// Appends the given section identifiers to the end of sections.
///
/// - Parameters:
/// - identifiers: The section identifiers to be appended.
public mutating func appendSections(_ identifiers: [SectionIdentifierType]) {
structure.append(sectionIDs: identifiers)
}
/// Inserts the given section identifiers before the specified section.
///
/// - Parameters:
/// - identifiers: The section identifiers to be inserted.
/// - toIdentifier: An identifier of setion.
public mutating func insertSections(_ identifiers: [SectionIdentifierType], beforeSection toIdentifier: SectionIdentifierType) {
structure.insert(sectionIDs: identifiers, before: toIdentifier)
}
/// Inserts the given section identifiers after the specified section.
///
/// - Parameters:
/// - identifiers: The section identifiers to be inserted.
/// - toIdentifier: An identifier of setion.
public mutating func insertSections(_ identifiers: [SectionIdentifierType], afterSection toIdentifier: SectionIdentifierType) {
structure.insert(sectionIDs: identifiers, after: toIdentifier)
}
/// Deletes the specified sections.
///
/// - Parameters:
/// - identifiers: The section identifiers to be deleted.
public mutating func deleteSections(_ identifiers: [SectionIdentifierType]) {
structure.remove(sectionIDs: identifiers)
}
/// Moves the given section identifier before the specified section.
///
/// - Parameters:
/// - identifier: A section identifier to be moved.
/// - toIdentifier: An identifier of section.
public mutating func moveSection(_ identifier: SectionIdentifierType, beforeSection toIdentifier: SectionIdentifierType) {
structure.move(sectionID: identifier, before: toIdentifier)
}
/// Moves the given section identifier after the specified section.
///
/// - Parameters:
/// - identifier: A section identifier to be moved.
/// - toIdentifier: An identifier of section.
public mutating func moveSection(_ identifier: SectionIdentifierType, afterSection toIdentifier: SectionIdentifierType) {
structure.move(sectionID: identifier, after: toIdentifier)
}
/// Reloads the specified sections.
///
/// - Parameters:
/// - identifiers: The section identifiers to be reloaded.
public mutating func reloadSections(_ identifiers: [SectionIdentifierType]) {
structure.update(sectionIDs: identifiers)
}
}