Skip to content

Commit

Permalink
Hide all connection details when the VPN isn’t connected (#3053)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1199333091098016/1207750283125995/f
Tech Design URL:
CC:

**Description**:

This PR fixes an issue with the VPN showing connection details for the last connection, even after disconnecting.

See https://github.com/duckduckgo/BrowserServicesKit/pull/858/files for where the issue was introduced - rather than modify that code, I'm adding checks on the iOS VPN UI side to ensure we only show the status row when appropriate.
  • Loading branch information
samsymons authored Jul 7, 2024
1 parent 094fdf3 commit 34f9e9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
6 changes: 3 additions & 3 deletions DuckDuckGo/NetworkProtectionStatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct NetworkProtectionStatusView: View {
toggle()
locationDetails()

if statusModel.shouldShowConnectionDetails {
if statusModel.isNetPEnabled && statusModel.shouldShowConnectionDetails && statusModel.ipAddress != nil {
connectionDetails()
}

Expand Down Expand Up @@ -178,8 +178,8 @@ struct NetworkProtectionStatusView: View {

NetworkProtectionThroughputItemView(
title: UserText.vpnDataVolume,
downloadSpeed: statusModel.downloadTotal,
uploadSpeed: statusModel.uploadTotal
downloadSpeed: statusModel.downloadTotal ?? NetworkProtectionStatusViewModel.Constants.defaultDownloadVolume,
uploadSpeed: statusModel.uploadTotal ?? NetworkProtectionStatusViewModel.Constants.defaultUploadVolume
)
} header: {
Text(UserText.netPStatusViewConnectionDetails).foregroundColor(.init(designSystemColor: .textSecondary))
Expand Down
36 changes: 23 additions & 13 deletions DuckDuckGo/NetworkProtectionStatusViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct NetworkProtectionLocationStatusModel {
// swiftlint:disable:next type_body_length
final class NetworkProtectionStatusViewModel: ObservableObject {

private enum Constants {
enum Constants {
static let defaultDownloadVolume = "0 KB"
static let defaultUploadVolume = "0 KB"
}
Expand Down Expand Up @@ -133,8 +133,8 @@ final class NetworkProtectionStatusViewModel: ObservableObject {
@Published public var ipAddress: String?
@Published public var dnsSettings: NetworkProtectionDNSSettings

@Published public var uploadTotal: String = Constants.defaultUploadVolume
@Published public var downloadTotal: String = Constants.defaultDownloadVolume
@Published public var uploadTotal: String?
@Published public var downloadTotal: String?
private var throughputUpdateTimer: Timer?

var shouldShowFAQ: Bool {
Expand Down Expand Up @@ -194,8 +194,8 @@ final class NetworkProtectionStatusViewModel: ObservableObject {
isConnectedPublisher
.sink { [weak self] isConnected in
if !isConnected {
self?.uploadTotal = Constants.defaultUploadVolume
self?.downloadTotal = Constants.defaultDownloadVolume
self?.uploadTotal = nil
self?.downloadTotal = nil
self?.throughputUpdateTimer?.invalidate()
self?.throughputUpdateTimer = nil
} else {
Expand All @@ -207,17 +207,19 @@ final class NetworkProtectionStatusViewModel: ObservableObject {

private func setUpToggledStatePublisher() {
statusObserver.publisher
.map {
switch $0 {
case .connected, .connecting:
return true
.receive(on: DispatchQueue.main)
.sink { [weak self] status in
switch status {
case .connected:
self?.isNetPEnabled = true
case .connecting:
self?.isNetPEnabled = true
self?.resetConnectionInformation()
default:
return false
self?.isNetPEnabled = false
self?.resetConnectionInformation()
}
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
.assign(to: \.isNetPEnabled, onWeaklyHeld: self)
.store(in: &cancellables)
}

Expand Down Expand Up @@ -433,6 +435,14 @@ final class NetworkProtectionStatusViewModel: ObservableObject {
let timeLapsed = Self.dateFormatter.string(from: timeLapsedInterval) ?? "00:00:00"
return UserText.netPStatusConnected(since: timeLapsed)
}

private func resetConnectionInformation() {
self.location = nil
self.ipAddress = nil
self.uploadTotal = nil
self.downloadTotal = nil
}

}

private extension ConnectionStatus {
Expand Down

0 comments on commit 34f9e9a

Please sign in to comment.