From aedd53e795fd36828b1478e48e8460c652814447 Mon Sep 17 00:00:00 2001 From: maltaisn Date: Sun, 13 Jun 2021 14:55:20 -0400 Subject: [PATCH] Fix new items not checked on line break insertion in checked group --- .../maltaisn/notes/ui/edit/EditViewModel.kt | 4 +++- .../ui/edit/adapter/EditListViewHolder.kt | 11 ++++++++-- .../notes/ui/edit/EditViewModelTest.kt | 22 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/edit/EditViewModel.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/edit/EditViewModel.kt index e97e0274..4efed6e9 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/ui/edit/EditViewModel.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/edit/EditViewModel.kt @@ -623,6 +623,7 @@ class EditViewModel @AssistedInject constructor( val item = listItems[pos] as EditItemItem if ('\n' in item.content.text) { // User inserted line breaks in list items, split it into multiple items. + // If this happens in the checked group when moving checked to the bottom, new items will be checked. val lines = item.content.text.split('\n') item.content.replaceAll(lines.first()) for (listItem in listItems) { @@ -632,9 +633,10 @@ class EditViewModel @AssistedInject constructor( } for (i in 1 until lines.size) { listItems.add(pos + i, EditItemItem(DefaultEditableText(lines[i]), - checked = false, editable = true, item.actualPos + i)) + checked = item.checked && moveCheckedToBottom, editable = true, item.actualPos + i)) } + moveCheckedItemsToBottom() // just to update checked count updateListItems() // If text was pasted, set focus at the end of last items pasted. diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/edit/adapter/EditListViewHolder.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/edit/adapter/EditListViewHolder.kt index 5e9e154c..d358717e 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/ui/edit/adapter/EditListViewHolder.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/edit/adapter/EditListViewHolder.kt @@ -149,7 +149,11 @@ class EditItemViewHolder(binding: ItemEditItemBinding, callback: EditAdapter.Cal itemEdt.strikethroughText = isChecked && callback.strikethroughCheckedItems itemEdt.isActivated = !isChecked // Controls text color selector. dragImv.isInvisible = isChecked && callback.moveCheckedToBottom - callback.onNoteItemCheckChanged(bindingAdapterPosition, isChecked) + + val pos = bindingAdapterPosition + if (pos != RecyclerView.NO_POSITION) { + callback.onNoteItemCheckChanged(pos, isChecked) + } } itemEdt.addTextChangedListener(clearSpansTextWatcher) @@ -159,7 +163,10 @@ class EditItemViewHolder(binding: ItemEditItemBinding, callback: EditAdapter.Cal // item can be split into multiple items. When user enters a single line break, // selection is set at the beginning of new item. On paste, i.e. when more than one // character is entered, selection is set at the end of last new item. - callback.onNoteItemChanged(bindingAdapterPosition, count > 1) + val pos = bindingAdapterPosition + if (pos != RecyclerView.NO_POSITION) { + callback.onNoteItemChanged(pos, count > 1) + } } itemEdt.setOnFocusChangeListener { _, hasFocus -> // Only show delete icon for currently focused item. diff --git a/app/src/test/kotlin/com/maltaisn/notes/ui/edit/EditViewModelTest.kt b/app/src/test/kotlin/com/maltaisn/notes/ui/edit/EditViewModelTest.kt index 8599990d..54c73c02 100644 --- a/app/src/test/kotlin/com/maltaisn/notes/ui/edit/EditViewModelTest.kt +++ b/app/src/test/kotlin/com/maltaisn/notes/ui/edit/EditViewModelTest.kt @@ -1023,6 +1023,28 @@ class EditViewModelTest { ), viewModel.editItems.getOrAwaitValue()) } + @Test + fun `should add new checked items if newlines inserted in checked section`() = mainCoroutineRule.runBlockingTest { + whenever(prefs.moveCheckedToBottom) doReturn true + viewModel.start(2) + + val item = viewModel.editItems.getOrAwaitValue()[5] as EditItemItem + item.content.append("\n") + + viewModel.onNoteItemChanged(5, false) + + assertEquals(listOf( + EditDateItem(dateFor("2020-03-30").time), + EditTitleItem("title".e, true), + EditItemItem("item 2".e, checked = false, editable = true, 2), + EditItemAddItem, + EditCheckedHeaderItem(2), + EditItemItem("item 1".e, checked = true, editable = true, 0), + EditItemItem("".e, checked = true, editable = true, 1), + EditItemLabelsItem(listOf(labelsRepo.requireLabelById(1))), + ), viewModel.editItems.getOrAwaitValue()) + } + @Test fun `should delete checked items in list note (move checked to bottom)`() = mainCoroutineRule.runBlockingTest { viewModel.start(7)