Skip to content

Commit d0d87d2

Browse files
committed
chore: convert AlertDialogFacade to ViewBinding
Issue 11116
1 parent 655eddc commit d0d87d2

2 files changed

Lines changed: 25 additions & 27 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/utils/AlertDialogFacade.kt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ import android.widget.Button
3030
import android.widget.CheckBox
3131
import android.widget.EditText
3232
import android.widget.FrameLayout
33-
import android.widget.ImageView
34-
import android.widget.ListView
35-
import android.widget.TextView
3633
import androidx.annotation.DrawableRes
3734
import androidx.annotation.StringRes
3835
import androidx.appcompat.app.AlertDialog
@@ -42,9 +39,12 @@ import androidx.recyclerview.widget.LinearLayoutManager
4239
import androidx.recyclerview.widget.RecyclerView
4340
import com.google.android.material.textfield.TextInputLayout
4441
import com.ichi2.anki.R
42+
import com.ichi2.anki.databinding.AlertDialogCheckboxBinding
43+
import com.ichi2.anki.databinding.AlertDialogTitleWithHelpBinding
44+
import com.ichi2.anki.databinding.DialogGenericRecyclerViewBinding
45+
import com.ichi2.anki.databinding.DialogListviewMessageBinding
4546
import com.ichi2.themes.Theme
4647
import com.ichi2.themes.Themes
47-
import com.ichi2.ui.FixedTextView
4848
import com.ichi2.utils.HandlerUtils.executeOnMainThread
4949
import timber.log.Timber
5050

@@ -209,8 +209,8 @@ fun AlertDialog.Builder.checkBoxPrompt(
209209
if (stringRes == null && text == null) {
210210
throw IllegalArgumentException("either `stringRes` or `text` must be set")
211211
}
212-
val checkBoxView = View.inflate(this.context, R.layout.alert_dialog_checkbox, null)
213-
val checkBox = checkBoxView.findViewById<CheckBox>(R.id.checkbox)
212+
val viewBinding = AlertDialogCheckboxBinding.inflate(LayoutInflater.from(context))
213+
val checkBox = viewBinding.checkbox
214214

215215
val checkBoxLabel = if (stringRes != null) context.getString(stringRes) else text
216216
checkBox.text = checkBoxLabel
@@ -220,7 +220,7 @@ fun AlertDialog.Builder.checkBoxPrompt(
220220
onToggle(isChecked)
221221
}
222222

223-
return this.setView(checkBoxView)
223+
return this.setView(viewBinding.root)
224224
}
225225

226226
fun AlertDialog.getCheckBoxPrompt(): CheckBox =
@@ -263,10 +263,11 @@ fun AlertDialog.Builder.customView(
263263
}
264264

265265
fun AlertDialog.Builder.customListAdapter(adapter: RecyclerView.Adapter<*>) {
266-
val recyclerView = LayoutInflater.from(context).inflate(R.layout.dialog_generic_recycler_view, null, false) as RecyclerView
266+
val viewBinding = DialogGenericRecyclerViewBinding.inflate(LayoutInflater.from(context))
267+
val recyclerView = viewBinding.dialogRecyclerView
267268
recyclerView.adapter = adapter
268269
recyclerView.layoutManager = LinearLayoutManager(context)
269-
this.setView(recyclerView)
270+
this.setView(viewBinding.root)
270271
}
271272

272273
/**
@@ -278,12 +279,13 @@ fun AlertDialog.Builder.customListAdapterWithDecoration(
278279
adapter: RecyclerView.Adapter<*>,
279280
context: Context,
280281
) {
281-
val recyclerView = LayoutInflater.from(context).inflate(R.layout.dialog_generic_recycler_view, null, false) as RecyclerView
282+
val viewBinding = DialogGenericRecyclerViewBinding.inflate(LayoutInflater.from(context))
283+
val recyclerView = viewBinding.dialogRecyclerView
282284
recyclerView.adapter = adapter
283285
recyclerView.layoutManager = LinearLayoutManager(context)
284286
val dividerItemDecoration = DividerItemDecoration(recyclerView.context, LinearLayoutManager.VERTICAL)
285287
recyclerView.addItemDecoration(dividerItemDecoration)
286-
this.setView(recyclerView)
288+
this.setView(viewBinding.root)
287289
}
288290

289291
/**
@@ -413,17 +415,15 @@ fun AlertDialog.Builder.listItemsAndMessage(
413415
items: List<CharSequence>,
414416
onClick: (dialog: DialogInterface, index: Int) -> Unit,
415417
): AlertDialog.Builder {
416-
val dialogView = View.inflate(this.context, R.layout.dialog_listview_message, null)
417-
dialogView.findViewById<FixedTextView>(R.id.dialog_message).text = message
418-
419-
val listView = dialogView.findViewById<ListView>(R.id.dialog_list_view)
420-
listView.adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, items)
418+
val viewBinding = DialogListviewMessageBinding.inflate(LayoutInflater.from(context))
419+
viewBinding.message.text = message
420+
viewBinding.listView.adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, items)
421421

422422
val dialog = this.create()
423-
listView.setOnItemClickListener { _, _, index, _ ->
423+
viewBinding.listView.setOnItemClickListener { _, _, index, _ ->
424424
onClick(dialog, index)
425425
}
426-
return this.setView(dialogView)
426+
return this.setView(viewBinding.root)
427427
}
428428

429429
/**
@@ -447,20 +447,18 @@ fun AlertDialog.Builder.titleWithHelpIcon(
447447
block: View.OnClickListener,
448448
) {
449449
// setup the view for the dialog
450-
val customTitleView = LayoutInflater.from(context).inflate(R.layout.alert_dialog_title_with_help, null, false)
451-
setCustomTitle(customTitleView)
450+
val viewBinding = AlertDialogTitleWithHelpBinding.inflate(LayoutInflater.from(context))
451+
setCustomTitle(viewBinding.root)
452452

453453
// apply a custom title
454-
val titleTextView = customTitleView.findViewById<TextView>(android.R.id.title)
455-
456454
if (stringRes != null) {
457-
titleTextView.setText(stringRes)
455+
viewBinding.title.setText(stringRes)
458456
} else if (text != null) {
459-
titleTextView.text = text
457+
viewBinding.title.text = text
460458
}
461459

462460
// set the action when clicking the help icon
463-
customTitleView.findViewById<ImageView>(R.id.help_icon).setOnClickListener { v ->
461+
viewBinding.helpIcon.setOnClickListener { v ->
464462
Timber.i("dialog help icon click")
465463
block.onClick(v)
466464
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
<!--Dialog Message-->
2727
<com.ichi2.ui.FixedTextView
28-
android:id="@+id/dialog_message"
28+
android:id="@+id/message"
2929
android:layout_width="match_parent"
3030
android:layout_height="wrap_content"
3131
android:paddingStart="@dimen/side_margin"
@@ -36,7 +36,7 @@
3636

3737
<!--Dialog ListItem-->
3838
<ListView
39-
android:id="@+id/dialog_list_view"
39+
android:id="@+id/list_view"
4040
android:layout_width="match_parent"
4141
android:layout_height="wrap_content"
4242
android:clipToPadding="false"

0 commit comments

Comments
 (0)