From f51eeb5ac4049daa7efaab734f6624c938f45d4a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 27 Mar 2021 20:55:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BC=83=E7=94=A8=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/read/config/BgTextConfigDialog.kt | 21 +++----- .../app/ui/config/OtherConfigFragment.kt | 30 +++-------- .../app/ui/config/ThemeConfigFragment.kt | 50 ++++++------------- 3 files changed, 28 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index 2d986a27b..ce648bede 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -8,6 +8,7 @@ import android.graphics.Color import android.net.Uri import android.os.Bundle import android.view.* +import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import com.jaredrummler.android.colorpicker.ColorPickerDialog import io.legado.app.R @@ -40,13 +41,15 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { } private val binding by viewBinding(DialogReadBgTextBinding::bind) - private val requestCodeBg = 123 private val requestCodeExport = 131 private val requestCodeImport = 132 private val configFileName = "readConfig.zip" private lateinit var adapter: BgAdapter private var primaryTextColor = 0 private var secondaryTextColor = 0 + private val selectBgImage = registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it) + } override fun onStart() { super.onStart() @@ -108,7 +111,9 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { tvName.text = getString(R.string.select_image) ivBg.setImageResource(R.drawable.ic_image) ivBg.setColorFilter(primaryTextColor) - root.setOnClickListener { selectImage() } + root.setOnClickListener { + selectBgImage.launch("image/*") + } } } requireContext().assets.list("bg")?.let { @@ -190,13 +195,6 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { } } - private fun selectImage() { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "image/*" - startActivityForResult(intent, requestCodeBg) - } - @Suppress("BlockingMethodInNonBlockingContext") private fun exportConfig(uri: Uri) { val exportFileName = if (ReadBookConfig.config.name.isBlank()) { @@ -373,11 +371,6 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { - requestCodeBg -> if (resultCode == RESULT_OK) { - data?.data?.let { uri -> - setBgFromUri(uri) - } - } requestCodeImport -> if (resultCode == RESULT_OK) { data?.data?.let { uri -> importConfig(uri) diff --git a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt index e8f27277b..a9cc4dcd8 100644 --- a/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt @@ -1,7 +1,6 @@ package io.legado.app.ui.config import android.annotation.SuppressLint -import android.app.Activity.RESULT_OK import android.content.ComponentName import android.content.Intent import android.content.SharedPreferences @@ -10,6 +9,7 @@ import android.net.Uri import android.os.Bundle import android.os.Process import android.view.View +import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import androidx.preference.ListPreference import androidx.preference.Preference @@ -38,14 +38,15 @@ import java.io.File class OtherConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { - private val requestCodeCover = 231 - private val packageManager = appCtx.packageManager private val componentName = ComponentName( appCtx, SharedReceiverActivity::class.java.name ) private val webPort get() = getPrefInt(PreferKey.webPort, 1122) + private val selectCoverImage = registerForActivityResult(ActivityResultContracts.GetContent()) { + setCoverFromUri(it) + } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { putPrefBoolean(PreferKey.processText, isProcessTextEnabled()) @@ -88,13 +89,13 @@ class OtherConfigFragment : BasePreferenceFragment(), } PreferKey.cleanCache -> clearCache() PreferKey.defaultCover -> if (getPrefString(PreferKey.defaultCover).isNullOrEmpty()) { - selectImage(requestCodeCover) + selectCoverImage.launch("image/*") } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.defaultCover) } else { - selectImage(requestCodeCover) + selectCoverImage.launch("image/*") } } } @@ -183,14 +184,6 @@ class OtherConfigFragment : BasePreferenceFragment(), }.show() } - @Suppress("SameParameterValue") - private fun selectImage(requestCode: Int) { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "image/*" - startActivityForResult(intent, requestCode) - } - private fun isProcessTextEnabled(): Boolean { return packageManager.getComponentEnabledSetting(componentName) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED } @@ -246,15 +239,4 @@ class OtherConfigFragment : BasePreferenceFragment(), } } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - when (requestCode) { - requestCodeCover -> if (resultCode == RESULT_OK) { - data?.data?.let { uri -> - setCoverFromUri(uri) - } - } - } - } - } \ No newline at end of file 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 3b13342a7..5cb141213 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 @@ -1,8 +1,6 @@ package io.legado.app.ui.config import android.annotation.SuppressLint -import android.app.Activity -import android.content.Intent import android.content.SharedPreferences import android.net.Uri import android.os.Build @@ -11,6 +9,7 @@ import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View +import androidx.activity.result.contract.ActivityResultContracts import androidx.documentfile.provider.DocumentFile import androidx.preference.Preference import io.legado.app.R @@ -38,8 +37,16 @@ import java.io.File class ThemeConfigFragment : BasePreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener { - private val requestCodeBgImage = 234 - private val requestCodeBgImageN = 342 + private val selectLightBg = registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImage) { + upTheme(false) + } + } + private val selectDarkBg = registerForActivityResult(ActivityResultContracts.GetContent()) { + setBgFromUri(it, PreferKey.bgImageN) { + upTheme(true) + } + } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config_theme) @@ -186,26 +193,26 @@ class ThemeConfigFragment : BasePreferenceFragment(), "themeList" -> ThemeListDialog().show(childFragmentManager, "themeList") "saveDayTheme", "saveNightTheme" -> saveThemeAlert(key) PreferKey.bgImage -> if (getPrefString(PreferKey.bgImage).isNullOrEmpty()) { - selectImage(requestCodeBgImage) + selectLightBg.launch("image/*") } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImage) upTheme(false) } else { - selectImage(requestCodeBgImage) + selectLightBg.launch("image/*") } } } PreferKey.bgImageN -> if (getPrefString(PreferKey.bgImageN).isNullOrEmpty()) { - selectImage(requestCodeBgImageN) + selectDarkBg.launch("image/*") } else { selector(items = arrayListOf("删除图片", "选择图片")) { _, i -> if (i == 0) { removePref(PreferKey.bgImageN) upTheme(true) } else { - selectImage(requestCodeBgImageN) + selectDarkBg.launch("image/*") } } } @@ -213,13 +220,6 @@ class ThemeConfigFragment : BasePreferenceFragment(), return super.onPreferenceTreeClick(preference) } - private fun selectImage(requestCode: Int) { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "image/*" - startActivityForResult(intent, requestCode) - } - @SuppressLint("InflateParams") private fun saveThemeAlert(key: String) { alert(R.string.theme_name) { @@ -302,24 +302,4 @@ class ThemeConfigFragment : BasePreferenceFragment(), } } - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - when (requestCode) { - requestCodeBgImage -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - setBgFromUri(uri, PreferKey.bgImage) { - upTheme(false) - } - } - } - requestCodeBgImageN -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - setBgFromUri(uri, PreferKey.bgImageN) { - upTheme(true) - } - } - } - } - } - } \ No newline at end of file