From e7b9f80011bf099f7467a9182910b7f1ce2a0700 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 13 Feb 2020 13:50:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/widget/prefs/IconListPreference.kt | 132 +++++++++--------- .../main/res/layout/item_icon_preference.xml | 5 +- app/src/main/res/xml/pref_config_theme.xml | 1 + 3 files changed, 71 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt index 4570e7fe3..bf7a5755d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/IconListPreference.kt @@ -85,84 +85,86 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference } return null } -} -class IconDialog : DialogFragment() { - - var onChanged: ((value: String) -> Unit)? = null - var entries: Array? = null - var entryValues: Array? = null - var iconNames: Array? = null - - override fun onStart() { - super.onStart() - val dm = DisplayMetrics() - activity?.windowManager?.defaultDisplay?.getMetrics(dm) - dialog?.window?.setLayout( - (dm.widthPixels * 0.9).toInt(), - (dm.heightPixels * 0.9).toInt() - ) - } - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - return inflater.inflate(R.layout.dialog_recycler_view, container) - } + class IconDialog : DialogFragment() { - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - tool_bar.setTitle(R.string.change_icon) - recycler_view.layoutManager = LinearLayoutManager(requireContext()) - val adapter = Adapter(requireContext()) - recycler_view.adapter = adapter - arguments?.let { - entries = it.getCharSequenceArray("entries") - entryValues = it.getCharSequenceArray("entryValues") - iconNames = it.getCharSequenceArray("iconNames") - entryValues?.let { values -> - adapter.setItems(values.toList()) + var onChanged: ((value: String) -> Unit)? = null + var dialogEntries: Array? = null + var dialogEntryValues: Array? = null + var dialogIconNames: Array? = null + + override fun onStart() { + super.onStart() + val dm = DisplayMetrics() + activity?.windowManager?.defaultDisplay?.getMetrics(dm) + dialog?.window?.setLayout( + (dm.widthPixels * 0.8).toInt(), + ViewGroup.LayoutParams.WRAP_CONTENT + ) + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + return inflater.inflate(R.layout.dialog_recycler_view, container) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + tool_bar.setTitle(R.string.change_icon) + recycler_view.layoutManager = LinearLayoutManager(requireContext()) + val adapter = Adapter(requireContext()) + recycler_view.adapter = adapter + arguments?.let { + dialogEntries = it.getCharSequenceArray("entries") + dialogEntryValues = it.getCharSequenceArray("entryValues") + dialogIconNames = it.getCharSequenceArray("iconNames") + dialogEntryValues?.let { values -> + adapter.setItems(values.toList()) + } } } - } - inner class Adapter(context: Context) : - SimpleRecyclerAdapter(context, R.layout.item_icon_preference) { + inner class Adapter(context: Context) : + SimpleRecyclerAdapter(context, R.layout.item_icon_preference) { - override fun convert( - holder: ItemViewHolder, - item: CharSequence, - payloads: MutableList - ) { - with(holder.itemView) { - val index = findIndexOfValue(item.toString()) - entries?.let { - label.text = it[index] - } - iconNames?.let { - val resId = context.resources - .getIdentifier(it[index].toString(), "mipmap", context.packageName) - val d = context.getCompatDrawable(resId) - icon.setImageDrawable(d) - } - onClick { - onChanged?.invoke(item.toString()) + override fun convert( + holder: ItemViewHolder, + item: CharSequence, + payloads: MutableList + ) { + with(holder.itemView) { + val index = findIndexOfValue(item.toString()) + dialogEntries?.let { + label.text = it[index] + } + dialogIconNames?.let { + val resId = context.resources + .getIdentifier(it[index].toString(), "mipmap", context.packageName) + val d = context.getCompatDrawable(resId) + icon.setImageDrawable(d) + } + onClick { + onChanged?.invoke(item.toString()) + this@IconDialog.dismiss() + } } } - } - private fun findIndexOfValue(value: String?): Int { - entryValues?.let { values -> - for (i in values.indices.reversed()) { - if (values[i] == value) { - return i + private fun findIndexOfValue(value: String?): Int { + dialogEntryValues?.let { values -> + for (i in values.indices.reversed()) { + if (values[i] == value) { + return i + } } } + return -1 } - return -1 } } -} \ No newline at end of file +} diff --git a/app/src/main/res/layout/item_icon_preference.xml b/app/src/main/res/layout/item_icon_preference.xml index b8231284c..543a0841c 100644 --- a/app/src/main/res/layout/item_icon_preference.xml +++ b/app/src/main/res/layout/item_icon_preference.xml @@ -11,13 +11,14 @@ android:layout_height="match_parent" android:adjustViewBounds="true" android:contentDescription="ICON" - android:padding="6dip" + android:padding="10dip" tools:ignore="HardcodedText" /> + tools:ignore="RtlHardcoded,RtlSymmetry" /> \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config_theme.xml b/app/src/main/res/xml/pref_config_theme.xml index 422fc3bff..16a309b65 100644 --- a/app/src/main/res/xml/pref_config_theme.xml +++ b/app/src/main/res/xml/pref_config_theme.xml @@ -6,6 +6,7 @@ android:defaultValue="icon_main" android:key="launcherIcon" android:title="@string/change_icon" + android:summary="@string/change_icon_summary" android:entryValues="@array/icons" android:entries="@array/icon_names" app:icons="@array/icons"