-
Notifications
You must be signed in to change notification settings - Fork 993
PM-38356 PM-37177: Bug: Update premium subscription trouble-states #7073
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package com.x8bit.bitwarden.data.billing.manager | ||
|
|
||
| import com.x8bit.bitwarden.data.billing.model.PremiumCard | ||
| import com.x8bit.bitwarden.data.billing.repository.model.SubscriptionStatusState | ||
| import com.x8bit.bitwarden.data.billing.repository.model.UpgradeLifecycleState | ||
| import kotlinx.coroutines.flow.StateFlow | ||
|
|
@@ -16,10 +17,9 @@ const val UPGRADED_TO_PREMIUM_LEARN_MORE_URL: String = | |
| interface PremiumStateManager { | ||
|
|
||
| /** | ||
| * Emits `true` when the current user is eligible to see the Premium upgrade banner, | ||
| * or `false` otherwise. | ||
| * Emits a [PremiumCard] for the current user indicating what Premium card should be displayed. | ||
| */ | ||
| val isPremiumUpgradeBannerEligibleFlow: StateFlow<Boolean> | ||
| val premiumCardStateFlow: StateFlow<PremiumCard> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π₯ |
||
|
|
||
| /** | ||
| * Emits `true` while the active user is eligible to see the "Upgraded to Premium" action | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,10 @@ import com.bitwarden.core.data.manager.model.FlagKey | |
| import com.bitwarden.core.data.repository.model.DataState | ||
| import com.bitwarden.data.repository.model.Environment | ||
| import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource | ||
| import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson | ||
| import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson | ||
| import com.x8bit.bitwarden.data.auth.repository.util.activeUserIdChangesFlow | ||
| import com.x8bit.bitwarden.data.billing.model.PremiumCard | ||
| import com.x8bit.bitwarden.data.billing.repository.BillingRepository | ||
| import com.x8bit.bitwarden.data.billing.repository.model.PremiumSubscriptionStatus | ||
| import com.x8bit.bitwarden.data.billing.repository.model.SubscriptionResult | ||
|
|
@@ -50,7 +52,7 @@ class PremiumStateManagerImpl( | |
| private val settingsDiskSource: SettingsDiskSource, | ||
| vaultRepository: VaultRepository, | ||
| private val featureFlagManager: FeatureFlagManager, | ||
| private val environmentRepository: EnvironmentRepository, | ||
| environmentRepository: EnvironmentRepository, | ||
| pushManager: PushManager, | ||
| private val clock: Clock, | ||
| dispatcherManager: DispatcherManager, | ||
|
|
@@ -123,62 +125,69 @@ class PremiumStateManagerImpl( | |
| ) | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| override val isPremiumUpgradeBannerEligibleFlow: StateFlow<Boolean> = | ||
| override val premiumCardStateFlow: StateFlow<PremiumCard> = | ||
| combine( | ||
| authDiskSource.userStateFlow, | ||
| authDiskSource.userStateFlow.map { it?.activeAccount }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π |
||
| billingRepository.isInAppBillingSupportedFlow, | ||
| featureFlagManager.getFeatureFlagFlow(FlagKey.MobilePremiumUpgrade), | ||
| authDiskSource.activeUserIdChangesFlow | ||
| .flatMapLatest { userId -> | ||
| userId | ||
| ?.let { id -> | ||
| settingsDiskSource | ||
| .getPremiumUpgradeBannerDismissedFlow(id) | ||
| .map { it ?: false } | ||
| } | ||
| ?: flowOf(false) | ||
| }, | ||
| authDiskSource.activeUserIdChangesFlow.flatMapLatest { userId -> | ||
| userId | ||
| ?.let { id -> | ||
| settingsDiskSource | ||
| .getPremiumUpgradeBannerDismissedFlow(id) | ||
| .map { it ?: false } | ||
| } | ||
| ?: flowOf(false) | ||
| }, | ||
| vaultRepository.vaultDataStateFlow, | ||
| ) { | ||
| userState, | ||
| account, | ||
| isInAppBillingSupported, | ||
| featureFlagEnabled, | ||
| isDismissed, | ||
| isUpgradeCardDismissed, | ||
| vaultDataState, | ||
| -> | ||
| BannerInputs( | ||
| userState = userState, | ||
| account = account, | ||
| isInAppBillingSupported = isInAppBillingSupported, | ||
| featureFlagEnabled = featureFlagEnabled, | ||
| isDismissed = isDismissed, | ||
| isUpgradeCardDismissed = isUpgradeCardDismissed, | ||
| vaultDataState = vaultDataState, | ||
| ) | ||
| } | ||
| .combine(upgradeLifecycleStateFlow) { inputs, lifecycle -> | ||
| val profile = inputs.userState?.activeAccount?.profile | ||
| ?: return@combine false | ||
| val isAccountOldEnough = profile.creationDate.isOlderThanDays( | ||
| days = PREMIUM_UPGRADE_MINIMUM_ACCOUNT_AGE_DAYS, | ||
| clock = clock, | ||
| ) | ||
| val itemCount = inputs.vaultDataState.activeVaultItemCount() | ||
| val lifecycleAllowsBanner = lifecycle is UpgradeLifecycleState.Free || | ||
| ( | ||
| lifecycle is UpgradeLifecycleState.Premium && | ||
| lifecycle.subscriptionStatus.isInTroubleState() | ||
| val profile = inputs.account?.profile ?: return@combine PremiumCard.NONE | ||
| if (!inputs.featureFlagEnabled) return@combine PremiumCard.NONE | ||
| val initialCard = when (lifecycle) { | ||
| UpgradeLifecycleState.Free -> PremiumCard.UPGRADE | ||
| UpgradeLifecycleState.UpgradePending -> PremiumCard.NONE | ||
| is UpgradeLifecycleState.Premium -> { | ||
| lifecycle.subscriptionStatus.premiumCardState() | ||
| } | ||
| } | ||
| when (initialCard) { | ||
| PremiumCard.UPGRADE -> { | ||
| val isAccountOldEnough = profile.creationDate.isOlderThanDays( | ||
| days = PREMIUM_UPGRADE_MINIMUM_ACCOUNT_AGE_DAYS, | ||
| clock = clock, | ||
| ) | ||
| val itemCount = inputs.vaultDataState.activeVaultItemCount() | ||
| val showCard = inputs.isInAppBillingSupported && | ||
| isAccountOldEnough && | ||
| itemCount >= PREMIUM_UPGRADE_MINIMUM_VAULT_ITEMS && | ||
| !inputs.isUpgradeCardDismissed | ||
| initialCard.takeIf { showCard } ?: PremiumCard.NONE | ||
| } | ||
|
|
||
| lifecycleAllowsBanner && | ||
| inputs.isInAppBillingSupported && | ||
| inputs.featureFlagEnabled && | ||
| !inputs.isDismissed && | ||
| isAccountOldEnough && | ||
| itemCount >= PREMIUM_UPGRADE_MINIMUM_VAULT_ITEMS | ||
| PremiumCard.NEEDS_ATTENTION, | ||
| PremiumCard.NONE, | ||
| -> initialCard | ||
| } | ||
| } | ||
| .stateIn( | ||
| scope = unconfinedScope, | ||
| started = SharingStarted.Eagerly, | ||
| initialValue = false, | ||
| initialValue = PremiumCard.NONE, | ||
| ) | ||
|
|
||
| override val isSelfHostedFlow: StateFlow<Boolean> = | ||
|
|
@@ -389,32 +398,41 @@ class PremiumStateManagerImpl( | |
| } | ||
|
|
||
| private data class BannerInputs( | ||
| val userState: UserStateJson?, | ||
| val account: AccountJson?, | ||
| val isInAppBillingSupported: Boolean, | ||
| val featureFlagEnabled: Boolean, | ||
| val isDismissed: Boolean, | ||
| val isUpgradeCardDismissed: Boolean, | ||
| val vaultDataState: DataState<VaultData>, | ||
| ) | ||
|
|
||
| /** | ||
| * Returns `true` when the given [SubscriptionStatusState] represents a subscription substate | ||
| * that should disqualify a user from being treated as effectively premium. | ||
| * Returns a [PremiumCard] for the given [SubscriptionStatusState] and subscription substate. | ||
| */ | ||
|
david-livefront marked this conversation as resolved.
|
||
| private fun SubscriptionStatusState.isInTroubleState(): Boolean = | ||
| this is SubscriptionStatusState.Available && | ||
| when (this.status) { | ||
| PremiumSubscriptionStatus.CANCELED, | ||
| PremiumSubscriptionStatus.EXPIRED, | ||
| PremiumSubscriptionStatus.PAST_DUE, | ||
| PremiumSubscriptionStatus.PAUSED, | ||
| PremiumSubscriptionStatus.UPDATE_PAYMENT, | ||
| -> true | ||
|
|
||
| PremiumSubscriptionStatus.ACTIVE, | ||
| PremiumSubscriptionStatus.PENDING_CANCELLATION, | ||
| -> false | ||
| private fun SubscriptionStatusState.premiumCardState(): PremiumCard = | ||
| when (this) { | ||
| is SubscriptionStatusState.Available -> { | ||
| when (this.status) { | ||
| PremiumSubscriptionStatus.PAST_DUE, | ||
| PremiumSubscriptionStatus.UPDATE_PAYMENT, | ||
| -> PremiumCard.NEEDS_ATTENTION | ||
|
|
||
| PremiumSubscriptionStatus.EXPIRED, | ||
| PremiumSubscriptionStatus.PAUSED, | ||
| -> PremiumCard.UPGRADE | ||
|
|
||
| PremiumSubscriptionStatus.ACTIVE, | ||
| PremiumSubscriptionStatus.CANCELED, | ||
| PremiumSubscriptionStatus.PENDING_CANCELLATION, | ||
| -> PremiumCard.NONE | ||
| } | ||
| } | ||
|
|
||
| is SubscriptionStatusState.Error, | ||
| SubscriptionStatusState.Loading, | ||
| SubscriptionStatusState.NoSubscription, | ||
| -> PremiumCard.NONE | ||
| } | ||
|
|
||
| /** | ||
| * Returns `true` if this [Instant] is older than the given number of [days] based on | ||
| * the provided [clock]. Returns `false` if the receiver is `null`. | ||
|
|
||
10 changes: 10 additions & 0 deletions
10
app/src/main/kotlin/com/x8bit/bitwarden/data/billing/model/PremiumCard.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.x8bit.bitwarden.data.billing.model | ||
|
|
||
| /** | ||
| * Represents which premium card should be displayed. | ||
| */ | ||
| enum class PremiumCard { | ||
| UPGRADE, | ||
| NEEDS_ATTENTION, | ||
| NONE, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.