Fix offscreen row pruning removing all rows from profile statuses

This commit is contained in:
Shadowfacts 2022-02-06 10:19:38 -05:00
parent 6ba5f70615
commit 804fdb439d
2 changed files with 11 additions and 19 deletions

View File

@ -52,6 +52,8 @@ class ProfileStatusesViewController: DiffableTimelineLikeTableViewController<Pro
return NSLocalizedString("Refresh Statuses", comment: "refresh statuses command discoverability title")
}
// MARK: - DiffableTimelineLikeTableViewController
override func cellProvider(_ tableView: UITableView, _ indexPath: IndexPath, _ item: Item) -> UITableViewCell? {
let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as! TimelineStatusTableViewCell

View File

@ -67,8 +67,7 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
}
private func pruneOffscreenRows() {
guard let lastVisibleRow = lastLastVisibleRow,
lastVisibleRow.section < tableView.numberOfSections else {
guard let lastVisibleRow = tableView.indexPathsForVisibleRows?.last else {
return
}
@ -77,33 +76,24 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
let lastVisibleRowSection = snapshot.sectionIdentifiers[lastVisibleRow.section]
let contentSections = snapshot.sectionIdentifiers.filter { timelineContentSections().contains($0) }
let contentSectionIndices = contentSections.compactMap(snapshot.indexOfSection(_:))
let maxContentSectionIndex = contentSectionIndices.max()!
guard let lastVisibleContentSectionIndex = contentSections.lastIndex(of: lastVisibleRowSection) else {
if lastVisibleRow.section < maxContentSectionIndex {
return
}
if lastVisibleContentSectionIndex < contentSections.count - 1 {
// there are more content sections below the current last visible one
let sectionsToRemove = contentSections[lastVisibleContentSectionIndex...]
snapshot.deleteSections(Array(sectionsToRemove))
let itemsToRemove = sectionsToRemove.filter {
snapshot.indexOfSection($0) != nil
}.flatMap(snapshot.itemIdentifiers(inSection:))
willRemoveItems(itemsToRemove)
} else if lastVisibleContentSectionIndex == contentSections.count - 1 {
} else if lastVisibleRow.section == maxContentSectionIndex {
let items = snapshot.itemIdentifiers(inSection: lastVisibleRowSection)
if lastVisibleRow.row < items.count - pageSize {
let itemsToRemove = Array(items.suffix(pageSize))
let numberOfPagesToPrune = (items.count - lastVisibleRow.row - 1) / pageSize
if numberOfPagesToPrune > 0 {
let itemsToRemove = Array(items.suffix(numberOfPagesToPrune * pageSize))
snapshot.deleteItems(itemsToRemove)
willRemoveItems(itemsToRemove)
} else {
return
}
} else {
// unreachable
return
}