- Layout
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewmodel"
type="com.example.MyViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/llNoDataLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:executionResult="@{viewmodel.state}"/>
</layout>
- ViewModel
class MyViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle
): ViewModel(){
private val _state = MutableStateFlow<ExecutionResult<List<glucometerData>>>(ExecutionResult.idle())
val state: StateFlow<ExecutionResult<List<glucometerData>>> = _state
}
- **Binding Adapter **
@BindingAdapter("executionResult")
fun View.visibility(state: ExecutionResult<List<Data>>?){
println("View.visibility:: Execution Result: state: $state")
when(state){
is ExecutionResult.Data -> {
this.isVisible = state.data.isNotEmpty()
}
is ExecutionResult.Error -> {
}
is ExecutionResult.Idle -> {
}
is ExecutionResult.Loading -> {
when(state.isLoading){
true -> this.isVisible = false
}
}
}
}
I am getting data from the repository. It will set Execution result in the following order: progress = true, data, progress = false. In the binding adpater, I am getting only the Idle state and progress = false. Rest of the state in between is getting skipped. Same thing is happening if I replace StateFlows with LiveData