feat: add missing scroll restore action for "See More Posts" title view

This commit is contained in:
CMK 2021-09-29 17:05:40 +08:00
parent 14cefe2d0d
commit 051f436abe
1 changed files with 21 additions and 2 deletions

View File

@ -439,9 +439,16 @@ extension HomeTimelineViewController {
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
switch scrollView { switch scrollView {
case tableView: case tableView:
// handle scrollToTop
let indexPath = IndexPath(row: 0, section: 0)
guard viewModel.diffableDataSource?.itemIdentifier(for: indexPath) != nil else {
return true
}
// save position
savePositionBeforeScrollToTop() savePositionBeforeScrollToTop()
return true // override by custom scrollToRow
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
return false
default: default:
assertionFailure() assertionFailure()
return true return true
@ -449,6 +456,16 @@ extension HomeTimelineViewController {
} }
private func savePositionBeforeScrollToTop() { private func savePositionBeforeScrollToTop() {
// check save action interval
// should not fast than 0.5s to prevent save when scrollToTop on-flying
if let record = viewModel.scrollPositionRecord.value {
let now = Date()
guard now.timeIntervalSince(record.timestamp) > 0.5 else {
// skip this save action
return
}
}
guard let diffableDataSource = viewModel.diffableDataSource else { return } guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let anchorIndexPaths = tableView.indexPathsForVisibleRows?.sorted() else { return } guard let anchorIndexPaths = tableView.indexPathsForVisibleRows?.sorted() else { return }
guard !anchorIndexPaths.isEmpty else { return } guard !anchorIndexPaths.isEmpty else { return }
@ -674,6 +691,8 @@ extension HomeTimelineViewController: HomeTimelineNavigationBarTitleViewDelegate
guard let diffableDataSource = viewModel.diffableDataSource else { return } guard let diffableDataSource = viewModel.diffableDataSource else { return }
let indexPath = IndexPath(row: 0, section: 0) let indexPath = IndexPath(row: 0, section: 0)
guard diffableDataSource.itemIdentifier(for: indexPath) != nil else { return } guard diffableDataSource.itemIdentifier(for: indexPath) != nil else { return }
savePositionBeforeScrollToTop()
tableView.scrollToRow(at: indexPath, at: .top, animated: true) tableView.scrollToRow(at: indexPath, at: .top, animated: true)
case .offlineButton: case .offlineButton:
// TODO: retry // TODO: retry