From 40fbb495ad212954a3f6da3db8201443541ed5a1 Mon Sep 17 00:00:00 2001 From: Michal Smaga Date: Fri, 22 Mar 2024 21:05:17 +0100 Subject: [PATCH] Rework the caching logic for subscription and entitlements (#2627) --- DuckDuckGo.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 6 +++--- DuckDuckGo/AppDelegate.swift | 19 ++++++++++--------- .../SubscriptionDebugViewController.swift | 4 ++-- ...etworkProtectionPacketTunnelProvider.swift | 2 +- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index 0bca2809f6..bbf3a43ffc 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -10033,7 +10033,7 @@ repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit"; requirement = { kind = exactVersion; - version = 129.1.2; + version = 129.1.3; }; }; B6F997C22B8F374300476735 /* XCRemoteSwiftPackageReference "apple-toolbox" */ = { diff --git a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 40fa1c998b..2078795732 100644 --- a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/DuckDuckGo/BrowserServicesKit", "state" : { - "revision" : "781b5f10b6030273e745ba3c0b71ff9317d8ade0", - "version" : "129.1.2" + "revision" : "91d9eb23c33ae8c1a6e19314c0349e42eab70326", + "version" : "129.1.3" } }, { @@ -183,7 +183,7 @@ { "identity" : "trackerradarkit", "kind" : "remoteSourceControl", - "location" : "https://github.com/duckduckgo/TrackerRadarKit", + "location" : "https://github.com/duckduckgo/TrackerRadarKit.git", "state" : { "revision" : "a6b7ba151d9dc6684484f3785293875ec01cc1ff", "version" : "1.2.2" diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index 774472e543..1c141e9ae1 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -435,7 +435,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } #endif SubscriptionPurchaseEnvironment.current = .appStore - await AccountManager().checkSubscriptionState() } } #endif @@ -508,18 +507,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func updateSubscriptionStatus() { #if SUBSCRIPTION Task { - guard let token = AccountManager().accessToken else { - return - } - let result = await SubscriptionService.getSubscription(accessToken: token) + let accountManager = AccountManager() - switch result { - case .success(let success): - if success.isActive { + guard let token = accountManager.accessToken else { return } + + if case .success(let subscription) = await SubscriptionService.getSubscription(accessToken: token, + cachePolicy: .reloadIgnoringLocalCacheData) { + if subscription.isActive { DailyPixel.fire(pixel: .privacyProSubscriptionActive) + } else { + accountManager.signOut() } - case .failure: break } + + _ = await accountManager.fetchEntitlements(cachePolicy: .reloadIgnoringLocalCacheData) } #endif } diff --git a/DuckDuckGo/SubscriptionDebugViewController.swift b/DuckDuckGo/SubscriptionDebugViewController.swift index 429420d8d0..8368fb92e2 100644 --- a/DuckDuckGo/SubscriptionDebugViewController.swift +++ b/DuckDuckGo/SubscriptionDebugViewController.swift @@ -222,7 +222,7 @@ final class SubscriptionDebugViewController: UITableViewController { showAlert(title: "Not authenticated", message: "No authenticated user found! - Subscription not available") return } - switch await SubscriptionService.getSubscription(accessToken: token) { + switch await SubscriptionService.getSubscription(accessToken: token, cachePolicy: .reloadIgnoringLocalCacheData) { case .success(let response): showAlert(title: "Subscription info", message: "\(response)") case .failure(let error): @@ -240,7 +240,7 @@ final class SubscriptionDebugViewController: UITableViewController { } let entitlements: [Entitlement.ProductName] = [.networkProtection, .dataBrokerProtection, .identityTheftRestoration] for entitlement in entitlements { - if case let .success(result) = await AccountManager().hasEntitlement(for: entitlement) { + if case let .success(result) = await AccountManager().hasEntitlement(for: entitlement, cachePolicy: .reloadIgnoringLocalCacheData) { let resultSummary = "Entitlement check for \(entitlement.rawValue): \(result)" results.append(resultSummary) print(resultSummary) diff --git a/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift b/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift index d2205689f2..a2d7eb5989 100644 --- a/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift +++ b/PacketTunnelProvider/NetworkProtection/NetworkProtectionPacketTunnelProvider.swift @@ -331,7 +331,7 @@ final class NetworkProtectionPacketTunnelProvider: PacketTunnelProvider { } let result = await AccountManager(subscriptionAppGroup: Bundle.main.appGroup(bundle: .subs)) - .hasEntitlement(for: .networkProtection, cachePolicy: .reloadIgnoringLocalCacheData) + .hasEntitlement(for: .networkProtection) switch result { case .success(let hasEntitlement): return .success(hasEntitlement)