diff --git a/app/src/main/java/com/example/composepaybutton/MainActivity.kt b/app/src/main/java/com/example/composepaybutton/MainActivity.kt index d830c35..78769cb 100644 --- a/app/src/main/java/com/example/composepaybutton/MainActivity.kt +++ b/app/src/main/java/com/example/composepaybutton/MainActivity.kt @@ -24,6 +24,7 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.width +import androidx.compose.material.Button import androidx.compose.material.Divider import androidx.compose.material.Text import androidx.compose.ui.Alignment @@ -103,6 +104,12 @@ class MainActivity : ComponentActivity() { // Disabled buttons PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods, type = ButtonType.Checkout, enabled = false) PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods, type = ButtonType.Checkout, theme = ButtonTheme.Light, enabled = false) + + Divider(thickness = 1.dp, color = Color.LightGray) + Text("Fallback UI") + + // Fallback UI in case of init failure + PayButton(onClick = onClick, allowedPaymentMethods = allowedPaymentMethods, onError = { println("Error: $it") }) { Button(onClick = onClick) { Text("Fallback UI") }} } } } diff --git a/compose-pay-button/src/main/java/com/google/pay/button/PayButton.kt b/compose-pay-button/src/main/java/com/google/pay/button/PayButton.kt index dee3c9a..c493fd0 100644 --- a/compose-pay-button/src/main/java/com/google/pay/button/PayButton.kt +++ b/compose-pay-button/src/main/java/com/google/pay/button/PayButton.kt @@ -17,15 +17,18 @@ package com.google.pay.button import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView - +import com.google.android.gms.wallet.button.ButtonConstants import com.google.android.gms.wallet.button.ButtonOptions import com.google.android.gms.wallet.button.PayButton as GmsPayButton -import com.google.android.gms.wallet.button.ButtonConstants enum class ButtonTheme(val value: Int) { Dark(ButtonConstants.ButtonTheme.DARK), @@ -55,35 +58,50 @@ fun PayButton( type: ButtonType = ButtonType.Buy, radius: Dp = 100.dp, enabled: Boolean = true, + onError: (Throwable) -> Unit = {}, + fallbackUi: @Composable (() -> Unit)? = null, ) { + var showFallback by remember { mutableStateOf(false) } + val radiusPixelValue = with(LocalDensity.current) { radius.toPx().toInt() } - AndroidView( - modifier = modifier, - factory = { context -> - GmsPayButton(context).apply { - this.initialize( - ButtonOptions.newBuilder() - .setButtonTheme(theme.value) - .setButtonType(type.value) - .setCornerRadius(radiusPixelValue) - .setAllowedPaymentMethods(allowedPaymentMethods) - .build() - ) - } - }, - update = { button -> - button.apply { - alpha = if (enabled) FULL_ALPHA else HALF_ALPHA - isEnabled = enabled + if (!showFallback) { + AndroidView( + modifier = modifier, + factory = { context -> + GmsPayButton(context).apply { + kotlin.runCatching { + this.initialize( + ButtonOptions.newBuilder() + .setButtonTheme(theme.value) + .setButtonType(type.value) + .setCornerRadius(radiusPixelValue) + .setAllowedPaymentMethods(allowedPaymentMethods) + .build() + ) + }.onFailure { + onError(it) + showFallback = true + } + } + }, + update = { button -> + if (!showFallback) { + button.apply { + alpha = if (enabled) FULL_ALPHA else HALF_ALPHA + isEnabled = enabled - if (enabled) { - setOnClickListener { onClick() } - } else { - setOnClickListener(null) + if (enabled) { + setOnClickListener { onClick() } + } else { + setOnClickListener(null) + } + } } } - } - ) -} \ No newline at end of file + ) + } else { + fallbackUi?.invoke() + } +}