Skip to content

Commit

Permalink
Remove check for internal list note consistency between content and m…
Browse files Browse the repository at this point in the history
…etadata

Can happen if text filter fails and list note item contains a newline. Not sure how that happens though. The crash report in Play Console is:

java.lang.IllegalStateException:
  at com.maltaisn.notes.model.entity.Note.getListItems (Note.java:129)
  at com.maltaisn.notes.ui.edit.EditViewModel.recreateListItems (EditViewModel.java:109)
  at com.maltaisn.notes.ui.edit.EditViewModel.toggleNoteType (EditViewModel.java:109)
  ...

The check in cause was removed to fallback gracefully.
  • Loading branch information
maltaisn committed Sep 4, 2021
1 parent add2ff2 commit 15ed58b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Fixed simultaneous notifications all opening the same note (#43).
- Fixed notification click not working if already editing a note.
- Fixed notification creating new note if clicked after note is deleted.
- Remove check for internal list note consistency between content and metadata.

## v1.4.0
- Added a label attribute to hide all notes with that label in active & archive destinations.
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/com/maltaisn/notes/model/entity/Note.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ data class Note(
return mutableListOf()
}

check(checked.size == items.size) { "Invalid list note data." }
// check(checked.size == items.size) { "Invalid list note data." }

return items.mapIndexedTo(mutableListOf()) { i, text ->
ListNoteItem(text.trim(), checked[i])
ListNoteItem(text.trim(), checked.getOrElse(i) { false })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,14 @@ class EditViewModel @AssistedInject constructor(
}
NoteType.LIST -> {
// Add items in the correct actual order
val items = arrayOfNulls<EditItemItem>(listItems.count { it is EditItemItem })
val items = MutableList(listItems.count { it is EditItemItem }) { TEMP_ITEM }
for (item in listItems) {
if (item is EditItemItem) {
items[item.actualPos] = item
}
}
content = items.joinToString("\n") { it!!.content.text }
metadata = ListNoteMetadata(items.map { it!!.checked })
content = items.joinToString("\n") { it.content.text }
metadata = ListNoteMetadata(items.map { it.checked })
}
}
note = note.copy(title = title, content = content,
Expand Down Expand Up @@ -862,5 +862,7 @@ class EditViewModel @AssistedInject constructor(

private const val KEY_NOTE_ID = "noteId"
private const val KEY_IS_NEW_NOTE = "isNewNote"

private val TEMP_ITEM = EditItemItem(DefaultEditableText(), false, false, 0)
}
}
11 changes: 11 additions & 0 deletions app/src/test/kotlin/com/maltaisn/notes/model/entity/NoteTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ class NoteTest {
}
}

@Test
fun `should handle mismatch in list note metadata and content`() {
val items = listOf(
ListNoteItem("0", false),
ListNoteItem("1", true),
ListNoteItem("2", true))
var note = listNote(items)
note = note.copy(content = note.content + "\n3")
assertEquals(items + ListNoteItem("3", false), note.listItems)
}

@Test
fun `should fail to create deleted note with reminder`() {
assertFailsWith<IllegalArgumentException> {
Expand Down

0 comments on commit 15ed58b

Please sign in to comment.