Skip to content

Instantly share code, notes, and snippets.

@Morfly
Last active October 27, 2022 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Morfly/b928513861cbc6e386df42fcfb455578 to your computer and use it in GitHub Desktop.
Save Morfly/b928513861cbc6e386df42fcfb455578 to your computer and use it in GitHub Desktop.
@Composable
inline fun <reified VM : ViewModel> injectedViewModel(
key: String? = null,
crossinline viewModelInstanceCreator: () -> VM
): VM {
val factory = remember(key) {
object : ViewModelProvider.Factory {
override fun <VM : ViewModel> create(modelClass: Class<VM>): VM {
@Suppress("UNCHECKED_CAST")
return viewModelInstanceCreator() as VM
}
}
}
return viewModel(key = key, factory = factory)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment