From a47667c15bffdec88de6ba20aee43f2f951b5721 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Wed, 2 Sep 2020 00:39:42 -0700 Subject: [PATCH] Fix scroll position maintenance --- View Controllers/StatusListViewController.swift | 8 +++++--- ViewModels/Sources/ViewModels/StatusListViewModel.swift | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/View Controllers/StatusListViewController.swift b/View Controllers/StatusListViewController.swift index 0f2328a..10e2215 100644 --- a/View Controllers/StatusListViewController.swift +++ b/View Controllers/StatusListViewController.swift @@ -70,10 +70,12 @@ class StatusListViewController: UITableViewController { self.dataSource.apply($0.snapshot(), animatingDifferences: false) { if let id = self.viewModel.maintainScrollPositionOfStatusID, - let indexPath = self.dataSource.indexPath(for: id), - let offsetFromNavigationBar = offsetFromNavigationBar { + let indexPath = self.dataSource.indexPath(for: id) { self.tableView.scrollToRow(at: indexPath, at: .top, animated: false) - self.tableView.contentOffset.y -= offsetFromNavigationBar + + if let offsetFromNavigationBar = offsetFromNavigationBar { + self.tableView.contentOffset.y -= offsetFromNavigationBar + } } } } diff --git a/ViewModels/Sources/ViewModels/StatusListViewModel.swift b/ViewModels/Sources/ViewModels/StatusListViewModel.swift index 88e2d6d..bcb845c 100644 --- a/ViewModels/Sources/ViewModels/StatusListViewModel.swift +++ b/ViewModels/Sources/ViewModels/StatusListViewModel.swift @@ -92,8 +92,10 @@ private extension StatusListViewModel { func determineIfScrollPositionShouldBeMaintained(newStatusSections: [[Status]]) { maintainScrollPositionOfStatusID = nil // clear old value + let flatStatusIDs = statusIDs.reduce([], +) + // Maintain scroll position of parent after initial load of context - if let contextParentID = contextParentID, statusIDs.reduce([], +) == [contextParentID] { + if let contextParentID = contextParentID, flatStatusIDs == [contextParentID] || flatStatusIDs == [] { maintainScrollPositionOfStatusID = contextParentID } }