2021-04-01 04:12:57 +02:00
|
|
|
//
|
|
|
|
// HashtagTimelineViewModel+Diffable.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by BradGao on 2021/3/30.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
|
|
|
|
extension HashtagTimelineViewModel {
|
|
|
|
func setupDiffableDataSource(
|
|
|
|
for tableView: UITableView,
|
|
|
|
dependency: NeedsDependency,
|
|
|
|
statusTableViewCellDelegate: StatusTableViewCellDelegate,
|
|
|
|
timelineMiddleLoaderTableViewCellDelegate: TimelineMiddleLoaderTableViewCellDelegate
|
|
|
|
) {
|
|
|
|
let timestampUpdatePublisher = Timer.publish(every: 1.0, on: .main, in: .common)
|
|
|
|
.autoconnect()
|
|
|
|
.share()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
|
|
|
|
diffableDataSource = StatusSection.tableViewDiffableDataSource(
|
|
|
|
for: tableView,
|
|
|
|
dependency: dependency,
|
|
|
|
managedObjectContext: context.managedObjectContext,
|
|
|
|
timestampUpdatePublisher: timestampUpdatePublisher,
|
|
|
|
statusTableViewCellDelegate: statusTableViewCellDelegate,
|
2021-04-13 13:46:42 +02:00
|
|
|
timelineMiddleLoaderTableViewCellDelegate: timelineMiddleLoaderTableViewCellDelegate,
|
|
|
|
threadReplyLoaderTableViewCellDelegate: nil
|
2021-04-01 04:12:57 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:37:05 +02:00
|
|
|
// MARK: - Compare old & new snapshots and generate new items
|
|
|
|
extension HashtagTimelineViewModel {
|
|
|
|
func generateStatusItems(newObjectIDs: [NSManagedObjectID]) {
|
2021-04-01 04:12:57 +02:00
|
|
|
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
|
|
|
|
guard let tableView = self.tableView else { return }
|
|
|
|
guard let navigationBar = self.contentOffsetAdjustableTimelineViewControllerDelegate?.navigationBar() else { return }
|
|
|
|
|
|
|
|
guard let diffableDataSource = self.diffableDataSource else { return }
|
|
|
|
|
2021-04-07 10:37:05 +02:00
|
|
|
let parentManagedObjectContext = fetchedResultsController.fetchedResultsController.managedObjectContext
|
2021-04-01 04:12:57 +02:00
|
|
|
let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
|
|
|
managedObjectContext.parent = parentManagedObjectContext
|
|
|
|
|
|
|
|
let oldSnapshot = diffableDataSource.snapshot()
|
2021-04-07 10:37:05 +02:00
|
|
|
// let snapshot = snapshot as NSDiffableDataSourceSnapshot<Int, NSManagedObjectID>
|
2021-04-01 04:12:57 +02:00
|
|
|
|
2021-04-07 07:57:03 +02:00
|
|
|
var oldSnapshotAttributeDict: [NSManagedObjectID : Item.StatusAttribute] = [:]
|
|
|
|
for item in oldSnapshot.itemIdentifiers {
|
|
|
|
guard case let .status(objectID, attribute) = item else { continue }
|
|
|
|
oldSnapshotAttributeDict[objectID] = attribute
|
|
|
|
}
|
|
|
|
|
2021-04-07 10:37:05 +02:00
|
|
|
let statusItemList: [Item] = newObjectIDs.map {
|
2021-04-07 07:57:03 +02:00
|
|
|
let attribute = oldSnapshotAttributeDict[$0] ?? Item.StatusAttribute()
|
|
|
|
return Item.status(objectID: $0, attribute: attribute)
|
2021-04-01 04:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var newSnapshot = NSDiffableDataSourceSnapshot<StatusSection, Item>()
|
|
|
|
newSnapshot.appendSections([.main])
|
|
|
|
|
|
|
|
// Check if there is a `needLoadMiddleIndex`
|
|
|
|
if let needLoadMiddleIndex = needLoadMiddleIndex, needLoadMiddleIndex < (statusItemList.count - 1) {
|
|
|
|
// If yes, insert a `middleLoader` at the index
|
|
|
|
var newItems = statusItemList
|
2021-04-07 10:37:05 +02:00
|
|
|
newItems.insert(.homeMiddleLoader(upperTimelineIndexAnchorObjectID: newObjectIDs[needLoadMiddleIndex]), at: (needLoadMiddleIndex + 1))
|
2021-04-01 04:12:57 +02:00
|
|
|
newSnapshot.appendItems(newItems, toSection: .main)
|
|
|
|
} else {
|
|
|
|
newSnapshot.appendItems(statusItemList, toSection: .main)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(self.loadoldestStateMachine.currentState is LoadOldestState.NoMore) {
|
|
|
|
newSnapshot.appendItems([.bottomLoader], toSection: .main)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let difference = self.calculateReloadSnapshotDifference(navigationBar: navigationBar, tableView: tableView, oldSnapshot: oldSnapshot, newSnapshot: newSnapshot) else {
|
|
|
|
diffableDataSource.apply(newSnapshot)
|
|
|
|
self.isFetchingLatestTimeline.value = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
diffableDataSource.apply(newSnapshot, animatingDifferences: false) {
|
|
|
|
tableView.scrollToRow(at: difference.targetIndexPath, at: .top, animated: false)
|
|
|
|
tableView.contentOffset.y = tableView.contentOffset.y - difference.offset
|
|
|
|
self.isFetchingLatestTimeline.value = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct Difference<T> {
|
|
|
|
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 }
|
2021-04-07 10:37:05 +02:00
|
|
|
guard let item = oldSnapshot.itemIdentifiers.first as? Item, case Item.status = item else { return nil }
|
2021-04-01 04:12:57 +02:00
|
|
|
|
|
|
|
let oldItemAtBeginning = oldSnapshot.itemIdentifiers(inSection: .main).first!
|
|
|
|
|
|
|
|
guard let oldItemBeginIndexInNewSnapshot = newSnapshot.itemIdentifiers(inSection: .main).firstIndex(of: oldItemAtBeginning) else { return nil }
|
|
|
|
|
|
|
|
if oldItemBeginIndexInNewSnapshot > 0 {
|
|
|
|
let targetIndexPath = IndexPath(row: oldItemBeginIndexInNewSnapshot, section: 0)
|
|
|
|
let offset = UIViewController.tableViewCellOriginOffsetToWindowTop(in: tableView, at: IndexPath(row: 0, section: 0), navigationBar: navigationBar)
|
|
|
|
return Difference(
|
|
|
|
targetIndexPath: targetIndexPath,
|
|
|
|
offset: offset
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|