2022-02-08 05:36:06 +01:00
|
|
|
//
|
|
|
|
// ReportViewModel+State.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-2-7.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import func QuartzCore.CACurrentMediaTime
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
extension ReportStatusViewModel {
|
2022-02-08 05:36:06 +01:00
|
|
|
class State: GKState {
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "ReportViewModel.State", category: "StateMachine")
|
|
|
|
|
|
|
|
let id = UUID()
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
String(describing: Self.self)
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
weak var viewModel: ReportStatusViewModel?
|
2022-02-08 05:36:06 +01:00
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
init(viewModel: ReportStatusViewModel) {
|
2022-02-08 05:36:06 +01:00
|
|
|
self.viewModel = viewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
2022-05-10 12:34:39 +02:00
|
|
|
let previousState = previousState as? ReportStatusViewModel.State
|
2022-02-08 05:36:06 +01:00
|
|
|
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)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
extension ReportStatusViewModel.State {
|
|
|
|
class Initial: ReportStatusViewModel.State {
|
2022-02-08 05:36:06 +01:00
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
guard let _ = viewModel else { return false }
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
class Loading: ReportStatusViewModel.State {
|
2022-02-08 05:36:06 +01: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)
|
2022-11-17 23:02:43 +01:00
|
|
|
guard let viewModel else { return }
|
2022-02-08 05:36:06 +01:00
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
let maxID = viewModel.statusFetchedResultsController.statusIDs.last
|
2022-02-08 05:36:06 +01:00
|
|
|
|
|
|
|
Task {
|
|
|
|
let managedObjectContext = viewModel.context.managedObjectContext
|
|
|
|
let _userID: MastodonUser.ID? = try await managedObjectContext.perform {
|
|
|
|
guard let user = viewModel.user.object(in: managedObjectContext) else { return nil }
|
|
|
|
return user.id
|
|
|
|
}
|
|
|
|
guard let userID = _userID else {
|
|
|
|
await enter(state: Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
let response = try await viewModel.context.apiService.userTimeline(
|
|
|
|
accountID: userID,
|
|
|
|
maxID: maxID,
|
|
|
|
sinceID: nil,
|
|
|
|
excludeReplies: true,
|
|
|
|
excludeReblogs: true,
|
|
|
|
onlyMedia: false,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
|
2022-02-08 05:36:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var hasNewStatusesAppend = false
|
2022-10-08 07:43:06 +02:00
|
|
|
var statusIDs = viewModel.statusFetchedResultsController.statusIDs
|
2022-02-08 05:36:06 +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-02-08 05:36:06 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
class Fail: ReportStatusViewModel.State {
|
2022-02-08 05:36:06 +01:00
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
class Idle: ReportStatusViewModel.State {
|
2022-02-08 05:36:06 +01:00
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
switch stateClass {
|
|
|
|
case is Loading.Type:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:34:39 +02:00
|
|
|
class NoMore: ReportStatusViewModel.State {
|
2022-02-08 05:36:06 +01:00
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
guard let viewModel = viewModel, let _ = stateMachine else { return }
|
|
|
|
guard let diffableDataSource = viewModel.diffableDataSource else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
var snapshot = diffableDataSource.snapshot()
|
|
|
|
snapshot.deleteItems([.bottomLoader])
|
|
|
|
diffableDataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|