fix: using notification id to make cell frame cache works

This commit is contained in:
CMK 2021-07-12 19:20:54 +08:00
parent bb0df27486
commit f213419ecb
2 changed files with 27 additions and 17 deletions

View File

@ -198,32 +198,34 @@ extension NotificationViewController {
// MARK: - StatusTableViewControllerAspect // MARK: - StatusTableViewControllerAspect
extension NotificationViewController: StatusTableViewControllerAspect { } extension NotificationViewController: StatusTableViewControllerAspect { }
// MARK: - TableViewCellHeightCacheableContainer extension NotificationViewController {
extension NotificationViewController: TableViewCellHeightCacheableContainer {
var cellFrameCache: NSCache<NSNumber, NSValue> {
viewModel.cellFrameCache
}
func cacheTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { func cacheTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let diffableDataSource = viewModel.diffableDataSource else { return } guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return } guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }
let key = item.hashValue switch item {
let frame = cell.frame case .notification(let objectID, _):
viewModel.cellFrameCache.setObject(NSValue(cgRect: frame), forKey: NSNumber(value: key)) guard let object = try? viewModel.fetchedResultsController.managedObjectContext.existingObject(with: objectID) as? MastodonNotification else { return }
let key = object.id as NSString
let frame = cell.frame
viewModel.cellFrameCache.setObject(NSValue(cgRect: frame), forKey: key)
case .bottomLoader:
break
}
} }
func handleTableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { func handleTableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
guard let diffableDataSource = viewModel.diffableDataSource else { return UITableView.automaticDimension } guard let diffableDataSource = viewModel.diffableDataSource else { return UITableView.automaticDimension }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return UITableView.automaticDimension } guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return UITableView.automaticDimension }
guard let frame = viewModel.cellFrameCache.object(forKey: NSNumber(value: item.hashValue))?.cgRectValue else { switch item {
if case .bottomLoader = item { case .notification(let objectID, _):
return TimelineLoaderTableViewCell.cellHeight guard let object = try? viewModel.fetchedResultsController.managedObjectContext.existingObject(with: objectID) as? MastodonNotification else { return UITableView.automaticDimension }
} else { let key = object.id as NSString
return UITableView.automaticDimension guard let frame = viewModel.cellFrameCache.object(forKey: key)?.cgRectValue else { return UITableView.automaticDimension }
} return frame.height
case .bottomLoader:
return TimelineLoaderTableViewCell.cellHeight
} }
return ceil(frame.height)
} }
} }
@ -237,6 +239,14 @@ extension NotificationViewController: UITableViewDelegate {
open(item: item) open(item: item)
} }
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cacheTableView(tableView, didEndDisplaying: cell, forRowAt: indexPath)
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return handleTableView(tableView, estimatedHeightForRowAt: indexPath)
}
} }
extension NotificationViewController { extension NotificationViewController {

View File

@ -29,7 +29,7 @@ final class NotificationViewModel: NSObject {
let activeMastodonAuthenticationBox: CurrentValueSubject<AuthenticationService.MastodonAuthenticationBox?, Never> let activeMastodonAuthenticationBox: CurrentValueSubject<AuthenticationService.MastodonAuthenticationBox?, Never>
let fetchedResultsController: NSFetchedResultsController<MastodonNotification>! let fetchedResultsController: NSFetchedResultsController<MastodonNotification>!
let notificationPredicate = CurrentValueSubject<NSPredicate?, Never>(nil) let notificationPredicate = CurrentValueSubject<NSPredicate?, Never>(nil)
let cellFrameCache = NSCache<NSNumber, NSValue>() let cellFrameCache = NSCache<NSString, NSValue>()
var needsScrollToTopAfterDataSourceUpdate = false var needsScrollToTopAfterDataSourceUpdate = false
let dataSourceDidUpdated = PassthroughSubject<Void, Never>() let dataSourceDidUpdated = PassthroughSubject<Void, Never>()