2021-04-01 04:12:57 +02:00
|
|
|
//
|
|
|
|
// HashtagTimelineViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by BradGao on 2021/3/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import GameplayKit
|
|
|
|
import MastodonSDK
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
final class HashtagTimelineViewModel {
|
|
|
|
|
|
|
|
let logger = Logger(subsystem: "HashtagTimelineViewModel", category: "ViewModel")
|
2021-04-01 04:12:57 +02:00
|
|
|
|
2021-04-07 10:56:31 +02:00
|
|
|
let hashtag: String
|
2021-04-01 04:12:57 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
var needLoadMiddleIndex: Int? = nil
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-10-09 14:07:57 +02:00
|
|
|
let authContext: AuthContext
|
2021-04-07 10:37:05 +02:00
|
|
|
let fetchedResultsController: StatusFetchedResultsController
|
2021-04-01 04:12:57 +02:00
|
|
|
let isFetchingLatestTimeline = CurrentValueSubject<Bool, Never>(false)
|
|
|
|
let timelinePredicate = CurrentValueSubject<NSPredicate?, Never>(nil)
|
2021-04-02 04:21:51 +02:00
|
|
|
let hashtagEntity = CurrentValueSubject<Mastodon.Entity.Tag?, Never>(nil)
|
2022-01-27 14:23:39 +01:00
|
|
|
let listBatchFetchViewModel = ListBatchFetchViewModel()
|
2021-04-01 04:12:57 +02:00
|
|
|
|
|
|
|
// output
|
2022-01-27 14:23:39 +01:00
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, StatusItem>?
|
|
|
|
let didLoadLatest = PassthroughSubject<Void, Never>()
|
|
|
|
|
2021-04-01 04:12:57 +02:00
|
|
|
// bottom loader
|
2022-05-16 05:34:20 +02:00
|
|
|
private(set) lazy var stateMachine: GKStateMachine = {
|
2021-04-01 04:12:57 +02:00
|
|
|
// exclude timeline middle fetcher state
|
|
|
|
let stateMachine = GKStateMachine(states: [
|
2022-05-16 05:34:20 +02:00
|
|
|
State.Initial(viewModel: self),
|
|
|
|
State.Reloading(viewModel: self),
|
|
|
|
State.Fail(viewModel: self),
|
|
|
|
State.Idle(viewModel: self),
|
|
|
|
State.Loading(viewModel: self),
|
|
|
|
State.NoMore(viewModel: self),
|
2021-04-01 04:12:57 +02:00
|
|
|
])
|
2022-05-16 05:34:20 +02:00
|
|
|
stateMachine.enter(State.Initial.self)
|
2021-04-01 04:12:57 +02:00
|
|
|
return stateMachine
|
|
|
|
}()
|
|
|
|
|
2022-10-09 14:07:57 +02:00
|
|
|
init(context: AppContext, authContext: AuthContext, hashtag: String) {
|
2021-04-01 04:12:57 +02:00
|
|
|
self.context = context
|
2022-10-09 14:07:57 +02:00
|
|
|
self.authContext = authContext
|
2021-04-07 10:56:31 +02:00
|
|
|
self.hashtag = hashtag
|
2022-01-27 14:23:39 +01:00
|
|
|
self.fetchedResultsController = StatusFetchedResultsController(
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
2022-10-09 14:07:57 +02:00
|
|
|
domain: authContext.mastodonAuthenticationBox.domain,
|
2022-01-27 14:23:39 +01:00
|
|
|
additionalTweetPredicate: nil
|
|
|
|
)
|
|
|
|
// end init
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s:", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|