Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow tunnel status from current profile #1146

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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