2021-02-07 07:42:50 +01:00
|
|
|
//
|
|
|
|
// HomeTimelineViewModel+LoadLatestState.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/2/5.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import func QuartzCore.CACurrentMediaTime
|
|
|
|
import Foundation
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
|
|
|
|
|
|
|
extension HomeTimelineViewModel {
|
|
|
|
class LoadLatestState: GKState {
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
let logger = Logger(subsystem: "HomeTimelineViewModel.LoadLatestState", category: "StateMachine")
|
|
|
|
|
|
|
|
let id = UUID()
|
|
|
|
|
|
|
|
var name: String {
|
|
|
|
String(describing: Self.self)
|
|
|
|
}
|
|
|
|
|
2021-02-07 07:42:50 +01:00
|
|
|
weak var viewModel: HomeTimelineViewModel?
|
|
|
|
|
|
|
|
init(viewModel: HomeTimelineViewModel) {
|
|
|
|
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? HomeTimelineViewModel.LoadLatestState
|
|
|
|
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>")")
|
2021-02-07 07:42:50 +01:00
|
|
|
viewModel?.loadLatestStateMachinePublisher.send(self)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
@MainActor
|
|
|
|
func enter(state: LoadLatestState.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-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HomeTimelineViewModel.LoadLatestState {
|
|
|
|
class Initial: HomeTimelineViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Loading: HomeTimelineViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Fail.self || stateClass == Idle.self
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
|
|
|
|
guard let activeMastodonAuthenticationBox = viewModel.context.authenticationService.activeMastodonAuthenticationBox.value else {
|
2021-02-26 11:36:38 +01:00
|
|
|
// sign out when loading will enter here
|
2021-02-07 07:42:50 +01:00
|
|
|
stateMachine.enter(Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
let latestFeedRecords = viewModel.fetchedResultsController.records.prefix(APIService.onceRequestStatusMaxCount)
|
|
|
|
let parentManagedObjectContext = viewModel.fetchedResultsController.fetchedResultsController.managedObjectContext
|
2021-02-07 07:42:50 +01:00
|
|
|
let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
|
|
|
managedObjectContext.parent = parentManagedObjectContext
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
Task {
|
2021-02-07 07:42:50 +01:00
|
|
|
let start = CACurrentMediaTime()
|
2022-01-27 14:23:39 +01:00
|
|
|
let latestStatusIDs: [Status.ID] = latestFeedRecords.compactMap { record in
|
|
|
|
guard let feed = record.object(in: managedObjectContext) else { return nil }
|
|
|
|
return feed.status?.id
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
let end = CACurrentMediaTime()
|
2021-04-01 08:39:15 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: collect statuses id cost: %.2fs", ((#file as NSString).lastPathComponent), #line, #function, end - start)
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
let response = try await viewModel.context.apiService.homeTimeline(
|
|
|
|
authenticationBox: activeMastodonAuthenticationBox
|
|
|
|
)
|
|
|
|
|
|
|
|
await enter(state: Idle.self)
|
|
|
|
viewModel.homeTimelineNavigationBarTitleViewModel.receiveLoadingStateCompletion(.finished)
|
|
|
|
|
|
|
|
// stop refresher if no new statuses
|
|
|
|
let statuses = response.value
|
|
|
|
let newStatuses = statuses.filter { !latestStatusIDs.contains($0.id) }
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): load \(newStatuses.count) new statuses")
|
|
|
|
|
|
|
|
if newStatuses.isEmpty {
|
|
|
|
viewModel.didLoadLatest.send()
|
|
|
|
} else {
|
|
|
|
if !latestStatusIDs.isEmpty {
|
|
|
|
viewModel.homeTimelineNavigationBarTitleViewModel.newPostsIncoming()
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
viewModel.timelineIsEmpty.value = latestStatusIDs.isEmpty && statuses.isEmpty
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch statuses failed: \(error.localizedDescription)")
|
|
|
|
await enter(state: Idle.self)
|
|
|
|
viewModel.didLoadLatest.send()
|
|
|
|
viewModel.homeTimelineNavigationBarTitleViewModel.receiveLoadingStateCompletion(.failure(error))
|
|
|
|
}
|
|
|
|
} // end Task
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fail: HomeTimelineViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self || stateClass == Idle.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Idle: HomeTimelineViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|