feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 8df8506e97
commit 16e7e8a4cd
  1. 8
      app/src/main/java/io/legado/app/ui/widget/prefs/ColorPreference.kt
  2. 15
      app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt

@ -417,14 +417,6 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
return dialog return dialog
} }
/**
* Create and show the [ColorPickerDialog] created with this builder.
*
* @param activity The current activity.
*/
fun show(activity: FragmentActivity) {
create().show(activity.supportFragmentManager, "color-picker-dialog")
}
} }
} }

@ -60,8 +60,9 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
override fun onClick() { override fun onClick() {
getActivity()?.let { getActivity()?.let {
IconDialog().apply { val dialog = IconDialog().apply {
val args = Bundle() val args = Bundle()
args.putString("value", value)
args.putCharSequenceArray("entries", entries) args.putCharSequenceArray("entries", entries)
args.putCharSequenceArray("entryValues", entryValues) args.putCharSequenceArray("entryValues", entryValues)
args.putCharSequenceArray("iconNames", iconNames) args.putCharSequenceArray("iconNames", iconNames)
@ -69,7 +70,11 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
onChanged = { value -> onChanged = { value ->
this@IconListPreference.value = value this@IconListPreference.value = value
} }
}.show(it.supportFragmentManager, "iconDialog") }
it.supportFragmentManager
.beginTransaction()
.add(dialog, getFragmentTag())
.commitAllowingStateLoss()
} }
} }
@ -86,10 +91,14 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
return null return null
} }
fun getFragmentTag(): String {
return "icon_$key"
}
class IconDialog : DialogFragment() { class IconDialog : DialogFragment() {
var onChanged: ((value: String) -> Unit)? = null var onChanged: ((value: String) -> Unit)? = null
var dialogValue: String? = null
var dialogEntries: Array<CharSequence>? = null var dialogEntries: Array<CharSequence>? = null
var dialogEntryValues: Array<CharSequence>? = null var dialogEntryValues: Array<CharSequence>? = null
var dialogIconNames: Array<CharSequence>? = null var dialogIconNames: Array<CharSequence>? = null
@ -119,6 +128,7 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
val adapter = Adapter(requireContext()) val adapter = Adapter(requireContext())
recycler_view.adapter = adapter recycler_view.adapter = adapter
arguments?.let { arguments?.let {
dialogValue = it.getString("value")
dialogEntries = it.getCharSequenceArray("entries") dialogEntries = it.getCharSequenceArray("entries")
dialogEntryValues = it.getCharSequenceArray("entryValues") dialogEntryValues = it.getCharSequenceArray("entryValues")
dialogIconNames = it.getCharSequenceArray("iconNames") dialogIconNames = it.getCharSequenceArray("iconNames")
@ -148,6 +158,7 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
val d = context.getCompatDrawable(resId) val d = context.getCompatDrawable(resId)
icon.setImageDrawable(d) icon.setImageDrawable(d)
} }
label.isChecked = item.toString() == dialogValue
onClick { onClick {
onChanged?.invoke(item.toString()) onChanged?.invoke(item.toString())
this@IconDialog.dismiss() this@IconDialog.dismiss()

Loading…
Cancel
Save