From ac1cd2cd66f47be5517e2ba2e5b5f4ddb4758cf9 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Tue, 28 Nov 2023 03:18:24 +0100 Subject: [PATCH] Update BSK for VPN settings (#2165) Task/Issue URL: https://app.asana.com/0/0/1205958731729756/f BSK PR: duckduckgo/BrowserServicesKit#565 macOS PR: duckduckgo/macos-browser#1858 Description Updates BSK with the latest changes. --- DuckDuckGo.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 4 ++-- ...orkProtectionConvenienceInitialisers.swift | 17 ++++++------- ...etworkProtectionVPNLocationViewModel.swift | 14 +++++------ ...kProtectionVPNNotificationsViewModel.swift | 10 ++++---- ...etworkProtectionVPNSettingsViewModel.swift | 8 +++---- ...kProtectionVPNLocationViewModelTests.swift | 24 +++++++++---------- ...etworkProtectionPacketTunnelProvider.swift | 6 ++--- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index b1832a4068..bd0be4db88 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -9199,7 +9199,7 @@ repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit"; requirement = { kind = exactVersion; - version = 85.1.1; + version = 86.0.0; }; }; C14882EB27F211A000D59F0C /* XCRemoteSwiftPackageReference "SwiftSoup" */ = { diff --git a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 1eb97f222f..548739b3ef 100644 --- a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -15,8 +15,8 @@ "repositoryURL": "https://github.com/DuckDuckGo/BrowserServicesKit", "state": { "branch": null, - "revision": "543e1d7ed9b5743d4e6b0ebe18a5fbf8f1441f02", - "version": "85.1.1" + "revision": "1331652ad0dc21c23b495b4a9a42e2a0eb44859d", + "version": "86.0.0" } }, { diff --git a/DuckDuckGo/NetworkProtectionConvenienceInitialisers.swift b/DuckDuckGo/NetworkProtectionConvenienceInitialisers.swift index 2557f2f1d3..193e9c959b 100644 --- a/DuckDuckGo/NetworkProtectionConvenienceInitialisers.swift +++ b/DuckDuckGo/NetworkProtectionConvenienceInitialisers.swift @@ -54,9 +54,9 @@ extension NetworkProtectionKeychainTokenStore { extension NetworkProtectionCodeRedemptionCoordinator { convenience init() { - let tunnelSettings = TunnelSettings(defaults: .networkProtectionGroupDefaults) + let settings = VPNSettings(defaults: .networkProtectionGroupDefaults) self.init( - environment: tunnelSettings.selectedEnvironment, + environment: settings.selectedEnvironment, tokenStore: NetworkProtectionKeychainTokenStore(), errorEvents: .networkProtectionAppDebugEvents ) @@ -65,27 +65,24 @@ extension NetworkProtectionCodeRedemptionCoordinator { extension NetworkProtectionVPNNotificationsViewModel { convenience init() { - let notificationsSettingsStore = NetworkProtectionNotificationsSettingsUserDefaultsStore(userDefaults: .networkProtectionGroupDefaults) self.init( notificationsAuthorization: NotificationsAuthorizationController(), - notificationsSettingsStore: notificationsSettingsStore + settings: VPNSettings(defaults: .networkProtectionGroupDefaults) ) } } extension NetworkProtectionVPNSettingsViewModel { convenience init() { - self.init( - tunnelSettings: TunnelSettings(defaults: .networkProtectionGroupDefaults) - ) + self.init(settings: VPNSettings(defaults: .networkProtectionGroupDefaults)) } } extension NetworkProtectionLocationListCompositeRepository { convenience init() { - let tunnelSettings = TunnelSettings(defaults: .networkProtectionGroupDefaults) + let settings = VPNSettings(defaults: .networkProtectionGroupDefaults) self.init( - environment: tunnelSettings.selectedEnvironment, + environment: settings.selectedEnvironment, tokenStore: NetworkProtectionKeychainTokenStore() ) } @@ -96,7 +93,7 @@ extension NetworkProtectionVPNLocationViewModel { let locationListRepository = NetworkProtectionLocationListCompositeRepository() self.init( locationListRepository: locationListRepository, - tunnelSettings: TunnelSettings(defaults: .networkProtectionGroupDefaults) + settings: VPNSettings(defaults: .networkProtectionGroupDefaults) ) } } diff --git a/DuckDuckGo/NetworkProtectionVPNLocationViewModel.swift b/DuckDuckGo/NetworkProtectionVPNLocationViewModel.swift index c67feabce2..38dfdd1b66 100644 --- a/DuckDuckGo/NetworkProtectionVPNLocationViewModel.swift +++ b/DuckDuckGo/NetworkProtectionVPNLocationViewModel.swift @@ -25,7 +25,7 @@ import NetworkProtection final class NetworkProtectionVPNLocationViewModel: ObservableObject { private let locationListRepository: NetworkProtectionLocationListRepository - private let tunnelSettings: TunnelSettings + private let settings: VPNSettings @Published public var state: LoadingState @Published public var isNearestSelected: Bool @@ -43,11 +43,11 @@ final class NetworkProtectionVPNLocationViewModel: ObservableObject { } } - init(locationListRepository: NetworkProtectionLocationListRepository, tunnelSettings: TunnelSettings) { + init(locationListRepository: NetworkProtectionLocationListRepository, settings: VPNSettings) { self.locationListRepository = locationListRepository - self.tunnelSettings = tunnelSettings + self.settings = settings state = .loading - self.isNearestSelected = tunnelSettings.selectedLocation == .nearest + self.isNearestSelected = settings.selectedLocation == .nearest } func onViewAppeared() async { @@ -55,20 +55,20 @@ final class NetworkProtectionVPNLocationViewModel: ObservableObject { } func onNearestItemSelection() async { - tunnelSettings.selectedLocation = .nearest + settings.selectedLocation = .nearest await reloadList() } func onCountryItemSelection(id: String, cityId: String? = nil) async { let location = NetworkProtectionSelectedLocation(country: id, city: cityId) - tunnelSettings.selectedLocation = .location(location) + settings.selectedLocation = .location(location) await reloadList() } @MainActor private func reloadList() async { guard let list = try? await locationListRepository.fetchLocationList() else { return } - let selectedLocation = self.tunnelSettings.selectedLocation + let selectedLocation = self.settings.selectedLocation let isNearestSelected = selectedLocation == .nearest let countryItems = list.map { currentLocation in diff --git a/DuckDuckGo/NetworkProtectionVPNNotificationsViewModel.swift b/DuckDuckGo/NetworkProtectionVPNNotificationsViewModel.swift index 62833eb25a..096f2194c9 100644 --- a/DuckDuckGo/NetworkProtectionVPNNotificationsViewModel.swift +++ b/DuckDuckGo/NetworkProtectionVPNNotificationsViewModel.swift @@ -31,16 +31,16 @@ enum NetworkProtectionNotificationsViewKind: Equatable { final class NetworkProtectionVPNNotificationsViewModel: ObservableObject { private var notificationsAuthorization: NotificationsAuthorizationControlling - private var notificationsSettingsStore: NetworkProtectionNotificationsSettingsStore + private var settings: VPNSettings @Published var viewKind: NetworkProtectionNotificationsViewKind = .loading var alertsEnabled: Bool { - self.notificationsSettingsStore.alertsEnabled + self.settings.notifyStatusChanges } init(notificationsAuthorization: NotificationsAuthorizationControlling, - notificationsSettingsStore: NetworkProtectionNotificationsSettingsStore) { + settings: VPNSettings) { self.notificationsAuthorization = notificationsAuthorization - self.notificationsSettingsStore = notificationsSettingsStore + self.settings = settings self.notificationsAuthorization.delegate = self } @@ -55,7 +55,7 @@ final class NetworkProtectionVPNNotificationsViewModel: ObservableObject { } func didToggleAlerts(to enabled: Bool) { - notificationsSettingsStore.alertsEnabled = enabled + settings.notifyStatusChanges = enabled } private func updateViewKind(for authorizationStatus: UNAuthorizationStatus) { diff --git a/DuckDuckGo/NetworkProtectionVPNSettingsViewModel.swift b/DuckDuckGo/NetworkProtectionVPNSettingsViewModel.swift index 19664143e8..14820ce112 100644 --- a/DuckDuckGo/NetworkProtectionVPNSettingsViewModel.swift +++ b/DuckDuckGo/NetworkProtectionVPNSettingsViewModel.swift @@ -24,14 +24,14 @@ import NetworkProtection import Combine final class NetworkProtectionVPNSettingsViewModel: ObservableObject { - private let tunnelSettings: TunnelSettings + private let settings: VPNSettings private var cancellable: AnyCancellable? @Published public var preferredLocation: String = UserText.netPPreferredLocationNearest - init(tunnelSettings: TunnelSettings) { - self.tunnelSettings = tunnelSettings - cancellable = tunnelSettings.selectedLocationPublisher.map { selectedLocation in + init(settings: VPNSettings) { + self.settings = settings + cancellable = settings.selectedLocationPublisher.map { selectedLocation in guard let selectedLocation = selectedLocation.location else { return UserText.netPPreferredLocationNearest } diff --git a/DuckDuckGoTests/NetworkProtectionVPNLocationViewModelTests.swift b/DuckDuckGoTests/NetworkProtectionVPNLocationViewModelTests.swift index 8bfdafd4db..73e6bf09be 100644 --- a/DuckDuckGoTests/NetworkProtectionVPNLocationViewModelTests.swift +++ b/DuckDuckGoTests/NetworkProtectionVPNLocationViewModelTests.swift @@ -28,7 +28,7 @@ import NetworkProtectionTestUtils final class NetworkProtectionVPNLocationViewModelTests: XCTestCase { private var listRepository: MockNetworkProtectionLocationListRepository! - private var tunnelSettings: TunnelSettings! + private var settings: VPNSettings! private var viewModel: NetworkProtectionVPNLocationViewModel! @MainActor @@ -36,13 +36,13 @@ final class NetworkProtectionVPNLocationViewModelTests: XCTestCase { super.setUp() listRepository = MockNetworkProtectionLocationListRepository() let testDefaults = UserDefaults(suiteName: #file + Thread.current.debugDescription)! - tunnelSettings = TunnelSettings(defaults: testDefaults) - viewModel = NetworkProtectionVPNLocationViewModel(locationListRepository: listRepository, tunnelSettings: tunnelSettings) + settings = VPNSettings(defaults: testDefaults) + viewModel = NetworkProtectionVPNLocationViewModel(locationListRepository: listRepository, settings: settings) } override func tearDown() { - tunnelSettings.selectedLocation = .nearest - tunnelSettings = nil + settings.selectedLocation = .nearest + settings = nil listRepository = nil viewModel = nil super.tearDown() @@ -70,28 +70,28 @@ final class NetworkProtectionVPNLocationViewModelTests: XCTestCase { func test_onViewAppeared_selectedCountryFromSettings_isSelectedSetToTrue() async throws { try await assertOnListLoad_countryIsSelected { [weak self] testCaseCountryId in - self?.tunnelSettings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: testCaseCountryId)) + self?.settings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: testCaseCountryId)) await self?.viewModel.onViewAppeared() } } func test_onViewAppeared_NOTSelectedCountryFromSettings_isSelectedSetToFalse() async throws { try await assertOnListLoad_isSelectedSetToFalse { [weak self] in - self?.tunnelSettings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: "US")) + self?.settings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: "US")) await self?.viewModel.onViewAppeared() } } func test_onViewAppeared_nearestSelectedInSettings_isNearestSelectedSetToTrue() async throws { try await assertNearestSelectedSetToTrue { [weak self] in - self?.tunnelSettings.selectedLocation = .nearest + self?.settings.selectedLocation = .nearest await self?.viewModel.onViewAppeared() } } func test_onViewAppeared_nearestNOTSelectedInSettings_isNearestSelectedSetToFalse() async throws { try await assertNearestSelectedSetToFalse { [weak self] testCaseCountryId in - self?.tunnelSettings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: testCaseCountryId)) + self?.settings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: testCaseCountryId)) await self?.viewModel.onViewAppeared() } } @@ -128,7 +128,7 @@ final class NetworkProtectionVPNLocationViewModelTests: XCTestCase { func test_onViewAppeared_cityIsSelected_itemIsSelected() async throws { try await assertOnListLoad_itemIsSelected { [weak self] testCase in - self?.tunnelSettings.selectedLocation = .location(testCase) + self?.settings.selectedLocation = .location(testCase) await self?.viewModel.onViewAppeared() } } @@ -140,7 +140,7 @@ final class NetworkProtectionVPNLocationViewModelTests: XCTestCase { ] listRepository.stubLocationList = countries - tunnelSettings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: "US", city: "New York")) + settings.selectedLocation = .location(NetworkProtectionSelectedLocation(country: "US", city: "New York")) await viewModel.onViewAppeared() let selectedItems = try loadedItems().flatMap { $0.cityPickerItems }.filter(\.isSelected) XCTAssertEqual(selectedItems.count, 0) @@ -148,7 +148,7 @@ final class NetworkProtectionVPNLocationViewModelTests: XCTestCase { func test_onViewAppeared_countryWithoutCityIsSelected_nearestItemIsSelected() async throws { try await assertOnListLoad_nearestItemIsSelected { [weak self] testCase in - self?.tunnelSettings.selectedLocation = .location(testCase) + self?.settings.selectedLocation = .location(testCase) await self?.viewModel.onViewAppeared() } } diff --git a/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift b/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift index 4943d79eb3..0f1fe33c1b 100644 --- a/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift +++ b/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift @@ -176,9 +176,9 @@ final class NetworkProtectionPacketTunnelProvider: PacketTunnelProvider { errorEvents: nil) let errorStore = NetworkProtectionTunnelErrorStore() let notificationsPresenter = NetworkProtectionUNNotificationPresenter() - let notificationsSettingsStore = NetworkProtectionNotificationsSettingsUserDefaultsStore(userDefaults: .networkProtectionGroupDefaults) + let settings = VPNSettings(defaults: .networkProtectionGroupDefaults) let nofificationsPresenterDecorator = NetworkProtectionNotificationsPresenterTogglableDecorator( - notificationSettingsStore: notificationsSettingsStore, + settings: settings, wrappee: notificationsPresenter ) notificationsPresenter.requestAuthorization() @@ -189,7 +189,7 @@ final class NetworkProtectionPacketTunnelProvider: PacketTunnelProvider { tokenStore: tokenStore, debugEvents: Self.networkProtectionDebugEvents(controllerErrorStore: errorStore), providerEvents: Self.packetTunnelProviderEvents, - tunnelSettings: TunnelSettings(defaults: .networkProtectionGroupDefaults)) + settings: settings) startMonitoringMemoryPressureEvents() observeServerChanges() APIRequest.Headers.setUserAgent(DefaultUserAgentManager.duckDuckGoUserAgent)