2022-04-12 11:32:38 +02:00
|
|
|
//
|
|
|
|
// DiscoveryPostsViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-12.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import GameplayKit
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
final class DiscoveryPostsViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
|
|
|
let statusFetchedResultsController: StatusFetchedResultsController
|
|
|
|
let listBatchFetchViewModel = ListBatchFetchViewModel()
|
|
|
|
|
|
|
|
// output
|
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, StatusItem>?
|
|
|
|
private(set) lazy var stateMachine: GKStateMachine = {
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
|
|
|
State.Initial(viewModel: self),
|
|
|
|
State.Reloading(viewModel: self),
|
|
|
|
State.Fail(viewModel: self),
|
|
|
|
State.Idle(viewModel: self),
|
|
|
|
State.Loading(viewModel: self),
|
|
|
|
State.NoMore(viewModel: self),
|
|
|
|
])
|
|
|
|
stateMachine.enter(State.Initial.self)
|
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
|
2022-04-13 14:43:16 +02:00
|
|
|
let didLoadLatest = PassthroughSubject<Void, Never>()
|
|
|
|
|
2022-04-12 11:32:38 +02:00
|
|
|
init(context: AppContext) {
|
|
|
|
self.context = context
|
|
|
|
self.statusFetchedResultsController = StatusFetchedResultsController(
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
|
|
|
domain: nil,
|
|
|
|
additionalTweetPredicate: nil
|
|
|
|
)
|
|
|
|
// end init
|
|
|
|
|
|
|
|
context.authenticationService.activeMastodonAuthentication
|
|
|
|
.map { $0?.domain }
|
|
|
|
.assign(to: \.value, on: statusFetchedResultsController.domain)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|