mastodon-ios/Mastodon/Scene/Notification/NotificationViewModel+LoadO...

147 lines
5.7 KiB
Swift
Raw Normal View History

2021-04-14 13:04:11 +02:00
//
// NotificationViewModel+LoadOldestState.swift
// Mastodon
//
// Created by sxiaojian on 2021/4/14.
//
2021-04-15 04:16:30 +02:00
import CoreDataStack
2021-04-14 13:04:11 +02:00
import Foundation
import GameplayKit
import MastodonSDK
2021-04-15 04:16:30 +02:00
import os.log
2021-04-14 13:04:11 +02:00
extension NotificationViewModel {
class LoadOldestState: 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-14 13:04:11 +02:00
viewModel?.loadOldestStateMachinePublisher.send(self)
}
}
}
extension NotificationViewModel.LoadOldestState {
class Initial: NotificationViewModel.LoadOldestState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
guard let viewModel = viewModel else { return false }
guard !(viewModel.fetchedResultsController.fetchedObjects ?? []).isEmpty else { return false }
return stateClass == Loading.self
}
}
class Loading: NotificationViewModel.LoadOldestState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
2021-04-15 04:16:30 +02:00
stateClass == Fail.self || stateClass == Idle.self || stateClass == NoMore.self
2021-04-14 13:04:11 +02:00
}
override func didEnter(from previousState: GKState?) {
super.didEnter(from: previousState)
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
2021-04-14 14:02:41 +02:00
guard let activeMastodonAuthenticationBox = viewModel.activeMastodonAuthenticationBox.value else {
2021-04-14 13:04:11 +02:00
assertionFailure()
stateMachine.enter(Fail.self)
return
}
2021-04-14 14:02:41 +02:00
let notifications: [MastodonNotification]? = {
let request = MastodonNotification.sortedFetchRequest
2021-04-16 07:45:54 +02:00
request.predicate = MastodonNotification.predicate(domain: activeMastodonAuthenticationBox.domain, userID: activeMastodonAuthenticationBox.userID)
2021-04-14 14:02:41 +02:00
request.returnsObjectsAsFaults = false
do {
return try self.viewModel?.context.managedObjectContext.fetch(request)
} catch {
assertionFailure(error.localizedDescription)
return nil
}
}()
guard let last = notifications?.last else {
2021-04-14 13:04:11 +02:00
stateMachine.enter(Idle.self)
return
}
let maxID = last.id
let query = Mastodon.API.Notifications.Query(
maxID: maxID,
sinceID: nil,
minID: nil,
limit: nil,
excludeTypes: [],
accountID: nil
)
2021-04-14 13:04:11 +02:00
viewModel.context.apiService.allNotifications(
domain: activeMastodonAuthenticationBox.domain,
query: query,
2021-04-16 07:45:54 +02:00
mastodonAuthenticationBox: activeMastodonAuthenticationBox
)
.sink { completion in
switch completion {
case .failure(let error):
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:
// handle isFetchingLatestTimeline in fetch controller delegate
break
}
stateMachine.enter(Idle.self)
} receiveValue: { [weak viewModel] response in
guard let viewModel = viewModel else { return }
switch viewModel.selectedIndex.value {
case .EveryThing:
if response.value.isEmpty {
stateMachine.enter(NoMore.self)
} else {
stateMachine.enter(Idle.self)
2021-04-14 13:04:11 +02:00
}
case .Mentions:
viewModel.noMoreNotification.value = response.value.isEmpty
let list = response.value.filter { $0.type == Mastodon.Entity.Notification.NotificationType.mention }
if list.isEmpty {
stateMachine.enter(NoMore.self)
} else {
stateMachine.enter(Idle.self)
2021-04-14 13:04:11 +02:00
}
}
}
.store(in: &viewModel.disposeBag)
2021-04-14 13:04:11 +02:00
}
}
class Fail: NotificationViewModel.LoadOldestState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
2021-04-15 04:16:30 +02:00
stateClass == Loading.self || stateClass == Idle.self
2021-04-14 13:04:11 +02:00
}
}
class Idle: NotificationViewModel.LoadOldestState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
2021-04-15 04:16:30 +02:00
stateClass == Loading.self
2021-04-14 13:04:11 +02:00
}
}
class NoMore: NotificationViewModel.LoadOldestState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
// reset state if needs
2021-04-15 04:16:30 +02:00
stateClass == Idle.self
2021-04-14 13:04:11 +02:00
}
override func didEnter(from previousState: GKState?) {
guard let viewModel = viewModel else { return }
guard let diffableDataSource = viewModel.diffableDataSource else {
assertionFailure()
return
}
2021-04-15 06:35:40 +02:00
DispatchQueue.main.async {
var snapshot = diffableDataSource.snapshot()
snapshot.deleteItems([.bottomLoader])
diffableDataSource.apply(snapshot)
}
2021-04-14 13:04:11 +02:00
}
}
}