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 Caching #2368

Merged
merged 13 commits into from
Mar 11, 2024
4 changes: 2 additions & 2 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13769,8 +13769,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/duckduckgo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 122.0.0;
branch = daniel/subscription.cache;
kind = branch;
};
};
AA06B6B52672AF8100F541C5 /* XCRemoteSwiftPackageReference "Sparkle" */ = {
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" : "83bcbbf0dace717db6e518e4d867d617c846a3b5",
"version" : "122.0.0"
"branch" : "daniel/subscription.cache",
"revision" : "8197cab1d79097a69140a6d00e35411112da5508"
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/Tab/UserScripts/SubscriptionPagesUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ final class SubscriptionPagesUseSubscriptionFeature: Subfeature {
os_log(.error, log: .subscription, "[Purchase] Error: %{public}s", String(reflecting: error))
await WindowControllersManager.shared.lastKeyMainWindowController?.showSomethingWentWrongAlert()
}
await pushPurchaseUpdate(originalMessage: message, purchaseUpdate: PurchaseUpdate(type: "canceled"))
return nil
}

Expand All @@ -241,7 +242,7 @@ final class SubscriptionPagesUseSubscriptionFeature: Subfeature {
await pushPurchaseUpdate(originalMessage: message, purchaseUpdate: purchaseUpdate)
case .failure(let error):
os_log(.error, log: .subscription, "[Purchase] Error: %{public}s", String(reflecting: error))
await pushPurchaseUpdate(originalMessage: message, purchaseUpdate: PurchaseUpdate(type: "completed"))
await pushPurchaseUpdate(originalMessage: message, purchaseUpdate: PurchaseUpdate(type: "canceled"))
}

os_log(.info, log: .subscription, "[Purchase] Purchase complete")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ public final class PreferencesSubscriptionModel: ObservableObject {

self.isUserAuthenticated = accountManager.isUserAuthenticated

if let cachedDate = SubscriptionService.cachedGetSubscriptionResponse?.expiresOrRenewsAt {
updateDescription(for: cachedDate)
if let token = accountManager.accessToken {
Task {
let subscriptionResult = await SubscriptionService.getSubscription(accessToken: token)
if case .success(let subscription) = subscriptionResult {
self.updateDescription(for: subscription.expiresOrRenewsAt)
}
}
}

signInObserver = NotificationCenter.default.addObserver(forName: .accountDidSignIn, object: nil, queue: .main) { [weak self] _ in
Expand Down Expand Up @@ -197,26 +202,26 @@ public final class PreferencesSubscriptionModel: ObservableObject {

guard let token = self?.accountManager.accessToken else { return }

if let cachedDate = SubscriptionService.cachedGetSubscriptionResponse?.expiresOrRenewsAt {
self?.updateDescription(for: cachedDate)
let subscriptionResult = await SubscriptionService.getSubscription(accessToken: token)

if cachedDate.timeIntervalSinceNow < 0 {
if case .success(let subscription) = subscriptionResult {
self?.updateDescription(for: subscription.expiresOrRenewsAt)
self?.subscriptionPlatform = subscription.platform

if subscription.expiresOrRenewsAt.timeIntervalSinceNow < 0 || !subscription.isActive {
self?.hasAccessToVPN = false
self?.hasAccessToDBP = false
self?.hasAccessToITR = false
}
}

if case .success(let subscription) = await SubscriptionService.getSubscription(accessToken: token) {
if !subscription.isActive {
self?.accountManager.signOut()
return
if !subscription.isActive {
self?.accountManager.signOut()
return
}
}

self?.updateDescription(for: subscription.expiresOrRenewsAt)

self?.subscriptionPlatform = subscription.platform
} else {
self?.accountManager.signOut()
}

if let self {
switch await self.accountManager.hasEntitlement(for: .networkProtection) {
case let .success(result):
Expand Down
Loading