From 403fc9db779379e64fd597bf67b898960dbb6a0d Mon Sep 17 00:00:00 2001 From: Anh Do Date: Thu, 7 Mar 2024 16:46:26 -0500 Subject: [PATCH] Move vpnFirstEnabled and networkPathChange out of VPNSettings --- .../PacketTunnelProvider.swift | 2 - .../UserDefaults+networkPathChange.swift | 7 ++- .../UserDefaults+vpnFirstEnabled.swift | 6 +- .../Settings/VPNSettings.swift | 56 ------------------- 4 files changed, 5 insertions(+), 66 deletions(-) diff --git a/Sources/NetworkProtection/PacketTunnelProvider.swift b/Sources/NetworkProtection/PacketTunnelProvider.swift index 4ff900ba0..5ba3b58c1 100644 --- a/Sources/NetworkProtection/PacketTunnelProvider.swift +++ b/Sources/NetworkProtection/PacketTunnelProvider.swift @@ -911,8 +911,6 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { .setRegistrationKeyValidity, .setSelectedEnvironment, .setShowInMenuBar, - .setVPNFirstEnabled, - .setNetworkPathChange, .setDisableRekeying: // Intentional no-op, as some setting changes don't require any further operation completionHandler?(nil) diff --git a/Sources/NetworkProtection/Settings/Extensions/UserDefaults+networkPathChange.swift b/Sources/NetworkProtection/Settings/Extensions/UserDefaults+networkPathChange.swift index 0f0c64560..5715adbcd 100644 --- a/Sources/NetworkProtection/Settings/Extensions/UserDefaults+networkPathChange.swift +++ b/Sources/NetworkProtection/Settings/Extensions/UserDefaults+networkPathChange.swift @@ -41,7 +41,7 @@ extension UserDefaults { } @objc - dynamic var networkPathChange: NetworkPathChange? { + private(set) dynamic var networkPathChange: NetworkPathChange? { get { guard let data = data(forKey: networkPathChangeKey) else { return nil } return try? JSONDecoder().decode(NetworkPathChange.self, from: data) @@ -53,7 +53,8 @@ extension UserDefaults { } } - var networkPathChangePublisher: AnyPublisher { - publisher(for: \.networkPathChange).eraseToAnyPublisher() + public func updateNetworkPath(with newPath: String?) { + networkPathChange = NetworkPathChange(oldPath: networkPathChange?.newPath ?? "unknown", + newPath: newPath ?? "unknown") } } diff --git a/Sources/NetworkProtection/Settings/Extensions/UserDefaults+vpnFirstEnabled.swift b/Sources/NetworkProtection/Settings/Extensions/UserDefaults+vpnFirstEnabled.swift index 0dbd4f4c1..f944336e2 100644 --- a/Sources/NetworkProtection/Settings/Extensions/UserDefaults+vpnFirstEnabled.swift +++ b/Sources/NetworkProtection/Settings/Extensions/UserDefaults+vpnFirstEnabled.swift @@ -25,7 +25,7 @@ extension UserDefaults { } @objc - dynamic var vpnFirstEnabled: Date? { + public dynamic var vpnFirstEnabled: Date? { get { value(forKey: vpnFirstEnabledKey) as? Date } @@ -34,8 +34,4 @@ extension UserDefaults { set(newValue, forKey: vpnFirstEnabledKey) } } - - var vpnFirstEnabledPublisher: AnyPublisher { - publisher(for: \.vpnFirstEnabled).eraseToAnyPublisher() - } } diff --git a/Sources/NetworkProtection/Settings/VPNSettings.swift b/Sources/NetworkProtection/Settings/VPNSettings.swift index 34e89ad77..f9dd01be0 100644 --- a/Sources/NetworkProtection/Settings/VPNSettings.swift +++ b/Sources/NetworkProtection/Settings/VPNSettings.swift @@ -39,8 +39,6 @@ public final class VPNSettings { case setSelectedLocation(_ selectedLocation: SelectedLocation) case setSelectedEnvironment(_ selectedEnvironment: SelectedEnvironment) case setShowInMenuBar(_ showInMenuBar: Bool) - case setVPNFirstEnabled(_ vpnFirstEnabled: Date?) - case setNetworkPathChange(_ newPath: String?) case setDisableRekeying(_ disableRekeying: Bool) } @@ -163,20 +161,6 @@ public final class VPNSettings { Change.setShowInMenuBar(showInMenuBar) }.eraseToAnyPublisher() - let vpnFirstEnabledPublisher = vpnFirstEnabledPublisher - .dropFirst() - .removeDuplicates() - .map { vpnFirstEnabled in - Change.setVPNFirstEnabled(vpnFirstEnabled) - }.eraseToAnyPublisher() - - let networkPathChangePublisher = networkPathChangePublisher - .dropFirst() - .removeDuplicates() - .map { networkPathChange in - Change.setNetworkPathChange(networkPathChange?.newPath) - }.eraseToAnyPublisher() - let disableRekeyingPublisher = disableRekeyingPublisher .dropFirst() .removeDuplicates() @@ -194,8 +178,6 @@ public final class VPNSettings { locationChangePublisher, environmentChangePublisher, showInMenuBarPublisher, - vpnFirstEnabledPublisher, - networkPathChangePublisher, disableRekeyingPublisher).eraseToAnyPublisher() }() @@ -242,12 +224,6 @@ public final class VPNSettings { self.selectedEnvironment = selectedEnvironment case .setShowInMenuBar(let showInMenuBar): self.showInMenuBar = showInMenuBar - case .setVPNFirstEnabled(let vpnFirstEnabled): - self.vpnFirstEnabled = vpnFirstEnabled - case .setNetworkPathChange(let newPath): - self.networkPathChange = UserDefaults.NetworkPathChange( - oldPath: networkPathChange?.newPath ?? "unknown", - newPath: newPath ?? "unknown") case .setDisableRekeying(let disableRekeying): self.disableRekeying = disableRekeying } @@ -442,38 +418,6 @@ public final class VPNSettings { } } - // MARK: - First time VPN is enabled - - public var vpnFirstEnabledPublisher: AnyPublisher { - defaults.vpnFirstEnabledPublisher - } - - public var vpnFirstEnabled: Date? { - get { - defaults.vpnFirstEnabled - } - - set { - defaults.vpnFirstEnabled = newValue - } - } - - // MARK: - Network path change info - - public var networkPathChangePublisher: AnyPublisher { - defaults.networkPathChangePublisher - } - - public var networkPathChange: UserDefaults.NetworkPathChange? { - get { - defaults.networkPathChange - } - - set { - defaults.networkPathChange = newValue - } - } - // MARK: - Disable Rekeying public var disableRekeyingPublisher: AnyPublisher {