Skip to content

Instantly share code, notes, and snippets.

@paulocns
Last active February 6, 2020 02:03
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/fb38a2cc4c06337e35fc3a319713ae61 to your computer and use it in GitHub Desktop.
Save paulocns/fb38a2cc4c06337e35fc3a319713ae61 to your computer and use it in GitHub Desktop.
abstract class BaseViewModel: ViewModel() {
protected operator fun <T> LiveData<T>.invoke(value: T) {
if (this is ViewModelLiveData) {
val viewModelLiveData = this
viewModelLiveData.setValue(value)
}
}
protected fun <T> liveData(value: T) = lazy {
ViewModelLiveData(value) as LiveData<T>
}
protected fun <T> liveData() = lazy {
ViewModelLiveData<T>() as LiveData<T>
}
private class ViewModelLiveData<T> : LiveData<T>{
constructor(value: T) : super(value)
constructor() : super()
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