Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get client without fragment activity context #112

Open
marius-zilinskas-tg opened this issue May 2, 2023 · 11 comments
Open

Unable to get client without fragment activity context #112

marius-zilinskas-tg opened this issue May 2, 2023 · 11 comments
Assignees

Comments

@marius-zilinskas-tg
Copy link

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?

@vladd-g
Copy link
Contributor

vladd-g commented May 2, 2023

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

@marius-zilinskas-tg
Copy link
Author

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.

@CAMOBAP
Copy link
Collaborator

CAMOBAP commented May 3, 2023

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?

@marius-zilinskas-tg
Copy link
Author

@CAMOBAP Yes, your proposed solution looks nice

@gilsonjuniorpro
Copy link

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)
    }
}

@CAMOBAP
Copy link
Collaborator

CAMOBAP commented May 29, 2023

@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

@gilsonjuniorpro
Copy link

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

@CAMOBAP
Copy link
Collaborator

CAMOBAP commented May 29, 2023

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

@gilsonjuniorpro
Copy link

Yeah, make sense, thanks for your help

@Sophon
Copy link

Sophon commented Jan 22, 2024

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

@CAMOBAP
Copy link
Collaborator

CAMOBAP commented Feb 7, 2024

@Sophon no update yet, work in progress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants