Simplify redundant parts of ProfileManager

- Drop overthought activeHeader

- Drop willUpdateCurrentProfile, use observable

- Drop willUpdateActiveId, observe value publisher
This commit is contained in:
Davide De Rosa 2022-05-03 10:25:10 +02:00
parent e0e3e03781
commit 93abaf538b
5 changed files with 14 additions and 40 deletions

View File

@ -339,7 +339,7 @@ extension OrganizerView {
extension OrganizerView { extension OrganizerView {
private func presentIfActiveProfile(_ id: UUID) { private func presentIfActiveProfile(_ id: UUID) {
guard id == profileManager.activeHeader?.id else { guard id == profileManager.activeProfileId else {
return return
} }
presentActiveProfile() presentActiveProfile()
@ -355,7 +355,7 @@ extension OrganizerView {
guard alertType == nil else { guard alertType == nil else {
return return
} }
guard let activeProfileId = profileManager.activeHeader?.id else { guard let activeProfileId = profileManager.activeProfileId else {
return return
} }

View File

@ -122,7 +122,7 @@ extension ProfileView {
profileManager.activateCurrentProfile() profileManager.activateCurrentProfile()
// IMPORTANT: save immediately to keep in sync with VPN status // IMPORTANT: save immediately to keep in sync with VPN status
appManager.activeProfileId = profileManager.activeHeader?.id appManager.activeProfileId = profileManager.activeProfileId
} }
} }
} }

View File

@ -35,7 +35,7 @@ extension VPNManager {
pp_log.warning("VPN is already connected") pp_log.warning("VPN is already connected")
return return
} }
guard let profileId = profileManager.activeHeader?.id else { guard let profileId = profileManager.activeProfileId else {
pp_log.warning("No active profile") pp_log.warning("No active profile")
return return
} }

View File

@ -148,7 +148,7 @@ extension VPNManager {
} }
private func observeProfileManager() { private func observeProfileManager() {
profileManager.willUpdateActiveId profileManager.$activeProfileId
.removeDuplicates() .removeDuplicates()
.sink { newId in .sink { newId in
Task { Task {
@ -156,7 +156,8 @@ extension VPNManager {
} }
}.store(in: &cancellables) }.store(in: &cancellables)
profileManager.willUpdateCurrentProfile profileManager.currentProfile.$value
.dropFirst()
.removeDuplicates() .removeDuplicates()
.sink { newProfile in .sink { newProfile in
Task { Task {

View File

@ -47,23 +47,16 @@ public class ProfileManager: ObservableObject {
public var availabilityFilter: ((Profile.Header) -> Bool)? public var availabilityFilter: ((Profile.Header) -> Bool)?
private var activeProfileId: UUID? { // MARK: Observables
willSet {
willUpdateActiveId.send(newValue) @Published public private(set) var activeProfileId: UUID? {
}
didSet { didSet {
pp_log.debug("Active profile updated: \(activeProfileId?.uuidString ?? "nil")") pp_log.debug("Active profile updated: \(activeProfileId?.uuidString ?? "nil")")
} }
} }
// MARK: Observables
public let currentProfile: ObservableProfile public let currentProfile: ObservableProfile
public let willUpdateActiveId = PassthroughSubject<UUID?, Never>()
public let willUpdateCurrentProfile = PassthroughSubject<Profile, Never>()
public let didCreateProfile = PassthroughSubject<Profile, Never>() public let didCreateProfile = PassthroughSubject<Profile, Never>()
private var cancellables: Set<AnyCancellable> = [] private var cancellables: Set<AnyCancellable> = []
@ -119,13 +112,7 @@ extension ProfileManager {
} }
public var hasActiveProfile: Bool { public var hasActiveProfile: Bool {
activeHeader != nil activeProfileId != nil
}
public var activeHeader: Profile.Header? {
availableHeaders.first {
$0.id == activeProfileId
}
} }
public func isActiveProfile(_ id: UUID) -> Bool { public func isActiveProfile(_ id: UUID) -> Bool {
@ -149,10 +136,10 @@ extension ProfileManager {
extension ProfileManager { extension ProfileManager {
public var activeProfile: Profile? { public var activeProfile: Profile? {
guard let activeHeader = activeHeader else { guard let id = activeProfileId else {
return nil return nil
} }
return profile(withId: activeHeader.id) return profile(withId: id)
} }
public func activateProfile(_ profile: Profile) { public func activateProfile(_ profile: Profile) {
@ -317,13 +304,6 @@ extension ProfileManager {
.sink { .sink {
self.willUpdateProfiles($0) self.willUpdateProfiles($0)
}.store(in: &cancellables) }.store(in: &cancellables)
currentProfile.$value
.dropFirst()
.removeDuplicates()
.sink {
self.willUpdateCurrentProfile($0)
}.store(in: &cancellables)
} }
private func willUpdateProfiles(_ newHeaders: [UUID: Profile.Header]) { 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]) { private func fixDuplicateNames(in newHeaders: [UUID: Profile.Header]) {
var allNames = newHeaders.values.map(\.name) var allNames = newHeaders.values.map(\.name)
let distinctNames = Set(allNames) let distinctNames = Set(allNames)