Skip to content

Commit

Permalink
Stop the timer when disabling the VPN.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsymons committed Mar 12, 2024
1 parent 534a64f commit 515be22
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
26 changes: 17 additions & 9 deletions DuckDuckGo/NetworkProtectionStatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,22 @@ struct NetworkProtectionStatusView: View {
private func locationDetails() -> some View {
Section {
if let location = statusModel.location {
var locationAttributedString: AttributedString {
var attributedString = AttributedString(statusModel.preferredLocation.isNearest ? "\(location) (Nearest)" : location)
attributedString.foregroundColor = .init(designSystemColor: .textPrimary)
if let range = attributedString.range(of: "(Nearest)") {
attributedString[range].foregroundColor = Color(.init(designSystemColor: .textSecondary))
}
return attributedString
}

NavigationLink(destination: NetworkProtectionVPNLocationView()) {
let title = statusModel.preferredLocation.isNearest ? "\(location) (Nearest)" : location
NetworkProtectionLocationItemView(title: title)
NetworkProtectionLocationItemView(title: locationAttributedString)
}
} else {
NavigationLink(destination: NetworkProtectionVPNLocationView()) {
NetworkProtectionLocationItemView(
title: statusModel.preferredLocation.title
title: AttributedString(statusModel.preferredLocation.title)
)
}
}
Expand Down Expand Up @@ -257,14 +265,14 @@ private struct NetworkProtectionErrorView: View {
}
}

@available(iOS 15.0, *)
private struct NetworkProtectionLocationItemView: View {
let title: String
let title: AttributedString

var body: some View {
HStack(spacing: 16) {
Text(title)
.daxBodyRegular()
.foregroundColor(.init(designSystemColor: .textPrimary))
}
.listRowBackground(Color(designSystemColor: .surface))
}
Expand Down Expand Up @@ -301,16 +309,16 @@ private struct NetworkProtectionThroughputItemView: View {

Spacer(minLength: 2)

Image("VPNUpload")
Image("VPNDownload")
.foregroundColor(.init(designSystemColor: .textSecondary))
Text(uploadSpeed)
Text(downloadSpeed)
.daxBodyRegular()
.foregroundColor(.init(designSystemColor: .textSecondary))

Image("VPNDownload")
Image("VPNUpload")
.foregroundColor(.init(designSystemColor: .textSecondary))
.padding(.leading, 4)
Text(downloadSpeed)
Text(uploadSpeed)
.daxBodyRegular()
.foregroundColor(.init(designSystemColor: .textSecondary))
}
Expand Down
17 changes: 17 additions & 0 deletions DuckDuckGo/NetworkProtectionStatusViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ final class NetworkProtectionStatusViewModel: ObservableObject {
.map(Self.statusImageID(connected:))
.assign(to: \.statusImageID, onWeaklyHeld: self)
.store(in: &cancellables)
isConnectedPublisher
.sink { [weak self] isConnected in
if !isConnected {
self?.uploadTotal = Constants.defaultUploadVolume
self?.downloadTotal = Constants.defaultDownloadVolume
self?.throughputUpdateTimer?.invalidate()
self?.throughputUpdateTimer = nil
} else {
self?.setUpThroughputRefreshTimer()
}
}
.store(in: &cancellables)
}

private func setUpToggledStatePublisher() {
Expand Down Expand Up @@ -273,6 +285,11 @@ final class NetworkProtectionStatusViewModel: ObservableObject {
}

private func setUpThroughputRefreshTimer() {
if let throughputUpdateTimer, throughputUpdateTimer.isValid {
// Prevent the timer from being set up multiple times
return
}

Task {
// Refresh as soon as the timer is set up, rather than waiting for 1 second:
await self.refreshDataVolumeTotals()
Expand Down

0 comments on commit 515be22

Please sign in to comment.