From 8003b4a92d96cad75451d51e004d701bf9c7cd34 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 1 May 2022 10:55:07 +0200 Subject: [PATCH] Observe currentProfile in ProfileView ProfileView is not interested in changes in other profiles notified by ProfileManager. Set isLoading inside ObservableObject for observable to be self-contained. Loses observation of profile deletion, but dismiss on removal is actually handled by OrganizerView, not ProfileView. Also drop unused presentationMode. --- Passepartout/App/Views/ProfileView.swift | 28 +++++++++---------- .../Managers/ProfileManager.swift | 16 ++++++----- .../Models/ObservableProfile.swift | 2 ++ 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Passepartout/App/Views/ProfileView.swift b/Passepartout/App/Views/ProfileView.swift index 76ced039..c970321c 100644 --- a/Passepartout/App/Views/ProfileView.swift +++ b/Passepartout/App/Views/ProfileView.swift @@ -43,22 +43,20 @@ struct ProfileView: View { } } - @Environment(\.presentationMode) private var presentationMode - - @ObservedObject private var profileManager: ProfileManager + @ObservedObject private var currentProfile: ObservableProfile private var isLoading: Bool { - profileManager.isLoadingCurrentProfile + currentProfile.isLoading } private var isExisting: Bool { - profileManager.isCurrentProfileExisting() + !currentProfile.value.isPlaceholder } @State private var modalType: ModalType? init() { - profileManager = .shared + currentProfile = ProfileManager.shared.currentProfile } var body: some View { @@ -71,7 +69,7 @@ struct ProfileView: View { } }.toolbar { MainMenu( - currentProfile: profileManager.currentProfile, + currentProfile: currentProfile, modalType: $modalType ).disabled(!isExisting) }.sheet(item: $modalType, content: presentedModal) @@ -80,23 +78,23 @@ struct ProfileView: View { } private var title: String { - profileManager.currentProfile.name + currentProfile.name } private var mainView: some View { List { VPNSection( - currentProfile: profileManager.currentProfile, + currentProfile: currentProfile, isLoading: isLoading ) if !isLoading { - ProviderSection(currentProfile: profileManager.currentProfile) + ProviderSection(currentProfile: currentProfile) ConfigurationSection( - currentProfile: profileManager.currentProfile, + currentProfile: currentProfile, modalType: $modalType ) - ExtraSection(currentProfile: profileManager.currentProfile) - DiagnosticsSection(currentProfile: profileManager.currentProfile) + ExtraSection(currentProfile: currentProfile) + DiagnosticsSection(currentProfile: currentProfile) } } } @@ -106,12 +104,12 @@ struct ProfileView: View { switch modalType { case .shortcuts: NavigationView { - ShortcutsView(target: profileManager.currentProfile.value) + ShortcutsView(target: currentProfile.value) }.themeGlobal() case .rename: NavigationView { - RenameView(currentProfile: profileManager.currentProfile) + RenameView(currentProfile: currentProfile) }.themeGlobal() case .paywallShortcuts: diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift index 8056c642..954dee1c 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift @@ -58,8 +58,6 @@ public class ProfileManager: ObservableObject { // MARK: Observables - @Published public private(set) var isLoadingCurrentProfile = false - public let currentProfile: ObservableProfile public let willUpdateActiveId = PassthroughSubject() @@ -255,7 +253,7 @@ extension ProfileManager { extension ProfileManager { public func loadCurrentProfile(withId id: UUID) throws { - guard !isLoadingCurrentProfile else { + guard !currentProfile.isLoading else { pp_log.warning("Already loading another profile") return } @@ -263,7 +261,7 @@ extension ProfileManager { pp_log.debug("Profile \(id) is already current profile") return } - isLoadingCurrentProfile = true + currentProfile.isLoading = true if isExistingProfile(withId: currentProfile.value.id) { pp_log.info("Committing changes of former current profile \(currentProfile.value.logDescription)") saveCurrentProfile() @@ -274,20 +272,24 @@ extension ProfileManager { currentProfile.value = result.profile if result.isReady { - isLoadingCurrentProfile = false + currentProfile.isLoading = false } else { Task { try await makeProfileReady(result.profile) - isLoadingCurrentProfile = false + currentProfile.isLoading = false } } } catch { currentProfile.value = .placeholder - isLoadingCurrentProfile = false + currentProfile.isLoading = false throw error } } + public func isCurrentProfileLoading() -> Bool { + currentProfile.isLoading + } + public func isCurrentProfileExisting() -> Bool { isExistingProfile(withId: currentProfile.value.id) } diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Models/ObservableProfile.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Models/ObservableProfile.swift index 42f0e8e7..52210e40 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Models/ObservableProfile.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Models/ObservableProfile.swift @@ -27,6 +27,8 @@ import Foundation import PassepartoutUtils public class ObservableProfile: ValueHolder, ObservableObject { + @Published public internal(set) var isLoading = false + @Published public var value: Profile public var name: String {