From 93abaf538b6a7946afe6b04ca082ead6850f95f6 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Tue, 3 May 2022 10:25:10 +0200 Subject: [PATCH] Simplify redundant parts of ProfileManager - Drop overthought activeHeader - Drop willUpdateCurrentProfile, use observable - Drop willUpdateActiveId, observe value publisher --- Passepartout/App/Views/OrganizerView.swift | 4 +- Passepartout/App/Views/ProfileView+VPN.swift | 2 +- .../Managers/VPNManager+Actions.swift | 2 +- .../Managers/VPNManager.swift | 7 ++-- .../Managers/ProfileManager.swift | 39 +++---------------- 5 files changed, 14 insertions(+), 40 deletions(-) diff --git a/Passepartout/App/Views/OrganizerView.swift b/Passepartout/App/Views/OrganizerView.swift index 133659e4..6f184952 100644 --- a/Passepartout/App/Views/OrganizerView.swift +++ b/Passepartout/App/Views/OrganizerView.swift @@ -339,7 +339,7 @@ extension OrganizerView { extension OrganizerView { private func presentIfActiveProfile(_ id: UUID) { - guard id == profileManager.activeHeader?.id else { + guard id == profileManager.activeProfileId else { return } presentActiveProfile() @@ -355,7 +355,7 @@ extension OrganizerView { guard alertType == nil else { return } - guard let activeProfileId = profileManager.activeHeader?.id else { + guard let activeProfileId = profileManager.activeProfileId else { return } diff --git a/Passepartout/App/Views/ProfileView+VPN.swift b/Passepartout/App/Views/ProfileView+VPN.swift index e2bb3583..7631f206 100644 --- a/Passepartout/App/Views/ProfileView+VPN.swift +++ b/Passepartout/App/Views/ProfileView+VPN.swift @@ -122,7 +122,7 @@ extension ProfileView { profileManager.activateCurrentProfile() // IMPORTANT: save immediately to keep in sync with VPN status - appManager.activeProfileId = profileManager.activeHeader?.id + appManager.activeProfileId = profileManager.activeProfileId } } } diff --git a/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager+Actions.swift b/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager+Actions.swift index 825c991a..8d54364d 100644 --- a/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager+Actions.swift +++ b/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager+Actions.swift @@ -35,7 +35,7 @@ extension VPNManager { pp_log.warning("VPN is already connected") return } - guard let profileId = profileManager.activeHeader?.id else { + guard let profileId = profileManager.activeProfileId else { pp_log.warning("No active profile") return } diff --git a/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift b/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift index 116aff3c..337c9676 100644 --- a/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift +++ b/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift @@ -148,15 +148,16 @@ extension VPNManager { } private func observeProfileManager() { - profileManager.willUpdateActiveId + profileManager.$activeProfileId .removeDuplicates() .sink { newId in Task { await self.willUpdateActiveId(newId) } }.store(in: &cancellables) - - profileManager.willUpdateCurrentProfile + + profileManager.currentProfile.$value + .dropFirst() .removeDuplicates() .sink { newProfile in Task { diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift index bcec14be..44ae97ff 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift @@ -47,23 +47,16 @@ public class ProfileManager: ObservableObject { public var availabilityFilter: ((Profile.Header) -> Bool)? - private var activeProfileId: UUID? { - willSet { - willUpdateActiveId.send(newValue) - } + // MARK: Observables + + @Published public private(set) var activeProfileId: UUID? { didSet { pp_log.debug("Active profile updated: \(activeProfileId?.uuidString ?? "nil")") } } - // MARK: Observables - public let currentProfile: ObservableProfile - public let willUpdateActiveId = PassthroughSubject() - - public let willUpdateCurrentProfile = PassthroughSubject() - public let didCreateProfile = PassthroughSubject() private var cancellables: Set = [] @@ -119,13 +112,7 @@ extension ProfileManager { } public var hasActiveProfile: Bool { - activeHeader != nil - } - - public var activeHeader: Profile.Header? { - availableHeaders.first { - $0.id == activeProfileId - } + activeProfileId != nil } public func isActiveProfile(_ id: UUID) -> Bool { @@ -149,10 +136,10 @@ extension ProfileManager { extension ProfileManager { public var activeProfile: Profile? { - guard let activeHeader = activeHeader else { + guard let id = activeProfileId else { return nil } - return profile(withId: activeHeader.id) + return profile(withId: id) } public func activateProfile(_ profile: Profile) { @@ -317,13 +304,6 @@ extension ProfileManager { .sink { self.willUpdateProfiles($0) }.store(in: &cancellables) - - currentProfile.$value - .dropFirst() - .removeDuplicates() - .sink { - self.willUpdateCurrentProfile($0) - }.store(in: &cancellables) } private func willUpdateProfiles(_ newHeaders: [UUID: Profile.Header]) { @@ -356,13 +336,6 @@ extension ProfileManager { } } - private func willUpdateCurrentProfile(_ newProfile: Profile) { - pp_log.debug("Current profile updated: \(newProfile.logDescription)") - // observe current profile explicitly (no objectWillChange) - - willUpdateCurrentProfile.send(newProfile) - } - private func fixDuplicateNames(in newHeaders: [UUID: Profile.Header]) { var allNames = newHeaders.values.map(\.name) let distinctNames = Set(allNames)