hcaptcha-android-sdk
hcaptcha-android-sdk copied to clipboard
Unable to get client without fragment activity context
HCaptcha.getClient(context) casts context to FragmentActivity. If application is written with Jetpack Compose - fragment activity can not be provided.
How to get HCaptcha client when fragment activity context can not be provided?
@marius-zilinskas-tg I had the same problem and simply changed the parent to FragmentActivity, and everything seems to work ok. Did you experience any problems with this approach?
It does solve the problem, however, I think this should be addressed from the SDK and not the client. I would like to avoid using FragmentActivity as a parent just because of one SDK.
According to my vision/understanding, for Jetpack API it will be nice if SDK exposes @Composable fun HCaptchaDialog(...)
.
So it can be used like this
@Composable
fun Content(showCaptcha: Boolean) {
...
if (showshowCaptcha) {
HCaptchaDialog(onSuccess = {
...
}, onFailure = {
...
}) { state -> // state for 'loading', 'ready'
... // custom loader can be presented here
}
}
...
}
@marius-zilinskas-tg @vladd-g could you please share your vision, how is the perfect integration looks for you?
@CAMOBAP Yes, your proposed solution looks nice
I am having the same problem when I tried with Hilt, please take a look to the code below
@Module
@InstallIn(ActivityComponent::class)
object HCaptchaModule {
private const val TOKEN_EXPIRATION_SECONDS = 120L
@ActivityScoped
@Provides
fun provideHCaptchaClient(@ActivityContext context: Context): HCaptcha {
val config = HCaptchaConfig.builder()
.siteKey(SITE_KEY)
.size(HCaptchaSize.NORMAL)
.loading(true)
.hideDialog(false)
.tokenExpiration(TOKEN_EXPIRATION_SECONDS)
.diagnosticLog(io.compassdigital.ca.base.BuildConfig.DEBUG)
.retryPredicate { config: HCaptchaConfig?, exception: HCaptchaException -> exception.hCaptchaError == HCaptchaError.SESSION_TIMEOUT }
.build()
return HCaptcha.getClient(context).setup(config)
}
}
@gilsonjuniorpro Unfortunately SDK based on DialogFragment
rely on FragmentActivity
to work correctly.
We have a dedicated issue to allow/research the possibility not to force our users to switch to FragmentActivity
Thank you, so using any version below 3.9.0 it works with hilt without asking for the FragmentActivity
@gilsonjuniorpro it works only because you actually pass FragmentActivity or Activity that extends it.
In SDKs below 3.9.0 there is unsafe cast (FragmentActivity) context
.
This is why we decided to make them explicit in API to avoid confusion.
Yeah, make sense, thanks for your help
Are there any updates regarding this? How does one use it in Compose without an Activity?
@Sophon no update yet, work in progress
Now we support JetPack Compose
.
More details in https://github.com/hCaptcha/hcaptcha-android-sdk?tab=readme-ov-file#installation
Any feedback is appreciated