From 34f9e9a5f07856b0ba8e0dcf43a912ad0e0069e2 Mon Sep 17 00:00:00 2001 From: Sam Symons Date: Mon, 8 Jul 2024 09:58:24 +1200 Subject: [PATCH] =?UTF-8?q?Hide=20all=20connection=20details=20when=20the?= =?UTF-8?q?=20VPN=20isn=E2=80=99t=20connected=20(#3053)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- DuckDuckGo/NetworkProtectionStatusView.swift | 6 ++-- .../NetworkProtectionStatusViewModel.swift | 36 ++++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/DuckDuckGo/NetworkProtectionStatusView.swift b/DuckDuckGo/NetworkProtectionStatusView.swift index 2dc751f821..67abeb033a 100644 --- a/DuckDuckGo/NetworkProtectionStatusView.swift +++ b/DuckDuckGo/NetworkProtectionStatusView.swift @@ -40,7 +40,7 @@ struct NetworkProtectionStatusView: View { toggle() locationDetails() - if statusModel.shouldShowConnectionDetails { + if statusModel.isNetPEnabled && statusModel.shouldShowConnectionDetails && statusModel.ipAddress != nil { connectionDetails() } @@ -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)) diff --git a/DuckDuckGo/NetworkProtectionStatusViewModel.swift b/DuckDuckGo/NetworkProtectionStatusViewModel.swift index e733d4639a..88ee269f64 100644 --- a/DuckDuckGo/NetworkProtectionStatusViewModel.swift +++ b/DuckDuckGo/NetworkProtectionStatusViewModel.swift @@ -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" } @@ -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 { @@ -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 { @@ -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) } @@ -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 {