|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.subscriptions.impl.switch_plan |
| 18 | + |
| 19 | +import android.annotation.SuppressLint |
| 20 | +import android.app.Activity |
| 21 | +import android.content.Context |
| 22 | +import android.content.DialogInterface |
| 23 | +import android.view.LayoutInflater |
| 24 | +import android.widget.FrameLayout |
| 25 | +import androidx.lifecycle.LifecycleOwner |
| 26 | +import androidx.lifecycle.lifecycleScope |
| 27 | +import com.duckduckgo.common.utils.DispatcherProvider |
| 28 | +import com.duckduckgo.subscriptions.impl.CurrentPurchase |
| 29 | +import com.duckduckgo.subscriptions.impl.R |
| 30 | +import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.MONTHLY_PLAN_ROW |
| 31 | +import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.MONTHLY_PLAN_US |
| 32 | +import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.YEARLY_PLAN_ROW |
| 33 | +import com.duckduckgo.subscriptions.impl.SubscriptionsConstants.YEARLY_PLAN_US |
| 34 | +import com.duckduckgo.subscriptions.impl.SubscriptionsManager |
| 35 | +import com.duckduckgo.subscriptions.impl.billing.SubscriptionReplacementMode |
| 36 | +import com.duckduckgo.subscriptions.impl.databinding.BottomSheetSwitchPlanBinding |
| 37 | +import com.duckduckgo.subscriptions.impl.ui.SubscriptionSettingsViewModel.SwitchPlanType |
| 38 | +import com.google.android.material.bottomsheet.BottomSheetBehavior |
| 39 | +import com.google.android.material.bottomsheet.BottomSheetDialog |
| 40 | +import com.google.android.material.shape.CornerFamily |
| 41 | +import com.google.android.material.shape.MaterialShapeDrawable |
| 42 | +import dagger.assisted.Assisted |
| 43 | +import dagger.assisted.AssistedInject |
| 44 | +import kotlinx.coroutines.launch |
| 45 | +import logcat.logcat |
| 46 | +import com.duckduckgo.mobile.android.R as CommonR |
| 47 | +import com.google.android.material.R as MaterialR |
| 48 | + |
| 49 | +@SuppressLint("NoBottomSheetDialog") |
| 50 | +class SwitchPlanBottomSheetDialog @AssistedInject constructor( |
| 51 | + @Assisted private val context: Context, |
| 52 | + @Assisted private val lifecycleOwner: LifecycleOwner, |
| 53 | + @Assisted private val switchType: SwitchPlanType, |
| 54 | + @Assisted private val onSwitchSuccess: () -> Unit, |
| 55 | + private val subscriptionsManager: SubscriptionsManager, |
| 56 | + private val dispatcherProvider: DispatcherProvider, |
| 57 | +) : BottomSheetDialog(context) { |
| 58 | + |
| 59 | + private val binding: BottomSheetSwitchPlanBinding = BottomSheetSwitchPlanBinding.inflate(LayoutInflater.from(context)) |
| 60 | + |
| 61 | + init { |
| 62 | + setContentView(binding.root) |
| 63 | + // We need the dialog to always be expanded and not draggable because the content takes up a lot of vertical space and requires a scroll view, |
| 64 | + // especially in landscape aspect-ratios. If the dialog started as collapsed, the drag would interfere with internal scroll. |
| 65 | + this.behavior.state = BottomSheetBehavior.STATE_EXPANDED |
| 66 | + this.behavior.isDraggable = false |
| 67 | + |
| 68 | + setOnShowListener { dialogInterface -> |
| 69 | + setRoundCorners(dialogInterface) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * By default, when bottom sheet dialog is expanded, the corners become squared. |
| 75 | + * This function ensures that the bottom sheet dialog will have rounded corners even when in an expanded state. |
| 76 | + */ |
| 77 | + private fun setRoundCorners(dialogInterface: DialogInterface) { |
| 78 | + val bottomSheetDialog = dialogInterface as BottomSheetDialog |
| 79 | + val bottomSheet = bottomSheetDialog.findViewById<FrameLayout>(MaterialR.id.design_bottom_sheet) |
| 80 | + |
| 81 | + val shapeDrawable = MaterialShapeDrawable.createWithElevationOverlay(context) |
| 82 | + shapeDrawable.shapeAppearanceModel = shapeDrawable.shapeAppearanceModel |
| 83 | + .toBuilder() |
| 84 | + .setTopLeftCorner(CornerFamily.ROUNDED, context.resources.getDimension(CommonR.dimen.dialogBorderRadius)) |
| 85 | + .setTopRightCorner(CornerFamily.ROUNDED, context.resources.getDimension(CommonR.dimen.dialogBorderRadius)) |
| 86 | + .build() |
| 87 | + bottomSheet?.background = shapeDrawable |
| 88 | + } |
| 89 | + |
| 90 | + override fun onAttachedToWindow() { |
| 91 | + super.onAttachedToWindow() |
| 92 | + |
| 93 | + configureViews() |
| 94 | + observePurchaseState() |
| 95 | + } |
| 96 | + |
| 97 | + private fun configureViews() { |
| 98 | + binding.switchBottomSheetDialogCloseButton.setOnClickListener { |
| 99 | + dismiss() |
| 100 | + } |
| 101 | + |
| 102 | + lifecycleOwner.lifecycleScope.launch(dispatcherProvider.io()) { |
| 103 | + val isUpgrade = switchType == SwitchPlanType.UPGRADE_TO_YEARLY |
| 104 | + val pricingInfo = subscriptionsManager.getSwitchPlanPricing(isUpgrade) |
| 105 | + |
| 106 | + launch(dispatcherProvider.main()) { |
| 107 | + when (switchType) { |
| 108 | + SwitchPlanType.UPGRADE_TO_YEARLY -> { |
| 109 | + // Configure for upgrade (Monthly → Yearly) |
| 110 | + binding.switchBottomSheetDialogTitle.text = context.getString(R.string.switchBottomSheetTitleUpgrade) |
| 111 | + binding.switchBottomSheetDialogSubTitle.text = context.getString( |
| 112 | + R.string.switchBottomSheetDescriptionUpgrade, |
| 113 | + pricingInfo?.yearlyMonthlyEquivalent ?: "", |
| 114 | + pricingInfo?.currentPrice ?: "", |
| 115 | + ) |
| 116 | + binding.switchBottomSheetDialogPrimaryButton.text = context.getString( |
| 117 | + R.string.switchBottomSheetPrimaryButtonUpgrade, |
| 118 | + pricingInfo?.targetPrice ?: "", |
| 119 | + ) |
| 120 | + binding.switchBottomSheetDialogSecondaryButton.text = context.getString(R.string.switchBottomSheetSecondaryButtonUpgrade) |
| 121 | + |
| 122 | + binding.switchBottomSheetDialogPrimaryButton.setOnClickListener { |
| 123 | + triggerSwitch(isUpgrade = true) |
| 124 | + dismiss() |
| 125 | + } |
| 126 | + binding.switchBottomSheetDialogSecondaryButton.setOnClickListener { |
| 127 | + dismiss() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + SwitchPlanType.DOWNGRADE_TO_MONTHLY -> { |
| 132 | + // Configure for downgrade (Yearly → Monthly) |
| 133 | + binding.switchBottomSheetDialogTitle.text = context.getString(R.string.switchBottomSheetTitleDowngrade) |
| 134 | + binding.switchBottomSheetDialogSubTitle.text = context.getString( |
| 135 | + R.string.switchBottomSheetDescriptionDowngrade, |
| 136 | + pricingInfo?.yearlyMonthlyEquivalent ?: "", |
| 137 | + pricingInfo?.targetPrice ?: "", |
| 138 | + ) |
| 139 | + binding.switchBottomSheetDialogPrimaryButton.text = context.getString(R.string.switchBottomSheetPrimaryButtonDowngrade) |
| 140 | + binding.switchBottomSheetDialogSecondaryButton.text = context.getString( |
| 141 | + R.string.switchBottomSheetSecondaryButtonDowngrade, |
| 142 | + pricingInfo?.targetPrice ?: "", |
| 143 | + ) |
| 144 | + |
| 145 | + binding.switchBottomSheetDialogPrimaryButton.setOnClickListener { |
| 146 | + dismiss() |
| 147 | + } |
| 148 | + binding.switchBottomSheetDialogSecondaryButton.setOnClickListener { |
| 149 | + triggerSwitch(isUpgrade = false) |
| 150 | + dismiss() |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private fun observePurchaseState() { |
| 159 | + lifecycleOwner.lifecycleScope.launch(dispatcherProvider.io()) { |
| 160 | + subscriptionsManager.currentPurchaseState.collect { |
| 161 | + when (it) { |
| 162 | + is CurrentPurchase.Success -> { |
| 163 | + logcat { "Switch flow: Successfully switched plans" } |
| 164 | + onSwitchSuccess.invoke() |
| 165 | + } |
| 166 | + |
| 167 | + is CurrentPurchase.Failure -> { |
| 168 | + logcat { "Switch flow: Failed to switch plans. Error: ${it.message}" } |
| 169 | + } |
| 170 | + |
| 171 | + is CurrentPurchase.Canceled -> { |
| 172 | + logcat { "Switch flow: Canceled switch plans" } |
| 173 | + } |
| 174 | + |
| 175 | + else -> {} |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private fun triggerSwitch(isUpgrade: Boolean) { |
| 182 | + lifecycleOwner.lifecycleScope.launch(dispatcherProvider.io()) { |
| 183 | + try { |
| 184 | + val subscription = subscriptionsManager.getSubscription() |
| 185 | + if (subscription == null) { |
| 186 | + launch(dispatcherProvider.main()) { |
| 187 | + logcat { "Switch flow: Failed to switch plans. No active subscription found" } |
| 188 | + dismiss() |
| 189 | + } |
| 190 | + return@launch |
| 191 | + } |
| 192 | + |
| 193 | + // Determine target plan based on current subscription |
| 194 | + val isUS = subscription.productId in listOf(MONTHLY_PLAN_US, YEARLY_PLAN_US) |
| 195 | + val targetPlanId = if (isUpgrade) { |
| 196 | + if (isUS) YEARLY_PLAN_US else YEARLY_PLAN_ROW |
| 197 | + } else { |
| 198 | + if (isUS) MONTHLY_PLAN_US else MONTHLY_PLAN_ROW |
| 199 | + } |
| 200 | + |
| 201 | + launch(dispatcherProvider.main()) { |
| 202 | + subscriptionsManager.switchSubscriptionPlan( |
| 203 | + activity = context as Activity, |
| 204 | + planId = targetPlanId, |
| 205 | + offerId = null, |
| 206 | + replacementMode = SubscriptionReplacementMode.WITHOUT_PRORATION, |
| 207 | + ) |
| 208 | + } |
| 209 | + } catch (e: Exception) { |
| 210 | + logcat { "Switch flow: Failed to switch plans. Exception: ${e.message}" } |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments