2021-11-01 12:54:07 +01:00
|
|
|
//
|
|
|
|
// FollowerListViewModel+State.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Cirno MainasuK on 2021-11-1.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import Foundation
|
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension FollowerListViewModel {
|
2022-01-27 14:23:39 +01:00
|
|
|
class State: GKState, NamingState {
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "FollowerListViewModel.State", category: "StateMachine")
|
|
|
|
|
|
|
|
let id = UUID()
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
String(describing: Self.self)
|
|
|
|
}
|
|
|
|
|
2021-11-01 12:54:07 +01:00
|
|
|
weak var viewModel: FollowerListViewModel?
|
|
|
|
|
|
|
|
init(viewModel: FollowerListViewModel) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
2022-01-27 14:23:39 +01:00
|
|
|
super.didEnter(from: previousState)
|
|
|
|
let previousState = previousState as? FollowerListViewModel.State
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [\(self.id.uuidString)] enter \(self.name), previous: \(previousState?.name ?? "<nil>")")
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
func enter(state: State.Type) {
|
|
|
|
stateMachine?.enter(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [\(self.id.uuidString)] \(self.name)")
|
2021-11-01 12:54:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FollowerListViewModel.State {
|
|
|
|
class Initial: FollowerListViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
guard let viewModel = viewModel else { return false }
|
|
|
|
switch stateClass {
|
|
|
|
case is Reloading.Type:
|
|
|
|
return viewModel.userID.value != nil
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Reloading: FollowerListViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.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 }
|
|
|
|
|
|
|
|
// reset
|
2022-02-10 12:30:41 +01:00
|
|
|
viewModel.userFetchedResultsController.userIDs = []
|
2021-11-01 12:54:07 +01:00
|
|
|
|
|
|
|
stateMachine.enter(Loading.self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fail: FollowerListViewModel.State {
|
|
|
|
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Idle: FollowerListViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Reloading.Type, is Loading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Loading: FollowerListViewModel.State {
|
|
|
|
|
|
|
|
var maxID: String?
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if previousState is Reloading {
|
|
|
|
maxID = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
|
|
|
|
|
|
|
|
guard let userID = viewModel.userID.value, !userID.isEmpty else {
|
|
|
|
stateMachine.enter(Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
guard let authenticationBox = viewModel.context.authenticationService.activeMastodonAuthenticationBox.value else {
|
2021-11-01 12:54:07 +01:00
|
|
|
stateMachine.enter(Fail.self)
|
|
|
|
return
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
let response = try await viewModel.context.apiService.followers(
|
|
|
|
userID: userID,
|
|
|
|
maxID: maxID,
|
|
|
|
authenticationBox: authenticationBox
|
|
|
|
)
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch \(response.value.count) followers")
|
|
|
|
|
|
|
|
var hasNewAppend = false
|
2022-02-10 12:30:41 +01:00
|
|
|
var userIDs = viewModel.userFetchedResultsController.userIDs
|
2022-01-27 14:23:39 +01:00
|
|
|
for user in response.value {
|
|
|
|
guard !userIDs.contains(user.id) else { continue }
|
|
|
|
userIDs.append(user.id)
|
|
|
|
hasNewAppend = true
|
|
|
|
}
|
|
|
|
|
|
|
|
let maxID = response.link?.maxID
|
|
|
|
|
|
|
|
if hasNewAppend && maxID != nil {
|
|
|
|
await enter(state: Idle.self)
|
|
|
|
} else {
|
|
|
|
await enter(state: NoMore.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.maxID = maxID
|
2022-02-10 12:30:41 +01:00
|
|
|
viewModel.userFetchedResultsController.userIDs = userIDs
|
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 follower fail: \(error.localizedDescription)")
|
|
|
|
await enter(state: Fail.self)
|
2021-11-01 12:54:07 +01:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
} // end Task
|
2021-11-01 12:54:07 +01:00
|
|
|
} // end func didEnter
|
|
|
|
}
|
|
|
|
|
|
|
|
class NoMore: FollowerListViewModel.State {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Reloading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|