How to scope a Usecase to a Feature / Activity in Koin

Viewed 161

I'm maintaining a large app (mostly) using a "one feature one activity"-architecture.

Now i'd like to scope a usecase, so it lives as long as the activity, something like this:

// koin module
scope<MyFeatureActivity> {
    viewModel { MyFeatureActivityViewModel() }
    viewModel { MyFeatureFragmentAViewModel(usecase = get()) }
    viewModel { MyFeatureFragmentBViewModel(usecase = get()) }
    scoped { MyFeatureUseCase() }
}

// fragments
class FeatureAFragment: AppCompatDialogFragment(){

    private val viewModel by viewModel<MyFeatureFragmentAViewModel>()

    ....
}

// activity
class MyFeatureActivity : ScopeActivity() { ... }

However, this doesn't work. When launching MyFeatureFragmentA from MyFeatureActivity it's throwing an Exception:

org.koin.core.error.NoBeanDefFoundException: 
|- No definition found for class:'MyFeatureAViewModel'. Check your definitions!

What am i doing wrong?

Please note: I would not like to just skip scopes and make the usecase a single (or a factory), since it actually stores some data relevant to only this activity: The data should be kept while we're in this feature, but dismissed when leaving it.

0 Answers
Related