2022-09-24 14:49:06 +00:00
|
|
|
//
|
|
|
|
// TimelineLikeController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/19/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import OSLog
|
|
|
|
|
|
|
|
protocol TimelineLikeControllerDelegate<TimelineItem>: AnyObject {
|
|
|
|
associatedtype TimelineItem
|
|
|
|
|
|
|
|
func loadInitial() async throws -> [TimelineItem]
|
|
|
|
|
|
|
|
func loadNewer() async throws -> [TimelineItem]
|
|
|
|
|
|
|
|
func canLoadOlder() async -> Bool
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
func loadOlder() async throws -> [TimelineItem]
|
|
|
|
|
|
|
|
func loadGap(in direction: TimelineGapDirection) async throws -> [TimelineItem]
|
2022-11-18 22:29:55 +00:00
|
|
|
|
2022-10-08 15:45:02 +00:00
|
|
|
func handleAddLoadingIndicator() async
|
|
|
|
func handleRemoveLoadingIndicator() async
|
|
|
|
func handleLoadAllError(_ error: Swift.Error) async
|
|
|
|
func handleReplaceAllItems(_ timelineItems: [TimelineItem]) async
|
|
|
|
func handleLoadNewerError(_ error: Swift.Error) async
|
|
|
|
func handlePrependItems(_ timelineItems: [TimelineItem]) async
|
|
|
|
func handleLoadOlderError(_ error: Swift.Error) async
|
|
|
|
func handleAppendItems(_ timelineItems: [TimelineItem]) async
|
2022-11-19 01:49:15 +00:00
|
|
|
func handleLoadGapError(_ error: Swift.Error, direction: TimelineGapDirection) async
|
|
|
|
func handleFillGap(_ timelineItems: [TimelineItem], direction: TimelineGapDirection) async
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "TimelineLikeController")
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
@MainActor
|
|
|
|
class TimelineLikeController<Item> {
|
2022-09-24 14:49:06 +00:00
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
private unowned var delegate: any TimelineLikeControllerDelegate<Item>
|
2022-09-24 14:49:06 +00:00
|
|
|
|
2022-09-24 15:31:52 +00:00
|
|
|
private(set) var state = State.notLoadedInitial {
|
2022-09-24 14:49:06 +00:00
|
|
|
willSet {
|
2022-10-08 19:12:10 +00:00
|
|
|
guard state.canTransition(to: newValue) else {
|
|
|
|
logger.error("State \(self.state.debugDescription, privacy: .public) cannot transition to \(newValue.debugDescription, privacy: .public)")
|
2022-10-30 21:10:58 +00:00
|
|
|
fatalError("State \(state) cannot transition to \(newValue)")
|
2022-10-08 19:12:10 +00:00
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
logger.debug("State: \(self.state.debugDescription, privacy: .public) -> \(newValue.debugDescription, privacy: .public)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
init(delegate: any TimelineLikeControllerDelegate<Item>) {
|
|
|
|
self.delegate = delegate
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadInitial() async {
|
2022-11-02 01:06:06 +00:00
|
|
|
guard state == .notLoadedInitial || state == .idle else {
|
2022-09-24 14:49:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let token = LoadAttemptToken()
|
|
|
|
state = .loadingInitial(token, hasAddedLoadingIndicator: false)
|
|
|
|
let loadingIndicator = DeferredLoadingIndicator(owner: self, state: state, addedIndicatorState: .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))
|
|
|
|
state = .idle
|
2022-11-03 03:00:29 +00:00
|
|
|
} catch is CancellationError {
|
|
|
|
return
|
2022-09-24 14:49:06 +00:00
|
|
|
} catch {
|
|
|
|
await loadingIndicator.end()
|
|
|
|
await emit(event: .loadAllError(error, token))
|
2022-11-10 00:15:08 +00:00
|
|
|
state = .notLoadedInitial
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 16:35:25 +00:00
|
|
|
/// Used to indicate to the controller that the initial set of posts have been restored externally.
|
2022-12-23 21:35:58 +00:00
|
|
|
func restoreInitial(doRestore: () async -> Void) async {
|
|
|
|
guard state == .notLoadedInitial || state == .idle else {
|
2022-11-23 16:35:25 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-20 18:38:33 +00:00
|
|
|
let token = LoadAttemptToken()
|
|
|
|
state = .restoringInitial(token, hasAddedLoadingIndicator: false)
|
|
|
|
let loadingIndicator = DeferredLoadingIndicator(owner: self, state: state, addedIndicatorState: .restoringInitial(token, hasAddedLoadingIndicator: true))
|
2022-12-23 21:35:58 +00:00
|
|
|
await doRestore()
|
2023-01-20 18:38:33 +00:00
|
|
|
await loadingIndicator.end()
|
2022-11-23 16:35:25 +00:00
|
|
|
state = .idle
|
|
|
|
}
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
func loadNewer() async {
|
|
|
|
guard state == .idle else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let token = LoadAttemptToken()
|
|
|
|
state = .loadingNewer(token)
|
|
|
|
do {
|
|
|
|
let items = try await delegate.loadNewer()
|
|
|
|
guard case .loadingNewer(token) = state else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await emit(event: .prependItems(items, token))
|
|
|
|
state = .idle
|
2022-11-03 03:00:29 +00:00
|
|
|
} catch is CancellationError {
|
|
|
|
return
|
2022-09-24 14:49:06 +00:00
|
|
|
} catch {
|
|
|
|
await emit(event: .loadNewerError(error, token))
|
|
|
|
state = .idle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadOlder() async {
|
|
|
|
guard state == .idle else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let token = LoadAttemptToken()
|
2022-10-31 21:45:36 +00:00
|
|
|
guard await delegate.canLoadOlder(),
|
|
|
|
// Make sure we're still in the idle state before continuing on, since that may have chnaged while waiting for user input.
|
|
|
|
// If the load more cell appears, then the users scrolls up and back down, the VC may kick off a second loadOlder task
|
|
|
|
// but we only want one to proceed. The actor prevents a data race, and this prevents multiple simultaneousl loadOlder tasks from running.
|
|
|
|
state == .idle else {
|
2022-09-24 14:49:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
state = .loadingOlder(token, hasAddedLoadingIndicator: false)
|
|
|
|
let loadingIndicator = DeferredLoadingIndicator(owner: self, state: state, addedIndicatorState: .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))
|
|
|
|
state = .idle
|
2022-11-03 03:00:29 +00:00
|
|
|
} catch is CancellationError {
|
|
|
|
return
|
2022-09-24 14:49:06 +00:00
|
|
|
} catch {
|
|
|
|
await loadingIndicator.end()
|
|
|
|
await emit(event: .loadOlderError(error, token))
|
|
|
|
state = .idle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
func fillGap(in direction: TimelineGapDirection) async {
|
2022-11-18 22:29:55 +00:00
|
|
|
guard state == .idle else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let token = LoadAttemptToken()
|
|
|
|
state = .loadingGap(token, direction)
|
|
|
|
do {
|
|
|
|
let items = try await delegate.loadGap(in: direction)
|
|
|
|
guard case .loadingGap(token, direction) = state else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await emit(event: .fillGap(items, direction, token))
|
|
|
|
state = .idle
|
|
|
|
} catch is CancellationError {
|
|
|
|
return
|
|
|
|
} catch {
|
|
|
|
await emit(event: .loadGapError(error, direction, token))
|
|
|
|
state = .idle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 14:49:06 +00:00
|
|
|
private func transition(to newState: State) {
|
|
|
|
self.state = newState
|
|
|
|
}
|
|
|
|
|
|
|
|
private func emit(event: Event) async {
|
2022-10-08 19:12:10 +00:00
|
|
|
guard state.canEmit(event: event) else {
|
|
|
|
logger.error("State \(self.state.debugDescription, privacy: .public) cannot emit event: \(event.debugDescription, privacy: .public)")
|
2022-10-30 21:10:58 +00:00
|
|
|
fatalError("State \(state) cannot emit event: \(event)")
|
2022-10-08 19:12:10 +00:00
|
|
|
}
|
2022-10-08 15:45:02 +00:00
|
|
|
switch event {
|
|
|
|
case .addLoadingIndicator:
|
|
|
|
await delegate.handleAddLoadingIndicator()
|
|
|
|
case .removeLoadingIndicator:
|
|
|
|
await delegate.handleRemoveLoadingIndicator()
|
|
|
|
case .loadAllError(let error, _):
|
|
|
|
await delegate.handleLoadAllError(error)
|
|
|
|
case .replaceAllItems(let items, _):
|
|
|
|
await delegate.handleReplaceAllItems(items)
|
|
|
|
case .loadNewerError(let error, _):
|
|
|
|
await delegate.handleLoadNewerError(error)
|
|
|
|
case .prependItems(let items, _):
|
|
|
|
await delegate.handlePrependItems(items)
|
|
|
|
case .loadOlderError(let error, _):
|
|
|
|
await delegate.handleLoadOlderError(error)
|
|
|
|
case .appendItems(let items, _):
|
|
|
|
await delegate.handleAppendItems(items)
|
2022-11-18 22:29:55 +00:00
|
|
|
case .loadGapError(let error, let direction, _):
|
|
|
|
await delegate.handleLoadGapError(error, direction: direction)
|
|
|
|
case .fillGap(let items, let direction, _):
|
|
|
|
await delegate.handleFillGap(items, direction: direction)
|
2022-10-08 15:45:02 +00:00
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum State: Equatable, CustomDebugStringConvertible {
|
2022-09-24 15:31:52 +00:00
|
|
|
case notLoadedInitial
|
2022-09-24 14:49:06 +00:00
|
|
|
case idle
|
2023-01-20 18:38:33 +00:00
|
|
|
case restoringInitial(LoadAttemptToken, hasAddedLoadingIndicator: Bool)
|
2022-09-24 14:49:06 +00:00
|
|
|
case loadingInitial(LoadAttemptToken, hasAddedLoadingIndicator: Bool)
|
|
|
|
case loadingNewer(LoadAttemptToken)
|
|
|
|
case loadingOlder(LoadAttemptToken, hasAddedLoadingIndicator: Bool)
|
2022-11-19 01:49:15 +00:00
|
|
|
case loadingGap(LoadAttemptToken, TimelineGapDirection)
|
2022-09-24 14:49:06 +00:00
|
|
|
|
|
|
|
var debugDescription: String {
|
|
|
|
switch self {
|
2022-09-24 15:31:52 +00:00
|
|
|
case .notLoadedInitial:
|
|
|
|
return "notLoadedInitial"
|
2022-09-24 14:49:06 +00:00
|
|
|
case .idle:
|
|
|
|
return "idle"
|
2023-01-20 18:38:33 +00:00
|
|
|
case .restoringInitial(let token, let hasAddedLoadingIndicator):
|
|
|
|
return "restoringInitial(\(ObjectIdentifier(token)), hasAddedLoadingIndicator: \(hasAddedLoadingIndicator))"
|
2022-09-24 14:49:06 +00:00
|
|
|
case .loadingInitial(let token, let hasAddedLoadingIndicator):
|
|
|
|
return "loadingInitial(\(ObjectIdentifier(token)), hasAddedLoadingIndicator: \(hasAddedLoadingIndicator))"
|
|
|
|
case .loadingNewer(let token):
|
|
|
|
return "loadingNewer(\(ObjectIdentifier(token)))"
|
|
|
|
case .loadingOlder(let token, let hasAddedLoadingIndicator):
|
|
|
|
return "loadingOlder(\(ObjectIdentifier(token)), hasAddedLoadingIndicator: \(hasAddedLoadingIndicator))"
|
2022-11-18 22:29:55 +00:00
|
|
|
case .loadingGap(let token, let direction):
|
2022-11-19 16:15:14 +00:00
|
|
|
return "loadingGap(\(ObjectIdentifier(token)), \(direction))"
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func canTransition(to: State) -> Bool {
|
|
|
|
switch self {
|
2022-09-24 15:31:52 +00:00
|
|
|
case .notLoadedInitial:
|
|
|
|
switch to {
|
2022-11-23 16:35:25 +00:00
|
|
|
case .restoringInitial, .loadingInitial(_, _):
|
2022-09-24 15:31:52 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
case .idle:
|
|
|
|
switch to {
|
2023-01-20 18:38:33 +00:00
|
|
|
case .restoringInitial(_, _), .loadingInitial(_, _), .loadingNewer(_), .loadingOlder(_, _), .loadingGap(_, _):
|
2022-09-24 14:49:06 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2023-01-20 18:38:33 +00:00
|
|
|
case .restoringInitial(let token, let hasAddedLoadingIndicator):
|
|
|
|
return to == .idle || (!hasAddedLoadingIndicator && to == .restoringInitial(token, hasAddedLoadingIndicator: true))
|
2022-09-24 14:49:06 +00:00
|
|
|
case .loadingInitial(let token, let hasAddedLoadingIndicator):
|
2022-11-10 00:15:08 +00:00
|
|
|
return to == .notLoadedInitial || to == .idle || (!hasAddedLoadingIndicator && to == .loadingInitial(token, hasAddedLoadingIndicator: true))
|
2022-09-24 14:49:06 +00:00
|
|
|
case .loadingNewer(_):
|
|
|
|
return to == .idle
|
|
|
|
case .loadingOlder(let token, let hasAddedLoadingIndicator):
|
|
|
|
return to == .idle || (!hasAddedLoadingIndicator && to == .loadingOlder(token, hasAddedLoadingIndicator: true))
|
2022-11-18 22:29:55 +00:00
|
|
|
case .loadingGap(_, _):
|
|
|
|
return to == .idle
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func canEmit(event: Event) -> Bool {
|
|
|
|
switch event {
|
|
|
|
case .addLoadingIndicator:
|
|
|
|
switch self {
|
2023-01-20 18:38:33 +00:00
|
|
|
case .restoringInitial(_, _), .loadingInitial(_, _), .loadingOlder(_, _):
|
2022-09-24 14:49:06 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case .removeLoadingIndicator:
|
|
|
|
switch self {
|
2023-01-20 18:38:33 +00:00
|
|
|
case .restoringInitial(_, hasAddedLoadingIndicator: true), .loadingInitial(_, hasAddedLoadingIndicator: true), .loadingOlder(_, hasAddedLoadingIndicator: true):
|
2022-09-24 14:49:06 +00:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case .loadAllError(_, let token), .replaceAllItems(_, let token):
|
|
|
|
switch self {
|
|
|
|
case .loadingInitial(token, _):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case .loadNewerError(_, let token), .prependItems(_, let token):
|
|
|
|
switch self {
|
|
|
|
case .loadingNewer(token):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
case .loadOlderError(_, let token), .appendItems(_, let token):
|
|
|
|
switch self {
|
|
|
|
case .loadingOlder(token, _):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2022-11-18 22:29:55 +00:00
|
|
|
case .loadGapError(_, let direction, let token), .fillGap(_, let direction, let token):
|
|
|
|
switch self {
|
|
|
|
case .loadingGap(token, direction):
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 19:12:10 +00:00
|
|
|
enum Event: CustomDebugStringConvertible {
|
2022-09-24 14:49:06 +00:00
|
|
|
case addLoadingIndicator
|
|
|
|
case removeLoadingIndicator
|
|
|
|
case loadAllError(Error, LoadAttemptToken)
|
|
|
|
case replaceAllItems([Item], LoadAttemptToken)
|
|
|
|
case loadNewerError(Error, LoadAttemptToken)
|
|
|
|
case prependItems([Item], LoadAttemptToken)
|
|
|
|
case loadOlderError(Error, LoadAttemptToken)
|
|
|
|
case appendItems([Item], LoadAttemptToken)
|
2022-11-19 01:49:15 +00:00
|
|
|
case loadGapError(Error, TimelineGapDirection, LoadAttemptToken)
|
|
|
|
case fillGap([Item], TimelineGapDirection, LoadAttemptToken)
|
2022-10-08 19:12:10 +00:00
|
|
|
|
|
|
|
var debugDescription: String {
|
|
|
|
switch self {
|
|
|
|
case .addLoadingIndicator:
|
|
|
|
return "addLoadingIndicator"
|
|
|
|
case .removeLoadingIndicator:
|
|
|
|
return "removeLoadingIndicator"
|
|
|
|
case .loadAllError(let error, let token):
|
|
|
|
return "loadAllError(\(error), \(token))"
|
|
|
|
case .replaceAllItems(_, let token):
|
|
|
|
return "replcaeAllItems(<omitted>, \(token))"
|
|
|
|
case .loadNewerError(let error, let token):
|
|
|
|
return "loadNewerError(\(error), \(token))"
|
|
|
|
case .prependItems(_, let token):
|
|
|
|
return "prependItems(<omitted>, \(token))"
|
|
|
|
case .loadOlderError(let error, let token):
|
|
|
|
return "loadOlderError(\(error), \(token))"
|
|
|
|
case .appendItems(_, let token):
|
|
|
|
return "appendItems(<omitted>, \(token))"
|
2022-11-18 22:29:55 +00:00
|
|
|
case .loadGapError(let error, let direction, let token):
|
|
|
|
return "loadGapError(\(error), \(direction), \(token))"
|
|
|
|
case .fillGap(_, let direction, let token):
|
|
|
|
return "loadGapError(<omitted>, \(direction), \(token))"
|
2022-10-08 19:12:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class LoadAttemptToken: Equatable {
|
|
|
|
static func ==(lhs: LoadAttemptToken, rhs: LoadAttemptToken) -> Bool {
|
|
|
|
return lhs === rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 18:38:33 +00:00
|
|
|
@MainActor
|
2022-09-24 14:49:06 +00:00
|
|
|
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
|
2023-01-20 18:38:33 +00:00
|
|
|
self.task = Task { @MainActor in
|
2022-09-24 14:49:06 +00:00
|
|
|
try await Task.sleep(nanoseconds: 150 * NSEC_PER_MSEC)
|
2023-01-20 18:38:33 +00:00
|
|
|
guard state == owner.state else {
|
2022-09-24 14:49:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
await owner.emit(event: .addLoadingIndicator)
|
2023-01-20 18:38:33 +00:00
|
|
|
owner.transition(to: addedIndicatorState)
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func end() async {
|
2023-01-20 18:38:33 +00:00
|
|
|
if owner.state == addedIndicatorState {
|
2022-09-24 14:49:06 +00:00
|
|
|
await owner.emit(event: .removeLoadingIndicator)
|
|
|
|
} else {
|
|
|
|
task.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:49:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum TimelineGapDirection {
|
|
|
|
/// Fill in below the gap. I.e., statuses that are immediately newer than the status below the gap.
|
|
|
|
case below
|
|
|
|
/// Fill in above the gap. I.e., statuses that are immediately older than the status above the gap.
|
|
|
|
case above
|
2022-12-05 23:43:32 +00:00
|
|
|
|
|
|
|
var accessibilityLabel: String {
|
|
|
|
switch self {
|
|
|
|
case .below:
|
|
|
|
return "Newer"
|
|
|
|
case .above:
|
|
|
|
return "Older"
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 14:49:06 +00:00
|
|
|
}
|