From e81918b94ec0cdd9f71e22f764ba3b4e99b061f9 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 9 Feb 2020 11:03:09 +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 --- .../main/java/io/legado/app/help/BookHelp.kt | 11 +-- .../app/ui/config/OtherConfigFragment.kt | 99 ++++++++++--------- .../io/legado/app/ui/main/my/MyFragment.kt | 2 - 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 67ea2bd30..98e421e85 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -15,18 +15,11 @@ import kotlin.math.min object BookHelp { private const val cacheFolderName = "book_cache" - var downloadPath: String = - App.INSTANCE.getPrefString(PreferKey.downloadPath) + val downloadPath: String + get() = App.INSTANCE.getPrefString(PreferKey.downloadPath) ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath - fun upDownloadPath() { - downloadPath = - App.INSTANCE.getPrefString(PreferKey.downloadPath) - ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath - ?: App.INSTANCE.cacheDir.absolutePath - } - private val downloadUri get() = Uri.parse(downloadPath) private fun bookFolderName(book: Book): String { 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 6a77a8c99..357b712af 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 @@ -18,6 +18,7 @@ import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat +import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.ui.filechooser.FileChooserDialog @@ -37,25 +38,21 @@ class OtherConfigFragment : PreferenceFragmentCompat(), ) override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { - putPrefBoolean("process_text", isProcessTextEnabled()) + putPrefBoolean(PreferKey.processText, isProcessTextEnabled()) addPreferencesFromResource(R.xml.pref_config_other) - upPreferenceSummary(findPreference(PreferKey.downloadPath), BookHelp.downloadPath) - upPreferenceSummary(findPreference(PreferKey.threadCount), AppConfig.threadCount.toString()) + upPreferenceSummary(PreferKey.downloadPath, BookHelp.downloadPath) + upPreferenceSummary(PreferKey.threadCount, AppConfig.threadCount.toString()) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - ATH.applyEdgeEffectColor(listView) - } - - override fun onResume() { - super.onResume() preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this) + ATH.applyEdgeEffectColor(listView) } - override fun onPause() { + override fun onDestroy() { + super.onDestroy() preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this) - super.onPause() } override fun onPreferenceTreeClick(preference: Preference?): Boolean { @@ -66,11 +63,9 @@ class OtherConfigFragment : PreferenceFragmentCompat(), .setMinValue(1) .setValue(AppConfig.threadCount) .show { - requireContext().putPrefInt(PreferKey.threadCount, it) - findPreference(PreferKey.threadCount)?.summary = - getString(R.string.threads_num, it.toString()) + AppConfig.threadCount = it } - PreferKey.downloadPath -> selectDownloadPathSys() + PreferKey.downloadPath -> selectDownloadPath() PreferKey.cleanCache -> { BookHelp.clearCache() toast(R.string.clear_cache_success) @@ -82,26 +77,30 @@ class OtherConfigFragment : PreferenceFragmentCompat(), override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { when (key) { PreferKey.downloadPath -> { - BookHelp.upDownloadPath() - findPreference(key)?.summary = BookHelp.downloadPath + upPreferenceSummary(key, BookHelp.downloadPath) } + PreferKey.threadCount -> upPreferenceSummary( + PreferKey.threadCount, + AppConfig.threadCount.toString() + ) PreferKey.recordLog -> LogUtils.upLevel() PreferKey.processText -> sharedPreferences?.let { - setProcessTextEnable(it.getBoolean("process_text", true)) + setProcessTextEnable(it.getBoolean(key, true)) } PreferKey.showRss -> postEvent(EventBus.SHOW_RSS, "unused") } } - private fun upPreferenceSummary(preference: Preference?, value: String) { - when (preference) { - is ListPreference -> { + private fun upPreferenceSummary(preferenceKey: String, value: String?) { + val preference = findPreference(preferenceKey) ?: return + when (preferenceKey) { + PreferKey.threadCount -> preference.summary = getString(R.string.threads_num, value) + else -> if (preference is ListPreference) { val index = preference.findIndexOfValue(value) // Set the summary to reflect the new value. preference.summary = if (index >= 0) preference.entries[index] else null - } - else -> { - preference?.summary = value + } else { + preference.summary = value } } } @@ -124,29 +123,39 @@ class OtherConfigFragment : PreferenceFragmentCompat(), } } - private fun selectDownloadPathSys() { - try { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - startActivityForResult(intent, requestCodeDownloadPath) - } catch (e: Exception) { - selectDownloadPath() - } - } - private fun selectDownloadPath() { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - FileChooserDialog.show( - childFragmentManager, - requestCodeDownloadPath, - mode = FileChooserDialog.DIRECTORY, - initPath = BookHelp.downloadPath - ) + alert { + titleResource = R.string.select_folder + items(resources.getStringArray(R.array.select_folder).toList()) { _, i -> + when (i) { + 1 -> { + removePref(PreferKey.downloadPath) + } + 2 -> { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, requestCodeDownloadPath) + } catch (e: Exception) { + e.printStackTrace() + toast(e.localizedMessage ?: "ERROR") + } + } + 3 -> PermissionsCompat.Builder(this@OtherConfigFragment) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + FileChooserDialog.show( + childFragmentManager, + requestCodeDownloadPath, + mode = FileChooserDialog.DIRECTORY, + initPath = BookHelp.downloadPath + ) + } + .request() + } } - .request() + } } override fun onFilePicked(requestCode: Int, currentPath: String) { @@ -165,8 +174,6 @@ class OtherConfigFragment : PreferenceFragmentCompat(), Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION ) putPrefString(PreferKey.downloadPath, uri.toString()) - findPreference(PreferKey.downloadPath)?.summary = uri.toString() - BookHelp.upDownloadPath() } } } 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 f39f7cd07..1fbd92bbc 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 @@ -13,7 +13,6 @@ import io.legado.app.R import io.legado.app.base.BaseFragment import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey -import io.legado.app.help.BookHelp import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.prefs.ATESwitchPreference import io.legado.app.service.WebService @@ -107,7 +106,6 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config), FileChooserDialog. toast(R.string.service_stop) } } - PreferKey.downloadPath -> BookHelp.upDownloadPath() "recordLog" -> LogUtils.upLevel() } }