Skip to content

Commit

Permalink
Merge pull request #113 from mohsenoid/fix/#109-fallback-on-init-exce…
Browse files Browse the repository at this point in the history
…ptions

[#109] Handle potential init exceptions and fallback
  • Loading branch information
dmengelt authored Jan 6, 2025
2 parents 42017ad + 0678b26 commit f30af8d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 27 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,12 @@ class MainActivity : ComponentActivity() {
}
}
```

## Error handling and Fallback UI

The `PayButton` composable wraps the underlying `PayButton` Android View, which may encounter errors during initialization. To handle these situations, the `PayButton` composable provides:

* **`onError` Callback:** Invoked when an error occurs during the button's initialization. This callback receives the `Throwable` that caused the error, allowing you to log it or take other actions.
* **`fallbackUi` Composable:** An optional composable function that is displayed in place of the button if an error occurs. If not provided, nothing will be displayed in case of error.

This mechanism ensures that your app can gracefully handle potential issues with the `PayButton` and provide a fallback experience to the user.
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 f30af8d

Please sign in to comment.