0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-19 19:42:17 +02:00

Fix adding a notetype when using the name of a standard notetype

This fixes a bug where the user couldn't add a notetype with the name
of one of the standard notetypes(even if it didn't existed), because
the code checked the new name against the existing notetypes names +
the standard notetypes names so it behaved like the name already
existed.

The change makes the code look only at the existing notetypes names.
This commit is contained in:
lukstbit 2024-06-24 08:54:54 +03:00 committed by Mike Hardy
parent 969c0e8f05
commit d8831cf24a

View File

@ -47,7 +47,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) {
private lateinit var dialogView: View
suspend fun showAddNewNotetypeDialog() {
dialogView = LayoutInflater.from(activity).inflate(R.layout.dialog_new_note_type, null)
val optionsToDisplay = activity.withProgress {
val (allOptions, currentNames) = activity.withProgress {
withCol {
val standardNotetypesModels = StockNotetype.Kind.entries
.filter { it != StockNotetype.Kind.UNRECOGNIZED }
@ -59,10 +59,14 @@ class AddNewNotesType(private val activity: ManageNotetypes) {
isStandard = true
)
}
mutableListOf<NotetypeBasicUiModel>().apply {
addAll(standardNotetypesModels)
addAll(getNotetypeNames().map { it.toUiModel() })
}
val foundNotetypes = getNotetypeNames()
Pair(
mutableListOf<NotetypeBasicUiModel>().apply {
addAll(standardNotetypesModels)
addAll(foundNotetypes.map { it.toUiModel() })
},
foundNotetypes.map { it.name }
)
}
}
val dialog = AlertDialog.Builder(activity).apply {
@ -73,7 +77,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) {
val selectedPosition =
dialogView.findViewById<Spinner>(R.id.notetype_new_type).selectedItemPosition
if (selectedPosition == AdapterView.INVALID_POSITION) return@positiveButton
val selectedOption = optionsToDisplay[selectedPosition]
val selectedOption = allOptions[selectedPosition]
if (selectedOption.isStandard) {
addStandardNotetype(newName, selectedOption)
} else {
@ -82,17 +86,20 @@ class AddNewNotesType(private val activity: ManageNotetypes) {
}
negativeButton(R.string.dialog_cancel)
}.show()
dialog.initializeViewsWith(optionsToDisplay)
dialog.initializeViewsWith(allOptions, currentNames)
}
private fun AlertDialog.initializeViewsWith(optionsToDisplay: List<NotetypeBasicUiModel>) {
private fun AlertDialog.initializeViewsWith(
optionsToDisplay: List<NotetypeBasicUiModel>,
currentNames: List<String>
) {
val addPrefixStr = context.resources.getString(R.string.model_browser_add_add)
val clonePrefixStr = context.resources.getString(R.string.model_browser_add_clone)
val nameInput = dialogView.findViewById<EditText>(R.id.notetype_new_name)
nameInput.addTextChangedListener { editableText ->
val currentName = editableText?.toString() ?: ""
positiveButton.isEnabled =
currentName.isNotEmpty() && !optionsToDisplay.map { it.name }.contains(currentName)
currentName.isNotEmpty() && !currentNames.contains(currentName)
}
dialogView.findViewById<Spinner>(R.id.notetype_new_type).apply {
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {