Skip to content

Instantly share code, notes, and snippets.

@marcellogalhardo
Last active May 28, 2020 13:46
Show Gist options
  • Save marcellogalhardo/a6eb96e8df4ab95ad73486bbcfa3d955 to your computer and use it in GitHub Desktop.
Save marcellogalhardo/a6eb96e8df4ab95ad73486bbcfa3d955 to your computer and use it in GitHub Desktop.
interface SavedStateViewModelFactory {
fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T
}
private fun SavedStateViewModelFactory.asViewModelProviderFactory(
owner: SavedStateRegistryOwner
): ViewModelProvider.Factory {
return object : AbstractSavedStateViewModelFactory(owner, owner.arguments) {
override fun <T : ViewModel?> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T {
return [email protected](key, modelClass, handle)
}
}
}
private val SavedStateRegistryOwner.arguments: Bundle
get() = when (this) {
is Activity -> intent?.extras
is Fragment -> arguments
else -> Bundle()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment