Remove deferred loading indicator, causes more trouble than it's worth

Closes #404
This commit is contained in:
Shadowfacts 2023-05-28 11:11:15 -07:00
parent 59231e513f
commit 5b321fcc78
1 changed files with 11 additions and 36 deletions

View File

@ -63,19 +63,20 @@ class TimelineLikeController<Item: Sendable> {
}
let token = LoadAttemptToken()
state = .loadingInitial(token, hasAddedLoadingIndicator: false)
let loadingIndicator = DeferredLoadingIndicator(owner: self, state: state, addedIndicatorState: .loadingInitial(token, hasAddedLoadingIndicator: true))
await emit(event: .addLoadingIndicator)
state = .loadingInitial(token, hasAddedLoadingIndicator: true)
do {
let items = try await delegate.loadInitial()
guard case .loadingInitial(token, _) = state else {
return
}
await loadingIndicator.end()
await emit(event: .replaceAllItems(items, token))
await emit(event: .removeLoadingIndicator)
state = .idle
} catch is CancellationError {
return
} catch {
await loadingIndicator.end()
await emit(event: .removeLoadingIndicator)
await emit(event: .loadAllError(error, token))
state = .notLoadedInitial
}
@ -88,9 +89,10 @@ class TimelineLikeController<Item: Sendable> {
}
let token = LoadAttemptToken()
state = .restoringInitial(token, hasAddedLoadingIndicator: false)
let loadingIndicator = DeferredLoadingIndicator(owner: self, state: state, addedIndicatorState: .restoringInitial(token, hasAddedLoadingIndicator: true))
await emit(event: .addLoadingIndicator)
state = .restoringInitial(token, hasAddedLoadingIndicator: true)
await doRestore()
await loadingIndicator.end()
await emit(event: .removeLoadingIndicator)
state = .idle
}
@ -128,19 +130,20 @@ class TimelineLikeController<Item: Sendable> {
return
}
state = .loadingOlder(token, hasAddedLoadingIndicator: false)
let loadingIndicator = DeferredLoadingIndicator(owner: self, state: state, addedIndicatorState: .loadingOlder(token, hasAddedLoadingIndicator: true))
await emit(event: .addLoadingIndicator)
state = .loadingOlder(token, hasAddedLoadingIndicator: true)
do {
let items = try await delegate.loadOlder()
guard case .loadingOlder(token, _) = state else {
return
}
await loadingIndicator.end()
await emit(event: .appendItems(items, token))
await emit(event: .removeLoadingIndicator)
state = .idle
} catch is CancellationError {
return
} catch {
await loadingIndicator.end()
await emit(event: .removeLoadingIndicator)
await emit(event: .loadOlderError(error, token))
state = .idle
}
@ -349,34 +352,6 @@ class TimelineLikeController<Item: Sendable> {
}
}
@MainActor
class DeferredLoadingIndicator {
private let owner: TimelineLikeController<Item>
private let addedIndicatorState: State
private let task: Task<Void, Error>
init(owner: TimelineLikeController<Item>, state: State, addedIndicatorState: State) {
self.owner = owner
self.addedIndicatorState = addedIndicatorState
self.task = Task { @MainActor in
try await Task.sleep(nanoseconds: 150 * NSEC_PER_MSEC)
guard state == owner.state else {
return
}
await owner.emit(event: .addLoadingIndicator)
owner.transition(to: addedIndicatorState)
}
}
func end() async {
if owner.state == addedIndicatorState {
await owner.emit(event: .removeLoadingIndicator)
} else {
task.cancel()
}
}
}
}
enum TimelineGapDirection {