From 8c0b7fd28ac40d20e4ce6b9a524695f6b82fdf39 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 25 Mar 2021 08:15:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/config/ThemeConfigFragment.kt | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 4074f32b6..4f30f23a0 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -36,6 +36,18 @@ import java.io.File class ThemeConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { + private val setLightBgImage = registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImage) { + upTheme(false) + } + } + + private val setDarkBgImage = registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImageN) { + upTheme(false) + } + } + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) if (Build.VERSION.SDK_INT < 26) { @@ -181,42 +193,26 @@ class ThemeConfigFragment : BasePreferenceFragment(), "themeList" -> ThemeListDialog().show(childFragmentManager, "themeList") "saveDayTheme", "saveNightTheme" -> saveThemeAlert(key) PreferKey.bgImage -> if (getPrefString(PreferKey.bgImage).isNullOrEmpty()) { - registerForActivityResult(ActivityResultContracts.GetContent()) { - setBgFromUri(it, PreferKey.bgImage) { - upTheme(false) - } - }.launch("image/*") + setLightBgImage.launch("image/*") } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImage) upTheme(false) } else { - registerForActivityResult(ActivityResultContracts.GetContent()) { - setBgFromUri(it, PreferKey.bgImage) { - upTheme(false) - } - }.launch("image/*") + setLightBgImage.launch("image/*") } } } PreferKey.bgImageN -> if (getPrefString(PreferKey.bgImageN).isNullOrEmpty()) { - registerForActivityResult(ActivityResultContracts.GetContent()) { - setBgFromUri(it, PreferKey.bgImageN) { - upTheme(false) - } - }.launch("image/*") + setDarkBgImage.launch("image/*") } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImageN) upTheme(true) } else { - registerForActivityResult(ActivityResultContracts.GetContent()) { - setBgFromUri(it, PreferKey.bgImageN) { - upTheme(false) - } - }.launch("image/*") + setDarkBgImage.launch("image/*") } } }