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

Fragment Pager Adapter Injection + ViewModel + fragmentManager #121

Closed
gpulido opened this issue May 8, 2018 · 1 comment
Closed

Fragment Pager Adapter Injection + ViewModel + fragmentManager #121

gpulido opened this issue May 8, 2018 · 1 comment
Labels
question Usage question

Comments

@gpulido
Copy link

gpulido commented May 8, 2018

Hello,
I have a FragmentAdapter with two constructor parameters: fragmentManager and a ViewModel.

I would like to inject it but I don't know what is the right way to put all the pieces together to declare the adapter in the koin module to be injected

The element that I need to inject:

class KeysPagerAdapter<TKey:BaseKey>(fragmentManager:FragmentManager, var viewModel:FocusableViewModel<TKey, List<TKey>>):       FragmentStatePagerAdapter(fragmentManager) {

where FocusableViewModel is an interface:

interface FocusableViewModel<TObject, TList:List<TObject>>: ObjectListViewModel<TObject, TList>

I want to create a keysPagerAdapter with the following viewModel injected:

class AnnotationsViewModel(contenderId: String, annotationId:String, val dao: ContenderDao): ViewModel(),
        FocusableViewModel<AnnotationDetailKey, List<AnnotationDetailKey>>

This viewModel is being created without problems to feed a fragment. It is defined in the koin module

viewModel {  params -> AnnotationsViewModel(params[EXTRA_CONTENDER_ID], params[EXTRA_ANNOTATION_ID], get()) }

and it is being used in a fragment:

val viewModel
            by viewModel<AnnotationsViewModel>{
                    mapOf(
                            Properties.EXTRA_CONTENDER_ID to (getKey<AnnotationsKey>()?.id?: Utils.EmptyUUID),
                            Properties.EXTRA_ANNOTATION_ID to Utils.EmptyUUID
                                    )
            }

So I need to be able to define the bean to inject the keysadapter and I'm not sure how to define it on the koin module and how to inject it to be used in a fragment.

I've tried with this:

 bean { params -> KeysPagerAdapter(params[FRAGMENT_MANAGER], get<AnnotationsViewModel>())}

And injecting it through:

val pagerAdapter by inject<KeysPagerAdapter<AnnotationDetailKey>>{ mapOf(Properties.FRAGMENT_MANAGER to childFragmentManager)}

But this fails at it complains about missing parameters EXTRA_CONTENDER_ID and EXTRA_ANNOTATION_ID.
It makes sense as it doesn't know how to resolve, but I don't know how to define the bean or how to pass the paramenters the inject to obtain it...

@arnaudgiuliani
Copy link
Member

Hello,

If you want to use function parameters in your definition,
you just have to use the params arguments as you do. To inject parameters values, open a lambda behind by inject or by viewModel and fill a map of values.

But this is what you already done. I don't understand well where is the problem :/

Can you provide a small project to be able to better understand your case?

Koin Dev Board automation moved this from New / Waiting to Done Jun 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usage question
Projects
None yet
Development

No branches or pull requests

2 participants