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 {
|
|
|
|
weak var viewModel: HomeTimelineViewModel?
|
|
|
|
|
|
|
|
init(viewModel: HomeTimelineViewModel) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
os_log("%{public}s[%{public}ld], %{public}s: enter %s, previous: %s", ((#file as NSString).lastPathComponent), #line, #function, self.debugDescription, previousState.debugDescription)
|
|
|
|
viewModel?.loadLatestStateMachinePublisher.send(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
let predicate = viewModel.fetchedResultsController.fetchRequest.predicate
|
|
|
|
let parentManagedObjectContext = viewModel.fetchedResultsController.managedObjectContext
|
|
|
|
let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
|
|
|
managedObjectContext.parent = parentManagedObjectContext
|
|
|
|
|
|
|
|
managedObjectContext.perform {
|
|
|
|
let start = CACurrentMediaTime()
|
2021-04-01 08:39:15 +02:00
|
|
|
let latestStatusIDs: [Status.ID]
|
2021-02-07 07:42:50 +01:00
|
|
|
let request = HomeTimelineIndex.sortedFetchRequest
|
|
|
|
request.returnsObjectsAsFaults = false
|
|
|
|
request.predicate = predicate
|
|
|
|
|
|
|
|
do {
|
|
|
|
let timelineIndexes = try managedObjectContext.fetch(request)
|
|
|
|
let endFetch = CACurrentMediaTime()
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: collect timelineIndexes cost: %.2fs", ((#file as NSString).lastPathComponent), #line, #function, endFetch - start)
|
2021-04-01 08:39:15 +02:00
|
|
|
latestStatusIDs = timelineIndexes
|
|
|
|
.prefix(APIService.onceRequestStatusMaxCount) // avoid performance issue
|
2021-02-07 07:42:50 +01:00
|
|
|
.compactMap { timelineIndex in
|
2021-04-01 08:39:15 +02:00
|
|
|
timelineIndex.value(forKeyPath: #keyPath(HomeTimelineIndex.status.id)) as? Status.ID
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
stateMachine.enter(Fail.self)
|
|
|
|
return
|
|
|
|
}
|
2021-03-29 11:44:52 +02:00
|
|
|
|
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)
|
2021-02-07 07:42:50 +01:00
|
|
|
|
|
|
|
// TODO: only set large count when using Wi-Fi
|
|
|
|
viewModel.context.apiService.homeTimeline(domain: activeMastodonAuthenticationBox.domain, authorizationBox: activeMastodonAuthenticationBox)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { completion in
|
2021-03-29 11:44:52 +02:00
|
|
|
viewModel.homeTimelineNavigationBarTitleViewModel.receiveLoadingStateCompletion(completion)
|
2021-02-07 07:42:50 +01:00
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
// TODO: handle error
|
|
|
|
viewModel.isFetchingLatestTimeline.value = false
|
2021-04-01 08:39:15 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: fetch statuses failed. %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
2021-02-07 07:42:50 +01:00
|
|
|
case .finished:
|
|
|
|
// handle isFetchingLatestTimeline in fetch controller delegate
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
stateMachine.enter(Idle.self)
|
|
|
|
|
|
|
|
} receiveValue: { response in
|
2021-04-01 08:39:15 +02:00
|
|
|
// stop refresher if no new statuses
|
|
|
|
let statuses = response.value
|
|
|
|
let newStatuses = statuses.filter { !latestStatusIDs.contains($0.id) }
|
|
|
|
os_log("%{public}s[%{public}ld], %{public}s: load %{public}ld new statuses", ((#file as NSString).lastPathComponent), #line, #function, newStatuses.count)
|
2021-03-15 13:03:40 +01:00
|
|
|
|
2021-04-01 08:39:15 +02:00
|
|
|
if newStatuses.isEmpty {
|
2021-02-07 07:42:50 +01:00
|
|
|
viewModel.isFetchingLatestTimeline.value = false
|
2021-03-15 13:03:40 +01:00
|
|
|
} else {
|
2021-04-01 08:39:15 +02:00
|
|
|
if !latestStatusIDs.isEmpty {
|
2021-03-29 11:44:52 +02:00
|
|
|
viewModel.homeTimelineNavigationBarTitleViewModel.newPostsIncoming()
|
|
|
|
}
|
2021-02-07 07:42:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &viewModel.disposeBag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|