Skip to content

Commit

Permalink
Rework the caching logic for subscription and entitlements (#2627)
Browse files Browse the repository at this point in the history
  • Loading branch information
miasma13 authored Mar 22, 2024
1 parent f2e2c31 commit 40fbb49
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" */ = {
Expand Down
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" : "781b5f10b6030273e745ba3c0b71ff9317d8ade0",
"version" : "129.1.2"
"revision" : "91d9eb23c33ae8c1a6e19314c0349e42eab70326",
"version" : "129.1.3"
}
},
{
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" : "a6b7ba151d9dc6684484f3785293875ec01cc1ff",
"version" : "1.2.2"
Expand Down
19 changes: 10 additions & 9 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
#endif
SubscriptionPurchaseEnvironment.current = .appStore
await AccountManager().checkSubscriptionState()
}
}
#endif
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/SubscriptionDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 40fbb49

Please sign in to comment.