mastodon-ios/Mastodon/Scene/Profile/Bookmark/BookmarkViewModel+State.swift

188 lines
6.5 KiB
Swift
Raw Normal View History

//
// BookmarkViewModel+State.swift
// Mastodon
//
// Created by ProtoLimit on 2022-07-19.
//
import os.log
import Foundation
import GameplayKit
import MastodonSDK
2022-10-08 07:43:06 +02:00
import MastodonCore
extension BookmarkViewModel {
class State: GKState, NamingState {
let logger = Logger(subsystem: "BookmarkViewModel.State", category: "StateMachine")
let id = UUID()
var name: String {
String(describing: Self.self)
}
weak var viewModel: BookmarkViewModel?
init(viewModel: BookmarkViewModel) {
self.viewModel = viewModel
}
override func didEnter(from previousState: GKState?) {
super.didEnter(from: previousState)
let previousState = previousState as? BookmarkViewModel.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)")
}
}
}
extension BookmarkViewModel.State {
class Initial: BookmarkViewModel.State {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
2022-10-09 15:40:02 +02:00
guard let _ = viewModel else { return false }
switch stateClass {
case is Reloading.Type:
return true
default:
return false
}
}
}
class Reloading: BookmarkViewModel.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-10-08 07:43:06 +02:00
viewModel.statusFetchedResultsController.statusIDs = []
stateMachine.enter(Loading.self)
}
}
class Fail: BookmarkViewModel.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: BookmarkViewModel.State {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
switch stateClass {
case is Reloading.Type, is Loading.Type:
return true
default:
return false
}
}
}
class Loading: BookmarkViewModel.State {
// prefer use `maxID` token in response header
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)
2022-10-09 15:40:02 +02:00
guard let viewModel = viewModel, let _ = stateMachine else { return }
if previousState is Reloading {
maxID = nil
}
Task {
do {
let response = try await viewModel.context.apiService.bookmarkedStatuses(
maxID: maxID,
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
)
var hasNewStatusesAppend = false
2022-10-08 07:43:06 +02:00
var statusIDs = viewModel.statusFetchedResultsController.statusIDs
for status in response.value {
guard !statusIDs.contains(status.id) else { continue }
statusIDs.append(status.id)
hasNewStatusesAppend = true
}
self.maxID = response.link?.maxID
let hasNextPage: Bool = {
guard let link = response.link else { return true } // assert has more when link invalid
return link.maxID != nil
}()
if hasNewStatusesAppend && hasNextPage {
await enter(state: Idle.self)
} else {
await enter(state: NoMore.self)
}
2022-10-08 07:43:06 +02:00
viewModel.statusFetchedResultsController.statusIDs = statusIDs
} catch {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch user bookmarks fail: \(error.localizedDescription)")
await enter(state: Fail.self)
}
} // end Task
} // end func
}
class NoMore: BookmarkViewModel.State {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
switch stateClass {
case is Reloading.Type:
return true
default:
return false
}
}
}
}