Skip to content

Commit

Permalink
fix: allow to cancell hCaptcha with back button
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Mar 3, 2025
1 parent a173a1a commit b5d9e7f
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions compose-sdk/src/main/java/com/hcaptcha/sdk/HCaptchaCompose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ package com.hcaptcha.sdk

import android.os.Handler
import android.os.Looper
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties

@Composable
public fun HCaptchaCompose(config: HCaptchaConfig, onResult: (HCaptchaResponse) -> Unit) {
Expand All @@ -29,25 +36,44 @@ public fun HCaptchaCompose(config: HCaptchaConfig, onResult: (HCaptchaResponse)
helper.value = HCaptchaWebViewHelper(
handler, context, config, internalConfig, verifier, this
)

}
}
var dismissed by remember { mutableStateOf(false) }

HCaptchaLog.d("HCaptchaCompose($config)")

DisposableEffect(dismissed) {
onDispose {
if (dismissed) {
verifier.onFailure(HCaptchaException(HCaptchaError.CHALLENGE_CLOSED));
helper.value?.destroy()
}
}
}

if (config.hideDialog) {
AndroidView(
modifier = Modifier.size(0.dp),
factory = { preloadedWebView }
)
} else {
} else if (!dismissed) {
Dialog(
onDismissRequest = {},
properties = DialogProperties(usePlatformDefaultWidth = false)
onDismissRequest = { dismissed = true }
) {
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { preloadedWebView }
)
Column(
modifier = Modifier.clickable(
interactionSource = MutableInteractionSource(),
indication = null,
onClick = { dismissed = true }
).fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
AndroidView(
factory = { preloadedWebView }
)
}
}
}
}

0 comments on commit b5d9e7f

Please sign in to comment.