Skip to content

Commit

Permalink
15. Subscriptions: Properly sign out users and cache state (#2520)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/0/1206707680638927/f

Description:
If the subscription has become inactive, the user may still see the options in settings. This forces a signout in that case and caches the subscription state data properly.
  • Loading branch information
afterxleep authored Mar 1, 2024
1 parent 11ec9e4 commit 3fbcab6
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions DuckDuckGo/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,14 @@ extension SettingsViewModel {
// Fetch available subscriptions from the backend (or sign out)
switch await SubscriptionService.getSubscriptionDetails(token: token) {
case .success(let response) where !response.isSubscriptionActive:
AccountManager().signOut()
setupSubscriptionPurchaseOptions()

// Account is active but there's not a valid subscription
signOutUser()

case .success(let response):

// Cache Subscription state
self.state.subscription.hasActiveSubscription = true
Self.cachedHasActiveSubscription = self.state.subscription.hasActiveSubscription

cacheSubscriptionState(active: true)

// Check entitlements and update UI accordingly
let entitlements: [AccountManager.Entitlement] = [.identityTheftRestoration, .dataBrokerProtection, .networkProtection]
Expand All @@ -364,19 +363,27 @@ extension SettingsViewModel {
}
}
}

// Enable Subscription purchase if there's no active subscription
if !self.state.subscription.hasActiveSubscription {
setupSubscriptionPurchaseOptions()
}

default:
setupSubscriptionPurchaseOptions()
signOutUser()
}
}

@available(iOS 15.0, *)
private func signOutUser() {
AccountManager().signOut()
cacheSubscriptionState(active: false)
setupSubscriptionPurchaseOptions()
}

private func cacheSubscriptionState(active: Bool) {
self.state.subscription.hasActiveSubscription = active
Self.cachedHasActiveSubscription = active
}

@available(iOS 15.0, *)
private func setupSubscriptionPurchaseOptions() {
self.state.subscription.hasActiveSubscription = false
cacheSubscriptionState(active: false)
PurchaseManager.shared.$availableProducts
.receive(on: RunLoop.main)
.sink { [weak self] products in
Expand Down

0 comments on commit 3fbcab6

Please sign in to comment.