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

Loading…
Cancel
Save