Hi everyone, I’m exploring the possibility of conn...
# android
d
Hi everyone, I’m exploring the possibility of connecting Coroutine 
StateFlow
 🤝 
DataBinding
 in a similar way as 
LiveData
 do. It seems there isn’t a default implementation at the moment that supports it. I found an example online which seems quite “burdensome” if one needs to implement it for a bigger project https://github.com/STAR-ZERO/sample-stateflow-databinding Do you use 
StateFlow
 🤝 
DataBinding
 ? How do you connect them?
f
please don’t. If you have to, you can just use Flow.asLiveData in your binding adapter.
☝️ 1
👍 1
n
For convenience I’d use
_mutableStateFlow: MutableStateFlow<T>
and expose it like
liveData: LiveData<T> get = _mutableStateFlow.asLiveData()
similar as @flosch suggested
d
Many thanks @flosch and @nrobi
I thought there might be a means to call the
StateFlow object
right from the
XML
files to regularly get its updated value. I’d resolve to exposing the object to a LiveData to get that done. Thanks again. CC: @flosch @nrobi
m
Guys how can i do this in XML right away ? Reason: i want to move everything into kotlin multi platform common module (No Live data) I tried to add
Copy code
<import type="androidx.lifecycle.FlowLiveDataConversions" />
and convert stateFlow to live data in xml like this
Copy code
app:visibility="@{(FlowLiveDataConversions.asLiveData(vm.state.loading))}" />
but having this error
Copy code
Cannot find a setter for <android.widget.ProgressBar app:visibility> that accepts parameter type 'androidx.lifecycle.LiveData<?>'
d
Visibility (in XML) should be accepting something like View.VISIBLE .GONE... Not Boolean. So you need to write a condition that if value is true/false, it should use View.GONE or the one you wish (as the case might be)
m
Sorry forgot to share, there is a binding adapter for this
Copy code
@BindingAdapter("visibility")
fun View.visibility(visible: Boolean) {
    visibility = if (visible) {
        bringToFront()
        View.VISIBLE
    } else {
        View.GONE
    }
}
So the problem now it is working with live data
Copy code
app:visibility="@{vm.state.loadingLiveData}" />
but it gives error when i use stateFlow with asLiveData extension in XML
Copy code
app:visibility="@{(FlowLiveDataConversions.asLiveData(vm.state.loadingStateDlow))}" />
f
What I meant was something like:
Copy code
@BindingAdapter("visibility")
fun View.visibility(visibilityFlow: StateFlow<Boolean>) {
    visibilityFlow.asLiveData(findViewTreeLifecycleOwner()) { visible ->
        visibility = if (visible) {
            bringToFront()
            View.VISIBLE
        } else {
            View.GONE
        }
    }
}