Skip to content

Instantly share code, notes, and snippets.

@thecodeside
Created February 20, 2022 18:03
Show Gist options
  • Save thecodeside/b9e49d88c7369a65f66fcb740711feeb to your computer and use it in GitHub Desktop.
Save thecodeside/b9e49d88c7369a65f66fcb740711feeb to your computer and use it in GitHub Desktop.
inline fun <reified VM : ViewModel> ComponentActivity.viewModels(
noinline customCreator: (() -> VM)? = null
): Lazy<VM> =
viewModels(factoryProducer = customCreator?.let { { ViewModelFactory(it) } })
inline fun <reified VM : ViewModel> Fragment.activityViewModels(
noinline customCreator: (() -> VM)? = null
): Lazy<VM> =
activityViewModels(factoryProducer = customCreator?.let { { ViewModelFactory(it) } })
inline fun <reified VM : ViewModel> Fragment.navGraphViewModels(
@IdRes navGraphId: Int,
noinline customCreator: (() -> VM)? = null
): Lazy<VM> =
navGraphViewModels(navGraphId, factoryProducer = customCreator?.let { { ViewModelFactory(it) } })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment