NotificationTimelineViewModel.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // NotificationTimelineViewModel.swift
  3. // Mastodon
  4. //
  5. // Created by MainasuK on 2022-1-21.
  6. //
  7. import os.log
  8. import UIKit
  9. import Combine
  10. import CoreDataStack
  11. import GameplayKit
  12. import MastodonSDK
  13. import MastodonCore
  14. final class NotificationTimelineViewModel {
  15. let logger = Logger(subsystem: "NotificationTimelineViewModel", category: "ViewModel")
  16. var disposeBag = Set<AnyCancellable>()
  17. // input
  18. let context: AppContext
  19. let authContext: AuthContext
  20. let scope: Scope
  21. let feedFetchedResultsController: FeedFetchedResultsController
  22. let listBatchFetchViewModel = ListBatchFetchViewModel()
  23. @Published var isLoadingLatest = false
  24. @Published var lastAutomaticFetchTimestamp: Date?
  25. // output
  26. var diffableDataSource: UITableViewDiffableDataSource<NotificationSection, NotificationItem>?
  27. var didLoadLatest = PassthroughSubject<Void, Never>()
  28. // bottom loader
  29. private(set) lazy var loadOldestStateMachine: GKStateMachine = {
  30. // exclude timeline middle fetcher state
  31. let stateMachine = GKStateMachine(states: [
  32. LoadOldestState.Initial(viewModel: self),
  33. LoadOldestState.Loading(viewModel: self),
  34. LoadOldestState.Fail(viewModel: self),
  35. LoadOldestState.Idle(viewModel: self),
  36. LoadOldestState.NoMore(viewModel: self),
  37. ])
  38. stateMachine.enter(LoadOldestState.Initial.self)
  39. return stateMachine
  40. }()
  41. init(
  42. context: AppContext,
  43. authContext: AuthContext,
  44. scope: Scope
  45. ) {
  46. self.context = context
  47. self.authContext = authContext
  48. self.scope = scope
  49. self.feedFetchedResultsController = FeedFetchedResultsController(managedObjectContext: context.managedObjectContext)
  50. // end init
  51. feedFetchedResultsController.predicate = NotificationTimelineViewModel.feedPredicate(
  52. authenticationBox: authContext.mastodonAuthenticationBox,
  53. scope: scope
  54. )
  55. }
  56. deinit {
  57. os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
  58. }
  59. }
  60. extension NotificationTimelineViewModel {
  61. typealias Scope = APIService.MastodonNotificationScope
  62. static func feedPredicate(
  63. authenticationBox: MastodonAuthenticationBox,
  64. scope: Scope
  65. ) -> NSPredicate {
  66. let domain = authenticationBox.domain
  67. let userID = authenticationBox.userID
  68. let acct = Feed.Acct.mastodon(
  69. domain: domain,
  70. userID: userID
  71. )
  72. let predicate: NSPredicate = {
  73. switch scope {
  74. case .everything:
  75. return NSCompoundPredicate(andPredicateWithSubpredicates: [
  76. Feed.hasNotificationPredicate(),
  77. Feed.predicate(
  78. kind: .notificationAll,
  79. acct: acct
  80. )
  81. ])
  82. case .mentions:
  83. return NSCompoundPredicate(andPredicateWithSubpredicates: [
  84. Feed.hasNotificationPredicate(),
  85. Feed.predicate(
  86. kind: .notificationMentions,
  87. acct: acct
  88. ),
  89. Feed.notificationTypePredicate(types: scope.includeTypes ?? [])
  90. ])
  91. }
  92. }()
  93. return predicate
  94. }
  95. }
  96. extension NotificationTimelineViewModel {
  97. // load lastest
  98. func loadLatest() async {
  99. isLoadingLatest = true
  100. defer { isLoadingLatest = false }
  101. do {
  102. _ = try await context.apiService.notifications(
  103. maxID: nil,
  104. scope: scope,
  105. authenticationBox: authContext.mastodonAuthenticationBox
  106. )
  107. } catch {
  108. didLoadLatest.send()
  109. logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(error.localizedDescription)")
  110. }
  111. }
  112. // load timeline gap
  113. func loadMore(item: NotificationItem) async {
  114. guard case let .feedLoader(record) = item else { return }
  115. let managedObjectContext = context.managedObjectContext
  116. let key = "LoadMore@\(record.objectID)"
  117. // return when already loading state
  118. guard managedObjectContext.cache(froKey: key) == nil else { return }
  119. guard let feed = record.object(in: managedObjectContext) else { return }
  120. guard let maxID = feed.notification?.id else { return }
  121. // keep transient property live
  122. managedObjectContext.cache(feed, key: key)
  123. defer {
  124. managedObjectContext.cache(nil, key: key)
  125. }
  126. // fetch data
  127. do {
  128. _ = try await context.apiService.notifications(
  129. maxID: maxID,
  130. scope: scope,
  131. authenticationBox: authContext.mastodonAuthenticationBox
  132. )
  133. } catch {
  134. logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch more failure: \(error.localizedDescription)")
  135. }
  136. }
  137. }