Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate NetP with subscription #2359

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2017,6 +2017,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
Loading