Skip to content

Commit

Permalink
Follow tunnel status from current profile (#1146)
Browse files Browse the repository at this point in the history
In preparation for #218, when there will be "multiple current profiles",
i.e. the enabled profiles in the OS settings.
  • Loading branch information
keeshux authored Feb 7, 2025
1 parent bb21f5b commit eb32aed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
41 changes: 23 additions & 18 deletions Packages/App/Sources/CommonLibrary/Business/ExtendedTunnel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -195,6 +195,7 @@ private extension ExtendedTunnel {
if tunnel.status == .active {
dataCount = value(forKey: TunnelEnvironmentKeys.dataCount)
}
objectWillChange.send()
}
.store(in: &subscriptions)
}
Expand Down Expand Up @@ -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
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion Packages/PassepartoutKit-Source

0 comments on commit eb32aed

Please sign in to comment.