From 1bc1abb79793fca273e474609b6d1b9336561456 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 12 Dec 2021 21:29:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/main/my/MyFragment.kt | 11 ++++ .../app/ui/widget/prefs/EditTextPreference.kt | 2 +- .../app/ui/widget/prefs/IconListPreference.kt | 2 +- .../app/ui/widget/prefs/NameListPreference.kt | 2 +- .../legado/app/ui/widget/prefs/Preference.kt | 4 +- .../app/ui/widget/prefs/PreferenceCategory.kt | 56 +++++++++---------- .../app/ui/widget/prefs/SwitchPreference.kt | 10 +++- 7 files changed, 52 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt index 07a91c0bf..e58e77bf0 100644 --- a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt @@ -14,6 +14,7 @@ import io.legado.app.constant.PreferKey import io.legado.app.databinding.FragmentMyConfigBinding import io.legado.app.help.AppConfig import io.legado.app.help.ThemeConfig +import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.primaryColor import io.legado.app.service.WebService import io.legado.app.ui.about.AboutActivity @@ -65,6 +66,15 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { putPrefBoolean(PreferKey.webService, WebService.isRun) addPreferencesFromResource(R.xml.pref_main) + findPreference("webService")?.onLongClick { + context?.selector(arrayListOf("复制地址", "浏览器打开")) { _, i -> + when (i) { + 0 -> context?.sendToClip(it.summary.toString()) + 1 -> context?.openUrl(it.summary.toString()) + } + } + true + } observeEventSticky(EventBus.WEB_SERVICE) { findPreference(PreferKey.webService)?.let { it.isChecked = WebService.isRun @@ -138,5 +148,6 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) { return super.onPreferenceTreeClick(preference) } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/EditTextPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/EditTextPreference.kt index 444f47506..97ffa92b8 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/EditTextPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/EditTextPreference.kt @@ -14,7 +14,7 @@ class EditTextPreference(context: Context, attrs: AttributeSet) : layoutResource = R.layout.view_preference } - override fun onBindViewHolder(holder: PreferenceViewHolder?) { + override fun onBindViewHolder(holder: PreferenceViewHolder) { Preference.bindView(context, holder, icon, title, summary, null, null) super.onBindViewHolder(holder) } 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 262489065..7bfbe4c47 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 @@ -51,7 +51,7 @@ class IconListPreference(context: Context, attrs: AttributeSet) : ListPreference } } - override fun onBindViewHolder(holder: PreferenceViewHolder?) { + override fun onBindViewHolder(holder: PreferenceViewHolder) { super.onBindViewHolder(holder) val v = Preference.bindView( context, diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt index bb1e5d791..cf06ec6f8 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt @@ -23,7 +23,7 @@ class NameListPreference(context: Context, attrs: AttributeSet) : ListPreference typedArray.recycle() } - override fun onBindViewHolder(holder: PreferenceViewHolder?) { + override fun onBindViewHolder(holder: PreferenceViewHolder) { val v = Preference.bindView( context, holder, diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt index baea502b3..42b83d0f9 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt @@ -106,7 +106,7 @@ class Preference(context: Context, attrs: AttributeSet) : } - override fun onBindViewHolder(holder: PreferenceViewHolder?) { + override fun onBindViewHolder(holder: PreferenceViewHolder) { bindView( context, holder, @@ -116,7 +116,7 @@ class Preference(context: Context, attrs: AttributeSet) : isBottomBackground = isBottomBackground ) super.onBindViewHolder(holder) - holder?.itemView?.onLongClick { + holder.itemView.onLongClick { onLongClick?.invoke() } } diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt index 69e4fd249..c0c8fae2d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt @@ -22,37 +22,35 @@ class PreferenceCategory(context: Context, attrs: AttributeSet) : layoutResource = R.layout.view_preference_category } - override fun onBindViewHolder(holder: PreferenceViewHolder?) { + override fun onBindViewHolder(holder: PreferenceViewHolder) { super.onBindViewHolder(holder) - holder?.let { - val view = it.findViewById(R.id.preference_title) - if (view is TextView) { // && !view.isInEditMode - view.text = title - if (view.isInEditMode) return - view.setTextColor(context.accentColor) - view.isVisible = title != null && title.isNotEmpty() + val view = holder.findViewById(R.id.preference_title) + if (view is TextView) { // && !view.isInEditMode + view.text = title + if (view.isInEditMode) return + view.setTextColor(context.accentColor) + view.isVisible = title != null && title.isNotEmpty() - val da = it.findViewById(R.id.preference_divider_above) - val dividerColor = if (AppConfig.isNightTheme) { - ColorUtils.withAlpha( - ColorUtils.shiftColor(context.backgroundColor, 1.05f), - 0.5f - ) - } else { - ColorUtils.withAlpha( - ColorUtils.shiftColor(context.backgroundColor, 0.95f), - 0.5f - ) - } - if (da is View) { - da.setBackgroundColor(dividerColor) - da.isVisible = it.isDividerAllowedAbove - } - val db = it.findViewById(R.id.preference_divider_below) - if (db is View) { - db.setBackgroundColor(dividerColor) - db.isVisible = it.isDividerAllowedBelow - } + val da = holder.findViewById(R.id.preference_divider_above) + val dividerColor = if (AppConfig.isNightTheme) { + ColorUtils.withAlpha( + ColorUtils.shiftColor(context.backgroundColor, 1.05f), + 0.5f + ) + } else { + ColorUtils.withAlpha( + ColorUtils.shiftColor(context.backgroundColor, 0.95f), + 0.5f + ) + } + if (da is View) { + da.setBackgroundColor(dividerColor) + da.isVisible = holder.isDividerAllowedAbove + } + val db = holder.findViewById(R.id.preference_divider_below) + if (db is View) { + db.setBackgroundColor(dividerColor) + db.isVisible = holder.isDividerAllowedBelow } } } diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt index 2cb47346b..19042e01b 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt @@ -13,6 +13,7 @@ class SwitchPreference(context: Context, attrs: AttributeSet) : SwitchPreferenceCompat(context, attrs) { private val isBottomBackground: Boolean + private var onLongClick: ((preference: SwitchPreference) -> Boolean)? = null init { layoutResource = R.layout.view_preference @@ -21,7 +22,7 @@ class SwitchPreference(context: Context, attrs: AttributeSet) : typedArray.recycle() } - override fun onBindViewHolder(holder: PreferenceViewHolder?) { + override fun onBindViewHolder(holder: PreferenceViewHolder) { val v = Preference.bindView( context, holder, @@ -35,7 +36,14 @@ class SwitchPreference(context: Context, attrs: AttributeSet) : if (v is SwitchCompat && !v.isInEditMode) { v.applyTint(context.accentColor) } + holder.itemView.setOnLongClickListener { + onLongClick?.invoke(this) ?: false + } super.onBindViewHolder(holder) } + fun onLongClick(listener: (preference: SwitchPreference) -> Boolean) { + onLongClick = listener + } + }