2021-04-01 04:12:57 +02:00
|
|
|
//
|
|
|
|
// HashtagTimelineViewModel+LoadLatestState.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by BradGao on 2021/3/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import GameplayKit
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension HashtagTimelineViewModel {
|
|
|
|
class LoadLatestState: GKState {
|
|
|
|
weak var viewModel: HashtagTimelineViewModel?
|
|
|
|
|
|
|
|
init(viewModel: HashtagTimelineViewModel) {
|
|
|
|
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 HashtagTimelineViewModel.LoadLatestState {
|
|
|
|
class Initial: HashtagTimelineViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Loading: HashtagTimelineViewModel.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 {
|
|
|
|
// sign out when loading will enter here
|
|
|
|
stateMachine.enter(Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: only set large count when using Wi-Fi
|
|
|
|
viewModel.context.apiService.hashtagTimeline(
|
|
|
|
domain: activeMastodonAuthenticationBox.domain,
|
2021-04-07 10:56:31 +02:00
|
|
|
hashtag: viewModel.hashtag,
|
2021-04-01 04:12:57 +02:00
|
|
|
authorizationBox: activeMastodonAuthenticationBox)
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
// TODO: handle error
|
|
|
|
viewModel.isFetchingLatestTimeline.value = false
|
2021-04-02 13:40:15 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: fetch statues failed. %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
|
2021-04-01 04:12:57 +02:00
|
|
|
case .finished:
|
|
|
|
// handle isFetchingLatestTimeline in fetch controller delegate
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
stateMachine.enter(Idle.self)
|
|
|
|
|
|
|
|
} receiveValue: { response in
|
|
|
|
let newStatusIDList = response.value.map { $0.id }
|
|
|
|
|
|
|
|
// When response data:
|
|
|
|
// 1. is not empty
|
|
|
|
// 2. last status are not recorded
|
|
|
|
// Then we may have middle data to load
|
2021-04-07 10:37:05 +02:00
|
|
|
var oldStatusIDs = viewModel.fetchedResultsController.statusIDs.value
|
|
|
|
if !oldStatusIDs.isEmpty, let lastNewStatusID = newStatusIDList.last,
|
|
|
|
!oldStatusIDs.contains(lastNewStatusID) {
|
2021-04-01 04:12:57 +02:00
|
|
|
viewModel.needLoadMiddleIndex = (newStatusIDList.count - 1)
|
|
|
|
} else {
|
|
|
|
viewModel.needLoadMiddleIndex = nil
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:37:05 +02:00
|
|
|
oldStatusIDs.insert(contentsOf: newStatusIDList, at: 0)
|
|
|
|
let newIDs = oldStatusIDs.removingDuplicates()
|
2021-04-01 04:12:57 +02:00
|
|
|
|
2021-04-07 10:37:05 +02:00
|
|
|
viewModel.fetchedResultsController.statusIDs.value = newIDs
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
.store(in: &viewModel.disposeBag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fail: HashtagTimelineViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self || stateClass == Idle.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Idle: HashtagTimelineViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
|
|
|
return stateClass == Loading.self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|