Skip to content

Commit 880d8fb

Browse files
committed
Add completion handler
ra1028#154
1 parent 02ca196 commit 880d8fb

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Sources/Extensions/UIKitExtension.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public extension UITableView {
1919
using stagedChangeset: StagedChangeset<C>,
2020
with animation: @autoclosure () -> RowAnimation,
2121
interrupt: ((Changeset<C>) -> Bool)? = nil,
22+
completion: ((Bool) -> Void)? = nil,
2223
setData: (C) -> Void
2324
) {
2425
reload(
@@ -30,6 +31,7 @@ public extension UITableView {
3031
insertRowsAnimation: animation(),
3132
reloadRowsAnimation: animation(),
3233
interrupt: interrupt,
34+
completion: completion,
3335
setData: setData
3436
)
3537
}
@@ -61,20 +63,25 @@ public extension UITableView {
6163
insertRowsAnimation: @autoclosure () -> RowAnimation,
6264
reloadRowsAnimation: @autoclosure () -> RowAnimation,
6365
interrupt: ((Changeset<C>) -> Bool)? = nil,
66+
completion: ((Bool) -> Void)? = nil,
6467
setData: (C) -> Void
6568
) {
6669
if case .none = window, let data = stagedChangeset.last?.data {
6770
setData(data)
68-
return reloadData()
71+
reloadData()
72+
completion?(false)
73+
return
6974
}
7075

7176
for changeset in stagedChangeset {
7277
if let interrupt = interrupt, interrupt(changeset), let data = stagedChangeset.last?.data {
7378
setData(data)
74-
return reloadData()
79+
reloadData()
80+
completion?(false)
81+
return
7582
}
7683

77-
_performBatchUpdates {
84+
_performBatchUpdates({
7885
setData(changeset.data)
7986

8087
if !changeset.sectionDeleted.isEmpty {
@@ -108,18 +115,19 @@ public extension UITableView {
108115
for (source, target) in changeset.elementMoved {
109116
moveRow(at: IndexPath(row: source.element, section: source.section), to: IndexPath(row: target.element, section: target.section))
110117
}
111-
}
118+
}, completionHandler: completion)
112119
}
113120
}
114121

115-
private func _performBatchUpdates(_ updates: () -> Void) {
122+
private func _performBatchUpdates(_ updates: () -> Void, completionHandler: ((Bool) -> Void)? = nil) {
116123
if #available(iOS 11.0, tvOS 11.0, *) {
117-
performBatchUpdates(updates)
124+
performBatchUpdates(updates, completion: completionHandler)
118125
}
119126
else {
120127
beginUpdates()
121128
updates()
122129
endUpdates()
130+
completionHandler?(true)
123131
}
124132
}
125133
}

0 commit comments

Comments
 (0)