Simplify redundant parts of ProfileManager
- Drop overthought activeHeader - Drop willUpdateCurrentProfile, use observable - Drop willUpdateActiveId, observe value publisher
This commit is contained in:
parent
e0e3e03781
commit
93abaf538b
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue