Skip to content

Commit

Permalink
feat: Validate duplicate deck name (#15808)
Browse files Browse the repository at this point in the history
* fix: can't create first deck if named 'default'
* Checks if deck name already exists before creating one.

Fixes 15678
Fixes 15759
  • Loading branch information
gourabsingha1 authored Mar 8, 2024
1 parent 6371e7b commit cebb457
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
13 changes: 13 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions AnkiDroid/src/main/res/layout/dialog_generic_text_input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
-->


<com.google.android.material.textfield.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dialog_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/content_vertical_padding"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:paddingBottom="4dp"
android:paddingTop="16dp" >
app:errorEnabled="true"
android:paddingTop="16dp">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/dialog_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
/>
</com.google.android.material.textfield.TextInputLayout>
</com.google.android.material.textfield.TextInputLayout>
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/res/values/03-dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,6 @@
Due to Android privacy changes, your data and automated backups will be inaccessible if the app is uninstalled
</string>

<string name="deck_already_exists">Deck already exists</string>

</resources>

0 comments on commit cebb457

Please sign in to comment.