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