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

Hilt integration #13

Merged
merged 3 commits into from
Oct 6, 2021
Merged

Hilt integration #13

merged 3 commits into from
Oct 6, 2021

Conversation

programadorthi
Copy link
Collaborator

@programadorthi programadorthi commented Sep 27, 2021

Adding Hilt support #11 . For now the support are using Multibinding to have a flexible approach. There is a basic support to Assisted Injection and to reuse your existing HiltViewModel too. HIltViewModel has no support to Asssited Injection.

All implementations are using Voyager ViewModelStore instead of Activity ViewModelStore to clear ViewModels on navigation pop.

Improvements and how to test will come soon.

To see working run the sample and start Hilt intregation.

Comment on lines 19 to 27
val componentActivity = context as? ComponentActivity
?: throw IllegalStateException("Invalid local context on AndroidScreen. It must be a ComponentActivity")
val androidScreenLifecycleOwner = getLifecycleOwner() as? AndroidScreenLifecycleOwner
?: throw IllegalStateException(
"Invalid LifecycleOwner on AndroidScreen. It must be an AndroidScreenLifecycleOwner"
)
val provider = ViewModelProvider(
androidScreenLifecycleOwner.viewModelStore,
viewModelProviderFactory ?: componentActivity.defaultViewModelProviderFactory
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since componentActivity is used as fallback, is better to avoid a premature throw with something like:

private val Context.defaultViewModelProviderFactory: ViewModelProvider.Factory
    get() = (this as? ComponentActivity)?.defaultViewModelProviderFactory
        ?: error("Invalid local context on AndroidScreen. It must be a ComponentActivity")```` 

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this content to a ContextExt.kt because we need a activity and it default view model provider factory

@adrielcafe
Copy link
Owner

It's great, thanks for your contribution!

@adrielcafe adrielcafe merged commit 6b99ca4 into main Oct 6, 2021
@adrielcafe adrielcafe deleted the hilt-integration branch October 6, 2021 17:14
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

Successfully merging this pull request may close these issues.

2 participants