From 1c4198a505444f3c3e47857a8c24c5fa1dcbd060 Mon Sep 17 00:00:00 2001 From: Alexande B Date: Mon, 3 Jun 2024 18:22:29 +0200 Subject: [PATCH] chore: add loading indicator to example-compose-app --- .../example/compose/ComposeActivity.kt | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/example-compose-app/src/main/java/com/hcaptcha/example/compose/ComposeActivity.kt b/example-compose-app/src/main/java/com/hcaptcha/example/compose/ComposeActivity.kt index fee6e93..95fb285 100644 --- a/example-compose-app/src/main/java/com/hcaptcha/example/compose/ComposeActivity.kt +++ b/example-compose-app/src/main/java/com/hcaptcha/example/compose/ComposeActivity.kt @@ -8,13 +8,17 @@ import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.Arrangement import androidx.compose.runtime.* import androidx.compose.material3.Button +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextField +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import com.hcaptcha.sdk.HCaptchaCompose import com.hcaptcha.sdk.HCaptchaConfig +import com.hcaptcha.sdk.HCaptchaEvent import com.hcaptcha.sdk.HCaptchaResponse class ComposeActivity : ComponentActivity() { @@ -22,7 +26,8 @@ class ComposeActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { - var hCaptchaVisible by remember { mutableStateOf(false) } + var hCaptchaStarted by remember { mutableStateOf(false) } + var hCaptchaLoaded by remember { mutableStateOf(false) } var text by remember { mutableStateOf("") } Column( @@ -39,11 +44,9 @@ class ComposeActivity : ComponentActivity() { .background(Color.Gray) ) - // Button to toggle WebView visibility Button( onClick = { - hCaptchaVisible = !hCaptchaVisible - // Additional logic when the button is clicked + hCaptchaStarted = !hCaptchaStarted }, modifier = Modifier .fillMaxWidth() @@ -52,8 +55,21 @@ class ComposeActivity : ComponentActivity() { Text(text = "Toggle WebView") } + if (hCaptchaStarted && !hCaptchaLoaded) { + Box( + contentAlignment = Alignment.Center, + modifier = Modifier.fillMaxSize() + ) { + CircularProgressIndicator( + modifier = Modifier.width(64.dp), + color = MaterialTheme.colorScheme.secondary, + trackColor = MaterialTheme.colorScheme.surfaceVariant, + ) + } + } + // WebView Dialog - if (hCaptchaVisible) { + if (hCaptchaStarted) { HCaptchaCompose(HCaptchaConfig .builder() .siteKey("10000000-ffff-ffff-ffff-000000000001") @@ -61,15 +77,20 @@ class ComposeActivity : ComponentActivity() { when (result) { is HCaptchaResponse.Success -> { text = "Success: ${result.token}" - hCaptchaVisible = false + hCaptchaStarted = false + hCaptchaLoaded = false println(text) } is HCaptchaResponse.Failure -> { - hCaptchaVisible = false + hCaptchaStarted = false + hCaptchaLoaded = false text = "Failure: ${result.error.message}" println(text) } is HCaptchaResponse.Event -> { + if (result.event == HCaptchaEvent.Opened) { + hCaptchaLoaded = true; + } println("Event: ${result.event}") } }