2021-04-12 10:31:53 +02:00
|
|
|
//
|
|
|
|
// NotificationViewModel+LoadLatestState.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-04-15 04:16:30 +02:00
|
|
|
import Foundation
|
2021-04-12 10:31:53 +02:00
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
2021-04-15 04:16:30 +02:00
|
|
|
import os.log
|
|
|
|
import func QuartzCore.CACurrentMediaTime
|
2021-04-12 10:31:53 +02:00
|
|
|
|
|
|
|
extension NotificationViewModel {
|
|
|
|
class LoadLatestState: GKState {
|
|
|
|
weak var viewModel: NotificationViewModel?
|
|
|
|
|
|
|
|
init(viewModel: NotificationViewModel) {
|
|
|
|
self.viewModel = viewModel
|
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
2021-04-15 04:16:30 +02:00
|
|
|
os_log("%{public}s[%{public}ld], %{public}s: enter %s, previous: %s", (#file as NSString).lastPathComponent, #line, #function, debugDescription, previousState.debugDescription)
|
2021-04-12 10:31:53 +02:00
|
|
|
viewModel?.loadLatestStateMachinePublisher.send(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NotificationViewModel.LoadLatestState {
|
|
|
|
class Initial: NotificationViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
2021-04-15 04:16:30 +02:00
|
|
|
stateClass == Loading.self
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Loading: NotificationViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
2021-04-15 04:16:30 +02:00
|
|
|
stateClass == Fail.self || stateClass == Idle.self
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func didEnter(from previousState: GKState?) {
|
|
|
|
super.didEnter(from: previousState)
|
|
|
|
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
|
2021-04-13 15:31:49 +02:00
|
|
|
guard let activeMastodonAuthenticationBox = viewModel.activeMastodonAuthenticationBox.value else {
|
2021-04-12 10:31:53 +02:00
|
|
|
// sign out when loading will enter here
|
|
|
|
stateMachine.enter(Fail.self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let query = Mastodon.API.Notifications.Query(
|
|
|
|
maxID: nil,
|
|
|
|
sinceID: nil,
|
|
|
|
minID: nil,
|
|
|
|
limit: nil,
|
2021-04-27 08:27:27 +02:00
|
|
|
excludeTypes: [],
|
2021-04-16 07:45:54 +02:00
|
|
|
accountID: nil
|
|
|
|
)
|
2021-04-12 10:31:53 +02:00
|
|
|
viewModel.context.apiService.allNotifications(
|
|
|
|
domain: activeMastodonAuthenticationBox.domain,
|
|
|
|
query: query,
|
2021-04-16 07:45:54 +02:00
|
|
|
mastodonAuthenticationBox: activeMastodonAuthenticationBox
|
|
|
|
)
|
2021-04-27 11:27:03 +02:00
|
|
|
.sink { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure(let error):
|
|
|
|
viewModel.isFetchingLatestNotification.value = false
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: fetch notification failed. %s", (#file as NSString).lastPathComponent, #line, #function, error.localizedDescription)
|
|
|
|
case .finished:
|
|
|
|
// toggle unread state
|
|
|
|
viewModel.context.notificationService.hasUnreadPushNotification.value = false
|
|
|
|
// handle isFetchingLatestTimeline in fetch controller delegate
|
|
|
|
break
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
2021-04-27 11:27:03 +02:00
|
|
|
|
|
|
|
stateMachine.enter(Idle.self)
|
|
|
|
} receiveValue: { response in
|
|
|
|
if response.value.isEmpty {
|
|
|
|
viewModel.isFetchingLatestNotification.value = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &viewModel.disposeBag)
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Fail: NotificationViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
2021-04-15 04:16:30 +02:00
|
|
|
stateClass == Loading.self || stateClass == Idle.self
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Idle: NotificationViewModel.LoadLatestState {
|
|
|
|
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
|
2021-04-15 04:16:30 +02:00
|
|
|
stateClass == Loading.self
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|