Return to previous item when the selected list/hashtag/instance is removed from the sidebar

This commit is contained in:
Shadowfacts 2022-11-30 16:47:06 -05:00
parent a2b3fc0628
commit 2f0acad866
1 changed files with 31 additions and 6 deletions

View File

@ -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<Item>()
exploreSnapshot.append([.listsHeader])
exploreSnapshot.expand([.listsHeader])
@ -240,24 +246,36 @@ class MainSidebarViewController: UIViewController {
}
@objc private func reloadSavedHashtags() {
var hashtagsSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
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<Item>()
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<Item>()
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<Item>()
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