2021-03-03 09:12:48 +01:00
|
|
|
//
|
|
|
|
// TableViewCellHeightCacheableContainer.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-3.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2021-05-21 10:52:47 +02:00
|
|
|
protocol TableViewCellHeightCacheableContainer {
|
2021-04-07 08:24:28 +02:00
|
|
|
var cellFrameCache: NSCache<NSNumber, NSValue> { get }
|
2021-05-21 10:52:47 +02:00
|
|
|
func cacheTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath)
|
|
|
|
func handleTableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
|
2021-04-07 08:24:28 +02:00
|
|
|
}
|
|
|
|
|
2021-05-21 10:52:47 +02:00
|
|
|
extension TableViewCellHeightCacheableContainer where Self: StatusProvider {
|
2021-04-07 08:24:28 +02:00
|
|
|
|
2021-04-07 10:01:57 +02:00
|
|
|
func cacheTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
2021-04-07 08:24:28 +02:00
|
|
|
guard let item = item(for: nil, indexPath: indexPath) else { return }
|
|
|
|
|
|
|
|
let key = item.hashValue
|
|
|
|
let frame = cell.frame
|
|
|
|
cellFrameCache.setObject(NSValue(cgRect: frame), forKey: NSNumber(value: key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleTableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
|
2021-04-08 05:26:15 +02:00
|
|
|
guard let item = item(for: nil, indexPath: indexPath) else { return UITableView.automaticDimension }
|
2021-04-07 08:24:28 +02:00
|
|
|
guard let frame = cellFrameCache.object(forKey: NSNumber(value: item.hashValue))?.cgRectValue else {
|
|
|
|
if case .bottomLoader = item {
|
|
|
|
return TimelineLoaderTableViewCell.cellHeight
|
|
|
|
} else {
|
2021-04-08 11:05:10 +02:00
|
|
|
return UITableView.automaticDimension
|
2021-04-07 08:24:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ceil(frame.height)
|
|
|
|
}
|
2021-03-03 09:12:48 +01:00
|
|
|
}
|