Created
February 6, 2020 01:53
-
-
Save paulocns/be3d0493ec10abf1f710e0a4566145dc 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
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