HashtagTimelineViewModel.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // HashtagTimelineViewModel.swift
  3. // Mastodon
  4. //
  5. // Created by BradGao on 2021/3/30.
  6. //
  7. import os.log
  8. import UIKit
  9. import Combine
  10. import CoreData
  11. import CoreDataStack
  12. import GameplayKit
  13. import MastodonSDK
  14. import MastodonCore
  15. final class HashtagTimelineViewModel {
  16. let logger = Logger(subsystem: "HashtagTimelineViewModel", category: "ViewModel")
  17. let hashtag: String
  18. var disposeBag = Set<AnyCancellable>()
  19. var needLoadMiddleIndex: Int? = nil
  20. // input
  21. let context: AppContext
  22. let authContext: AuthContext
  23. let fetchedResultsController: StatusFetchedResultsController
  24. let isFetchingLatestTimeline = CurrentValueSubject<Bool, Never>(false)
  25. let timelinePredicate = CurrentValueSubject<NSPredicate?, Never>(nil)
  26. let hashtagEntity = CurrentValueSubject<Mastodon.Entity.Tag?, Never>(nil)
  27. let listBatchFetchViewModel = ListBatchFetchViewModel()
  28. // output
  29. var diffableDataSource: UITableViewDiffableDataSource<StatusSection, StatusItem>?
  30. let didLoadLatest = PassthroughSubject<Void, Never>()
  31. // bottom loader
  32. private(set) lazy var stateMachine: GKStateMachine = {
  33. // exclude timeline middle fetcher state
  34. let stateMachine = GKStateMachine(states: [
  35. State.Initial(viewModel: self),
  36. State.Reloading(viewModel: self),
  37. State.Fail(viewModel: self),
  38. State.Idle(viewModel: self),
  39. State.Loading(viewModel: self),
  40. State.NoMore(viewModel: self),
  41. ])
  42. stateMachine.enter(State.Initial.self)
  43. return stateMachine
  44. }()
  45. init(context: AppContext, authContext: AuthContext, hashtag: String) {
  46. self.context = context
  47. self.authContext = authContext
  48. self.hashtag = hashtag
  49. self.fetchedResultsController = StatusFetchedResultsController(
  50. managedObjectContext: context.managedObjectContext,
  51. domain: authContext.mastodonAuthenticationBox.domain,
  52. additionalTweetPredicate: nil
  53. )
  54. // end init
  55. }
  56. deinit {
  57. os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s:", ((#file as NSString).lastPathComponent), #line, #function)
  58. }
  59. }