Skip to content

Commit

Permalink
refactor: use dialog instead of snackbar for warning
Browse files Browse the repository at this point in the history
  • Loading branch information
criticalAY authored and david-allison committed May 26, 2024
1 parent 626fb02 commit 8e0db75
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,25 +318,32 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
SAME_NUMBER, INCREMENT_NUMBER
}

@NeedsTest("Error message should be null after save")
private var addNoteErrorMessage: String? = null

private fun displayErrorSavingNote() {
val errorMessage = snackbarErrorText
// Anki allows to proceed in case we try to add non cloze text in cloze field with warning,
// this snackbar helps replicate similar behaviour
if (errorMessage == TR.addingYouHaveAClozeDeletionNote()) {
showSnackbar(errorMessage, Snackbar.LENGTH_INDEFINITE) {
setAction(R.string.dialog_ok) {
lifecycleScope.launch {
saveNoteWithProgress()
}
}
}
noClozeDialog(errorMessage)
} else {
showSnackbar(errorMessage)
}
}

private fun noClozeDialog(errorMessage: String) {
AlertDialog.Builder(this).show {
message(text = errorMessage)
positiveButton(text = TR.actionsSave()) {
lifecycleScope.launch {
saveNoteWithProgress()
}
}
negativeButton(R.string.dialog_cancel)
}
}

@VisibleForTesting
val snackbarErrorText: String
get() = when {
Expand Down Expand Up @@ -964,7 +971,6 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
sourceText = null
refreshNoteData(FieldChangeType.refreshWithStickyFields(shouldReplaceNewlines()))
showSnackbar(TR.addingAdded(), Snackbar.LENGTH_SHORT)
addNoteErrorMessage = null

if (caller == CALLER_NOTEEDITOR || aedictIntent) {
closeEditorAfterSave = true
Expand Down Expand Up @@ -1033,6 +1039,7 @@ class NoteEditor : AnkiActivity(), DeckSelectionListener, SubtitleListener, Tags
displayErrorSavingNote()
return@launch
}
addNoteErrorMessage = null
saveNoteWithProgress()
}
} else {
Expand Down

0 comments on commit 8e0db75

Please sign in to comment.