feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 50ce3412bb
commit e7b9f80011
  1. 28
      app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt
  2. 5
      app/src/main/res/layout/item_icon_preference.xml
  3. 1
      app/src/main/res/xml/pref_config_theme.xml

@ -85,22 +85,22 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference
} }
return null return null
} }
}
class IconDialog : DialogFragment() { class IconDialog : DialogFragment() {
var onChanged: ((value: String) -> Unit)? = null var onChanged: ((value: String) -> Unit)? = null
var entries: Array<CharSequence>? = null var dialogEntries: Array<CharSequence>? = null
var entryValues: Array<CharSequence>? = null var dialogEntryValues: Array<CharSequence>? = null
var iconNames: Array<CharSequence>? = null var dialogIconNames: Array<CharSequence>? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
val dm = DisplayMetrics() val dm = DisplayMetrics()
activity?.windowManager?.defaultDisplay?.getMetrics(dm) activity?.windowManager?.defaultDisplay?.getMetrics(dm)
dialog?.window?.setLayout( dialog?.window?.setLayout(
(dm.widthPixels * 0.9).toInt(), (dm.widthPixels * 0.8).toInt(),
(dm.heightPixels * 0.9).toInt() ViewGroup.LayoutParams.WRAP_CONTENT
) )
} }
@ -119,10 +119,10 @@ class IconDialog : DialogFragment() {
val adapter = Adapter(requireContext()) val adapter = Adapter(requireContext())
recycler_view.adapter = adapter recycler_view.adapter = adapter
arguments?.let { arguments?.let {
entries = it.getCharSequenceArray("entries") dialogEntries = it.getCharSequenceArray("entries")
entryValues = it.getCharSequenceArray("entryValues") dialogEntryValues = it.getCharSequenceArray("entryValues")
iconNames = it.getCharSequenceArray("iconNames") dialogIconNames = it.getCharSequenceArray("iconNames")
entryValues?.let { values -> dialogEntryValues?.let { values ->
adapter.setItems(values.toList()) adapter.setItems(values.toList())
} }
} }
@ -139,10 +139,10 @@ class IconDialog : DialogFragment() {
) { ) {
with(holder.itemView) { with(holder.itemView) {
val index = findIndexOfValue(item.toString()) val index = findIndexOfValue(item.toString())
entries?.let { dialogEntries?.let {
label.text = it[index] label.text = it[index]
} }
iconNames?.let { dialogIconNames?.let {
val resId = context.resources val resId = context.resources
.getIdentifier(it[index].toString(), "mipmap", context.packageName) .getIdentifier(it[index].toString(), "mipmap", context.packageName)
val d = context.getCompatDrawable(resId) val d = context.getCompatDrawable(resId)
@ -150,12 +150,13 @@ class IconDialog : DialogFragment() {
} }
onClick { onClick {
onChanged?.invoke(item.toString()) onChanged?.invoke(item.toString())
this@IconDialog.dismiss()
} }
} }
} }
private fun findIndexOfValue(value: String?): Int { private fun findIndexOfValue(value: String?): Int {
entryValues?.let { values -> dialogEntryValues?.let { values ->
for (i in values.indices.reversed()) { for (i in values.indices.reversed()) {
if (values[i] == value) { if (values[i] == value) {
return i return i
@ -166,3 +167,4 @@ class IconDialog : DialogFragment() {
} }
} }
} }
}

@ -11,13 +11,14 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:adjustViewBounds="true" android:adjustViewBounds="true"
android:contentDescription="ICON" android:contentDescription="ICON"
android:padding="6dip" android:padding="10dip"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
<CheckedTextView <CheckedTextView
android:id="@+id/label" android:id="@+id/label"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingRight="10dp"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/icon" android:layout_toRightOf="@+id/icon"
android:checkMark="?android:attr/listChoiceIndicatorSingle" android:checkMark="?android:attr/listChoiceIndicatorSingle"
@ -28,6 +29,6 @@
android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorAlertDialogListItem" android:textColor="?android:attr/textColorAlertDialogListItem"
android:textIsSelectable="false" android:textIsSelectable="false"
tools:ignore="RtlHardcoded" /> tools:ignore="RtlHardcoded,RtlSymmetry" />
</RelativeLayout> </RelativeLayout>

@ -6,6 +6,7 @@
android:defaultValue="icon_main" android:defaultValue="icon_main"
android:key="launcherIcon" android:key="launcherIcon"
android:title="@string/change_icon" android:title="@string/change_icon"
android:summary="@string/change_icon_summary"
android:entryValues="@array/icons" android:entryValues="@array/icons"
android:entries="@array/icon_names" android:entries="@array/icon_names"
app:icons="@array/icons" app:icons="@array/icons"

Loading…
Cancel
Save