Created
April 8, 2019 08:39
-
-
Save Kvest/163093c6d9c02a03bd505497da1eea77 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 T : ViewModel> AppCompatActivity.getViewModel(): T { | |
return ViewModelProviders.of(this)[T::class.java] | |
} | |
inline fun <reified T : ViewModel> AppCompatActivity.withViewModel(body: T.() -> Unit): T { | |
val vm = getViewModel<T>() | |
vm.body() | |
return vm | |
} | |
inline fun <reified T : ViewModel> AppCompatActivity.withViewModel( | |
factory: ViewModelProvider.Factory, | |
body: T.() -> Unit | |
): T { | |
val vm = getViewModel<T>(factory) | |
vm.body() | |
return vm | |
} | |
inline fun <reified T : ViewModel> AppCompatActivity.getViewModel(factory: ViewModelProvider.Factory): T { | |
return ViewModelProviders.of(this, factory)[T::class.java] | |
} | |
inline fun <reified T : ViewModel> Fragment.getViewModel(): T { | |
return ViewModelProviders.of(this)[T::class.java] | |
} | |
inline fun <reified T : ViewModel> Fragment.withViewModel(body: T.() -> Unit): T { | |
val vm = getViewModel<T>() | |
vm.body() | |
return vm | |
} | |
inline fun <reified T : ViewModel> Fragment.withViewModel( | |
factory: ViewModelProvider.Factory, | |
body: T.() -> Unit | |
): T { | |
val vm = getViewModel<T>(factory) | |
vm.body() | |
return vm | |
} | |
inline fun <reified T : ViewModel> Fragment.getViewModel(factory: ViewModelProvider.Factory): T { | |
return ViewModelProviders.of(this, factory)[T::class.java] | |
} | |
fun <T : Any, L : LiveData<T>> LifecycleOwner.observe(liveData: L, body: (T?) -> Unit) { | |
liveData.observe(this, Observer(body)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment