Skip to content

Commit

Permalink
[google-pay#109] Handle potential init exceptions and fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenoid committed Dec 30, 2024
1 parent 42017ad commit 49c63d8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") }}
}
}
}
Expand Down
72 changes: 45 additions & 27 deletions compose-pay-button/src/main/java/com/google/pay/button/PayButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
}
}
}
}
}
)
}
)
} else {
fallbackUi?.invoke()
}
}

0 comments on commit 49c63d8

Please sign in to comment.