Skip to content

Commit 8749e37

Browse files
manocormencriticalAY
authored andcommitted
Enable discard changes dialog for all changes
1 parent 47da534 commit 8749e37

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ open class CardTemplateEditor :
366366
return tempNoteType != null && tempNoteType!!.notetype.toString() != oldNoteType.toString()
367367
}
368368

369+
private fun enableDiscardChangesDialog() {
370+
displayDiscardChangesCallback.isEnabled = noteTypeHasChanged()
371+
}
372+
369373
private fun showDiscardChangesDialog() =
370374
DiscardChangesDialog.showDialog(this) {
371375
Timber.i("TemplateEditor:: OK button pressed to confirm discard changes")
@@ -408,6 +412,7 @@ open class CardTemplateEditor :
408412

409413
// Deck Override can change from "on" <-> "off"
410414
invalidateOptionsMenu()
415+
enableDiscardChangesDialog()
411416
}
412417

413418
override fun onKeyUp(
@@ -683,7 +688,7 @@ open class CardTemplateEditor :
683688
}
684689
refreshFragmentRunnable = updateRunnable
685690
refreshFragmentHandler.postDelayed(updateRunnable, REFRESH_PREVIEW_DELAY)
686-
templateEditor.displayDiscardChangesCallback.isEnabled = noteTypeHasChanged()
691+
templateEditor.enableDiscardChangesDialog()
687692
}
688693

689694
override fun beforeTextChanged(
@@ -848,6 +853,7 @@ open class CardTemplateEditor :
848853
existingNames = existingNames,
849854
) { newName ->
850855
template.name = newName.value
856+
templateEditor.enableDiscardChangesDialog()
851857
Timber.i("updated card template name")
852858
Timber.d("updated name of template %d to '%s'", ordinal, newName)
853859

@@ -1343,6 +1349,7 @@ open class CardTemplateEditor :
13431349
val currentTemplate = getCurrentTemplate()
13441350
if (currentTemplate != null) {
13451351
result.applyTo(currentTemplate)
1352+
templateEditor.enableDiscardChangesDialog()
13461353
}
13471354
}
13481355

@@ -1428,6 +1435,7 @@ open class CardTemplateEditor :
14281435
try {
14291436
templateEditor.getColUnsafe.modSchema(check = true)
14301437
schemaChangingAction.run()
1438+
templateEditor.enableDiscardChangesDialog()
14311439
templateEditor.loadTemplatePreviewerFragmentIfFragmented()
14321440
} catch (e: ConfirmModSchemaException) {
14331441
e.log()
@@ -1437,6 +1445,7 @@ open class CardTemplateEditor :
14371445
Runnable {
14381446
templateEditor.getColUnsafe.modSchema(check = false)
14391447
schemaChangingAction.run()
1448+
templateEditor.enableDiscardChangesDialog()
14401449
templateEditor.dismissAllDialogFragments()
14411450
}
14421451
val cancel = Runnable { templateEditor.dismissAllDialogFragments() }

AnkiDroid/src/test/java/com/ichi2/anki/CardTemplateEditorTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,49 @@ class CardTemplateEditorTest : RobolectricTest() {
695695
MatcherAssert.assertThat("Deck ID element should be null", template.jsonObject["did"], Matchers.equalTo(JSONObject.NULL))
696696
}
697697

698+
@Test
699+
fun `ensure 'Discard changes' dialog is enabled after note type changes - Issue 18518`() {
700+
fun assertDiscardChangesDialogShown(shadowEditor: ShadowActivity) {
701+
advanceRobolectricLooper()
702+
assertTrue("Unable to click?", shadowEditor.clickMenuItem(android.R.id.home))
703+
advanceRobolectricLooper()
704+
assertEquals("Wrong dialog shown?", "Discard changes?", getAlertDialogText(true))
705+
clickAlertDialogButton(DialogInterface.BUTTON_POSITIVE, false)
706+
}
707+
708+
// Case 1: Show dialog after deck override
709+
withCardTemplateEditor {
710+
val shadowEditor = shadowOf(this)
711+
onDeckSelected(SelectableDeck.Deck(1, "hello"))
712+
assertDiscardChangesDialogShown(shadowEditor)
713+
}
714+
715+
// Case 2: Show dialog after changing card browser appearance
716+
withCardTemplateEditor {
717+
val shadowEditor = shadowOf(this)
718+
assertTrue(
719+
"Unable to click?",
720+
shadowEditor.clickMenuItem(R.id.action_card_browser_appearance),
721+
)
722+
advanceRobolectricLooper()
723+
shadowEditor.receiveResult(
724+
shadowEditor.nextStartedActivity,
725+
Activity.RESULT_OK,
726+
Intent()
727+
.putExtra(CardTemplateBrowserAppearanceEditor.INTENT_QUESTION_FORMAT, "q")
728+
.putExtra(CardTemplateBrowserAppearanceEditor.INTENT_ANSWER_FORMAT, "a"),
729+
)
730+
assertDiscardChangesDialogShown(shadowEditor)
731+
}
732+
733+
// Case 3: Show dialog after adding a card type
734+
withCardTemplateEditor {
735+
val shadowEditor = shadowOf(this)
736+
addCardType(this, shadowEditor)
737+
assertDiscardChangesDialogShown(shadowEditor)
738+
}
739+
}
740+
698741
@Test
699742
fun testContentPreservedAfterChangingEditorView() {
700743
val noteTypeName = "Basic"

0 commit comments

Comments
 (0)