Skip to content

Commit 0086523

Browse files
committed
chore: convert NotetypeAdapter to ViewBinding
Issue 11116
1 parent d99fe7f commit 0086523

2 files changed

Lines changed: 20 additions & 27 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/notetype/NotetypeAdapter.kt

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ package com.ichi2.anki.notetype
1717

1818
import android.content.Context
1919
import android.view.LayoutInflater
20-
import android.view.View
2120
import android.view.ViewGroup
22-
import android.widget.Button
23-
import android.widget.TextView
2421
import androidx.recyclerview.widget.DiffUtil
2522
import androidx.recyclerview.widget.ListAdapter
2623
import androidx.recyclerview.widget.RecyclerView
2724
import com.ichi2.anki.R
25+
import com.ichi2.anki.databinding.ItemManageNoteTypeBinding
2826

2927
private val notetypeNamesAndCountDiff =
3028
object : DiffUtil.ItemCallback<ManageNoteTypeUiModel>() {
@@ -53,7 +51,7 @@ internal class NotetypesAdapter(
5351
viewType: Int,
5452
): NotetypeViewHolder =
5553
NotetypeViewHolder(
56-
rowView = layoutInflater.inflate(R.layout.item_manage_note_type, parent, false),
54+
binding = ItemManageNoteTypeBinding.inflate(layoutInflater, parent, false),
5755
onDelete = onDelete,
5856
onRename = onRename,
5957
onEditCards = onEditCards,
@@ -69,31 +67,26 @@ internal class NotetypesAdapter(
6967
}
7068

7169
internal class NotetypeViewHolder(
72-
rowView: View,
70+
private val binding: ItemManageNoteTypeBinding,
7371
onShowFields: (ManageNoteTypeUiModel) -> Unit,
7472
onEditCards: (ManageNoteTypeUiModel) -> Unit,
7573
onRename: (ManageNoteTypeUiModel) -> Unit,
7674
onDelete: (ManageNoteTypeUiModel) -> Unit,
77-
) : RecyclerView.ViewHolder(rowView) {
78-
val name: TextView = rowView.findViewById(R.id.note_name)
79-
val useCount: TextView = rowView.findViewById(R.id.note_use_count)
80-
private val btnDelete: Button = rowView.findViewById(R.id.note_delete)
81-
private val btnRename: Button = rowView.findViewById(R.id.note_rename)
82-
private val btnEditCards: Button = rowView.findViewById(R.id.note_edit_cards)
83-
private var mManageNoteTypeUiModel: ManageNoteTypeUiModel? = null
84-
private val resources = rowView.context.resources
75+
) : RecyclerView.ViewHolder(binding.root) {
76+
private var manageNoteTypeUiModel: ManageNoteTypeUiModel? = null
77+
private val resources = binding.root.context.resources
8578

8679
init {
87-
rowView.setOnClickListener { mManageNoteTypeUiModel?.let(onShowFields) }
88-
btnEditCards.setOnClickListener { mManageNoteTypeUiModel?.let(onEditCards) }
89-
btnDelete.setOnClickListener { mManageNoteTypeUiModel?.let(onDelete) }
90-
btnRename.setOnClickListener { mManageNoteTypeUiModel?.let(onRename) }
80+
binding.root.setOnClickListener { manageNoteTypeUiModel?.let(onShowFields) }
81+
binding.editCardsButton.setOnClickListener { manageNoteTypeUiModel?.let(onEditCards) }
82+
binding.deleteNoteTypeButton.setOnClickListener { manageNoteTypeUiModel?.let(onDelete) }
83+
binding.renameNoteTypeButton.setOnClickListener { manageNoteTypeUiModel?.let(onRename) }
9184
}
9285

9386
fun bind(manageNoteTypeUiModel: ManageNoteTypeUiModel) {
94-
this.mManageNoteTypeUiModel = manageNoteTypeUiModel
95-
name.text = manageNoteTypeUiModel.name
96-
useCount.text =
87+
this.manageNoteTypeUiModel = manageNoteTypeUiModel
88+
binding.noteName.text = manageNoteTypeUiModel.name
89+
binding.noteUseCount.text =
9790
resources.getQuantityString(
9891
R.plurals.model_browser_of_type,
9992
manageNoteTypeUiModel.useCount,

AnkiDroid/src/main/res/layout/item_manage_note_type.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
android:layout_width="match_parent"
3838
android:layout_height="wrap_content" >
3939
<Button
40-
android:id="@+id/note_edit_cards"
40+
android:id="@+id/edit_cards_button"
4141
android:layout_width="wrap_content"
4242
android:layout_height="wrap_content"
4343
style="@style/Widget.Material3.Button.TextButton"
@@ -46,33 +46,33 @@
4646
app:layout_constraintTop_toTopOf="parent"
4747
app:layout_constraintBottom_toBottomOf="parent"
4848
app:layout_constraintStart_toStartOf="parent"
49-
app:layout_constraintEnd_toStartOf="@id/note_rename"
49+
app:layout_constraintEnd_toStartOf="@id/rename_note_type_button"
5050
app:layout_constraintHorizontal_chainStyle="packed"
5151
app:layout_constraintHorizontal_bias="0.0" />
5252

5353
<com.google.android.material.button.MaterialButton
54-
android:id="@+id/note_rename"
54+
android:id="@+id/rename_note_type_button"
5555
android:layout_width="wrap_content"
5656
android:layout_height="wrap_content"
5757
style="@style/Widget.Material3.Button.TextButton"
5858
android:textColor="?attr/colorAccent"
5959
android:text="@string/model_browser_rename"
6060
app:layout_constraintTop_toTopOf="parent"
6161
app:layout_constraintBottom_toBottomOf="parent"
62-
app:layout_constraintStart_toEndOf="@id/note_edit_cards"
62+
app:layout_constraintStart_toEndOf="@id/edit_cards_button"
6363
app:layout_constraintEnd_toStartOf="@id/spacer" />
6464

6565
<android.widget.Space
6666
android:id="@+id/spacer"
6767
android:layout_width="0dp"
6868
android:layout_height="0dp"
6969
android:layout_weight="1"
70-
app:layout_constraintStart_toEndOf="@id/note_rename"
71-
app:layout_constraintEnd_toStartOf="@id/note_delete"
70+
app:layout_constraintStart_toEndOf="@id/rename_note_type_button"
71+
app:layout_constraintEnd_toStartOf="@id/delete_note_type_button"
7272
/>
7373

7474
<com.google.android.material.button.MaterialButton
75-
android:id="@+id/note_delete"
75+
android:id="@+id/delete_note_type_button"
7676
android:layout_width="wrap_content"
7777
android:layout_height="wrap_content"
7878
style="@style/Widget.Material3.Button.TextButton"

0 commit comments

Comments
 (0)