From 6c22f4052ce354695c0dfb9f94fc64daff049386 Mon Sep 17 00:00:00 2001 From: Anh Do <18567+quanganhdo@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:55:33 -0500 Subject: [PATCH] Integrate NetP with subscription (#2359) --- DuckDuckGo/MainViewController.swift | 56 +++++++++++++++++++++++++ DuckDuckGo/en.lproj/Localizable.strings | 12 ++++++ 2 files changed, 68 insertions(+) diff --git a/DuckDuckGo/MainViewController.swift b/DuckDuckGo/MainViewController.swift index b112492b87..785dc198a8 100644 --- a/DuckDuckGo/MainViewController.swift +++ b/DuckDuckGo/MainViewController.swift @@ -31,6 +31,10 @@ import Persistence import PrivacyDashboard import Networking +#if NETWORK_PROTECTION +import NetworkProtection +#endif + // swiftlint:disable file_length // swiftlint:disable type_body_length class MainViewController: UIViewController { @@ -98,6 +102,10 @@ class MainViewController: UIViewController { private var syncFeatureFlagsCancellable: AnyCancellable? private var favoritesDisplayModeCancellable: AnyCancellable? private var emailCancellables = Set() + +#if NETWORK_PROTECTION + private var netpCancellables = Set() +#endif private lazy var featureFlagger = AppDependencyProvider.shared.featureFlagger @@ -244,6 +252,10 @@ class MainViewController: UIViewController { addLaunchTabNotificationObserver() subscribeToEmailProtectionStatusNotifications() +#if NETWORK_PROTECTION + subscribeToNetworkProtectionSubscriptionEvents() +#endif + findInPageView.delegate = self findInPageBottomLayoutConstraint.constant = 0 registerForKeyboardNotifications() @@ -1221,6 +1233,50 @@ class MainViewController: UIViewController { .store(in: &emailCancellables) } +#if NETWORK_PROTECTION + private func subscribeToNetworkProtectionSubscriptionEvents() { + NotificationCenter.default.publisher(for: .accountDidSignIn) + .receive(on: DispatchQueue.main) + .sink { [weak self] notification in + self?.onNetworkProtectionAccountSignIn(notification) + } + .store(in: &netpCancellables) + NotificationCenter.default.publisher(for: .accountDidSignOut) + .receive(on: DispatchQueue.main) + .sink { [weak self] notification in + self?.onNetworkProtectionAccountSignOut(notification) + } + .store(in: &netpCancellables) + } + + @objc + private func onNetworkProtectionAccountSignIn(_ notification: Notification) { + guard let token = AccountManager().accessToken else { + assertionFailure("[NetP Subscription] AccountManager signed in but token could not be retrieved") + return + } + + Task { + do { + try await NetworkProtectionCodeRedemptionCoordinator().exchange(accessToken: token) + print("[NetP Subscription] Exchanged access token for auth token successfully") + } catch { + print("[NetP Subscription] Failed to exchange access token for auth token: \(error)") + } + } + } + + @objc + private func onNetworkProtectionAccountSignOut(_ notification: Notification) { + do { + try NetworkProtectionKeychainTokenStore().deleteToken() + print("[NetP Subscription] Deleted NetP auth token after signing out from Privacy Pro") + } catch { + print("[NetP Subscription] Failed to delete NetP auth token after signing out from Privacy Pro: \(error)") + } + } +#endif + @objc private func onDuckDuckGoEmailSignIn(_ notification: Notification) { fireEmailPixel(.emailEnabled, notification: notification) diff --git a/DuckDuckGo/en.lproj/Localizable.strings b/DuckDuckGo/en.lproj/Localizable.strings index 815afd61b6..91a2b13227 100644 --- a/DuckDuckGo/en.lproj/Localizable.strings +++ b/DuckDuckGo/en.lproj/Localizable.strings @@ -2067,6 +2067,18 @@ But if you *do* want a peek under the hood, you can find more information about /* Subscription Expiration Data */ "subscription.subscription.active.caption" = "Your Privacy Pro subscription renews on %@"; +/* Cancel action for the existing subscription dialog */ +"subscription.subscription.found.cancel" = "Cancel"; + +/* Restore action for the existing subscription dialog */ +"subscription.subscription.found.restore" = "Restore"; + +/* Message for the existing subscription dialog */ +"subscription.subscription.found.text" = "We found a subscription associated with this Apple ID."; + +/* Title for the existing subscription dialog */ +"subscription.subscription.found.title" = "Subscription Found"; + /* Message confirming that recovery code was copied to clipboard */ "sync.code.copied" = "Recovery code copied to clipboard";