Last active
December 27, 2021 12:55
-
-
Save svinouze/97878c32c6e159c2b61589599fa28eed 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
inline fun <reified VM : ViewModel> ComponentActivity.assistedViewModel( | |
crossinline viewModelProducer: (SavedStateHandle) -> VM, | |
): Lazy<VM> = lazy { | |
ViewModelProvider(this,createSavedStateViewModelFactory(intent.extras, viewModelProducer)) | |
.get(VM::class.java) | |
} | |
@PublishedApi | |
internal inline fun <reified VM : ViewModel> SavedStateRegistryOwner.createSavedStateViewModelFactory( | |
arguments: Bundle?, | |
crossinline creator: (SavedStateHandle) -> VM, | |
): ViewModelProvider.Factory = object : AbstractSavedStateViewModelFactory(this, arguments) { | |
@Suppress("UNCHECKED_CAST") | |
override fun <VM : ViewModel?> create( | |
key: String, | |
modelClass: Class<VM>, | |
handle: SavedStateHandle, | |
): VM = creator(handle) as VM | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment