From 18e9f7cbb5151ff2055eb3f3e76b4ff89b1c4d5f Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Tue, 19 Apr 2022 18:34:02 +0200 Subject: [PATCH] Clear profile selection if current profile removed Do it before ForEach rearranges NavigationLink elements, because it may trigger undesired navigation to a deleted profile. Restore related assertion. --- Passepartout/App/Views/OrganizerView+Profiles.swift | 12 ++++++++++++ .../Managers/ProfileManager.swift | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Passepartout/App/Views/OrganizerView+Profiles.swift b/Passepartout/App/Views/OrganizerView+Profiles.swift index 252212cd..b2a49ce7 100644 --- a/Passepartout/App/Views/OrganizerView+Profiles.swift +++ b/Passepartout/App/Views/OrganizerView+Profiles.swift @@ -160,11 +160,23 @@ extension OrganizerView.ProfilesList { } private func removeProfiles(_ indexSet: IndexSet) { + withAnimation { + doRemoveProfiles(indexSet) + } + } + + private func doRemoveProfiles(_ indexSet: IndexSet) { let headers = profileManager.headers.sorted() var toDelete: [UUID] = [] indexSet.forEach { toDelete.append(headers[$0].id) } + + // clear selection before removal to avoid triggering a bogus navigation push + if let selectedProfileId = selectedProfileId, toDelete.contains(selectedProfileId) { + self.selectedProfileId = nil + } + profileManager.removeProfiles(withIds: toDelete) } diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift index e9a5f687..372e16bd 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift @@ -162,7 +162,7 @@ extension ProfileManager { } guard let profile = strategy.profile(withId: id) else { -// assertionFailure("Profile in headers yet not found in persistent store") + assertionFailure("Profile in headers yet not found in persistent store") return nil } guard availabilityFilter?(profile.header) ?? true else {