-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathUIKitExtension.swift
More file actions
328 lines (300 loc) · 15.4 KB
/
UIKitExtension.swift
File metadata and controls
328 lines (300 loc) · 15.4 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#if os(iOS) || os(tvOS)
import UIKit
public extension UITableView {
/// Enum describing various scenarios, that might have happened while Table View was being updated
enum CommitCompletion {
/// Table View was updated without animation
case nonAnimated
/// Table View animation did not complete, `performBatchUpdates` ended with `finished = false`
case interrupted
/// Table View animation was completed.
case completed
}
/// Applies multiple animated updates in stages using `StagedChangeset`.
///
/// - Note: There are combination of changes that crash when applied simultaneously in `performBatchUpdates`.
/// Assumes that `StagedChangeset` has a minimum staged changesets to avoid it.
/// The data of the data-source needs to be updated synchronously before `performBatchUpdates` in every stages.
///
/// - Parameters:
/// - stagedChangeset: A staged set of changes.
/// - animation: An option to animate the updates.
/// - interrupt: A closure that takes an changeset as its argument and returns `true` if the animated
/// updates should be stopped and performed reloadData. Default is nil.
/// - changesetCompleted: Completion handler that is executed after each animted or non-animated changeset commit.
/// - completedChangeset: The completed changeset.
/// - commitKind: Enum describing how was update handled.
/// - setData: A closure that takes the collection as a parameter.
/// The collection should be set to data-source of UITableView.
@available(iOS 11.0, tvOS 11.0, *)
func reload<C>(
using stagedChangeset: StagedChangeset<C>,
with animation: @autoclosure () -> RowAnimation,
interrupt: ((Changeset<C>) -> Bool)? = nil,
changesetCompleted: @escaping (_ completedChangeset: Changeset<C>, _ commitKind: CommitCompletion) -> Void,
setData: (C) -> Void
) {
_reload(
using: stagedChangeset,
deleteSectionsAnimation: animation,
insertSectionsAnimation: animation,
reloadSectionsAnimation: animation,
deleteRowsAnimation: animation,
insertRowsAnimation: animation,
reloadRowsAnimation: animation,
interrupt: interrupt,
changesetCompleted: changesetCompleted,
setData: setData
)
}
/// Applies multiple animated updates in stages using `StagedChangeset`.
///
/// - Note: There are combination of changes that crash when applied simultaneously in `performBatchUpdates`.
/// Assumes that `StagedChangeset` has a minimum staged changesets to avoid it.
/// The data of the data-source needs to be updated synchronously before `performBatchUpdates` in every stages.
///
/// - Parameters:
/// - stagedChangeset: A staged set of changes.
/// - deleteSectionsAnimation: An option to animate the section deletion.
/// - insertSectionsAnimation: An option to animate the section insertion.
/// - reloadSectionsAnimation: An option to animate the section reload.
/// - deleteRowsAnimation: An option to animate the row deletion.
/// - insertRowsAnimation: An option to animate the row insertion.
/// - reloadRowsAnimation: An option to animate the row reload.
/// - interrupt: A closure that takes an changeset as its argument and returns `true` if the animated
/// updates should be stopped and performed reloadData. Default is nil.
/// - changesetCompleted: Completion handler that is executed after each animted or non-animated changeset commit.
/// - completedChangeset: The completed changeset.
/// - commitKind: Enum describing how was update handled.
/// - setData: A closure that takes the collection as a parameter.
/// The collection should be set to data-source of UITableView.
@available(iOS 11.0, tvOS 11.0, *)
// swiftlint:disable:next function_parameter_count
func reload<C>(
using stagedChangeset: StagedChangeset<C>,
deleteSectionsAnimation: @autoclosure () -> RowAnimation,
insertSectionsAnimation: @autoclosure () -> RowAnimation,
reloadSectionsAnimation: @autoclosure () -> RowAnimation,
deleteRowsAnimation: @autoclosure () -> RowAnimation,
insertRowsAnimation: @autoclosure () -> RowAnimation,
reloadRowsAnimation: @autoclosure () -> RowAnimation,
interrupt: ((Changeset<C>) -> Bool)? = nil,
changesetCompleted: @escaping (_ completedChangeset: Changeset<C>, _ commitKind: CommitCompletion) -> Void,
setData: (C) -> Void
) {
_reload(
using: stagedChangeset,
deleteSectionsAnimation: deleteSectionsAnimation,
insertSectionsAnimation: insertSectionsAnimation,
reloadSectionsAnimation: reloadSectionsAnimation,
deleteRowsAnimation: deleteRowsAnimation,
insertRowsAnimation: insertRowsAnimation,
reloadRowsAnimation: reloadRowsAnimation,
interrupt: interrupt,
changesetCompleted: nil,
setData: setData
)
}
/// Applies multiple animated updates in stages using `StagedChangeset`.
///
/// - Note: There are combination of changes that crash when applied simultaneously in `performBatchUpdates`.
/// Assumes that `StagedChangeset` has a minimum staged changesets to avoid it.
/// The data of the data-source needs to be updated synchronously before `performBatchUpdates` in every stages.
///
/// - Parameters:
/// - stagedChangeset: A staged set of changes.
/// - animation: An option to animate the updates.
/// - interrupt: A closure that takes an changeset as its argument and returns `true` if the animated
/// updates should be stopped and performed reloadData. Default is nil.
/// - setData: A closure that takes the collection as a parameter.
/// The collection should be set to data-source of UITableView.
func reload<C>(
using stagedChangeset: StagedChangeset<C>,
with animation: @autoclosure () -> RowAnimation,
interrupt: ((Changeset<C>) -> Bool)? = nil,
setData: (C) -> Void
) {
_reload(
using: stagedChangeset,
deleteSectionsAnimation: animation,
insertSectionsAnimation: animation,
reloadSectionsAnimation: animation,
deleteRowsAnimation: animation,
insertRowsAnimation: animation,
reloadRowsAnimation: animation,
interrupt: interrupt,
changesetCompleted: nil,
setData: setData
)
}
/// Applies multiple animated updates in stages using `StagedChangeset`.
///
/// - Note: There are combination of changes that crash when applied simultaneously in `performBatchUpdates`.
/// Assumes that `StagedChangeset` has a minimum staged changesets to avoid it.
/// The data of the data-source needs to be updated synchronously before `performBatchUpdates` in every stages.
///
/// - Parameters:
/// - stagedChangeset: A staged set of changes.
/// - deleteSectionsAnimation: An option to animate the section deletion.
/// - insertSectionsAnimation: An option to animate the section insertion.
/// - reloadSectionsAnimation: An option to animate the section reload.
/// - deleteRowsAnimation: An option to animate the row deletion.
/// - insertRowsAnimation: An option to animate the row insertion.
/// - reloadRowsAnimation: An option to animate the row reload.
/// - interrupt: A closure that takes an changeset as its argument and returns `true` if the animated
/// updates should be stopped and performed reloadData. Default is nil.
/// - setData: A closure that takes the collection as a parameter.
/// The collection should be set to data-source of UITableView.
func _reload<C>(
using stagedChangeset: StagedChangeset<C>,
deleteSectionsAnimation: @autoclosure () -> RowAnimation,
insertSectionsAnimation: @autoclosure () -> RowAnimation,
reloadSectionsAnimation: @autoclosure () -> RowAnimation,
deleteRowsAnimation: @autoclosure () -> RowAnimation,
insertRowsAnimation: @autoclosure () -> RowAnimation,
reloadRowsAnimation: @autoclosure () -> RowAnimation,
interrupt: ((Changeset<C>) -> Bool)? = nil,
setData: (C) -> Void
) {
_reload(
using: stagedChangeset,
deleteSectionsAnimation: deleteSectionsAnimation,
insertSectionsAnimation: insertSectionsAnimation,
reloadSectionsAnimation: reloadSectionsAnimation,
deleteRowsAnimation: deleteRowsAnimation,
insertRowsAnimation: insertRowsAnimation,
reloadRowsAnimation: reloadRowsAnimation,
interrupt: interrupt,
changesetCompleted: nil,
setData: setData
)
}
// swiftlint:disable:next function_parameter_count
private func _reload<C>(
using stagedChangeset: StagedChangeset<C>,
deleteSectionsAnimation: () -> RowAnimation,
insertSectionsAnimation: () -> RowAnimation,
reloadSectionsAnimation: () -> RowAnimation,
deleteRowsAnimation: () -> RowAnimation,
insertRowsAnimation: () -> RowAnimation,
reloadRowsAnimation: () -> RowAnimation,
interrupt: ((Changeset<C>) -> Bool)? = nil,
changesetCompleted: ((Changeset<C>, CommitCompletion) -> Void)?,
setData: (C) -> Void
) {
if case .none = window, let lastChangeset = stagedChangeset.last {
setData(lastChangeset.data)
reloadData()
changesetCompleted?(lastChangeset, .nonAnimated)
return
}
for changeset in stagedChangeset {
if let interrupt = interrupt, interrupt(changeset), let lastChangeset = stagedChangeset.last {
setData(lastChangeset.data)
reloadData()
changesetCompleted?(lastChangeset, .nonAnimated)
return
}
_performBatch(
updates: {
setData(changeset.data)
if !changeset.sectionDeleted.isEmpty {
deleteSections(IndexSet(changeset.sectionDeleted), with: deleteSectionsAnimation())
}
if !changeset.sectionInserted.isEmpty {
insertSections(IndexSet(changeset.sectionInserted), with: insertSectionsAnimation())
}
if !changeset.sectionUpdated.isEmpty {
reloadSections(IndexSet(changeset.sectionUpdated), with: reloadSectionsAnimation())
}
for (source, target) in changeset.sectionMoved {
moveSection(source, toSection: target)
}
if !changeset.elementDeleted.isEmpty {
deleteRows(at: changeset.elementDeleted.map { IndexPath(row: $0.element, section: $0.section) }, with: deleteRowsAnimation())
}
if !changeset.elementInserted.isEmpty {
insertRows(at: changeset.elementInserted.map { IndexPath(row: $0.element, section: $0.section) }, with: insertRowsAnimation())
}
if !changeset.elementUpdated.isEmpty {
reloadRows(at: changeset.elementUpdated.map { IndexPath(row: $0.element, section: $0.section) }, with: reloadRowsAnimation())
}
for (source, target) in changeset.elementMoved {
moveRow(at: IndexPath(row: source.element, section: source.section), to: IndexPath(row: target.element, section: target.section))
}
},
completion: changesetCompleted.flatMap { completion -> ((Bool) -> Void) in
return { finished in completion(changeset, finished ? .completed : .interrupted) }
}
)
}
}
private func _performBatch(updates: () -> Void, completion: ((Bool) -> Void)?) {
if #available(iOS 11.0, tvOS 11.0, *) {
performBatchUpdates(updates, completion: completion)
}
else {
beginUpdates()
updates()
endUpdates()
}
}
}
public extension UICollectionView {
/// Applies multiple animated updates in stages using `StagedChangeset`.
///
/// - Note: There are combination of changes that crash when applied simultaneously in `performBatchUpdates`.
/// Assumes that `StagedChangeset` has a minimum staged changesets to avoid it.
/// The data of the data-source needs to be updated synchronously before `performBatchUpdates` in every stages.
///
/// - Parameters:
/// - stagedChangeset: A staged set of changes.
/// - interrupt: A closure that takes an changeset as its argument and returns `true` if the animated
/// updates should be stopped and performed reloadData. Default is nil.
/// - setData: A closure that takes the collection as a parameter.
/// The collection should be set to data-source of UICollectionView.
func reload<C>(
using stagedChangeset: StagedChangeset<C>,
interrupt: ((Changeset<C>) -> Bool)? = nil,
setData: (C) -> Void
) {
if case .none = window, let data = stagedChangeset.last?.data {
setData(data)
return reloadData()
}
for changeset in stagedChangeset {
if let interrupt = interrupt, interrupt(changeset), let data = stagedChangeset.last?.data {
setData(data)
return reloadData()
}
performBatchUpdates({
setData(changeset.data)
if !changeset.sectionDeleted.isEmpty {
deleteSections(IndexSet(changeset.sectionDeleted))
}
if !changeset.sectionInserted.isEmpty {
insertSections(IndexSet(changeset.sectionInserted))
}
if !changeset.sectionUpdated.isEmpty {
reloadSections(IndexSet(changeset.sectionUpdated))
}
for (source, target) in changeset.sectionMoved {
moveSection(source, toSection: target)
}
if !changeset.elementDeleted.isEmpty {
deleteItems(at: changeset.elementDeleted.map { IndexPath(item: $0.element, section: $0.section) })
}
if !changeset.elementInserted.isEmpty {
insertItems(at: changeset.elementInserted.map { IndexPath(item: $0.element, section: $0.section) })
}
if !changeset.elementUpdated.isEmpty {
reloadItems(at: changeset.elementUpdated.map { IndexPath(item: $0.element, section: $0.section) })
}
for (source, target) in changeset.elementMoved {
moveItem(at: IndexPath(item: source.element, section: source.section), to: IndexPath(item: target.element, section: target.section))
}
})
}
}
}
#endif