Skip to content

Instantly share code, notes, and snippets.

@appspell
Created May 11, 2021 06:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save appspell/dc1b028ee621a5922657e01c5177c5ec to your computer and use it in GitHub Desktop.
@Composable
inline fun <reified T : ViewModel> daggerViewModel(
key: String? = null,
crossinline viewModelInstanceCreator: () -> T
): T =
androidx.lifecycle.viewmodel.compose.viewModel(
modelClass = T::class.java,
key = key,
factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return viewModelInstanceCreator() as T
}
}
)
@seyoung-hyun
Copy link

Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment