Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditorFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ class NoteEditorFragment :
NoteEditorActivityResultCallback {
// Note type can change regardless of exit type - update ourselves and CardBrowser
reloadRequired = true
val previousFieldNames = editorNote!!.notetype.fieldsNames.toList()
editorNote!!.notetype = getColUnsafe.notetypes.get(editorNote!!.noteTypeId)!!
if (currentEditedCard == null ||
!editorNote!!
Expand All @@ -355,6 +356,7 @@ class NoteEditorFragment :
Timber.d("onActivityResult() template edit return - current card is gone, close note editor")
showSnackbar(getString(R.string.template_for_current_card_deleted))
closeNoteEditor()
return@NoteEditorActivityResultCallback
} else {
Timber.d("onActivityResult() template edit return, in add mode, just re-display")
updateCards(editorNote!!.notetype)
Expand All @@ -369,6 +371,7 @@ class NoteEditorFragment :
editorNote = currentEditedCard!!.note // update the NoteEditor's working note reference
updateCards(editorNote!!.notetype)
}
repopulateFieldsIfChanged(previousFieldNames)
},
)

Expand Down Expand Up @@ -2893,6 +2896,20 @@ class NoteEditorFragment :
}
}

@VisibleForTesting
fun repopulateFieldsIfChanged(previousFieldNames: List<String>) {
val newFieldNames = editorNote!!.notetype.fieldsNames.toList()
if (newFieldNames != previousFieldNames) {
Timber.d(
"Note type fields changed from %s to %s, repopulating edit fields",
previousFieldNames,
newFieldNames,
)
populateEditFields(FieldChangeType.changeFieldCount(shouldReplaceNewlines()))
}
updateToolbar()
}

companion object {
// DA 2020-04-13 - Refactoring Plans once tested:
// * There is a difference in functionality depending on whether we are editing
Expand Down
63 changes: 63 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/NoteEditorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,69 @@ class NoteEditorTest : RobolectricTest() {
assertFalse(hasUnsavedChanges())
}

@Test
fun `restoring notetype to stock updates field count in editor`() =
runTest {
val notetype = col.notetypes.byName("Basic")!!
col.notetypes.addFieldModChanged(notetype, col.notetypes.newField("Extra"))
col.notetypes.update(notetype)
col.notetypes.clearCache()

val note = col.newNote(col.notetypes.get(notetype.id)!!)
note.setField(0, "Front")
note.setField(1, "Back")
note.setField(2, "Extra")
col.addNote(note, col.decks.id("Default"))

val editor = getNoteEditorEditingExistingBasicNote(note, REVIEWER)
advanceRobolectricLooper()
assertThat(editor.editFields!!.size, equalTo(3))

// simulate restoreNotetypeToStock: backend drops back to 2 fields
val restored = col.notetypes.get(notetype.id)!!
col.notetypes.removeField(restored, restored.fields.last())
col.notetypes.update(restored)
col.notetypes.clearCache()

val previousFieldNames =
editor.editorNote!!
.notetype.fieldsNames
.toList()
editor.editorNote!!.notetype = col.notetypes.get(notetype.id)!!
editor.repopulateFieldsIfChanged(previousFieldNames)
advanceRobolectricLooper()

assertThat(
"Field count should match restored notetype",
editor.editFields!!.size,
equalTo(2),
)
}

@Test
fun `returning from template editor without field changes does not repopulate fields`() =
runTest {
val note = addBasicNote("Front", "Back")
val editor = getNoteEditorEditingExistingBasicNote(note, REVIEWER)
advanceRobolectricLooper()

val originalRef = editor.editFields
val previousFieldNames =
editor.editorNote!!
.notetype.fieldsNames
.toList()

editor.repopulateFieldsIfChanged(previousFieldNames)
advanceRobolectricLooper()

assertThat(editor.editFields!!.size, equalTo(2))
assertThat(
"editFields reference must not change when fields are unchanged",
editor.editFields,
equalTo(originalRef),
)
}

private suspend fun withNoteEditorAdding(
from: FromScreen = FromScreen.DECK_LIST,
block: suspend NoteEditorFragment.() -> Unit,
Expand Down
Loading