diff --git a/DuckDuckGo/SettingsViewModel.swift b/DuckDuckGo/SettingsViewModel.swift index 1623d2e0c8..2121cbc65e 100644 --- a/DuckDuckGo/SettingsViewModel.swift +++ b/DuckDuckGo/SettingsViewModel.swift @@ -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] @@ -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