mastodon-ios/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift

290 lines
13 KiB
Swift
Raw Normal View History

2021-04-13 13:46:42 +02:00
//
// ThreadViewModel+Diffable.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-4-12.
//
import UIKit
import Combine
import CoreData
import CoreDataStack
2022-10-08 07:43:06 +02:00
import MastodonCore
import MastodonSDK
2021-04-13 13:46:42 +02:00
extension ThreadViewModel {
@MainActor
2021-04-13 13:46:42 +02:00
func setupDiffableDataSource(
tableView: UITableView,
statusTableViewCellDelegate: StatusTableViewCellDelegate
2021-04-13 13:46:42 +02:00
) {
diffableDataSource = StatusSection.diffableDataSource(
tableView: tableView,
context: context,
configuration: StatusSection.Configuration(
authContext: authContext,
statusTableViewCellDelegate: statusTableViewCellDelegate,
2022-02-15 12:44:45 +01:00
timelineMiddleLoaderTableViewCellDelegate: nil,
filterContext: .thread,
activeFilters: context.statusFilterService.$activeFilters
)
2021-04-13 13:46:42 +02:00
)
// make initial snapshot animation smooth
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
2021-04-13 13:46:42 +02:00
snapshot.appendSections([.main])
if let root = self.root {
if case let .root(threadContext) = root,
let status = threadContext.status.object(in: context.managedObjectContext),
status.inReplyToID != nil
{
snapshot.appendItems([.topLoader], toSection: .main)
}
snapshot.appendItems([.thread(root)], toSection: .main)
} else {
2021-04-13 13:46:42 +02:00
}
diffableDataSource?.apply(snapshot, animatingDifferences: false)
2021-04-13 13:46:42 +02:00
$threadContext
.receive(on: DispatchQueue.main)
.sink { [weak self] threadContext in
guard let self = self else { return }
guard let _ = threadContext else {
return
}
self.loadThreadStateMachine.enter(LoadThreadState.Loading.self)
}
.store(in: &disposeBag)
2021-04-13 13:46:42 +02:00
Publishers.CombineLatest3(
$root,
mastodonStatusThreadViewModel.$ancestors,
mastodonStatusThreadViewModel.$descendants
2021-04-13 13:46:42 +02:00
)
.throttle(for: 1, scheduler: DispatchQueue.main, latest: true)
.sink { [weak self] root, ancestors, descendants in
2021-04-13 13:46:42 +02:00
guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return }
Task { @MainActor in
let oldSnapshot = diffableDataSource.snapshot()
var newSnapshot = NSDiffableDataSourceSnapshot<StatusSection, StatusItem>()
newSnapshot.appendSections([.main])
2021-04-13 13:46:42 +02:00
// top loader
let _hasReplyTo: Bool? = try? await self.context.managedObjectContext.perform {
guard case let .root(threadContext) = root else { return nil }
guard let status = threadContext.status.object(in: self.context.managedObjectContext) else { return nil }
return status.inReplyToID != nil
2021-04-13 13:46:42 +02:00
}
if let hasReplyTo = _hasReplyTo, hasReplyTo {
let state = self.loadThreadStateMachine.currentState
if state is LoadThreadState.NoMore {
// do nothing
} else {
newSnapshot.appendItems([.topLoader], toSection: .main)
}
2021-04-13 13:46:42 +02:00
}
// replies
newSnapshot.appendItems(ancestors.reversed(), toSection: .main)
// root
if let root = root {
let item = StatusItem.thread(root)
newSnapshot.appendItems([item], toSection: .main)
}
// leafs
newSnapshot.appendItems(descendants, toSection: .main)
// bottom loader
if let currentState = self.loadThreadStateMachine.currentState {
switch currentState {
case is LoadThreadState.Initial,
is LoadThreadState.Loading,
is LoadThreadState.Fail:
newSnapshot.appendItems([.bottomLoader], toSection: .main)
default:
break
}
}
let hasChanges = newSnapshot.itemIdentifiers != oldSnapshot.itemIdentifiers
if !hasChanges {
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): snapshot not changes")
2021-04-13 13:46:42 +02:00
return
} else {
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): snapshot has changes")
2021-04-13 13:46:42 +02:00
}
guard let difference = self.calculateReloadSnapshotDifference(
tableView: tableView,
oldSnapshot: oldSnapshot,
newSnapshot: newSnapshot
) else {
await self.updateDataSource(snapshot: newSnapshot, animatingDifferences: false)
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): applied new snapshot without tweak")
return
2021-04-13 13:46:42 +02:00
}
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [Snapshot] oldSnapshot: \(oldSnapshot.itemIdentifiers.debugDescription)")
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [Snapshot] newSnapshot: \(newSnapshot.itemIdentifiers.debugDescription)")
await self.updateSnapshotUsingReloadData(
tableView: tableView,
oldSnapshot: oldSnapshot,
newSnapshot: newSnapshot,
difference: difference
)
} // end Task
2021-04-13 13:46:42 +02:00
}
.store(in: &disposeBag)
}
}
extension ThreadViewModel {
@MainActor func updateDataSource(
snapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem>,
animatingDifferences: Bool
) async {
diffableDataSource?.apply(snapshot, animatingDifferences: animatingDifferences)
}
@MainActor func updateSnapshotUsingReloadData(
snapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem>
) async {
if #available(iOS 15.0, *) {
await self.diffableDataSource?.applySnapshotUsingReloadData(snapshot)
} else {
diffableDataSource?.applySnapshot(snapshot, animated: false, completion: nil)
}
2021-04-13 13:46:42 +02:00
}
// Some UI tweaks to present replies and conversation smoothly
@MainActor private func updateSnapshotUsingReloadData(
tableView: UITableView,
oldSnapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem>,
newSnapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem>,
difference: ThreadViewModel.Difference // <StatusItem>
) async {
let replies: [StatusItem] = {
newSnapshot.itemIdentifiers.filter { item in
guard case let .thread(thread) = item else { return false }
guard case .reply = thread else { return false }
return true
}
}()
// additional margin for .topLoader
let oldTopMargin: CGFloat = {
let marginHeight = TimelineTopLoaderTableViewCell.cellHeight
if oldSnapshot.itemIdentifiers.contains(.topLoader) || !replies.isEmpty {
return marginHeight
}
return .zero
}()
await self.updateSnapshotUsingReloadData(snapshot: newSnapshot)
// note:
// tweak the content offset and bottom inset
// make the table view stable when data reload
// the keypoint is set the bottom inset to make the root padding with "TopLoaderHeight" to top edge
// and restore the "TopLoaderHeight" when bottom inset adjusted
// set bottom inset. Make root item pin to top.
if let item = root.flatMap({ StatusItem.thread($0) }),
let index = newSnapshot.indexOfItem(item),
let cell = tableView.cellForRow(at: IndexPath(row: index, section: 0))
{
// always set bottom inset due to lazy reply loading
// otherwise tableView will jump when insert replies
let bottomSpacing = tableView.safeAreaLayoutGuide.layoutFrame.height - cell.frame.height - oldTopMargin
let additionalInset = round(tableView.contentSize.height - cell.frame.maxY)
tableView.contentInset.bottom = max(0, bottomSpacing - additionalInset)
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): content inset bottom: \(tableView.contentInset.bottom)")
}
// set scroll position
tableView.scrollToRow(at: difference.targetIndexPath, at: .top, animated: false)
tableView.contentOffset.y = {
var offset: CGFloat = tableView.contentOffset.y - difference.sourceDistanceToTableViewTopEdge
if tableView.contentInset.bottom != 0.0 {
// needs restore top margin if bottom inset adjusted
offset += oldTopMargin
}
return offset
}()
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): applied new snapshot")
}
2021-04-13 13:46:42 +02:00
}
extension ThreadViewModel {
struct Difference {
let item: StatusItem
2021-04-13 13:46:42 +02:00
let sourceIndexPath: IndexPath
let sourceDistanceToTableViewTopEdge: CGFloat
2021-04-13 13:46:42 +02:00
let targetIndexPath: IndexPath
}
@MainActor private func calculateReloadSnapshotDifference(
2021-04-13 13:46:42 +02:00
tableView: UITableView,
oldSnapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem>,
newSnapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem>
) -> Difference? {
2021-04-13 13:46:42 +02:00
guard oldSnapshot.numberOfItems != 0 else { return nil }
guard let indexPathsForVisibleRows = tableView.indexPathsForVisibleRows?.sorted() else { return nil }
// find index of the first visible item in both old and new snapshot
2021-04-13 13:46:42 +02:00
var _index: Int?
let items = oldSnapshot.itemIdentifiers(inSection: .main)
for (i, item) in items.enumerated() {
guard let indexPath = indexPathsForVisibleRows.first(where: { $0.row == i }) else { continue }
guard newSnapshot.indexOfItem(item) != nil else { continue }
let rectForCell = tableView.rectForRow(at: indexPath)
let distanceToTableViewTopEdge = tableView.convert(rectForCell, to: nil).origin.y - tableView.safeAreaInsets.top
guard distanceToTableViewTopEdge >= 0 else { continue }
2021-04-13 13:46:42 +02:00
_index = i
break
}
guard let index = _index else { return nil }
2021-04-13 13:46:42 +02:00
let sourceIndexPath = IndexPath(row: index, section: 0)
let rectForSourceItemCell = tableView.rectForRow(at: sourceIndexPath)
let sourceDistanceToTableViewTopEdge: CGFloat = {
if tableView.window != nil {
return tableView.convert(rectForSourceItemCell, to: nil).origin.y - tableView.safeAreaInsets.top
} else {
return rectForSourceItemCell.origin.y - tableView.contentOffset.y - tableView.safeAreaInsets.top
}
}()
guard sourceIndexPath.section < oldSnapshot.numberOfSections,
sourceIndexPath.row < oldSnapshot.numberOfItems(inSection: oldSnapshot.sectionIdentifiers[sourceIndexPath.section])
else { return nil }
let sectionIdentifier = oldSnapshot.sectionIdentifiers[sourceIndexPath.section]
let item = oldSnapshot.itemIdentifiers(inSection: sectionIdentifier)[sourceIndexPath.row]
guard let targetIndexPathRow = newSnapshot.indexOfItem(item),
let newSectionIdentifier = newSnapshot.sectionIdentifier(containingItem: item),
let targetIndexPathSection = newSnapshot.indexOfSection(newSectionIdentifier)
else { return nil }
let targetIndexPath = IndexPath(row: targetIndexPathRow, section: targetIndexPathSection)
2021-04-13 13:46:42 +02:00
return Difference(
item: item,
sourceIndexPath: sourceIndexPath,
sourceDistanceToTableViewTopEdge: sourceDistanceToTableViewTopEdge,
targetIndexPath: targetIndexPath
2021-04-13 13:46:42 +02:00
)
}
}