-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b2b08e
commit a23b51f
Showing
13 changed files
with
399 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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 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
105 changes: 105 additions & 0 deletions
105
...orizontalsystems/bankwallet/modules/usersubscription/BuySubscriptionChoosePlanFragment.kt
This file contains 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,105 @@ | ||
package io.horizontalsystems.bankwallet.modules.usersubscription | ||
|
||
import android.os.Parcelable | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.compose.ui.unit.dp | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.NavController | ||
import io.horizontalsystems.bankwallet.core.BaseComposeFragment | ||
import io.horizontalsystems.bankwallet.core.requireInput | ||
import io.horizontalsystems.bankwallet.core.setNavigationResultX | ||
import io.horizontalsystems.bankwallet.ui.compose.ComposeAppTheme | ||
import io.horizontalsystems.bankwallet.ui.compose.components.AppBar | ||
import io.horizontalsystems.bankwallet.ui.compose.components.ButtonPrimaryDefault | ||
import io.horizontalsystems.bankwallet.ui.compose.components.HsBackButton | ||
import io.horizontalsystems.bankwallet.ui.compose.components.TextImportantWarning | ||
import io.horizontalsystems.bankwallet.ui.compose.components.VSpacer | ||
import io.horizontalsystems.core.SnackbarDuration | ||
import io.horizontalsystems.core.helpers.HudHelper | ||
import io.horizontalsystems.subscriptions.core.BasePlan | ||
import kotlinx.coroutines.delay | ||
import kotlinx.parcelize.Parcelize | ||
|
||
class BuySubscriptionChoosePlanFragment : BaseComposeFragment() { | ||
|
||
@Composable | ||
override fun GetContent(navController: NavController) { | ||
BuySubscriptionChoosePlanScreen(navController, requireActivity(), navController.requireInput()) | ||
} | ||
|
||
@Parcelize | ||
data class Input(val subscriptionId: String) : Parcelable | ||
|
||
@Parcelize | ||
class Result : Parcelable | ||
} | ||
|
||
@Composable | ||
fun BuySubscriptionChoosePlanScreen( | ||
navController: NavController, | ||
activity: FragmentActivity, | ||
input: BuySubscriptionChoosePlanFragment.Input | ||
) { | ||
val viewModel = viewModel<BuySubscriptionChoosePlanViewModel>( | ||
factory = BuySubscriptionChoosePlanViewModel.Factory(input.subscriptionId) | ||
) | ||
|
||
val view = LocalView.current | ||
|
||
val uiState = viewModel.uiState | ||
|
||
LaunchedEffect(uiState.purchase) { | ||
uiState.purchase?.let { purchase -> | ||
HudHelper.showSuccessMessage(view, purchase.toString(), SnackbarDuration.LONG) | ||
|
||
delay(300) | ||
|
||
navController.setNavigationResultX(BuySubscriptionChoosePlanFragment.Result()) | ||
navController.popBackStack() | ||
} | ||
} | ||
|
||
Scaffold( | ||
backgroundColor = ComposeAppTheme.colors.tyler, | ||
topBar = { | ||
AppBar( | ||
title = "Subscriptions", | ||
navigationIcon = { | ||
HsBackButton(onClick = { navController.popBackStack() }) | ||
}, | ||
) | ||
} | ||
) { | ||
Column(modifier = Modifier.padding(it)) { | ||
uiState.basePlans.forEach { basePlan -> | ||
ButtonPrimaryDefault( | ||
modifier = Modifier.fillMaxWidth(), | ||
title = basePlan.stringRepresentation(), | ||
onClick = { | ||
viewModel.launchPurchaseFlow(basePlan.id, activity) | ||
}, | ||
enabled = uiState.choosePlanEnabled | ||
) | ||
VSpacer(height = 12.dp) | ||
} | ||
|
||
uiState.error?.let { | ||
TextImportantWarning(text = it.message ?: it.javaClass.name) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun BasePlan.stringRepresentation(): String { | ||
return pricingPhases.map { | ||
"${it.formattedPrice}/${it.billingPeriod}" | ||
}.joinToString(" then ") | ||
} |
69 changes: 69 additions & 0 deletions
69
...rizontalsystems/bankwallet/modules/usersubscription/BuySubscriptionChoosePlanViewModel.kt
This file contains 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,69 @@ | ||
package io.horizontalsystems.bankwallet.modules.usersubscription | ||
|
||
import android.app.Activity | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import io.horizontalsystems.bankwallet.core.ViewModelUiState | ||
import io.horizontalsystems.subscriptions.core.BasePlan | ||
import io.horizontalsystems.subscriptions.core.HSPurchase | ||
import io.horizontalsystems.subscriptions.core.UserSubscriptionManager | ||
import kotlinx.coroutines.launch | ||
|
||
class BuySubscriptionChoosePlanViewModel(private val subscriptionId: String) : ViewModelUiState<BuySubscriptionChoosePlanUiState>() { | ||
private var basePlans: List<BasePlan> = listOf() | ||
private var purchaseInProgress = false | ||
private var purchase: HSPurchase? = null | ||
private var error: Throwable? = null | ||
|
||
init { | ||
viewModelScope.launch { | ||
basePlans = UserSubscriptionManager.getBasePlans(subscriptionId) | ||
|
||
emitState() | ||
} | ||
} | ||
|
||
override fun createState() = BuySubscriptionChoosePlanUiState( | ||
basePlans = basePlans, | ||
purchaseInProgress = purchaseInProgress, | ||
error = error, | ||
purchase = purchase | ||
) | ||
|
||
fun launchPurchaseFlow(planId: String, activity: Activity) { | ||
purchaseInProgress = true | ||
error = null | ||
emitState() | ||
|
||
viewModelScope.launch { | ||
try { | ||
val hsPurchase = | ||
UserSubscriptionManager.launchPurchaseFlow(subscriptionId, planId, activity) | ||
|
||
purchase = hsPurchase | ||
} catch (e: Throwable) { | ||
error = e | ||
} | ||
|
||
purchaseInProgress = false | ||
emitState() | ||
} | ||
} | ||
|
||
class Factory(private val subscriptionId: String) : ViewModelProvider.Factory { | ||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return BuySubscriptionChoosePlanViewModel(subscriptionId) as T | ||
} | ||
} | ||
} | ||
|
||
data class BuySubscriptionChoosePlanUiState( | ||
val basePlans: List<BasePlan>, | ||
val purchaseInProgress: Boolean, | ||
val error: Throwable?, | ||
val purchase: HSPurchase? | ||
) { | ||
val choosePlanEnabled = !purchaseInProgress | ||
} |
This file contains 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
23 changes: 10 additions & 13 deletions
23
...java/io/horizontalsystems/bankwallet/modules/usersubscription/BuySubscriptionViewModel.kt
This file contains 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,30 +1,27 @@ | ||
package io.horizontalsystems.bankwallet.modules.usersubscription | ||
|
||
import android.app.Activity | ||
import androidx.lifecycle.viewModelScope | ||
import io.horizontalsystems.bankwallet.core.ViewModelUiState | ||
import io.horizontalsystems.subscriptions.core.SubscriptionPlan | ||
import io.horizontalsystems.subscriptions.core.Subscription | ||
import io.horizontalsystems.subscriptions.core.UserSubscriptionManager | ||
import kotlinx.coroutines.launch | ||
|
||
class BuySubscriptionViewModel : ViewModelUiState<BuySubscriptionUiState>() { | ||
private var plans = listOf<SubscriptionPlan>() | ||
|
||
override fun createState() = BuySubscriptionUiState( | ||
plans = plans | ||
) | ||
|
||
fun launchPurchaseFlow(planId: String, activity: Activity) { | ||
UserSubscriptionManager.launchPurchaseFlow(planId, activity) | ||
} | ||
private var subscriptions = listOf<Subscription>() | ||
|
||
init { | ||
viewModelScope.launch { | ||
plans = UserSubscriptionManager.getPlans() | ||
subscriptions = UserSubscriptionManager.getSubscriptions() | ||
|
||
emitState() | ||
} | ||
} | ||
|
||
override fun createState() = BuySubscriptionUiState( | ||
subscriptions = subscriptions | ||
) | ||
} | ||
|
||
data class BuySubscriptionUiState(val plans: List<SubscriptionPlan>) | ||
data class BuySubscriptionUiState( | ||
val subscriptions: List<Subscription> | ||
) |
This file contains 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
14 changes: 14 additions & 0 deletions
14
subscriptions-core/src/main/java/io/horizontalsystems/subscriptions/core/HSPurchase.kt
This file contains 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,14 @@ | ||
package io.horizontalsystems.subscriptions.core | ||
|
||
data class HSPurchase( | ||
val status: Status | ||
) { | ||
enum class Status { | ||
Pending, Purchased | ||
} | ||
} | ||
|
||
data class HSPurchaseFailure( | ||
val code: String, | ||
override val message: String, | ||
) : Exception() |
10 changes: 10 additions & 0 deletions
10
subscriptions-core/src/main/java/io/horizontalsystems/subscriptions/core/Subscription.kt
This file contains 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 io.horizontalsystems.subscriptions.core | ||
|
||
data class Subscription(val id: String, val name: String, val description: String) | ||
|
||
data class BasePlan( | ||
val id: String, | ||
val pricingPhases: List<PricingPhase>, | ||
) | ||
|
||
data class PricingPhase(val formattedPrice: String, val billingPeriod: String) |
3 changes: 0 additions & 3 deletions
3
subscriptions-core/src/main/java/io/horizontalsystems/subscriptions/core/SubscriptionPlan.kt
This file was deleted.
Oops, something went wrong.
This file contains 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 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.