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 {
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
}

View File

@ -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
}
}
}

View File

@ -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
}

View File

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

View File

@ -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<UUID?, Never>()
public let willUpdateCurrentProfile = PassthroughSubject<Profile, Never>()
public let didCreateProfile = PassthroughSubject<Profile, Never>()
private var cancellables: Set<AnyCancellable> = []
@ -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)