@@ -8,6 +8,7 @@ import com.flipcash.app.auth.AuthManager
88import com.flipcash.features.purchase.BuildConfig
99import com.flipcash.features.purchase.R
1010import com.flipcash.services.billing.BillingClient
11+ import com.flipcash.services.billing.BillingClientState
1112import com.flipcash.services.billing.IapPaymentEvent
1213import com.flipcash.services.billing.IapProduct
1314import com.getcode.manager.TopBarManager
@@ -16,9 +17,12 @@ import com.getcode.view.BaseViewModel2
1617import com.getcode.view.LoadingSuccessState
1718import dagger.hilt.android.lifecycle.HiltViewModel
1819import kotlinx.coroutines.delay
20+ import kotlinx.coroutines.flow.distinctUntilChanged
21+ import kotlinx.coroutines.flow.filter
1922import kotlinx.coroutines.flow.filterIsInstance
2023import kotlinx.coroutines.flow.flatMapLatest
2124import kotlinx.coroutines.flow.launchIn
25+ import kotlinx.coroutines.flow.map
2226import kotlinx.coroutines.flow.mapNotNull
2327import kotlinx.coroutines.flow.onEach
2428import kotlinx.coroutines.launch
@@ -83,22 +87,25 @@ internal class PurchaseAccountViewModel @Inject constructor(
8387 }
8488
8589 init {
86- viewModelScope.launch {
87- val receivedWelcomeBonus =
88- billingClient.hasPaidFor(IapProduct .CreateAccountWithWelcomeBonus )
89- val (product, cost) = if (! receivedWelcomeBonus) {
90- IapProduct .CreateAccountWithWelcomeBonus to billingClient.costOf(IapProduct .CreateAccountWithWelcomeBonus )
91- } else {
92- IapProduct .CreateAccount to billingClient.costOf(IapProduct .CreateAccount )
93- }
90+ billingClient.state
91+ .filter { it == BillingClientState .Connected }
92+ .distinctUntilChanged()
93+ .onEach {
94+ val receivedWelcomeBonus =
95+ billingClient.hasPaidFor(IapProduct .CreateAccountWithWelcomeBonus )
96+ val (product, cost) = if (! receivedWelcomeBonus) {
97+ IapProduct .CreateAccountWithWelcomeBonus to billingClient.costOf(IapProduct .CreateAccountWithWelcomeBonus )
98+ } else {
99+ IapProduct .CreateAccount to billingClient.costOf(IapProduct .CreateAccount )
100+ }
94101
95- dispatchEvent(
96- Event .OnProductChanged (
97- product = product,
98- cost = cost
102+ dispatchEvent(
103+ Event .OnProductChanged (
104+ product = product,
105+ cost = cost
106+ )
99107 )
100- )
101- }
108+ }.launchIn(viewModelScope)
102109
103110 eventFlow
104111 .filterIsInstance<Event .BuyAccount >()
@@ -151,6 +158,7 @@ internal class PurchaseAccountViewModel @Inject constructor(
151158
152159 companion object {
153160 val updateStateForEvent: (Event ) -> ((State ) -> State ) = { event ->
161+ println (" PurchaseAccountViewModel.updateStateForEvent $event " )
154162 when (event) {
155163 Event .OnAccountCreated -> { state -> state }
156164 is Event .BuyAccount -> { state -> state }
0 commit comments