dropbox / kaiken

User scoping library for Android applications.
Apache License 2.0
69 stars 11 forks source link

Support findInjector in fragment chain #57

Open tsenggordon opened 2 years ago

tsenggordon commented 2 years ago

I have a feature that utilize Android navigation. The activity implements the getInjectorFactory() to provide the component for all the fragments in the nav_graph. However, we have another project that requires the logic to be condense in a Fragment instead of Activity, and the fragment owns the nav_graph. Under current implementation, Fragment.findInjector() only checks the fragment itself, and its parent activity, but not the fragment chain. I would like to know if there is an workaround to have the OWNER fragment implements getInjectorFactory() like the activity does, instead of having to implement InjectorHolder for every single fragment in the graph (It is also inefficient to do so, components are re-created, and if we have to pass fields in the component factory, it is very hard to pass those arguments.) @rodgavila do you know any workaround?

rodgavila commented 2 years ago

Hey @tsenggordon. My guess is that currently that intermediate fragment is an AuthAwareFragment. What you'll need to do is have that intermediate fragment be either an AuthRequiredFragment or an AuthOptionalFragment that will create a new scope under it:

https://github.com/dropbox/kaiken/blob/37d279723bdc689a4e5cf3f52412523025115e61/scoping/src/main/java/com/dropbox/kaiken/scoping/AuthAwareFragment.kt#L12

Let me know if this works.

tsenggordon commented 2 years ago

@rodgavila, sorry I missed one important detail, is that the parent Activity is not a kaiken supported activity (It is an AppCompatActivity), is it still possible the make the change you suggested? If not, is there a way to do user scoping without having the parent activity being KaikenActivitiy?

rodgavila commented 2 years ago

The parent activity will need to be a Kaiken activity, no way around that. Is there an issue preventing you from ch as find that to be a Kaiken activity? It should be a very minimal change. And ideally all Activities should eventually be Kaiken activities.

matt-ramotar commented 2 years ago

Hey @rodgavila and @tsenggordon - The parent activity is our main activity. It implements AuthOptionalActivity. I changed the scoping of the intermediate fragments to implement AuthRequiredNavHostFragment and AuthRequiredFragment. However I still get error that Fragment does not implement InjectorHolder and neither its parent activity. Thoughts?

rodgavila commented 2 years ago

Hi @matt-ramotar. Could further elaborate on the class structure? Who extends from what? Just to make sure we're on the same page.

Is the AuthRequiredFragment contained inside the AuthRequiredNavHostFragment? That would seem off. Be cause the contained fragment could simply inherit it's auth required scope from the parent AuthRequiredNavHostFragment. Changing it to an AuthAwareFragment would help.

matt-ramotar commented 2 years ago

Yeah of course. Overview below. Thanks for taking a look. Really appreciate it

Our IAP flow can be launched from MainActivity and IapActivity:

When IAP flow is launched from MainActivity:

Exception:

rallat commented 2 years ago

The main activity that hosts the fragments needs to be an InjectorHolder of the component otherwise, it won't find the component.