forked from shadowfacts/Tusker
Prevent loading indicator from potentially being added multiple times
Not sure how this could happen, but it's caused 1 crash in the wild so w/e
This commit is contained in:
parent
85ced7ff5f
commit
b560bcd8dc
|
@ -25,6 +25,7 @@ class DiffableTimelineLikeTableViewController<Section: DiffableTimelineLikeSecti
|
||||||
|
|
||||||
private(set) var state = State.unloaded
|
private(set) var state = State.unloaded
|
||||||
private var lastLastVisibleRow: IndexPath?
|
private var lastLastVisibleRow: IndexPath?
|
||||||
|
private var currentLoadingIndicatorWorkItem: DispatchWorkItem?
|
||||||
|
|
||||||
private(set) var dataSource: UITableViewDiffableDataSource<Section, Item>!
|
private(set) var dataSource: UITableViewDiffableDataSource<Section, Item>!
|
||||||
|
|
||||||
|
@ -113,13 +114,24 @@ class DiffableTimelineLikeTableViewController<Section: DiffableTimelineLikeSecti
|
||||||
}
|
}
|
||||||
|
|
||||||
private func showLoadingIndicatorDelayed() -> DispatchWorkItem {
|
private func showLoadingIndicatorDelayed() -> DispatchWorkItem {
|
||||||
|
currentLoadingIndicatorWorkItem?.cancel()
|
||||||
let workItem = DispatchWorkItem { [weak self] in
|
let workItem = DispatchWorkItem { [weak self] in
|
||||||
guard let self = self else { return }
|
guard let self = self else { return }
|
||||||
var snapshot = self.dataSource.snapshot()
|
var snapshot = self.dataSource.snapshot()
|
||||||
snapshot.appendSections([.loadingIndicator])
|
var changed = false
|
||||||
snapshot.appendItems([.loadingIndicator])
|
if !snapshot.sectionIdentifiers.contains(.loadingIndicator) {
|
||||||
self.dataSource.apply(snapshot, animatingDifferences: false)
|
snapshot.appendSections([.loadingIndicator])
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if changed || !snapshot.itemIdentifiers(inSection: .loadingIndicator).contains(.loadingIndicator) {
|
||||||
|
snapshot.appendItems([.loadingIndicator], toSection: .loadingIndicator)
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
if changed {
|
||||||
|
self.dataSource.apply(snapshot, animatingDifferences: false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
currentLoadingIndicatorWorkItem = workItem
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250), execute: workItem)
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(250), execute: workItem)
|
||||||
return workItem
|
return workItem
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue