There is this love/hate relationship with DataBind...
# android-architecture
s
There is this love/hate relationship with DataBinding with us and hate seems to be winning out.... We started a project with just Coroutines and Flows. So far, so good. 😀
đŸ’Ș 1
s
Just curious about why you hate it.
s
I don’t like it because I don’t like code in XML, even if it is little bit of code. If something goes wrong, a bug needs to be investigated, I have a harder time doing my sleuthing (examining/navigating call-stacks and such) And too often, my IDE shows sources with all red stuff, but compilation works fine. Too often, these related to
XXXXXBinding
not being found by the IDE and it showing up red. An “invalidate-cache and restart” is the only way to get rid of those; fresh rebuild won’t work. This is why I hate it a bit. 🙂
s
Yeah man. Valid points. Although I do like the option of passing a viewModel through dataBinding, and calling the methods directly without them passing through an activity or an fragment.
Too bad the IDE support is shit. 😂
o
If you need to debug your xml when doing databinding, you're probably doing databinding wrong. Databinding works best when your xml model is just dumb view state and doesnt contain any logic.
➕ 1
For example, this is how I do databinding: one_line_avatar_row:
Copy code
<?xml version="1.0" encoding="utf-8"?>

<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="oneLineAvatarRow"
            type=".presentation.OneLineAvatarRow" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical"
        android:onClick="@{() -> oneLineAvatarRow.onClick.invoke()}"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingTop="8dp"
        android:paddingRight="16dp"
        android:paddingBottom="8dp">

        <ImageView
            android:layout_width="56dp"
            android:layout_height="56dp"
            app:avatarSrc="@{oneLineAvatarRow.avatar}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:fontFamily="@font/poppins_semibold"
            android:includeFontPadding="false"
            android:text="@{oneLineAvatarRow.text}"
            android:textAppearance="@style/TextAppearance.MaterialComponents.Body1"
            tools:text="Rik Jansen" />

    </LinearLayout>
</layout>
OneLineAvatarRow.kt:
Copy code
interface OneLineAvatarRow {
    val id: String
    val avatar: ImageSource
    val text: String
    val onClick: () -> Unit
}
Just keep it about view state, and it will make it more readable
s
Still, if I try to do a call-hierarchy on the
avatar
of the
oneLineAvatarRow
, my results are not great. I prefer no code at all in my xml. And sometimes folks put more code in than necessary
. anywhose, it’s a matter of taste and I don’t like it 🙂
o
Why would you check the call hierarchy of the xml? You probably have a List of OneLineAvatarRow in your viewmodel, debug there, not in your innocent xml file.
s
I won’t do it the ‘call-hierarchy’ from the XML file, but from the ViewModel’s kotlin file.
o
Right, so from the viewmodel, databinding is already just out of the picture