1

I have encountered some unexpected behaviour with LiveData and data binding libraries. I had implemented CustomLiveData as in this answer https://stackoverflow.com/a/48194074/13321296, so I just can call notifyChange() inside parent class to update UI.

I have parent object(some methods omitted for brevity):

class Day(val tasks: MutableList<RunningTask>,
          state: DayState = DayState.WAITING,
          var dayStartTime: Long = 0L,
          currentTaskPos: Int = 0): BaseObservable() {

    var state: DayState = state
        set(value) {
            field = value
            notifyChange()
        }

    var currentTaskPos: Int = currentTaskPos
        set(value) {
            field = value
            notifyChange()
        }

    fun start() {
        dayStartTime = System.currentTimeMillis()
        state = DayState.ACTIVE
        resetTasks()
        tasks[currentTaskPos].start()

        notifyChange()
    }
}

Child object:

class RunningTask(
    startTime: Long,

    var name: String = "",
    private val originalDuration: Long = 0L,

    val sound: String
): BaseObservable() {
    var startTime: Long = startTime
        set(value) {
            field = value
            uiStartTime = convertMillisToStringFormat(value)
        }

    @Bindable
    var uiStartTime: String = convertMillisToStringFormat(startTime)
        set(value) {
            field = value
            notifyPropertyChanged(BR.uiStartTime)
        }

    var duration: Long = originalDuration
        set(value) {
            field = value
        }

    var state: State = State.WAITING

    var progress: Long = 0L
        set(value) {
            field = value
        }

    var timePaused: Long = 0L
    var timeRemain: String = convertMillisToStringFormat(duration)

    enum class State {
        WAITING, ACTIVE, COMPLETED, DISABLED
    }

    fun start() {
        state = State.ACTIVE
    }
}

The problem is what data binding from item_main_screen_task.xml is not updated when I change items inside of Day's tasks field, e.g. calling method start(), but other fields, such as state, do update correctly, so I guess the problem is with list inside of it.

fragment_main_screen.xml, recyclerview is populated with Day class field tasks:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <import type="android.view.View"/>

        <variable
            name="viewmodel"
            type="com.sillyapps.meantime.ui.mainscreen.MainViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/tasks"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:visibility="@{viewmodel.noTemplate ? View.GONE : View.VISIBLE}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toTopOf="@+id/play_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
            tools:listitem="@layout/item_main_screen_task"
            tools:visibility="visible" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

item_main_screen_task, taskState attribute is just basically BindingAdapter what sets background drawable according to Day's state enum:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="task"
            type="com.sillyapps.meantime.data.RunningTask" />

        <variable
            name="taskAdapterPosition"
            type="Integer" />

        <variable
            name="clickListener"
            type="com.sillyapps.meantime.ui.ItemClickListener" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:taskState="@{task.state}"

        android:onClick="@{() -> clickListener.onClickItem(taskAdapterPosition)}">

        <TextView
            android:id="@+id/time"
            style="@style/TimeItemStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:text="@{task.uiStartTime}"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/enter_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="17:00" />

        <TextView
            android:id="@+id/enter_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"

            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"

            android:text="@{task.name}"
            app:layout_constraintBottom_toBottomOf="@+id/time"
            app:layout_constraintEnd_toStartOf="@+id/progress"
            app:layout_constraintStart_toEndOf="@+id/time"
            app:layout_constraintTop_toTopOf="@+id/time"
            tools:text="Свободное время" />

        <TextView
            android:id="@+id/progress"
            style="@style/TimeItemStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_marginEnd="8dp"

            android:text="@{task.timeRemain}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/enter_name"
            app:layout_constraintTop_toTopOf="@+id/time"
            tools:text="01:00" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

Thanks in advance.

Darkhan
  • 17
  • 3

1 Answers1

-1

Turns out that solution was very simple, but somewhat unexpected

The child class should extend BaseObservable, and call notifyChange() on setters of every data-binded fields, something like that:

class RunningTask(
    startTime: Long,

    var name: String = "",
    private val originalDuration: Long = 0L,

    val sound: String
): BaseObservable() {
    var startTime: Long = startTime
        set(value) {
            field = value
            uiStartTime = convertMillisToStringFormat(value)
        }

    @Bindable
    var uiStartTime: String = convertMillisToStringFormat(startTime)
        set(value) {
            field = value
            notifyPropertyChanged(BR.uiStartTime)
        }

    var state: State = State.WAITING
        set(value) {
             field = value
             notifyChange()
        }
    
    ...
}

Appears that I'd already implemented this in uiStartTime before coming up with question, but I just didn't know exact reason why it's worked

Darkhan
  • 17
  • 3