In ViewModel we can apply Encapsulation
for MutableLiveData
as shown in Android architecture :
private val _dataLoading = MutableLiveData<Boolean>()
val dataLoading: LiveData<Boolean>
get() = _dataLoading
As a result dataLoading
can not be changed from Fragment or Activity since it is LiveData
and not MutableLiveData
How about ObservableBoolean
that we may use for DataBinding
in ViewModel class for instance :
val isLoading = ObservableBoolean(false)
It can not be private since we use it in DataBinding
:
<data>
<variable
name="vm"
type="com.sample.android.ui.DetailViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:visibleGone="@{!vm.isLoading}">
So, how we can apply Encapsulation
for ObservableBoolean
as presented in Google sample?
you could have
private val loading = ObservableBoolean(false)
and in your vm
fun isLoading() = loading
if you want to change the loading state from your fragment/activity you could expose a setter
fun setLoading(value: Boolean) {
loading.set(value)
}