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") return NSLocalizedString("Refresh Statuses", comment: "refresh statuses command discoverability title")
} }
// MARK: - DiffableTimelineLikeTableViewController
override func cellProvider(_ tableView: UITableView, _ indexPath: IndexPath, _ item: Item) -> UITableViewCell? { override func cellProvider(_ tableView: UITableView, _ indexPath: IndexPath, _ item: Item) -> UITableViewCell? {
let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as! TimelineStatusTableViewCell let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as! TimelineStatusTableViewCell

View File

@ -67,8 +67,7 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
} }
private func pruneOffscreenRows() { private func pruneOffscreenRows() {
guard let lastVisibleRow = lastLastVisibleRow, guard let lastVisibleRow = tableView.indexPathsForVisibleRows?.last else {
lastVisibleRow.section < tableView.numberOfSections else {
return return
} }
@ -77,33 +76,24 @@ class DiffableTimelineLikeTableViewController<Section: Hashable & CaseIterable,
let lastVisibleRowSection = snapshot.sectionIdentifiers[lastVisibleRow.section] let lastVisibleRowSection = snapshot.sectionIdentifiers[lastVisibleRow.section]
let contentSections = snapshot.sectionIdentifiers.filter { timelineContentSections().contains($0) } 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 return
} } else if lastVisibleRow.section == maxContentSectionIndex {
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 {
let items = snapshot.itemIdentifiers(inSection: lastVisibleRowSection) let items = snapshot.itemIdentifiers(inSection: lastVisibleRowSection)
if lastVisibleRow.row < items.count - pageSize { let numberOfPagesToPrune = (items.count - lastVisibleRow.row - 1) / pageSize
let itemsToRemove = Array(items.suffix(pageSize)) if numberOfPagesToPrune > 0 {
let itemsToRemove = Array(items.suffix(numberOfPagesToPrune * pageSize))
snapshot.deleteItems(itemsToRemove) snapshot.deleteItems(itemsToRemove)
willRemoveItems(itemsToRemove) willRemoveItems(itemsToRemove)
} else { } else {
return return
} }
} else { } else {
// unreachable
return return
} }