Skip to content

Commit

Permalink
Fix category list not updating after name edit
Browse files Browse the repository at this point in the history
The previous code had a strange bug. After you edit the category name with the dialog, it is *not* updated in the list. But looking at the database with the database inspector, you see that it was indeed updated. Also, if you click on edit again, the dialog does show the correctly updated name. But the actual list (recyclerview) does not.

If you use the database inspector to manually change the name of a category, it is updated perfectly fine as you would expect livedata from room. It is even updated when the list is in the background of a dialog, but still visible.

So we know that the room->livedata pipeline works. But apparently, it's not activated by the editing dialog.

(I tried several things including the "nuclear" adapter.notifyDataSetChanged(), which worked, but should be used only if nothing else works.)

The current solution changes what happens when   the change dialog executes the positive button steps. When the user clicked "rename", before the change, we simply changed the name (which was a var for this reason) of the category object and try to update this object in the database.
Now category is immutable (only vals) and we create a new category object with new name that is saved via viewModel.

Now it works.

To be honest, I don't know why. I can only speculate. But the important thing is that now it works as expected.

#12
  • Loading branch information
eucalypto committed Jul 7, 2021
1 parent dba79e9 commit 053c0f4
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ActivityListViewModelFactory(private val repository: Repository) : ViewMod
throw IllegalArgumentException("Unknown ViewModel class: ${modelClass.name}")
}

@Suppress("unchecked_cast")
return ActivityListViewModel(repository) as T
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private constructor(private val binding: CategoryListItemBinding) :
onDeleteClicked: (Category) -> Unit
) {
this.category = category
binding.categoryName.text = category.name
binding.category = category
binding.contextMenuButton.setOnClickListener {
showContextMenu(
it,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class CategoryListFragment : Fragment() {
inputBinding.categoryNameEdit.editText?.let {
val input = it.text.toString()
if (input.isBlank()) return@let
category.name = input
viewModel.saveCategory(category)
viewModel.saveCategory(category.withName(input))
}
}
.setView(inputBinding.root)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.eucalypto.timetracker.domain.model
import java.util.*

data class Category(
var name: String = "",
val name: String = "",
val id: UUID = UUID.randomUUID()
) {

Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/category_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="category"
type="net.eucalypto.timetracker.domain.model.Category" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -12,6 +19,7 @@
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{category.name}"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down

0 comments on commit 053c0f4

Please sign in to comment.