2021-02-07 07:42:50 +01:00
|
|
|
//
|
|
|
|
// HomeTimelineViewModel+LoadOldestState.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/5.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import GameplayKit
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonSDK
|
2023-12-13 15:07:16 +01:00
|
|
|
import MastodonCore
|
2021-02-07 07:42:50 +01:00
|
|
|
|
|
|
|
extension HomeTimelineViewModel {
|
2022-10-10 13:14:52 +02:00
|
|
|
class LoadOldestState: GKState {
|
2023-09-21 12:52:05 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let id = UUID()
|
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
weak var viewModel: HomeTimelineViewModel?
|
|
|
|
|
|
|
|
init(viewModel: HomeTimelineViewModel) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
@MainActor
|
|
|
|
func enter(state: LoadOldestState.Type) {
|
|
|
|
stateMachine?.enter(state)
|
|
|
|
}
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HomeTimelineViewModel.LoadOldestState {
|
|
|
|
class Initial: HomeTimelineViewModel.LoadOldestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
guard let viewModel = viewModel else { return false }
|
2024-01-08 11:17:40 +01:00
|
|
|
guard !viewModel.dataController.records.isEmpty else { return false }
|
2021-02-07 07:42:50 +01:00
|
|
|
return stateClass == Loading.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Loading: HomeTimelineViewModel.LoadOldestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Fail.self || stateClass == Idle.self || stateClass == NoMore.self
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
|
|
|
|
|
2024-01-08 11:17:40 +01:00
|
|
|
guard let lastFeedRecord = viewModel.dataController.records.last else {
|
2021-02-07 07:42:50 +01:00
|
|
|
stateMachine.enter(Idle.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
Task {
|
2023-12-28 13:47:07 +01:00
|
|
|
let _maxID = lastFeedRecord.status?.id
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
guard let maxID = _maxID else {
|
|
|
|
await self.enter(state: Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2023-12-13 15:07:16 +01:00
|
|
|
await AuthenticationServiceProvider.shared.fetchAccounts(apiService: viewModel.context.apiService)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let response = try await viewModel.context.apiService.homeTimeline(
|
|
|
|
maxID: maxID,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
let statuses = response.value
|
|
|
|
// enter no more state when no new statuses
|
|
|
|
if statuses.isEmpty || (statuses.count == 1 && statuses[0].id == maxID) {
|
2022-01-27 14:23:39 +01:00
|
|
|
await self.enter(state: NoMore.self)
|
2021-02-07 07:42:50 +01:00
|
|
|
} else {
|
2022-01-27 14:23:39 +01:00
|
|
|
await self.enter(state: Idle.self)
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
viewModel.homeTimelineNavigationBarTitleViewModel.receiveLoadingStateCompletion(.finished)
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
await self.enter(state: Fail.self)
|
|
|
|
viewModel.homeTimelineNavigationBarTitleViewModel.receiveLoadingStateCompletion(.failure(error))
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
} // end Task
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fail: HomeTimelineViewModel.LoadOldestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self || stateClass == Idle.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Idle: HomeTimelineViewModel.LoadOldestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NoMore: HomeTimelineViewModel.LoadOldestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
// reset state if needs
|
|
|
|
return stateClass == Idle.self
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
guard let viewModel = viewModel else { return }
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
2021-04-15 06:35:40 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
var snapshot = diffableDataSource.snapshot()
|
|
|
|
snapshot.deleteItems([.bottomLoader])
|
|
|
|
diffableDataSource.apply(snapshot)
|
|
|
|
}
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|