1
+ import UIKit
2
+
1
3
/// A class for backporting `NSDiffableDataSourceSnapshot` introduced in iOS 13.0+, macOS 10.15+, tvOS 13.0+.
2
4
/// Represents the mutable state of diffable data source of UI.
3
5
public struct DiffableDataSourceSnapshot < SectionIdentifierType: Hashable , ItemIdentifierType: Hashable > {
4
6
internal var structure = SnapshotStructure < SectionIdentifierType , ItemIdentifierType > ( )
5
7
8
+ private let forceFallback : Bool
9
+ private var _nativeSnapshot : Any ?
10
+ @available ( iOS 13 . 0 , * )
11
+ internal var nativeSnapshot : NSDiffableDataSourceSnapshot < SectionIdentifierType , ItemIdentifierType > {
12
+ get {
13
+ return _nativeSnapshot as! NSDiffableDataSourceSnapshot < SectionIdentifierType , ItemIdentifierType >
14
+ }
15
+ set {
16
+ _nativeSnapshot = newValue
17
+ }
18
+ }
19
+
6
20
/// Creates a new empty snapshot object.
7
- public init ( ) { }
21
+ public init ( ) {
22
+ self . init ( forceFallback: false )
23
+ }
24
+
25
+ internal init ( forceFallback: Bool ) {
26
+ self . forceFallback = forceFallback
27
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
28
+ nativeSnapshot = . init( )
29
+ return
30
+ }
31
+ }
32
+
33
+ @available ( iOS 13 . 0 , * )
34
+ static func from( nativeSnapshot: NSDiffableDataSourceSnapshot < SectionIdentifierType , ItemIdentifierType > ) -> Self {
35
+ var snapshot = DiffableDataSourceSnapshot < SectionIdentifierType , ItemIdentifierType > ( )
36
+ snapshot. nativeSnapshot = nativeSnapshot
37
+ return snapshot
38
+ }
8
39
9
40
/// The number of item identifiers in the snapshot.
10
41
public var numberOfItems : Int {
42
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
43
+ return nativeSnapshot. numberOfItems
44
+ }
11
45
return itemIdentifiers. count
12
46
}
13
47
14
48
/// The number of section identifiers in the snapshot.
15
49
public var numberOfSections : Int {
50
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
51
+ return nativeSnapshot. numberOfSections
52
+ }
16
53
return sectionIdentifiers. count
17
54
}
18
55
19
56
/// All section identifiers in the snapshot.
20
57
public var sectionIdentifiers : [ SectionIdentifierType ] {
58
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
59
+ return nativeSnapshot. sectionIdentifiers
60
+ }
21
61
return structure. allSectionIDs
22
62
}
23
63
24
64
/// All item identifiers in the snapshot.
25
65
public var itemIdentifiers : [ ItemIdentifierType ] {
66
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
67
+ return nativeSnapshot. itemIdentifiers
68
+ }
26
69
return structure. allItemIDs
27
70
}
28
71
@@ -33,6 +76,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
33
76
///
34
77
/// - Returns: The number of item identifiers in the specified section.
35
78
public func numberOfItems( inSection identifier: SectionIdentifierType ) -> Int {
79
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
80
+ return nativeSnapshot. numberOfItems ( inSection: identifier)
81
+ }
36
82
return itemIdentifiers ( inSection: identifier) . count
37
83
}
38
84
@@ -43,6 +89,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
43
89
///
44
90
/// - Returns: The item identifiers in the specified section.
45
91
public func itemIdentifiers( inSection identifier: SectionIdentifierType ) -> [ ItemIdentifierType ] {
92
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
93
+ return nativeSnapshot. itemIdentifiers ( inSection: identifier)
94
+ }
46
95
return structure. items ( in: identifier)
47
96
}
48
97
@@ -53,6 +102,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
53
102
///
54
103
/// - Returns: A section identifier containing the specified item.
55
104
public func sectionIdentifier( containingItem identifier: ItemIdentifierType ) -> SectionIdentifierType ? {
105
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
106
+ return nativeSnapshot. sectionIdentifier ( containingItem: identifier)
107
+ }
56
108
return structure. section ( containing: identifier)
57
109
}
58
110
@@ -63,6 +115,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
63
115
///
64
116
/// - Returns: An index of the specified item.
65
117
public func indexOfItem( _ identifier: ItemIdentifierType ) -> Int ? {
118
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
119
+ return nativeSnapshot. indexOfItem ( identifier)
120
+ }
66
121
return itemIdentifiers. firstIndex { $0. isEqualHash ( to: identifier) }
67
122
}
68
123
@@ -73,6 +128,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
73
128
///
74
129
/// - Returns: An index of the specified section.
75
130
public func indexOfSection( _ identifier: SectionIdentifierType ) -> Int ? {
131
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
132
+ return nativeSnapshot. indexOfSection ( identifier)
133
+ }
76
134
return sectionIdentifiers. firstIndex { $0. isEqualHash ( to: identifier) }
77
135
}
78
136
@@ -82,6 +140,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
82
140
/// - identifiers: The item identifiers to be appended.
83
141
/// - sectionIdentifier: An identifier of section to append the given identiciers.
84
142
public mutating func appendItems( _ identifiers: [ ItemIdentifierType ] , toSection sectionIdentifier: SectionIdentifierType ? = nil ) {
143
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
144
+ return nativeSnapshot. appendItems ( identifiers, toSection: sectionIdentifier)
145
+ }
85
146
structure. append ( itemIDs: identifiers, to: sectionIdentifier)
86
147
}
87
148
@@ -91,6 +152,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
91
152
/// - identifiers: The item identifiers to be inserted.
92
153
/// - beforeIdentifier: An identifier of item.
93
154
public mutating func insertItems( _ identifiers: [ ItemIdentifierType ] , beforeItem beforeIdentifier: ItemIdentifierType ) {
155
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
156
+ return nativeSnapshot. insertItems ( identifiers, beforeItem: beforeIdentifier)
157
+ }
94
158
structure. insert ( itemIDs: identifiers, before: beforeIdentifier)
95
159
}
96
160
@@ -100,6 +164,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
100
164
/// - identifiers: The item identifiers to be inserted.
101
165
/// - afterIdentifier: An identifier of item.
102
166
public mutating func insertItems( _ identifiers: [ ItemIdentifierType ] , afterItem afterIdentifier: ItemIdentifierType ) {
167
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
168
+ return nativeSnapshot. insertItems ( identifiers, afterItem: afterIdentifier)
169
+ }
103
170
structure. insert ( itemIDs: identifiers, after: afterIdentifier)
104
171
}
105
172
@@ -108,11 +175,17 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
108
175
/// - Parameters:
109
176
/// - identifiers: The item identifiers to be deleted.
110
177
public mutating func deleteItems( _ identifiers: [ ItemIdentifierType ] ) {
178
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
179
+ return nativeSnapshot. deleteItems ( identifiers)
180
+ }
111
181
structure. remove ( itemIDs: identifiers)
112
182
}
113
183
114
184
/// Deletes the all items in the snapshot.
115
185
public mutating func deleteAllItems( ) {
186
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
187
+ return nativeSnapshot. deleteAllItems ( )
188
+ }
116
189
structure. removeAllItems ( )
117
190
}
118
191
@@ -122,6 +195,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
122
195
/// - identifier: An item identifier to be moved.
123
196
/// - toIdentifier: An identifier of item.
124
197
public mutating func moveItem( _ identifier: ItemIdentifierType , beforeItem toIdentifier: ItemIdentifierType ) {
198
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
199
+ return nativeSnapshot. moveItem ( identifier, beforeItem: toIdentifier)
200
+ }
125
201
structure. move ( itemID: identifier, before: toIdentifier)
126
202
}
127
203
@@ -131,6 +207,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
131
207
/// - identifier: An item identifier to be moved.
132
208
/// - toIdentifier: An identifier of item.
133
209
public mutating func moveItem( _ identifier: ItemIdentifierType , afterItem toIdentifier: ItemIdentifierType ) {
210
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
211
+ return nativeSnapshot. moveItem ( identifier, afterItem: toIdentifier)
212
+ }
134
213
structure. move ( itemID: identifier, after: toIdentifier)
135
214
}
136
215
@@ -139,6 +218,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
139
218
/// - Parameters:
140
219
/// - identifiers: The item identifiers to be reloaded.
141
220
public mutating func reloadItems( _ identifiers: [ ItemIdentifierType ] ) {
221
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
222
+ return nativeSnapshot. reloadItems ( identifiers)
223
+ }
142
224
structure. update ( itemIDs: identifiers)
143
225
}
144
226
@@ -147,6 +229,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
147
229
/// - Parameters:
148
230
/// - identifiers: The section identifiers to be appended.
149
231
public mutating func appendSections( _ identifiers: [ SectionIdentifierType ] ) {
232
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
233
+ return nativeSnapshot. appendSections ( identifiers)
234
+ }
150
235
structure. append ( sectionIDs: identifiers)
151
236
}
152
237
@@ -156,6 +241,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
156
241
/// - identifiers: The section identifiers to be inserted.
157
242
/// - toIdentifier: An identifier of setion.
158
243
public mutating func insertSections( _ identifiers: [ SectionIdentifierType ] , beforeSection toIdentifier: SectionIdentifierType ) {
244
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
245
+ return nativeSnapshot. insertSections ( identifiers, beforeSection: toIdentifier)
246
+ }
159
247
structure. insert ( sectionIDs: identifiers, before: toIdentifier)
160
248
}
161
249
@@ -165,6 +253,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
165
253
/// - identifiers: The section identifiers to be inserted.
166
254
/// - toIdentifier: An identifier of setion.
167
255
public mutating func insertSections( _ identifiers: [ SectionIdentifierType ] , afterSection toIdentifier: SectionIdentifierType ) {
256
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
257
+ return nativeSnapshot. insertSections ( identifiers, afterSection: toIdentifier)
258
+ }
168
259
structure. insert ( sectionIDs: identifiers, after: toIdentifier)
169
260
}
170
261
@@ -173,6 +264,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
173
264
/// - Parameters:
174
265
/// - identifiers: The section identifiers to be deleted.
175
266
public mutating func deleteSections( _ identifiers: [ SectionIdentifierType ] ) {
267
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
268
+ return nativeSnapshot. deleteSections ( identifiers)
269
+ }
176
270
structure. remove ( sectionIDs: identifiers)
177
271
}
178
272
@@ -182,6 +276,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
182
276
/// - identifier: A section identifier to be moved.
183
277
/// - toIdentifier: An identifier of section.
184
278
public mutating func moveSection( _ identifier: SectionIdentifierType , beforeSection toIdentifier: SectionIdentifierType ) {
279
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
280
+ return nativeSnapshot. moveSection ( identifier, beforeSection: toIdentifier)
281
+ }
185
282
structure. move ( sectionID: identifier, before: toIdentifier)
186
283
}
187
284
@@ -191,6 +288,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
191
288
/// - identifier: A section identifier to be moved.
192
289
/// - toIdentifier: An identifier of section.
193
290
public mutating func moveSection( _ identifier: SectionIdentifierType , afterSection toIdentifier: SectionIdentifierType ) {
291
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
292
+ return nativeSnapshot. moveSection ( identifier, afterSection: toIdentifier)
293
+ }
194
294
structure. move ( sectionID: identifier, after: toIdentifier)
195
295
}
196
296
@@ -199,6 +299,9 @@ public struct DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemId
199
299
/// - Parameters:
200
300
/// - identifiers: The section identifiers to be reloaded.
201
301
public mutating func reloadSections( _ identifiers: [ SectionIdentifierType ] ) {
302
+ if #available( iOS 13 . 0 , * ) , !forceFallback {
303
+ return nativeSnapshot. reloadSections ( identifiers)
304
+ }
202
305
structure. update ( sectionIDs: identifiers)
203
306
}
204
307
}
0 commit comments