refactor: Use withStyledAttributes in FontFamilyDialogFragment (#827)

Ensures it is not possible to forget to recycle the attribute array.
This commit is contained in:
Nik Clayton 2024-07-22 16:29:53 +02:00 committed by GitHub
parent 526ee27a6a
commit c4bc7f81da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 13 deletions

View File

@ -26,9 +26,11 @@ import android.widget.ArrayAdapter
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.R import androidx.appcompat.R
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.content.withStyledAttributes
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.ListPreferenceDialogFragmentCompat import androidx.preference.ListPreferenceDialogFragmentCompat
import app.pachli.core.designsystem.EmbeddedFontFamily import app.pachli.core.designsystem.EmbeddedFontFamily
import kotlin.properties.Delegates
/** /**
* Dialog fragment for choosing a font family. Displays the list of font families with each * Dialog fragment for choosing a font family. Displays the list of font families with each
@ -38,6 +40,7 @@ class FontFamilyDialogFragment : ListPreferenceDialogFragmentCompat() {
private var clickedDialogEntryIndex = 0 private var clickedDialogEntryIndex = 0
private lateinit var entries: Array<CharSequence> private lateinit var entries: Array<CharSequence>
private lateinit var entryValues: Array<CharSequence> private lateinit var entryValues: Array<CharSequence>
private var itemLayout by Delegates.notNull<Int>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -54,6 +57,13 @@ class FontFamilyDialogFragment : ListPreferenceDialogFragmentCompat() {
entries = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRIES) as Array<CharSequence> entries = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRIES) as Array<CharSequence>
entryValues = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRY_VALUES) as Array<CharSequence> entryValues = savedInstanceState.getCharSequenceArray(SAVE_STATE_ENTRY_VALUES) as Array<CharSequence>
} }
// Use the same layout AlertDialog uses, as android.R.layout.simple_list_item_single_choice
// puts the radio button at the end of the line, but the default dialog style puts it at
// the start.
requireContext().withStyledAttributes(null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0) {
itemLayout = getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0)
}
} }
override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) { override fun onPrepareDialogBuilder(builder: AlertDialog.Builder) {
@ -61,19 +71,7 @@ class FontFamilyDialogFragment : ListPreferenceDialogFragmentCompat() {
val context = requireContext() val context = requireContext()
// Use the same layout AlertDialog uses, as android.R.layout.simple_list_item_single_choice val adapter = object : ArrayAdapter<CharSequence>(context, itemLayout, entries) {
// puts the radio button at the end of the line, but the default dialog style puts it at
// the start.
val a = context.obtainStyledAttributes(
null,
R.styleable.AlertDialog,
R.attr.alertDialogStyle,
0,
)
val layout = a.getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0)
a.recycle()
val adapter = object : ArrayAdapter<CharSequence>(context, layout, entries) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent) val view = super.getView(position, convertView, parent)