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

Subscription refactoring #2842

Merged
merged 57 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
140680f
Accountmanager injected and abstracted
federicocappelli May 2, 2024
199844e
.profraw ignored
federicocappelli May 2, 2024
ff61d7e
Merge branch 'main' into fcappelli/subscription_refactoring
federicocappelli May 3, 2024
df39472
BSK as branch
federicocappelli May 3, 2024
243feb2
Merge branch 'main' into fcappelli/subscription_refactoring
federicocappelli May 3, 2024
9c19c17
Merge branch 'main' into fcappelli/subscription_refactoring
federicocappelli May 3, 2024
8718719
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 9, 2024
24d28b1
DI #1
federicocappelli May 10, 2024
ffc80df
DI #2 + lint
federicocappelli May 10, 2024
06f51b0
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 10, 2024
982b388
todo added
federicocappelli May 10, 2024
bfccc11
cleanup
federicocappelli May 10, 2024
dc71688
vpnsettings env restored
federicocappelli May 10, 2024
c430099
it builds!
federicocappelli May 13, 2024
b6dc664
local BSK removed
federicocappelli May 13, 2024
b8ad64f
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 13, 2024
7dea7ef
lint + some unit tests fix
federicocappelli May 13, 2024
da6eb79
unit tests fixes and task moved from model init to view
federicocappelli May 13, 2024
25e0838
local BSK removed
federicocappelli May 13, 2024
bfb33d1
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 13, 2024
8653221
subscription debug menu #1
federicocappelli May 14, 2024
83a71fa
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 14, 2024
84be66f
alpha build fixed, settings menu enabled
federicocappelli May 14, 2024
cb61c12
subscription debug menu reimplemented
federicocappelli May 14, 2024
d88ab0b
lint
federicocappelli May 14, 2024
14dcb42
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 15, 2024
735217f
BSK updated
federicocappelli May 15, 2024
468d886
SubscriptionEnvironment Default moved to main app
federicocappelli May 15, 2024
ece04c4
infinite init loop fixed and Platform renamed PurchasePlatform
federicocappelli May 15, 2024
0713783
VPNSettings environment is now aligned with subscription environment
federicocappelli May 15, 2024
d793b13
Merge from main
federicocappelli May 15, 2024
b3562ba
unit tests fix
federicocappelli May 15, 2024
102b54f
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 16, 2024
4873bf7
lint and unit tests fix
federicocappelli May 16, 2024
38d1725
BSK update
federicocappelli May 16, 2024
e3ac5c4
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 16, 2024
6e937b3
BSK updated, renaming and access control improvements
federicocappelli May 16, 2024
6af618d
testing with speeding up unit/ui tests
federicocappelli May 16, 2024
17234dd
flaky test disabled
federicocappelli May 16, 2024
783d891
unit test fixed
federicocappelli May 16, 2024
b485a5a
getStorePurchaseManager() renamed storePurchaseManager()
federicocappelli May 16, 2024
1742a63
various improvements suggested in the PR
federicocappelli May 16, 2024
53c5528
BSK updated
federicocappelli May 16, 2024
4982935
merge from main
federicocappelli May 17, 2024
3b4b2c0
Update DuckDuckGo/Subscription/ViewModel/SubscriptionFlowViewModel.swift
federicocappelli May 17, 2024
bb01dc5
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 17, 2024
f1d4cf4
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 17, 2024
6f7b033
accountmanager fixed
federicocappelli May 17, 2024
e7df003
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 20, 2024
6606f4d
BSK update
federicocappelli May 21, 2024
87ff524
PR improvements
federicocappelli May 22, 2024
7f424d4
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 22, 2024
77632a8
mock fixed
federicocappelli May 22, 2024
a3ec653
commented code removed
federicocappelli May 22, 2024
6769682
unused code removed
federicocappelli May 22, 2024
17dfc80
Merge branch 'main' into fcappelli/subscription_refactoring_2
federicocappelli May 22, 2024
e463080
BSK v146.0.0
federicocappelli May 22, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ Configuration/ExternalDeveloper.xcconfig
scripts/assets

DuckDuckGoTests/NetworkProtectionVPNLocationViewModelTests.swift*.plist
*.profraw
72 changes: 58 additions & 14 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/DuckDuckGo/BrowserServicesKit",
"state" : {
"revision" : "b901a47ad442356b0953ffdb768ea1fae47fe912",
"version" : "145.3.0"
"branch" : "fcappelli/subscription_refactoring_2",
"revision" : "3819577dbab2f3b90ba61a9141ac3efc869be982"
}
},
{
Expand Down Expand Up @@ -138,7 +138,7 @@
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "46989693916f56d1186bd59ac15124caef896560",
"version" : "1.3.1"
Expand Down Expand Up @@ -183,7 +183,7 @@
{
"identity" : "trackerradarkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/TrackerRadarKit",
"location" : "https://github.com/duckduckgo/TrackerRadarKit.git",
"state" : {
"revision" : "6c84fd19139414fc0edbf9673ade06e532a564f0",
"version" : "2.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<Test
Identifier = "AppTrackingProtectionListModelTests/testWhenNewChangesAreWrittenToTheDatabase_ThenTheSectionsPropertyIsUpdated()">
</Test>
<Test
Identifier = "AutoconsentMessageProtocolTests/testEval()">
</Test>
</SkippedTests>
</TestableReference>
<TestableReference
Expand Down
8 changes: 4 additions & 4 deletions DuckDuckGo/AppDelegate+Waitlists.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ extension AppDelegate {
func checkWaitlists() {

#if NETWORK_PROTECTION
if vpnFeatureVisibility.shouldKeepVPNAccessViaWaitlist() {
if AppDependencyProvider.shared.vpnFeatureVisibility.shouldKeepVPNAccessViaWaitlist() {
checkNetworkProtectionWaitlist()
}
#endif
}

#if NETWORK_PROTECTION
private func checkNetworkProtectionWaitlist() {
let accessController = NetworkProtectionAccessController()

VPNWaitlist.shared.fetchInviteCodeIfAvailable { [weak self] error in
guard error == nil else {
return
Expand All @@ -65,7 +63,9 @@ extension AppDelegate {
func fetchVPNWaitlistAuthToken(inviteCode: String) {
Task {
do {
try await NetworkProtectionCodeRedemptionCoordinator().redeem(inviteCode)
try await NetworkProtectionCodeRedemptionCoordinator(accountManager:
AppDependencyProvider.shared.subscriptionManager.accountManager
).redeem(inviteCode)
VPNWaitlist.shared.sendInviteCodeAvailableNotification()
} catch {}
}
Expand Down
78 changes: 26 additions & 52 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ import WebKit

// swiftlint:disable file_length
// swiftlint:disable type_body_length

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// swiftlint:enable type_body_length

private static let ShowKeyboardOnLaunchThreshold = TimeInterval(20)
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
// swiftlint:enable type_body_length

private static let ShowKeyboardOnLaunchThreshold = TimeInterval(20)
private struct ShortcutKey {
static let clipboard = "com.duckduckgo.mobile.ios.clipboard"
static let passwords = "com.duckduckgo.mobile.ios.passwords"
Expand All @@ -69,7 +66,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
#if NETWORK_PROTECTION
private let widgetRefreshModel = NetworkProtectionWidgetRefreshModel()
private let tunnelDefaults = UserDefaults.networkProtectionGroupDefaults
lazy var vpnFeatureVisibility = DefaultNetworkProtectionVisibility()
#endif

private var autoClear: AutoClear?
Expand All @@ -91,9 +87,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

@UserDefaultsWrapper(key: .privacyConfigCustomURL, defaultValue: nil)
private var privacyConfigCustomURL: String?

@UserDefaultsWrapper(key: .privacyProEnvironment, defaultValue: SubscriptionPurchaseEnvironment.ServiceEnvironment.default.description)
private var privacyProEnvironment: String

// swiftlint:disable:next function_body_length cyclomatic_complexity
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Expand Down Expand Up @@ -223,7 +216,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
PixelExperiment.install()

// MARK: Sync initialisation

#if DEBUG
let defaultEnvironment = ServerEnvironment.development
#else
Expand Down Expand Up @@ -323,12 +315,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

#if NETWORK_PROTECTION
widgetRefreshModel.beginObservingVPNStatus()
NetworkProtectionAccessController().refreshNetworkProtectionAccess()
AppDependencyProvider.shared.networkProtectionAccessController.refreshNetworkProtectionAccess()
#endif

setupSubscriptionsEnvironment()

if vpnFeatureVisibility.shouldKeepVPNAccessViaWaitlist() {
if AppDependencyProvider.shared.vpnFeatureVisibility.shouldKeepVPNAccessViaWaitlist() {
clearDebugWaitlistState()
}

Expand Down Expand Up @@ -406,7 +396,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

private func presentExpiredEntitlementNotificationIfNeeded() {
let presenter = NetworkProtectionNotificationsPresenterTogglableDecorator(
settings: VPNSettings(defaults: .networkProtectionGroupDefaults),
settings: AppDependencyProvider.shared.vpnSettings,
defaults: .networkProtectionGroupDefaults,
wrappee: NetworkProtectionUNNotificationPresenter()
)
Expand All @@ -431,21 +421,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}


private func setupSubscriptionsEnvironment() {
Task {
#if ALPHA || DEBUG
let defaultEnvironment = SubscriptionPurchaseEnvironment.ServiceEnvironment.staging
#else
let defaultEnvironment = SubscriptionPurchaseEnvironment.ServiceEnvironment.production
#endif
let environment = SubscriptionPurchaseEnvironment.ServiceEnvironment(rawValue: privacyProEnvironment) ?? defaultEnvironment
SubscriptionPurchaseEnvironment.currentServiceEnvironment = environment
VPNSettings(defaults: .networkProtectionGroupDefaults).selectedEnvironment = (environment == .production) ? .production : .staging
SubscriptionPurchaseEnvironment.current = .appStore
}
}

private func reportAdAttribution() {
guard AdAttributionPixelReporter.isAdAttributionReportingEnabled else { return }

Expand Down Expand Up @@ -522,53 +497,54 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

private func stopTunnelAndShowThankYouMessagingIfNeeded() {
if AccountManager().isUserAuthenticated {

if AppDependencyProvider.shared.accountManager.isUserAuthenticated {
tunnelDefaults.vpnEarlyAccessOverAlertAlreadyShown = true
return
}

if vpnFeatureVisibility.shouldShowThankYouMessaging() && !tunnelDefaults.vpnEarlyAccessOverAlertAlreadyShown {
if AppDependencyProvider.shared.vpnFeatureVisibility.shouldShowThankYouMessaging()
&& !tunnelDefaults.vpnEarlyAccessOverAlertAlreadyShown {
Task {
await self.stopAndRemoveVPN(with: "thank-you-dialog")
}
} else if vpnFeatureVisibility.isPrivacyProLaunched() && !AccountManager().isUserAuthenticated {
} else if AppDependencyProvider.shared.vpnFeatureVisibility.isPrivacyProLaunched()
&& !AppDependencyProvider.shared.accountManager.isUserAuthenticated {
Task {
await self.stopAndRemoveVPN(with: "subscription-check")
}
}
}

private func stopAndRemoveVPN(with reason: String) async {
let controller = NetworkProtectionTunnelController()
guard await controller.isInstalled else {
guard await AppDependencyProvider.shared.networkProtectionTunnelController.isInstalled else {
return
}

let isConnected = await controller.isConnected
let isConnected = await AppDependencyProvider.shared.networkProtectionTunnelController.isConnected

DailyPixel.fireDailyAndCount(pixel: .privacyProVPNBetaStoppedWhenPrivacyProEnabled, withAdditionalParameters: [
"reason": reason,
"vpn-connected": String(isConnected)
])

await controller.stop()
await controller.removeVPN()
await AppDependencyProvider.shared.networkProtectionTunnelController.stop()
await AppDependencyProvider.shared.networkProtectionTunnelController.removeVPN()
}

func updateSubscriptionStatus() {
Task {
let accountManager = AccountManager()

guard let token = accountManager.accessToken else { return }

if case .success(let subscription) = await SubscriptionService.getSubscription(accessToken: token,
guard let token = AppDependencyProvider.shared.accountManager.accessToken else { return }
var subscriptionService: SubscriptionService {
AppDependencyProvider.shared.subscriptionManager.subscriptionService
}
if case .success(let subscription) = await subscriptionService.getSubscription(accessToken: token,
cachePolicy: .reloadIgnoringLocalCacheData) {
if subscription.isActive {
DailyPixel.fire(pixel: .privacyProSubscriptionActive)
}
}

_ = await accountManager.fetchEntitlements(cachePolicy: .reloadIgnoringLocalCacheData)
await AppDependencyProvider.shared.accountManager.fetchEntitlements(cachePolicy: .reloadIgnoringLocalCacheData)
}
}

Expand Down Expand Up @@ -868,8 +844,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

#if NETWORK_PROTECTION
if shortcutItem.type == ShortcutKey.openVPNSettings {
let visibility = DefaultNetworkProtectionVisibility()
if visibility.shouldShowVPNShortcut() {
if AppDependencyProvider.shared.vpnFeatureVisibility.shouldShowVPNShortcut() {
presentNetworkProtectionStatusSettingsModal()
}
}
Expand Down Expand Up @@ -906,7 +881,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func refreshShortcuts() {
#if NETWORK_PROTECTION
guard vpnFeatureVisibility.shouldShowVPNShortcut() else {
guard AppDependencyProvider.shared.vpnFeatureVisibility.shouldShowVPNShortcut() else {
UIApplication.shared.shortcutItems = nil
return
}
Expand Down Expand Up @@ -981,15 +956,14 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
#if NETWORK_PROTECTION
if NetworkProtectionNotificationIdentifier(rawValue: identifier) != nil {
Task {
let accountManager = AccountManager()
if case .success(let hasEntitlements) = await accountManager.hasEntitlement(for: .networkProtection),
if case .success(let hasEntitlements) = await AppDependencyProvider.shared.accountManager.hasEntitlement(for: .networkProtection),
hasEntitlements {
presentNetworkProtectionStatusSettingsModal()
}
}
}

if vpnFeatureVisibility.shouldKeepVPNAccessViaWaitlist(), identifier == VPNWaitlist.notificationIdentifier {
if AppDependencyProvider.shared.vpnFeatureVisibility.shouldKeepVPNAccessViaWaitlist(), identifier == VPNWaitlist.notificationIdentifier {
presentNetworkProtectionWaitlistModal()
}
#endif
Expand Down
Loading
Loading