From 016330188fa6e736d9abb9470b4caf95fa2ddb72 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Sun, 7 Jul 2024 03:38:39 +0200 Subject: [PATCH] Clarify note type ui model I found the code slightly unclear and tried to improve it. * NotetypeBasicUiModel was not related to basic note types * isStandard and useCount did not seem to me to have a clear meaning in anki lexicon, so must be document * toUiModel can't be appled to a standard entry as far as I understand it, and is in any case never applied to a standard note type currently, so we can remove the argument. --- .../ichi2/anki/notetype/AddNewNotesType.kt | 10 ++-- .../ichi2/anki/notetype/ManageNotetypes.kt | 36 ++++++++++---- .../ichi2/anki/notetype/NotetypeAdapter.kt | 48 +++++++++---------- .../ichi2/anki/notetype/NotetypeUiModel.kt | 25 +++++++--- 4 files changed, 73 insertions(+), 46 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/AddNewNotesType.kt b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/AddNewNotesType.kt index aa3f0bfbf4..35c9481730 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/AddNewNotesType.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/AddNewNotesType.kt @@ -53,7 +53,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) { .filter { it != StockNotetype.Kind.UNRECOGNIZED } .map { val stockNotetype = BackendUtils.from_json_bytes(getStockNotetypeLegacy(it)) - NotetypeBasicUiModel( + AddNotetypeUiModel( id = it.number.toLong(), name = stockNotetype.get("name") as String, isStandard = true @@ -61,7 +61,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) { } val foundNotetypes = getNotetypeNames() Pair( - mutableListOf().apply { + mutableListOf().apply { addAll(standardNotetypesModels) addAll(foundNotetypes.map { it.toUiModel() }) }, @@ -90,7 +90,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) { } private fun AlertDialog.initializeViewsWith( - optionsToDisplay: List, + optionsToDisplay: List, currentNames: List ) { val addPrefixStr = context.resources.getString(R.string.model_browser_add_add) @@ -131,7 +131,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) { } } - private fun addStandardNotetype(newName: String, selectedOption: NotetypeBasicUiModel) { + private fun addStandardNotetype(newName: String, selectedOption: AddNotetypeUiModel) { activity.launchCatchingTask { activity.runAndRefreshAfter { val kind = StockNotetype.Kind.forNumber(selectedOption.id.toInt()) @@ -144,7 +144,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) { } } - private fun cloneStandardNotetype(newName: String, model: NotetypeBasicUiModel) { + private fun cloneStandardNotetype(newName: String, model: AddNotetypeUiModel) { activity.launchCatchingTask { activity.runAndRefreshAfter { val targetNotetype = getNotetype(model.id) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt index 4e0dceddeb..f33549fc4c 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/ManageNotetypes.kt @@ -29,17 +29,33 @@ import androidx.appcompat.widget.SearchView import androidx.recyclerview.widget.RecyclerView import anki.notetypes.copy import com.google.android.material.floatingactionbutton.FloatingActionButton -import com.ichi2.anki.* +import com.ichi2.anki.AnkiActivity +import com.ichi2.anki.CardTemplateEditor import com.ichi2.anki.CollectionManager.withCol +import com.ichi2.anki.ModelFieldEditor +import com.ichi2.anki.R +import com.ichi2.anki.launchCatchingTask import com.ichi2.anki.snackbar.showSnackbar -import com.ichi2.libanki.* -import com.ichi2.utils.* +import com.ichi2.anki.userAcceptsSchemaChange +import com.ichi2.anki.withProgress +import com.ichi2.libanki.getNotetype +import com.ichi2.libanki.getNotetypeNameIdUseCount +import com.ichi2.libanki.getNotetypeNames +import com.ichi2.libanki.removeNotetype +import com.ichi2.libanki.updateNotetype +import com.ichi2.utils.getInputField +import com.ichi2.utils.input +import com.ichi2.utils.message +import com.ichi2.utils.negativeButton +import com.ichi2.utils.positiveButton +import com.ichi2.utils.show +import com.ichi2.utils.title class ManageNotetypes : AnkiActivity() { private lateinit var actionBar: ActionBar private lateinit var noteTypesList: RecyclerView - private var currentNotetypes: List = emptyList() + private var currentNotetypes: List = emptyList() private val notetypesAdapter: NotetypesAdapter by lazy { NotetypesAdapter( @@ -113,9 +129,9 @@ class ManageNotetypes : AnkiActivity() { } @SuppressLint("CheckResult") - private fun renameNotetype(noteTypeUiModel: NoteTypeUiModel) { + private fun renameNotetype(manageNoteTypeUiModel: ManageNoteTypeUiModel) { launchCatchingTask { - val allNotetypes = mutableListOf() + val allNotetypes = mutableListOf() allNotetypes.addAll( withProgress { withCol { getNotetypeNames().map { it.toUiModel() } } @@ -126,7 +142,7 @@ class ManageNotetypes : AnkiActivity() { positiveButton(R.string.rename) { launchCatchingTask { runAndRefreshAfter { - val initialNotetype = getNotetype(noteTypeUiModel.id) + val initialNotetype = getNotetype(manageNoteTypeUiModel.id) val renamedNotetype = initialNotetype.copy { this.name = (it as AlertDialog).getInputField().text.toString() } @@ -137,7 +153,7 @@ class ManageNotetypes : AnkiActivity() { negativeButton(R.string.dialog_cancel) setView(R.layout.dialog_generic_text_input) }.input( - prefill = noteTypeUiModel.name, + prefill = manageNoteTypeUiModel.name, waitForPositiveButton = false, displayKeyboard = true, callback = { dialog, text -> @@ -151,7 +167,7 @@ class ManageNotetypes : AnkiActivity() { } } - private fun deleteNotetype(noteTypeUiModel: NoteTypeUiModel) { + private fun deleteNotetype(manageNoteTypeUiModel: ManageNoteTypeUiModel) { launchCatchingTask { val messageResourceId: Int? = if (userAcceptsSchemaChange()) { withProgress { @@ -174,7 +190,7 @@ class ManageNotetypes : AnkiActivity() { message(messageResourceId) positiveButton(R.string.dialog_positive_delete) { launchCatchingTask { - runAndRefreshAfter { removeNotetype(noteTypeUiModel.id) } + runAndRefreshAfter { removeNotetype(manageNoteTypeUiModel.id) } } } negativeButton(R.string.dialog_cancel) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeAdapter.kt b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeAdapter.kt index 871c631ee0..7104f0010b 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeAdapter.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeAdapter.kt @@ -27,27 +27,27 @@ import androidx.recyclerview.widget.RecyclerView import com.ichi2.anki.R private val notetypeNamesAndCountDiff = - object : DiffUtil.ItemCallback() { + object : DiffUtil.ItemCallback() { override fun areItemsTheSame( - oldItem: NoteTypeUiModel, - newItem: NoteTypeUiModel + oldItem: ManageNoteTypeUiModel, + newItem: ManageNoteTypeUiModel ): Boolean = oldItem.id == newItem.id && oldItem.name == newItem.name && oldItem.useCount == newItem.useCount override fun areContentsTheSame( - oldItem: NoteTypeUiModel, - newItem: NoteTypeUiModel + oldItem: ManageNoteTypeUiModel, + newItem: ManageNoteTypeUiModel ): Boolean = oldItem.id == newItem.id && oldItem.name == newItem.name && oldItem.useCount == newItem.useCount } internal class NotetypesAdapter( context: Context, - private val onShowFields: (NoteTypeUiModel) -> Unit, - private val onEditCards: (NoteTypeUiModel) -> Unit, - private val onRename: (NoteTypeUiModel) -> Unit, - private val onDelete: (NoteTypeUiModel) -> Unit -) : ListAdapter(notetypeNamesAndCountDiff) { + private val onShowFields: (ManageNoteTypeUiModel) -> Unit, + private val onEditCards: (ManageNoteTypeUiModel) -> Unit, + private val onRename: (ManageNoteTypeUiModel) -> Unit, + private val onDelete: (ManageNoteTypeUiModel) -> Unit +) : ListAdapter(notetypeNamesAndCountDiff) { private val layoutInflater = LayoutInflater.from(context) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NotetypeViewHolder { @@ -67,33 +67,33 @@ internal class NotetypesAdapter( internal class NotetypeViewHolder( rowView: View, - onShowFields: (NoteTypeUiModel) -> Unit, - onEditCards: (NoteTypeUiModel) -> Unit, - onRename: (NoteTypeUiModel) -> Unit, - onDelete: (NoteTypeUiModel) -> Unit + onShowFields: (ManageNoteTypeUiModel) -> Unit, + onEditCards: (ManageNoteTypeUiModel) -> Unit, + onRename: (ManageNoteTypeUiModel) -> Unit, + onDelete: (ManageNoteTypeUiModel) -> Unit ) : RecyclerView.ViewHolder(rowView) { val name: TextView = rowView.findViewById(R.id.note_name) val useCount: TextView = rowView.findViewById(R.id.note_use_count) private val btnDelete: Button = rowView.findViewById(R.id.note_delete) private val btnRename: Button = rowView.findViewById(R.id.note_rename) private val btnEditCards: Button = rowView.findViewById(R.id.note_edit_cards) - private var noteTypeUiModel: NoteTypeUiModel? = null + private var mManageNoteTypeUiModel: ManageNoteTypeUiModel? = null private val resources = rowView.context.resources init { - rowView.setOnClickListener { noteTypeUiModel?.let(onShowFields) } - btnEditCards.setOnClickListener { noteTypeUiModel?.let(onEditCards) } - btnDelete.setOnClickListener { noteTypeUiModel?.let(onDelete) } - btnRename.setOnClickListener { noteTypeUiModel?.let(onRename) } + rowView.setOnClickListener { mManageNoteTypeUiModel?.let(onShowFields) } + btnEditCards.setOnClickListener { mManageNoteTypeUiModel?.let(onEditCards) } + btnDelete.setOnClickListener { mManageNoteTypeUiModel?.let(onDelete) } + btnRename.setOnClickListener { mManageNoteTypeUiModel?.let(onRename) } } - fun bind(noteTypeUiModel: NoteTypeUiModel) { - this.noteTypeUiModel = noteTypeUiModel - name.text = noteTypeUiModel.name + fun bind(manageNoteTypeUiModel: ManageNoteTypeUiModel) { + this.mManageNoteTypeUiModel = manageNoteTypeUiModel + name.text = manageNoteTypeUiModel.name useCount.text = resources.getQuantityString( R.plurals.model_browser_of_type, - noteTypeUiModel.useCount, - noteTypeUiModel.useCount + manageNoteTypeUiModel.useCount, + manageNoteTypeUiModel.useCount ) } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeUiModel.kt b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeUiModel.kt index 6303072c3c..99945bb0ee 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeUiModel.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeUiModel.kt @@ -22,23 +22,34 @@ import anki.notetypes.NotetypeNameIdUseCount * Data holder class which contains the data to display a single note type in [ManageNotetypes]'s * list of notetypes. */ -internal data class NoteTypeUiModel( +internal data class ManageNoteTypeUiModel( val id: Long, val name: String, + /** + * The number of note using this note type. + */ val useCount: Int ) -internal fun NotetypeNameIdUseCount.toUiModel(): NoteTypeUiModel = - NoteTypeUiModel(id, name, useCount) +internal fun NotetypeNameIdUseCount.toUiModel(): ManageNoteTypeUiModel = + ManageNoteTypeUiModel(id, name, useCount) /** - * Simplest data holder class which contains only the id and name of a notetype. + * Data holder class which contains the data to display a single note type in [AddNewNotesType]'s + * list of notetypes. */ -internal data class NotetypeBasicUiModel( +internal data class AddNotetypeUiModel( val id: Long, val name: String, + /** + * Whether this is a note type provided by Anki by default. + * If false, this is one of the note type currently in this collection (potentially a clone of a standard note type) + */ val isStandard: Boolean = false ) -internal fun NotetypeNameId.toUiModel(isStandard: Boolean = false): NotetypeBasicUiModel = - NotetypeBasicUiModel(id, name, isStandard) +/** + * A note type from current collection as a [AddNotetypeUiModel]. + */ +internal fun NotetypeNameId.toUiModel(): AddNotetypeUiModel = + AddNotetypeUiModel(id, name, false)