Skip to content

Commit

Permalink
Update BSK for VPN settings (#2165)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
diegoreymendez authored Nov 28, 2023
1 parent 8263fbe commit ac1cd2c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9199,7 +9199,7 @@
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 85.1.1;
version = 86.0.0;
};
};
C14882EB27F211A000D59F0C /* XCRemoteSwiftPackageReference "SwiftSoup" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
17 changes: 7 additions & 10 deletions DuckDuckGo/NetworkProtectionConvenienceInitialisers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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()
)
}
Expand All @@ -96,7 +93,7 @@ extension NetworkProtectionVPNLocationViewModel {
let locationListRepository = NetworkProtectionLocationListCompositeRepository()
self.init(
locationListRepository: locationListRepository,
tunnelSettings: TunnelSettings(defaults: .networkProtectionGroupDefaults)
settings: VPNSettings(defaults: .networkProtectionGroupDefaults)
)
}
}
Expand Down
14 changes: 7 additions & 7 deletions DuckDuckGo/NetworkProtectionVPNLocationViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -43,32 +43,32 @@ 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 {
await reloadList()
}

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
Expand Down
10 changes: 5 additions & 5 deletions DuckDuckGo/NetworkProtectionVPNNotificationsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions DuckDuckGo/NetworkProtectionVPNSettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 12 additions & 12 deletions DuckDuckGoTests/NetworkProtectionVPNLocationViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ import NetworkProtectionTestUtils

final class NetworkProtectionVPNLocationViewModelTests: XCTestCase {
private var listRepository: MockNetworkProtectionLocationListRepository!
private var tunnelSettings: TunnelSettings!
private var settings: VPNSettings!
private var viewModel: NetworkProtectionVPNLocationViewModel!

@MainActor
override func setUp() {
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()
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -140,15 +140,15 @@ 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)
}

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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit ac1cd2c

Please sign in to comment.