diff --git a/Tusker/Screens/Main/MainSidebarViewController.swift b/Tusker/Screens/Main/MainSidebarViewController.swift index a0ff71eeaa..7e7fce2849 100644 --- a/Tusker/Screens/Main/MainSidebarViewController.swift +++ b/Tusker/Screens/Main/MainSidebarViewController.swift @@ -159,29 +159,47 @@ class MainSidebarViewController: UIViewController { exploreSnapshot.append(lists.map { .list($0) }, to: .listsHeader) exploreSnapshot.append([.addList], to: .listsHeader) DispatchQueue.main.async { - self.dataSource.apply(exploreSnapshot, to: .lists) + let selected = self.collectionView.indexPathsForSelectedItems?.first + + self.dataSource.apply(exploreSnapshot, to: .lists) { + if let selected = selected { + self.collectionView.selectItem(at: selected, animated: false, scrollPosition: .centeredVertically) + } + } } } } @objc private func reloadSavedHashtags() { + let selected = collectionView.indexPathsForSelectedItems?.first + var hashtagsSnapshot = NSDiffableDataSourceSectionSnapshot() hashtagsSnapshot.append([.savedHashtagsHeader]) hashtagsSnapshot.expand([.savedHashtagsHeader]) let sortedHashtags = SavedDataManager.shared.sortedHashtags(for: mastodonController.accountInfo!) hashtagsSnapshot.append(sortedHashtags.map { .savedHashtag($0) }, to: .savedHashtagsHeader) hashtagsSnapshot.append([.addSavedHashtag], to: .savedHashtagsHeader) - self.dataSource.apply(hashtagsSnapshot, to: .savedHashtags, animatingDifferences: false) + self.dataSource.apply(hashtagsSnapshot, to: .savedHashtags, animatingDifferences: false) { + if let selected = selected { + self.collectionView.selectItem(at: selected, animated: false, scrollPosition: .centeredVertically) + } + } } @objc private func reloadSavedInstances() { + let selected = collectionView.indexPathsForSelectedItems?.first + var instancesSnapshot = NSDiffableDataSourceSectionSnapshot() instancesSnapshot.append([.savedInstancesHeader]) instancesSnapshot.expand([.savedInstancesHeader]) let sortedInstances = SavedDataManager.shared.savedInstances(for: mastodonController.accountInfo!) instancesSnapshot.append(sortedInstances.map { .savedInstance($0) }, to: .savedInstancesHeader) instancesSnapshot.append([.addSavedInstance], to: .savedInstancesHeader) - self.dataSource.apply(instancesSnapshot, to: .savedInstances, animatingDifferences: false) + self.dataSource.apply(instancesSnapshot, to: .savedInstances, animatingDifferences: false) { + if let selected = selected { + self.collectionView.selectItem(at: selected, animated: false, scrollPosition: .centeredVertically) + } + } } // todo: deduplicate with ExploreViewController