@@ -11,7 +11,10 @@ import com.flipcash.services.billing.BillingClient
1111import com.flipcash.services.billing.BillingClientState
1212import com.flipcash.services.billing.IapPaymentEvent
1313import com.flipcash.services.billing.IapProduct
14+ import com.flipcash.services.billing.ProductPrice
1415import com.getcode.manager.TopBarManager
16+ import com.getcode.opencode.model.financial.CurrencyCode
17+ import com.getcode.opencode.model.financial.Fiat
1518import com.getcode.util.resources.ResourceHelper
1619import com.getcode.view.BaseViewModel2
1720import com.getcode.view.LoadingSuccessState
@@ -40,7 +43,8 @@ internal class PurchaseAccountViewModel @Inject constructor(
4043) {
4144 data class State (
4245 internal val productToBuy : IapProduct ? = null ,
43- private val costOfAccount : String = " " ,
46+ internal val costOfAccount : ProductPrice ? = null ,
47+ private val formattedCost : String = " " ,
4448 val creatingAccount : LoadingSuccessState = LoadingSuccessState (),
4549 ) {
4650 val hasProduct: Boolean
@@ -50,10 +54,10 @@ internal class PurchaseAccountViewModel @Inject constructor(
5054 get() = productToBuy != IapProduct .CreateAccountWithWelcomeBonus
5155
5256 private val safeCost: String
53- get() = costOfAccount .trim().takeIf { it.isNotEmpty() } ? : if (BuildConfig .DEBUG ) " 💰💰💰" else " \$ XX"
57+ get() = formattedCost .trim().takeIf { it.isNotEmpty() } ? : if (BuildConfig .DEBUG ) " 💰💰💰" else " \$ XX"
5458
5559 private val safeReward: String
56- get() = costOfAccount .trim().takeIf { it.isNotEmpty() } ? : if (BuildConfig .DEBUG ) " ¯\\ _(ツ)_/¯" else " \$ XX"
60+ get() = formattedCost .trim().takeIf { it.isNotEmpty() } ? : if (BuildConfig .DEBUG ) " ¯\\ _(ツ)_/¯" else " \$ XX"
5761
5862 private val titleForWelcomeBonus: String
5963 @Composable get() = stringResource(R .string.title_finalizeAccountCreationWithWelcomeBonus, safeCost, safeReward)
@@ -83,7 +87,8 @@ internal class PurchaseAccountViewModel @Inject constructor(
8387 }
8488
8589 sealed interface Event {
86- data class OnProductChanged (val product : IapProduct , val cost : String ) : Event
90+ data class OnProductChanged (val product : IapProduct , val cost : ProductPrice ? ) : Event
91+ data class OnPriceFormatted (val cost : String ): Event
8792 data class BuyAccount (val activity : Activity ) : Event
8893 data class OnCreatingChanged (val creating : Boolean , val created : Boolean = false ) : Event
8994 data object OnAccountCreated : Event
@@ -109,6 +114,13 @@ internal class PurchaseAccountViewModel @Inject constructor(
109114 )
110115 }.launchIn(viewModelScope)
111116
117+ stateFlow
118+ .mapNotNull { it.costOfAccount }
119+ .map { it.amount to (CurrencyCode .tryValueOf(it.currency) ? : CurrencyCode .USD ) }
120+ .map { (amount, currency) -> Fiat (amount, currency) }
121+ .onEach { dispatchEvent(Event .OnPriceFormatted (it.formatted(truncated = true ))) }
122+ .launchIn(viewModelScope)
123+
112124 eventFlow
113125 .filterIsInstance<Event .BuyAccount >()
114126 .mapNotNull {
@@ -160,14 +172,15 @@ internal class PurchaseAccountViewModel @Inject constructor(
160172
161173 companion object {
162174 val updateStateForEvent: (Event ) -> ((State ) -> State ) = { event ->
163- println (" PurchaseAccountViewModel.updateStateForEvent $event " )
164175 when (event) {
165176 Event .OnAccountCreated -> { state -> state }
166177 is Event .BuyAccount -> { state -> state }
167178 is Event .OnProductChanged -> { state ->
168179 state.copy(productToBuy = event.product, costOfAccount = event.cost)
169180 }
170181
182+ is Event .OnPriceFormatted -> { state -> state.copy(formattedCost = event.cost) }
183+
171184 is Event .OnCreatingChanged -> { state ->
172185 state.copy(
173186 creatingAccount = LoadingSuccessState (
0 commit comments