Thread
#multiplatform
    m

    Mgj

    2 years ago
    Anyone managed to use android databinding with binding to
    StateFlow
    or
    MutableStateFlow
    properties? I've created custom binding adapters before, but i cannot get it to work properly with StateFlows
    louiscad

    louiscad

    2 years ago
    What doesn't work exactly?
    m

    Mgj

    2 years ago
    I couldnt get two-way binding to work. Usually you would have something like:
    @InverseBindingAdapter(attribute = "flowText")
    @JvmStatic
    fun getText(view: MyEditText): String {
        return view.text.toString()
    }
    
    @BindingAdapter("flowText")
    @JvmStatic
    fun setText(view: MyEditText, newValue: String) {
        if (view.text.toString != newValue) {
            view.text = newValue
            view.textChangedListener?.onChange()
        }
    }
    
    @BindingAdapter("flowTextAttrChanged")
    @JvmStatic
    fun setListeners(
        view: MyEditText,
        attrChange: InverseBindingListener
    ) {
        view.textChangedListener = attrChange
    }
    But when replacing
    String
    with
    MutableStateFlow<String>
    the InverseBindingAdapter would never be called or used by the generated binding code
    I ended up just forcing two-way binding on it by subscribing to the StateFlow in the BindingAdapter and updating it with a TextWatcher 🤷 not really ideal but it seems to work
    Orhan Tozan

    Orhan Tozan

    2 years ago
    Thats why I use LiveData, it has out of the box db support
    m

    Mgj

    2 years ago
    Right, i use LiveData for native android app's but for KMP i'd like to share the viewmodels between platforms... not possible with LiveData
    louiscad

    louiscad

    2 years ago
    You have
    flow.asLiveData()
    m

    Mgj

    2 years ago
    But surely that cannot be used in common code right? I'd have to do that in the android code
    Orhan Tozan

    Orhan Tozan

    2 years ago
    Yea, just like you use BindingAdapter
    m

    Mgj

    2 years ago
    Right. Hmm its an interesting option for sure, thanks
    @louiscad
    asLiveData
    is neat, but i cant seem to find a asMutableLiveData... Seems like i'd still have to make bindingAdapter-like code to enable two-way binding
    louiscad

    louiscad

    2 years ago
    @Mgj You can make your own
    asMutableLiveData
    if you want.
    m

    Mgj

    2 years ago
    Should probably have made it more clear, its what i meant by "bindingAdapter-like code". Its still a cool idea but loses some of its appeal