Skip to content

Commit fa7b64f

Browse files
committed
chore(flipcash/iap): add logging and await for connection before querying
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent e079c6c commit fa7b64f

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

apps/flipcash/features/purchase/src/main/kotlin/com/flipcash/app/purchase/internal/PurchaseAccountViewModel.kt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.flipcash.app.auth.AuthManager
88
import com.flipcash.features.purchase.BuildConfig
99
import com.flipcash.features.purchase.R
1010
import com.flipcash.services.billing.BillingClient
11+
import com.flipcash.services.billing.BillingClientState
1112
import com.flipcash.services.billing.IapPaymentEvent
1213
import com.flipcash.services.billing.IapProduct
1314
import com.getcode.manager.TopBarManager
@@ -16,9 +17,12 @@ import com.getcode.view.BaseViewModel2
1617
import com.getcode.view.LoadingSuccessState
1718
import dagger.hilt.android.lifecycle.HiltViewModel
1819
import kotlinx.coroutines.delay
20+
import kotlinx.coroutines.flow.distinctUntilChanged
21+
import kotlinx.coroutines.flow.filter
1922
import kotlinx.coroutines.flow.filterIsInstance
2023
import kotlinx.coroutines.flow.flatMapLatest
2124
import kotlinx.coroutines.flow.launchIn
25+
import kotlinx.coroutines.flow.map
2226
import kotlinx.coroutines.flow.mapNotNull
2327
import kotlinx.coroutines.flow.onEach
2428
import 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 }

services/flipcash/src/main/kotlin/com/flipcash/services/internal/billing/GooglePlayBillingClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ internal class GooglePlayBillingClient(
119119
purchases[product.productId] == PurchaseState.PURCHASED
120120

121121
override fun costOf(product: IapProduct): String {
122+
printLog("checking cost of ${product.productId} in ${productDetails.entries}")
122123
var details = productDetails[product.productId]
123124
if (details == null) {
124125
queryProduct(product)
@@ -284,6 +285,7 @@ internal class GooglePlayBillingClient(
284285
client.queryProductDetailsAsync(
285286
queryProductDetailsParams
286287
) { result, productDetailsList ->
288+
printLog("QUERY ${product.productId} ${result.debugMessage}")
287289
printLog("products for ${product.productId} = ${productDetailsList.count()}")
288290
if (productDetailsList.isNotEmpty()) {
289291
productDetails[product.productId] = productDetailsList.first()

0 commit comments

Comments
 (0)