Skip to content

Instantly share code, notes, and snippets.

@paulocns
Created February 6, 2020 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulocns/be3d0493ec10abf1f710e0a4566145dc to your computer and use it in GitHub Desktop.
Save paulocns/be3d0493ec10abf1f710e0a4566145dc to your computer and use it in GitHub Desktop.
interface ViewModelLiveDataExtension {
operator fun <T> LiveData<T>.invoke(value: T) {
if (this is ViewModelLiveData) {
val viewModelLiveData = this
viewModelLiveData.setValue(value)
}
}
fun <T> ViewModel.liveData(value: T? = null) = lazy {
ViewModelLiveData<T>().apply { value?.let { setValue(value) } } as LiveData<T>
}
private class ViewModelLiveData<T> : LiveData<T>(){
public override fun setValue(value: T) {
super.setValue(value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment