Skip to content

Commit

Permalink
Integrate NetP with subscription (#2359)
Browse files Browse the repository at this point in the history
  • Loading branch information
quanganhdo authored Jan 24, 2024
1 parent 459cee9 commit 6c22f40
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
56 changes: 56 additions & 0 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -98,6 +102,10 @@ class MainViewController: UIViewController {
private var syncFeatureFlagsCancellable: AnyCancellable?
private var favoritesDisplayModeCancellable: AnyCancellable?
private var emailCancellables = Set<AnyCancellable>()

#if NETWORK_PROTECTION
private var netpCancellables = Set<AnyCancellable>()
#endif

private lazy var featureFlagger = AppDependencyProvider.shared.featureFlagger

Expand Down Expand Up @@ -244,6 +252,10 @@ class MainViewController: UIViewController {
addLaunchTabNotificationObserver()
subscribeToEmailProtectionStatusNotifications()

#if NETWORK_PROTECTION
subscribeToNetworkProtectionSubscriptionEvents()
#endif

findInPageView.delegate = self
findInPageBottomLayoutConstraint.constant = 0
registerForKeyboardNotifications()
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions DuckDuckGo/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit 6c22f40

Please sign in to comment.