2021-04-01 08:39:15 +02:00
|
|
|
//
|
|
|
|
// UserTimelineViewModel+State.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import Foundation
|
|
|
|
import GameplayKit
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2021-04-01 08:39:15 +02:00
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension UserTimelineViewModel {
|
2022-10-10 13:14:52 +02:00
|
|
|
class State: GKState {
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
let logger = Logger(subsystem: "UserTimelineViewModel.State", category: "StateMachine")
|
|
|
|
|
|
|
|
let id = UUID()
|
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
weak var viewModel: UserTimelineViewModel?
|
|
|
|
|
|
|
|
init(viewModel: UserTimelineViewModel) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
2022-01-27 14:23:39 +01:00
|
|
|
super.didEnter(from: previousState)
|
2022-10-10 13:14:52 +02:00
|
|
|
|
|
|
|
let from = previousState.flatMap { String(describing: $0) } ?? "nil"
|
|
|
|
let to = String(describing: self)
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(from) -> \(to)")
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
func enter(state: State.Type) {
|
|
|
|
stateMachine?.enter(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
2022-10-10 13:14:52 +02:00
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [\(self.id.uuidString)] \(String(describing: self))")
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UserTimelineViewModel.State {
|
|
|
|
class Initial: UserTimelineViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
guard let viewModel = viewModel else { return false }
|
|
|
|
switch stateClass {
|
|
|
|
case is Reloading.Type:
|
2022-05-13 11:23:35 +02:00
|
|
|
return viewModel.userIdentifier != nil
|
2021-04-01 08:39:15 +02:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Reloading: UserTimelineViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
2021-04-07 08:24:28 +02:00
|
|
|
case is Loading.Type:
|
2021-04-01 08:39:15 +02:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
|
|
|
|
|
|
|
|
// reset
|
2022-10-08 07:43:06 +02:00
|
|
|
viewModel.statusFetchedResultsController.statusIDs = []
|
2021-04-07 08:24:28 +02:00
|
|
|
|
|
|
|
stateMachine.enter(Loading.self)
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fail: UserTimelineViewModel.State {
|
2021-04-07 08:24:28 +02:00
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
2021-04-07 08:24:28 +02:00
|
|
|
case is Loading.Type:
|
2021-04-01 08:39:15 +02:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 08:24:28 +02:00
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
guard let _ = viewModel, let stateMachine = stateMachine else { return }
|
|
|
|
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading 3s later…", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
stateMachine.enter(Loading.self)
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Idle: UserTimelineViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
2021-04-07 08:24:28 +02:00
|
|
|
case is Reloading.Type, is Loading.Type:
|
2021-04-01 08:39:15 +02:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 08:24:28 +02:00
|
|
|
class Loading: UserTimelineViewModel.State {
|
2021-04-01 08:39:15 +02:00
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Fail.Type:
|
|
|
|
return true
|
|
|
|
case is Idle.Type:
|
|
|
|
return true
|
|
|
|
case is NoMore.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
|
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
let maxID = viewModel.statusFetchedResultsController.statusIDs.last
|
2021-04-01 08:39:15 +02:00
|
|
|
|
2022-05-13 11:23:35 +02:00
|
|
|
guard let userID = viewModel.userIdentifier?.userID, !userID.isEmpty else {
|
2021-04-01 08:39:15 +02:00
|
|
|
stateMachine.enter(Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let queryFilter = viewModel.queryFilter
|
|
|
|
|
|
|
|
Task {
|
|
|
|
|
|
|
|
do {
|
|
|
|
let response = try await viewModel.context.apiService.userTimeline(
|
|
|
|
accountID: userID,
|
|
|
|
maxID: maxID,
|
|
|
|
sinceID: nil,
|
|
|
|
excludeReplies: queryFilter.excludeReplies,
|
|
|
|
excludeReblogs: queryFilter.excludeReblogs,
|
|
|
|
onlyMedia: queryFilter.onlyMedia,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var hasNewStatusesAppend = false
|
2022-10-08 07:43:06 +02:00
|
|
|
var statusIDs = viewModel.statusFetchedResultsController.statusIDs
|
2022-01-27 14:23:39 +01:00
|
|
|
for status in response.value {
|
|
|
|
guard !statusIDs.contains(status.id) else { continue }
|
|
|
|
statusIDs.append(status.id)
|
|
|
|
hasNewStatusesAppend = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasNewStatusesAppend {
|
|
|
|
await enter(state: Idle.self)
|
|
|
|
} else {
|
|
|
|
await enter(state: NoMore.self)
|
|
|
|
}
|
2022-10-08 07:43:06 +02:00
|
|
|
viewModel.statusFetchedResultsController.statusIDs = statusIDs
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
} catch {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch user timeline fail: \(error.localizedDescription)")
|
|
|
|
await enter(state: Fail.self)
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
} // end Task
|
|
|
|
} // end func
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:43:08 +02:00
|
|
|
class NoMore: UserTimelineViewModel.State {
|
2021-04-01 08:39:15 +02:00
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Reloading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:43:08 +02:00
|
|
|
override func didEnter(from previousState: GKState?) {
|
2021-04-07 07:57:03 +02:00
|
|
|
super.didEnter(from: previousState)
|
2021-04-07 10:55:07 +02:00
|
|
|
guard let viewModel = viewModel, let _ = stateMachine else { return }
|
2021-04-06 10:43:08 +02:00
|
|
|
|
2021-07-15 11:26:04 +02:00
|
|
|
// trigger data source update. otherwise, spinner always display
|
2022-05-26 17:19:47 +02:00
|
|
|
viewModel.isSuspended = viewModel.isSuspended
|
2022-04-29 07:35:03 +02:00
|
|
|
|
|
|
|
// remove bottom loader
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else { return }
|
|
|
|
var snapshot = diffableDataSource.snapshot()
|
|
|
|
snapshot.deleteItems([.bottomLoader])
|
|
|
|
diffableDataSource.apply(snapshot)
|
2021-04-01 08:39:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|