Follow tunnel status from current profile (#1146)

In preparation for #218, when there will be "multiple current profiles",
i.e. the enabled profiles in the OS settings.
This commit is contained in:
Davide 2025-02-07 16:42:00 +01:00 committed by GitHub
parent bb21f5be05
commit eb32aed2a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 23 deletions

View File

@ -45,14 +45,12 @@ public final class ExtendedTunnel: ObservableObject {
environment.environmentValue(forKey: key)
}
@Published
public private(set) var lastErrorCode: PassepartoutError.Code? {
didSet {
pp_log(.app, .info, "ExtendedTunnel.lastErrorCode -> \(lastErrorCode?.rawValue ?? "nil")")
}
}
@Published
public private(set) var dataCount: DataCount?
private var subscriptions: Set<AnyCancellable>
@ -92,6 +90,10 @@ extension ExtendedTunnel {
tunnel.currentProfile ?? lastUsedProfile
}
public var currentProfiles: [Profile.ID: TunnelCurrentProfile] {
tunnel.currentProfiles
}
public var currentProfilePublisher: AnyPublisher<TunnelCurrentProfile?, Never> {
tunnel
.$currentProfile
@ -151,33 +153,31 @@ extension ExtendedTunnel {
private extension ExtendedTunnel {
func observeObjects() {
tunnel
.$status
.$currentProfile
.receive(on: DispatchQueue.main)
.sink { [weak self] in
guard let self else {
return
}
switch $0 {
// update last used profile
if let id = $0?.id {
defaults?.set(id.uuidString, forKey: AppPreference.lastUsedProfileId.key)
}
// follow status updates
switch $0?.status ?? .inactive {
case .active:
break
case .activating:
lastErrorCode = nil
dataCount = nil
default:
lastErrorCode = value(forKey: TunnelEnvironmentKeys.lastErrorCode)
}
if $0 != .active {
dataCount = nil
}
}
.store(in: &subscriptions)
tunnel
.$currentProfile
.receive(on: DispatchQueue.main)
.sink { [weak self] in
if let id = $0?.id {
self?.defaults?.set(id.uuidString, forKey: AppPreference.lastUsedProfileId.key)
}
self?.objectWillChange.send()
objectWillChange.send()
}
.store(in: &subscriptions)
@ -195,6 +195,7 @@ private extension ExtendedTunnel {
if tunnel.status == .active {
dataCount = value(forKey: TunnelEnvironmentKeys.dataCount)
}
objectWillChange.send()
}
.store(in: &subscriptions)
}
@ -226,7 +227,11 @@ private extension ExtendedTunnel {
let uuid = UUID(uuidString: uuidString) else {
return nil
}
return TunnelCurrentProfile(id: uuid, onDemand: false)
return TunnelCurrentProfile(
id: uuid,
status: .inactive,
onDemand: false
)
}
}

View File

@ -48,9 +48,9 @@ extension ExtendedTunnelTests {
let exp = expectation(description: "Last error code")
var didCall = false
sut
.$lastErrorCode
.objectWillChange
.sink {
if !didCall, $0 != nil {
if !didCall, sut.lastErrorCode != nil {
didCall = true
exp.fulfill()
}
@ -78,9 +78,9 @@ extension ExtendedTunnelTests {
let exp = expectation(description: "Data count")
var didCall = false
sut
.$dataCount
.objectWillChange
.sink {
if !didCall, $0 != nil {
if !didCall, sut.dataCount != nil {
didCall = true
exp.fulfill()
}

@ -1 +1 @@
Subproject commit e501530c9d396bf800fc64c0f1e29dfba9493786
Subproject commit 65582f668f4dccda310820c209865e8818179f24