Merge pull request #718 from mastodon/fix-issue-714

Fix timeline reload blink issue
This commit is contained in:
CMK 2022-12-02 00:21:29 +08:00 committed by GitHub
commit 0261dfc72a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 10 deletions

View File

@ -41,7 +41,7 @@ extension HomeTimelineViewModel {
guard let self = self else { return } guard let self = self else { return }
guard let diffableDataSource = self.diffableDataSource else { return } guard let diffableDataSource = self.diffableDataSource else { return }
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): incoming \(records.count) objects") self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): incoming \(records.count) objects")
Task { Task { @MainActor in
let start = CACurrentMediaTime() let start = CACurrentMediaTime()
defer { defer {
let end = CACurrentMediaTime() let end = CACurrentMediaTime()
@ -98,22 +98,22 @@ extension HomeTimelineViewModel {
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): snapshot has changes") self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): snapshot has changes")
} }
guard let difference = await self.calculateReloadSnapshotDifference( guard let difference = self.calculateReloadSnapshotDifference(
tableView: tableView, tableView: tableView,
oldSnapshot: oldSnapshot, oldSnapshot: oldSnapshot,
newSnapshot: newSnapshot newSnapshot: newSnapshot
) else { ) else {
await self.updateSnapshotUsingReloadData(snapshot: newSnapshot) self.updateSnapshotUsingReloadData(snapshot: newSnapshot)
self.didLoadLatest.send() self.didLoadLatest.send()
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): applied new snapshot") self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): applied new snapshot")
return return
} }
await self.updateSnapshotUsingReloadData(snapshot: newSnapshot) self.updateSnapshotUsingReloadData(snapshot: newSnapshot)
await tableView.scrollToRow(at: difference.targetIndexPath, at: .top, animated: false) tableView.scrollToRow(at: difference.targetIndexPath, at: .top, animated: false)
var contentOffset = await tableView.contentOffset var contentOffset = tableView.contentOffset
contentOffset.y = await tableView.contentOffset.y - difference.sourceDistanceToTableViewTopEdge contentOffset.y = tableView.contentOffset.y - difference.sourceDistanceToTableViewTopEdge
await tableView.setContentOffset(contentOffset, animated: false) tableView.setContentOffset(contentOffset, animated: false)
self.didLoadLatest.send() self.didLoadLatest.send()
self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): applied new snapshot") self.logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): applied new snapshot")
} // end Task } // end Task
@ -135,9 +135,9 @@ extension HomeTimelineViewModel {
@MainActor func updateSnapshotUsingReloadData( @MainActor func updateSnapshotUsingReloadData(
snapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem> snapshot: NSDiffableDataSourceSnapshot<StatusSection, StatusItem>
) async { ) {
if #available(iOS 15.0, *) { if #available(iOS 15.0, *) {
await self.diffableDataSource?.applySnapshotUsingReloadData(snapshot) self.diffableDataSource?.applySnapshotUsingReloadData(snapshot)
} else { } else {
diffableDataSource?.applySnapshot(snapshot, animated: false, completion: nil) diffableDataSource?.applySnapshot(snapshot, animated: false, completion: nil)
} }