Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadM...

109 lines
4.7 KiB
Swift
Raw Normal View History

2021-02-07 07:42:50 +01:00
//
// HomeTimelineViewModel+LoadMiddleState.swift
// Mastodon
//
// Created by sxiaojian on 2021/2/5.
//
import os.log
import Foundation
import GameplayKit
import CoreData
import CoreDataStack
extension HomeTimelineViewModel {
class LoadMiddleState: GKState {
weak var viewModel: HomeTimelineViewModel?
let upperTimelineIndexObjectID: NSManagedObjectID
init(viewModel: HomeTimelineViewModel, upperTimelineIndexObjectID: NSManagedObjectID) {
self.viewModel = viewModel
self.upperTimelineIndexObjectID = upperTimelineIndexObjectID
}
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)
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
var dict = viewModel.loadMiddleSateMachineList.value
dict[upperTimelineIndexObjectID] = stateMachine
viewModel.loadMiddleSateMachineList.value = dict // trigger value change
}
}
}
extension HomeTimelineViewModel.LoadMiddleState {
class Initial: HomeTimelineViewModel.LoadMiddleState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass == Loading.self
}
}
class Loading: HomeTimelineViewModel.LoadMiddleState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
// guard let viewModel = viewModel else { return false }
return stateClass == Success.self || stateClass == Fail.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 {
stateMachine.enter(Fail.self)
return
}
guard let timelineIndex = (viewModel.fetchedResultsController.fetchedObjects ?? []).first(where: { $0.objectID == upperTimelineIndexObjectID }) else {
stateMachine.enter(Fail.self)
return
}
2021-04-01 08:39:15 +02:00
let statusIDs = (viewModel.fetchedResultsController.fetchedObjects ?? []).compactMap { timelineIndex in
timelineIndex.status.id
2021-02-07 07:42:50 +01:00
}
// TODO: only set large count when using Wi-Fi
2021-04-01 08:39:15 +02:00
let maxID = timelineIndex.status.id
2021-02-07 07:42:50 +01:00
viewModel.context.apiService.homeTimeline(domain: activeMastodonAuthenticationBox.domain,maxID: maxID, authorizationBox: activeMastodonAuthenticationBox)
.delay(for: .seconds(1), scheduler: DispatchQueue.main)
.receive(on: DispatchQueue.main)
.sink { completion in
viewModel.homeTimelineNavigationBarTitleViewModel.receiveLoadingStateCompletion(completion)
2021-02-07 07:42:50 +01:00
switch completion {
case .failure(let error):
// TODO: handle error
2021-04-01 08:39:15 +02:00
os_log("%{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
stateMachine.enter(Fail.self)
case .finished:
break
}
} receiveValue: { response in
2021-04-01 08:39:15 +02:00
let statuses = response.value
let newStatuses = statuses.filter { !statusIDs.contains($0.id) }
os_log("%{public}s[%{public}ld], %{public}s: load %{public}ld statuses, %{public}%ld new statuses", ((#file as NSString).lastPathComponent), #line, #function, statuses.count, newStatuses.count)
if newStatuses.isEmpty {
2021-02-07 07:42:50 +01:00
stateMachine.enter(Fail.self)
} else {
stateMachine.enter(Success.self)
}
}
.store(in: &viewModel.disposeBag)
}
}
class Fail: HomeTimelineViewModel.LoadMiddleState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
// guard let viewModel = viewModel else { return false }
return stateClass == Loading.self
}
}
class Success: HomeTimelineViewModel.LoadMiddleState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
// guard let viewModel = viewModel else { return false }
return false
}
}
}