Skip to content

Commit

Permalink
chore: replace DisposableEffect with Dialog+AndroidView
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Jun 3, 2024
1 parent eff4d65 commit 113ff4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
4 changes: 1 addition & 3 deletions compose-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ android {

dependencies {
api project(':sdk')
implementation "androidx.activity:activity-compose:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation 'androidx.compose.material3:material3:1.2.1'
implementation "androidx.compose.foundation:foundation:$compose_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
32 changes: 19 additions & 13 deletions compose-sdk/src/main/java/com/hcaptcha/sdk/HCaptchaCompose.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.hcaptcha.sdk

import android.app.Activity
import android.app.Dialog
import android.os.Handler
import android.os.Looper
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.Modifier
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) {
val handler = Handler(Looper.getMainLooper())
val context = LocalContext.current
val verifier = object : IHCaptchaVerifier {
override fun onLoaded() {
onResult(HCaptchaResponse.Event(HCaptchaEvent.Loaded))
Expand Down Expand Up @@ -39,16 +40,21 @@ public fun HCaptchaCompose(config: HCaptchaConfig, onResult: (HCaptchaResponse)
}
val internalConfig = HCaptchaInternalConfig(com.hcaptcha.sdk.HCaptchaHtml())

DisposableEffect(context) {
val dialog = Dialog(context)
dialog.setContentView(
HCaptchaWebView(context).apply {
HCaptchaWebViewHelper(handler, context, config, internalConfig, verifier, this)
Dialog(onDismissRequest = {}, properties = DialogProperties(usePlatformDefaultWidth = false)) {
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
HCaptchaWebView(context).apply {
HCaptchaWebViewHelper(
handler,
context,
config,
internalConfig,
verifier,
this
)
}
}
)
dialog.show()
onDispose {
dialog.dismiss()
}
}
}

0 comments on commit 113ff4b

Please sign in to comment.