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

Clean up subscription state caching in settings logic #2925

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
45 changes: 20 additions & 25 deletions DuckDuckGo/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,16 @@ extension SettingsViewModel {
// Update if can purchase based on App Store product availability
state.subscription.canPurchase = subscriptionManager.canPurchase

// Update if user is signed in based on the presence of token
state.subscription.isSignedIn = subscriptionManager.accountManager.isUserAuthenticated

// Active subscription check
guard let token = subscriptionManager.accountManager.accessToken else {
// Reset state in case cache was outdated
state.subscription.hasActiveSubscription = false
state.subscription.entitlements = []
state.subscription.platform = .unknown

subscriptionStateCache.set(state.subscription) // Sync cache
return
}
Expand All @@ -777,34 +785,21 @@ extension SettingsViewModel {
switch subscriptionResult {

case .success(let subscription):

state.subscription.isSignedIn = true
state.subscription.platform = subscription.platform

if subscription.isActive {
state.subscription.hasActiveSubscription = true

// Check entitlements and update state
let entitlements: [Entitlement.ProductName] = [.networkProtection, .dataBrokerProtection, .identityTheftRestoration]
for entitlement in entitlements {
if case .success = await subscriptionManager.accountManager.hasEntitlement(for: entitlement) {
switch entitlement {
case .identityTheftRestoration:
self.state.subscription.entitlements.append(.identityTheftRestoration)
case .dataBrokerProtection:
self.state.subscription.entitlements.append(.dataBrokerProtection)
case .networkProtection:
self.state.subscription.entitlements.append(.networkProtection)
case .unknown:
return
}
}
state.subscription.hasActiveSubscription = subscription.isActive

// Check entitlements and update state
var currentEntitlements: [Entitlement.ProductName] = []
let entitlementsToCheck: [Entitlement.ProductName] = [.networkProtection, .dataBrokerProtection, .identityTheftRestoration]

for entitlement in entitlementsToCheck {
if case .success = await subscriptionManager.accountManager.hasEntitlement(for: entitlement) {
currentEntitlements.append(entitlement)
}
} else {
// Mark the subscription as 'inactive'
state.subscription.hasActiveSubscription = false
}


self.state.subscription.entitlements = currentEntitlements

case .failure:
break
}
Expand Down
Loading