hcaptcha-android-sdk icon indicating copy to clipboard operation
hcaptcha-android-sdk copied to clipboard

Unable to get client without fragment activity context

Open marius-zilinskas-tg opened this issue 1 year ago • 11 comments

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 avatar May 02 '23 09:05 marius-zilinskas-tg

@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?

vladd-g avatar May 02 '23 16:05 vladd-g

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.

marius-zilinskas-tg avatar May 03 '23 04:05 marius-zilinskas-tg

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 avatar May 03 '23 05:05 CAMOBAP

@CAMOBAP Yes, your proposed solution looks nice

marius-zilinskas-tg avatar May 03 '23 05:05 marius-zilinskas-tg

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 avatar May 29 '23 13:05 gilsonjuniorpro

@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

CAMOBAP avatar May 29 '23 13:05 CAMOBAP

Thank you, so using any version below 3.9.0 it works with hilt without asking for the FragmentActivity

gilsonjuniorpro avatar May 29 '23 13:05 gilsonjuniorpro

@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.

CAMOBAP avatar May 29 '23 14:05 CAMOBAP

Yeah, make sense, thanks for your help

gilsonjuniorpro avatar May 29 '23 14:05 gilsonjuniorpro

Are there any updates regarding this? How does one use it in Compose without an Activity?

Sophon avatar Jan 22 '24 09:01 Sophon

@Sophon no update yet, work in progress

CAMOBAP avatar Feb 07 '24 11:02 CAMOBAP

Now we support JetPack Compose.

More details in https://github.com/hCaptcha/hcaptcha-android-sdk?tab=readme-ov-file#installation

Any feedback is appreciated

CAMOBAP avatar Jul 02 '24 07:07 CAMOBAP