-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
Hilt integration #13
Conversation
Added support to assisted injection with screen model
voyager-hilt/src/main/java/cafe/adriel/voyager/hilt/ScreenFactory.kt
Outdated
Show resolved
Hide resolved
voyager-hilt/src/main/java/cafe/adriel/voyager/hilt/ScreenFactoryKey.kt
Outdated
Show resolved
Hide resolved
voyager-hilt/src/main/java/cafe/adriel/voyager/hilt/ScreenModelEntryPoint.kt
Outdated
Show resolved
Hide resolved
voyager-hilt/src/main/java/cafe/adriel/voyager/hilt/ScreenModel.kt
Outdated
Show resolved
Hide resolved
voyager-hilt/src/main/java/cafe/adriel/voyager/hilt/ScreenModel.kt
Outdated
Show resolved
Hide resolved
voyager-hilt/src/main/java/cafe/adriel/voyager/hilt/ScreenModel.kt
Outdated
Show resolved
Hide resolved
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 |
There was a problem hiding this comment.
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")````
There was a problem hiding this comment.
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
It's great, thanks for your contribution! |
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.