Created
November 15, 2018 11:15
-
-
Save lewisevans/74f8d2631157e088db9a0e2dce358973 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
abstract class BaseFragment<VM : BaseViewModel> : DaggerFragment() { | |
@Inject | |
internal lateinit var viewModelFactory: ViewModelFactory<VM> | |
protected lateinit var viewModel: VM | |
internal inline fun <reified VM : ViewModel> viewModelWith(frag: Fragment): VM = | |
ViewModelProviders.of(frag, viewModelFactory).get(VM::class.java) | |
internal inline fun <reified VM : ViewModel> viewModelWith(act: FragmentActivity): VM = | |
ViewModelProviders.of(act, viewModelFactory).get(VM::class.java) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it in Fragment like:
viewModel = viewModelWith(activity!!)
and it's automatically cast