Last active
October 27, 2022 04:34
-
-
Save Morfly/b928513861cbc6e386df42fcfb455578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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