Skip to content

Commit 7f8da09

Browse files
committed
fix(flipcash/iap): make GPBC a singleton; await for connection
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent bf21048 commit 7f8da09

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import com.getcode.navigation.core.LocalCodeNavigator
3737
import com.getcode.theme.CodeTheme
3838
import com.getcode.ui.theme.ButtonState
3939
import com.getcode.ui.theme.CodeButton
40+
import com.getcode.ui.theme.CodeCircularProgressIndicator
4041
import com.getcode.ui.theme.CodeScaffold
4142
import com.getcode.util.getActivity
4243
import com.getcode.util.permissions.LocalPermissionChecker
@@ -86,6 +87,7 @@ internal fun PurchaseAccountScreenContent(viewModel: PurchaseAccountViewModel) {
8687
modifier = Modifier
8788
.fillMaxWidth(),
8889
buttonState = ButtonState.Filled,
90+
enabled = state.hasProduct,
8991
isLoading = state.creatingAccount.loading,
9092
isSuccess = state.creatingAccount.success,
9193
text = stringResource(R.string.action_purchaseAccount),
@@ -142,20 +144,24 @@ internal fun PurchaseAccountScreenContent(viewModel: PurchaseAccountViewModel) {
142144
contentDescription = null,
143145
)
144146

145-
Text(
146-
modifier = Modifier.padding(top = CodeTheme.dimens.grid.x10),
147-
text = state.title,
148-
style = CodeTheme.typography.textLarge,
149-
textAlign = TextAlign.Center,
150-
color = CodeTheme.colors.textMain
151-
)
147+
if (state.hasProduct) {
148+
Text(
149+
modifier = Modifier.padding(top = CodeTheme.dimens.grid.x10),
150+
text = state.title,
151+
style = CodeTheme.typography.textLarge,
152+
textAlign = TextAlign.Center,
153+
color = CodeTheme.colors.textMain
154+
)
152155

153-
Text(
154-
text = state.subtitle,
155-
style = CodeTheme.typography.textMedium,
156-
textAlign = TextAlign.Center,
157-
color = CodeTheme.colors.textSecondary
158-
)
156+
Text(
157+
text = state.subtitle,
158+
style = CodeTheme.typography.textMedium,
159+
textAlign = TextAlign.Center,
160+
color = CodeTheme.colors.textSecondary
161+
)
162+
} else {
163+
CodeCircularProgressIndicator()
164+
}
159165
}
160166
}
161167
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ internal class PurchaseAccountViewModel @Inject constructor(
4343
private val costOfAccount: String = "",
4444
val creatingAccount: LoadingSuccessState = LoadingSuccessState(),
4545
) {
46+
val hasProduct: Boolean
47+
get() = productToBuy != null
48+
4649
private val receivedWelcomeBonus
4750
get() = productToBuy != IapProduct.CreateAccountWithWelcomeBonus
4851

@@ -89,7 +92,6 @@ internal class PurchaseAccountViewModel @Inject constructor(
8992
init {
9093
billingClient.state
9194
.filter { it == BillingClientState.Connected }
92-
.distinctUntilChanged()
9395
.onEach {
9496
val receivedWelcomeBonus =
9597
billingClient.hasPaidFor(IapProduct.CreateAccountWithWelcomeBonus)

services/flipcash/src/main/kotlin/com/flipcash/services/inject/FlipcashModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ internal object FlipcashModule {
102102
): PushRepository = InternalPushRepository(service)
103103

104104
@Provides
105+
@Singleton
105106
internal fun providesBillingClient(
106107
@ApplicationContext context: Context,
107108
repository: PurchaseRepository,

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import com.flipcash.services.internal.model.billing.IapMetadata
2929
import com.flipcash.services.internal.model.billing.Receipt
3030
import com.flipcash.services.repository.PurchaseRepository
3131
import com.flipcash.services.user.UserManager
32-
import com.getcode.opencode.model.core.uuid
3332
import com.getcode.utils.TraceType
3433
import com.getcode.utils.trace
3534
import com.google.common.collect.ImmutableList
@@ -67,12 +66,8 @@ internal class GooglePlayBillingClient(
6766
override val eventFlow: SharedFlow<IapPaymentEvent> = _eventFlow.asSharedFlow()
6867

6968
private val _stateFlow = MutableStateFlow(BillingClientState.Disconnected)
70-
override val state: StateFlow<BillingClientState> = _stateFlow.asStateFlow()
71-
72-
data class State(
73-
val connected: Boolean = false,
74-
val failedToConnect: Boolean = false,
75-
)
69+
override val state: StateFlow<BillingClientState>
70+
get() = _stateFlow.asStateFlow()
7671

7772
private val client = GooglePlayBillingClient.newBuilder(context)
7873
.setListener(this)
@@ -179,7 +174,7 @@ internal class GooglePlayBillingClient(
179174

180175
val details = productDetails[item.products.first()]
181176
val price = details?.oneTimePurchaseOfferDetails?.priceAmountMicros
182-
?.let { priceMicros -> priceMicros / 1_000_000.0 }?.toFloat() ?: 0.0f
177+
?.let { priceMicros -> priceMicros / 1_000_000.0 } ?: 0.0
183178
val currency = details?.oneTimePurchaseOfferDetails?.priceCurrencyCode ?: "USD"
184179

185180
purchaseRepository.onPurchaseCompleted(

0 commit comments

Comments
 (0)