diff --git a/Tusker/Screens/Main/MainSidebarViewController.swift b/Tusker/Screens/Main/MainSidebarViewController.swift index 98b63dfa..4101d260 100644 --- a/Tusker/Screens/Main/MainSidebarViewController.swift +++ b/Tusker/Screens/Main/MainSidebarViewController.swift @@ -209,6 +209,12 @@ class MainSidebarViewController: UIViewController { } private func reloadLists(_ lists: [List]) { + if let selectedItem, + case .list(let list) = selectedItem, + !lists.contains(where: { $0.id == list.id }) { + returnToPreviousItem() + } + var exploreSnapshot = NSDiffableDataSourceSectionSnapshot() exploreSnapshot.append([.listsHeader]) exploreSnapshot.expand([.listsHeader]) @@ -240,24 +246,36 @@ class MainSidebarViewController: UIViewController { } @objc private func reloadSavedHashtags() { - var hashtagsSnapshot = NSDiffableDataSourceSectionSnapshot() - hashtagsSnapshot.append([.savedHashtagsHeader]) - hashtagsSnapshot.expand([.savedHashtagsHeader]) let hashtags = fetchSavedHashtags().map { Item.savedHashtag(Hashtag(name: $0.name, url: $0.url)) } + if let selectedItem, + case .savedHashtag(_) = selectedItem, + !hashtags.contains(selectedItem) { + returnToPreviousItem() + } + + var hashtagsSnapshot = NSDiffableDataSourceSectionSnapshot() + hashtagsSnapshot.append([.savedHashtagsHeader]) + hashtagsSnapshot.expand([.savedHashtagsHeader]) hashtagsSnapshot.append(hashtags, to: .savedHashtagsHeader) hashtagsSnapshot.append([.addSavedHashtag], to: .savedHashtagsHeader) self.dataSource.apply(hashtagsSnapshot, to: .savedHashtags) } @objc private func reloadSavedInstances() { - var instancesSnapshot = NSDiffableDataSourceSectionSnapshot() - instancesSnapshot.append([.savedInstancesHeader]) - instancesSnapshot.expand([.savedInstancesHeader]) let instances = fetchSavedInstances().map { Item.savedInstance($0.url) } + if let selectedItem, + case .savedInstance(_) = selectedItem, + !instances.contains(selectedItem) { + returnToPreviousItem() + } + + var instancesSnapshot = NSDiffableDataSourceSectionSnapshot() + instancesSnapshot.append([.savedInstancesHeader]) + instancesSnapshot.expand([.savedInstancesHeader]) instancesSnapshot.append(instances, to: .savedInstancesHeader) instancesSnapshot.append([.addSavedInstance], to: .savedInstancesHeader) self.dataSource.apply(instancesSnapshot, to: .savedInstances) @@ -278,6 +296,13 @@ class MainSidebarViewController: UIViewController { return } } + + private func returnToPreviousItem() { + let item = previouslySelectedItem ?? .tab(.timelines) + previouslySelectedItem = nil + select(item: item, animated: true) + sidebarDelegate?.sidebar(self, didSelectItem: item) + } private func showAddList() { let service = CreateListService(mastodonController: mastodonController, present: { self.present($0, animated: true