mastodon-ios/Mastodon/Scene/PublicTimeline/PublicTimelineViewModel.swift

166 lines
6.6 KiB
Swift
Raw Normal View History

2021-01-28 09:10:30 +01:00
//
// PublicTimelineViewModel.swift
// Mastodon
//
// Created by sxiaojian on 2021/1/27.
//
2021-02-02 12:04:24 +01:00
import AlamofireImage
2021-01-28 09:10:30 +01:00
import Combine
import CoreData
import CoreDataStack
2021-02-02 12:04:24 +01:00
import GameplayKit
2021-01-28 09:10:30 +01:00
import MastodonSDK
2021-02-02 12:04:24 +01:00
import os.log
import UIKit
2021-01-28 09:10:30 +01:00
class PublicTimelineViewModel: NSObject {
var disposeBag = Set<AnyCancellable>()
// input
let context: AppContext
2021-04-01 08:39:15 +02:00
let fetchedResultsController: NSFetchedResultsController<Status>
2021-02-04 08:09:58 +01:00
2021-02-02 12:04:24 +01:00
let isFetchingLatestTimeline = CurrentValueSubject<Bool, Never>(false)
2021-01-28 09:10:30 +01:00
2021-02-04 08:09:58 +01:00
// middle loader
let loadMiddleSateMachineList = CurrentValueSubject<[String: GKStateMachine], Never>([:])
weak var tableView: UITableView?
weak var contentOffsetAdjustableTimelineViewControllerDelegate: ContentOffsetAdjustableTimelineViewControllerDelegate?
2021-02-04 08:09:58 +01:00
//
2021-04-01 08:39:15 +02:00
var statusIDsWhichHasGap = [String]()
2021-01-28 09:10:30 +01:00
// output
var diffableDataSource: UITableViewDiffableDataSource<StatusSection, Item>?
2021-01-28 09:10:30 +01:00
2021-02-02 12:04:24 +01:00
lazy var stateMachine: GKStateMachine = {
let stateMachine = GKStateMachine(states: [
State.Initial(viewModel: self),
State.Loading(viewModel: self),
State.Fail(viewModel: self),
State.Idle(viewModel: self),
State.LoadingMore(viewModel: self),
])
stateMachine.enter(State.Initial.self)
return stateMachine
}()
2021-04-01 08:39:15 +02:00
let statusIDs = CurrentValueSubject<[String], Never>([])
2021-01-28 09:10:30 +01:00
let items = CurrentValueSubject<[Item], Never>([])
var cellFrameCache = NSCache<NSNumber, NSValue>()
init(context: AppContext) {
self.context = context
self.fetchedResultsController = {
2021-04-01 08:39:15 +02:00
let fetchRequest = Status.sortedFetchRequest
fetchRequest.predicate = Status.predicate(domain: "", ids: [])
2021-01-28 09:10:30 +01:00
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.fetchBatchSize = 20
let controller = NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: context.managedObjectContext,
sectionNameKeyPath: nil,
cacheName: nil
)
return controller
}()
super.init()
2021-02-02 12:04:24 +01:00
fetchedResultsController.delegate = self
2021-01-28 09:10:30 +01:00
items
.receive(on: DispatchQueue.main)
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.sink { [weak self] items in
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
guard let navigationBar = self.contentOffsetAdjustableTimelineViewControllerDelegate?.navigationBar() else { return }
guard let tableView = self.tableView else { return }
let oldSnapshot = diffableDataSource.snapshot()
2021-02-02 12:04:24 +01:00
os_log("%{public}s[%{public}ld], %{public}s: items did change", (#file as NSString).lastPathComponent, #line, #function)
2021-01-28 09:10:30 +01:00
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, Item>()
2021-01-28 09:10:30 +01:00
snapshot.appendSections([.main])
snapshot.appendItems(items)
2021-02-03 06:01:50 +01:00
if let currentState = self.stateMachine.currentState {
switch currentState {
case is State.Idle, is State.LoadingMore, is State.Fail:
snapshot.appendItems([.bottomLoader], toSection: .main)
default:
break
}
}
DispatchQueue.main.async {
guard let difference = self.calculateReloadSnapshotDifference(navigationBar: navigationBar, tableView: tableView, oldSnapshot: oldSnapshot, newSnapshot: snapshot) else {
diffableDataSource.apply(snapshot)
self.isFetchingLatestTimeline.value = false
return
}
diffableDataSource.apply(snapshot, animatingDifferences: false) {
tableView.scrollToRow(at: difference.targetIndexPath, at: .top, animated: false)
tableView.contentOffset.y = tableView.contentOffset.y - difference.offset
self.isFetchingLatestTimeline.value = false
}
}
2021-01-28 09:10:30 +01:00
}
.store(in: &disposeBag)
2021-04-01 08:39:15 +02:00
statusIDs
2021-01-28 09:10:30 +01:00
.receive(on: DispatchQueue.main)
.sink { [weak self] ids in
guard let self = self else { return }
let domain = self.context.authenticationService.activeMastodonAuthenticationBox.value?.domain ?? ""
2021-04-01 08:39:15 +02:00
self.fetchedResultsController.fetchRequest.predicate = Status.predicate(domain: domain, ids: ids)
2021-01-28 09:10:30 +01:00
do {
try self.fetchedResultsController.performFetch()
} catch {
assertionFailure(error.localizedDescription)
}
}
.store(in: &disposeBag)
}
deinit {
2021-02-02 12:04:24 +01:00
os_log("%{public}s[%{public}ld], %{public}s", (#file as NSString).lastPathComponent, #line, #function)
2021-01-28 09:10:30 +01:00
}
private struct Difference<T> {
let item: T
let sourceIndexPath: IndexPath
let targetIndexPath: IndexPath
let offset: CGFloat
}
private func calculateReloadSnapshotDifference<T: Hashable>(
navigationBar: UINavigationBar,
tableView: UITableView,
oldSnapshot: NSDiffableDataSourceSnapshot<StatusSection, T>,
newSnapshot: NSDiffableDataSourceSnapshot<StatusSection, T>
) -> Difference<T>? {
guard oldSnapshot.numberOfItems != 0 else { return nil }
// old snapshot not empty. set source index path to first item if not match
let sourceIndexPath = UIViewController.topVisibleTableViewCellIndexPath(in: tableView, navigationBar: navigationBar) ?? IndexPath(row: 0, section: 0)
guard sourceIndexPath.row < oldSnapshot.itemIdentifiers(inSection: .main).count else { return nil }
let timelineItem = oldSnapshot.itemIdentifiers(inSection: .main)[sourceIndexPath.row]
guard let itemIndex = newSnapshot.itemIdentifiers(inSection: .main).firstIndex(of: timelineItem) else { return nil }
let targetIndexPath = IndexPath(row: itemIndex, section: 0)
let offset = UIViewController.tableViewCellOriginOffsetToWindowTop(in: tableView, at: sourceIndexPath, navigationBar: navigationBar)
return Difference(
item: timelineItem,
sourceIndexPath: sourceIndexPath,
targetIndexPath: targetIndexPath,
offset: offset
)
}
2021-01-28 09:10:30 +01:00
}