mastodon-ios/Mastodon/Scene/HashtagTimeline/HashtagTimelineViewModel.swift

73 lines
2.2 KiB
Swift
Raw Normal View History

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
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
let authContext: AuthContext
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)
let listBatchFetchViewModel = ListBatchFetchViewModel()
2021-04-01 04:12:57 +02:00
// output
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, StatusItem>?
let didLoadLatest = PassthroughSubject<Void, Never>()
2021-04-01 04:12:57 +02:00
// bottom loader
private(set) lazy var stateMachine: GKStateMachine = {
2021-04-01 04:12:57 +02:00
// exclude timeline middle fetcher state
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),
2021-04-01 04:12:57 +02:00
])
stateMachine.enter(State.Initial.self)
2021-04-01 04:12:57 +02:00
return stateMachine
}()
init(context: AppContext, authContext: AuthContext, hashtag: String) {
2021-04-01 04:12:57 +02:00
self.context = context
self.authContext = authContext
2021-04-07 10:56:31 +02:00
self.hashtag = hashtag
self.fetchedResultsController = StatusFetchedResultsController(
managedObjectContext: context.managedObjectContext,
domain: authContext.mastodonAuthenticationBox.domain,
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)
}
}