Skip to content

Instantly share code, notes, and snippets.

@Pluu
Created March 16, 2020 16:09
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 Pluu/de84c5190ed81e9fb8f3d85ee4bb7f20 to your computer and use it in GitHub Desktop.
Save Pluu/de84c5190ed81e9fb8f3d85ee4bb7f20 to your computer and use it in GitHub Desktop.
@MainThread
inline fun <reified VM : ViewModel> ComponentActivity.viewModels(
noinline factoryProducer: (() -> Factory)? = null
): Lazy<VM> {
val factoryPromise = factoryProducer ?: {
defaultViewModelProviderFactory
}
return ViewModelLazy(VM::class, { viewModelStore }, factoryPromise)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment