Last active
October 2, 2024 04:15
-
-
Save Elforama/969c2de0b3227f927fbf3f65654acf63 to your computer and use it in GitHub Desktop.
Android view model factory implementation allowing easy view model constructor injection.
This file contains hidden or 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
/** | |
* Usage example: | |
* | |
* @Inject | |
* lateinit var mFactory: VMInjectionFactory<MyViewModel> | |
*/ | |
class VMInjectionFactory <out Type: ViewModel> @Inject constructor(val vm: Lazy<Type>): ViewModelProvider.Factory { | |
@Suppress("UNCHECKED_CAST") | |
override fun <T : ViewModel> create(p0: Class<T>): T { | |
return vm.get() as T | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you know what's the difference betweenLazy<> and Provider<> wrapper here?