Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#109] Handle potential init exceptions and fallback #113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
)
Comment on lines +75 to +82
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block can be broken by "Fatal Exception: java.lang.IllegalStateException" or other potential crashes requiring handling and a potential fallback.

}.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()
}
}