mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 03:52:15 +02:00
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.
This commit is contained in:
parent
34263d1c87
commit
016330188f
@ -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<NotetypeBasicUiModel>().apply {
|
||||
mutableListOf<AddNotetypeUiModel>().apply {
|
||||
addAll(standardNotetypesModels)
|
||||
addAll(foundNotetypes.map { it.toUiModel() })
|
||||
},
|
||||
@ -90,7 +90,7 @@ class AddNewNotesType(private val activity: ManageNotetypes) {
|
||||
}
|
||||
|
||||
private fun AlertDialog.initializeViewsWith(
|
||||
optionsToDisplay: List<NotetypeBasicUiModel>,
|
||||
optionsToDisplay: List<AddNotetypeUiModel>,
|
||||
currentNames: List<String>
|
||||
) {
|
||||
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)
|
||||
|
@ -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<NoteTypeUiModel> = emptyList()
|
||||
private var currentNotetypes: List<ManageNoteTypeUiModel> = 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<NotetypeBasicUiModel>()
|
||||
val allNotetypes = mutableListOf<AddNotetypeUiModel>()
|
||||
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)
|
||||
|
@ -27,27 +27,27 @@ import androidx.recyclerview.widget.RecyclerView
|
||||
import com.ichi2.anki.R
|
||||
|
||||
private val notetypeNamesAndCountDiff =
|
||||
object : DiffUtil.ItemCallback<NoteTypeUiModel>() {
|
||||
object : DiffUtil.ItemCallback<ManageNoteTypeUiModel>() {
|
||||
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<NoteTypeUiModel, NotetypeViewHolder>(notetypeNamesAndCountDiff) {
|
||||
private val onShowFields: (ManageNoteTypeUiModel) -> Unit,
|
||||
private val onEditCards: (ManageNoteTypeUiModel) -> Unit,
|
||||
private val onRename: (ManageNoteTypeUiModel) -> Unit,
|
||||
private val onDelete: (ManageNoteTypeUiModel) -> Unit
|
||||
) : ListAdapter<ManageNoteTypeUiModel, NotetypeViewHolder>(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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user