Skip to content

Commit

Permalink
Fix new items not checked on line break insertion in checked group
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaisn committed Jun 13, 2021
1 parent 209a00a commit aedd53e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aedd53e

Please sign in to comment.