From 9f0883d0cba1c7fff421af928e815ac187505e54 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 7 Feb 2021 10:43:13 -0500 Subject: [PATCH] Fix sidebar item getting deselected on add list/instance/hashtag --- .../Main/MainSidebarViewController.swift | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Tusker/Screens/Main/MainSidebarViewController.swift b/Tusker/Screens/Main/MainSidebarViewController.swift index a0ff71ee..7e7fce28 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