From cebb4574df41f662364328549dc091416d5abe9b Mon Sep 17 00:00:00 2001 From: Gourab Singha <110812339+gourabsingha1@users.noreply.github.com> Date: Fri, 8 Mar 2024 20:50:48 +0530 Subject: [PATCH] feat: Validate duplicate deck name (#15808) * fix: can't create first deck if named 'default' * Checks if deck name already exists before creating one. Fixes 15678 Fixes 15759 --- .../java/com/ichi2/anki/dialogs/CreateDeckDialog.kt | 13 +++++++++++++ .../main/res/layout/dialog_generic_text_input.xml | 9 +++++---- AnkiDroid/src/main/res/values/03-dialogs.xml | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt index 715f55caa989..bc1ab2f2205d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt @@ -26,10 +26,12 @@ import com.ichi2.anki.R import com.ichi2.anki.UIUtils.showThemedToast import com.ichi2.anki.snackbar.showSnackbar import com.ichi2.annotations.NeedsTest +import com.ichi2.libanki.Collection import com.ichi2.libanki.DeckId import com.ichi2.libanki.Decks import com.ichi2.libanki.getOrCreateFilteredDeck import com.ichi2.utils.getInputField +import com.ichi2.utils.getInputTextLayout import com.ichi2.utils.input import com.ichi2.utils.negativeButton import com.ichi2.utils.positiveButton @@ -94,12 +96,23 @@ class CreateDeckDialog( dialog.positiveButton.isEnabled = false return@input } + if (deckExists(getColUnsafe, maybeDeckName)) { + dialog.getInputTextLayout().error = context.getString(R.string.deck_already_exists) + dialog.positiveButton.isEnabled = false + return@input + } + dialog.getInputTextLayout().error = null dialog.positiveButton.isEnabled = true } shownDialog = dialog return dialog } + /** + * @return true if the collection contains a deck with the given name + */ + private fun deckExists(col: Collection, name: String): Boolean = col.decks.byName(name) != null + /** * Returns the fully qualified deck name for the provided input * @param dialogText The user supplied text in the dialog diff --git a/AnkiDroid/src/main/res/layout/dialog_generic_text_input.xml b/AnkiDroid/src/main/res/layout/dialog_generic_text_input.xml index 4dca75848fb3..d826e579595d 100644 --- a/AnkiDroid/src/main/res/layout/dialog_generic_text_input.xml +++ b/AnkiDroid/src/main/res/layout/dialog_generic_text_input.xml @@ -15,8 +15,8 @@ --> - + app:errorEnabled="true" + android:paddingTop="16dp"> - \ No newline at end of file + diff --git a/AnkiDroid/src/main/res/values/03-dialogs.xml b/AnkiDroid/src/main/res/values/03-dialogs.xml index ab5af6b92fcf..6fc976a43808 100644 --- a/AnkiDroid/src/main/res/values/03-dialogs.xml +++ b/AnkiDroid/src/main/res/values/03-dialogs.xml @@ -259,4 +259,6 @@ Due to Android privacy changes, your data and automated backups will be inaccessible if the app is uninstalled + Deck already exists +