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 fa6b63c39..de8c0a5a7 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -15,7 +15,7 @@ import kotlin.math.min object BookHelp { private const val cacheFolderName = "book_cache" - private var downloadPath: String = + var downloadPath: String = App.INSTANCE.getPrefString(PreferKey.downloadPath) ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath 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 8f3a7e244..6a77a8c99 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 @@ -27,7 +27,6 @@ import io.legado.app.utils.* class OtherConfigFragment : PreferenceFragmentCompat(), FileChooserDialog.CallBack, - Preference.OnPreferenceChangeListener, SharedPreferences.OnSharedPreferenceChangeListener { private val requestCodeDownloadPath = 25324 @@ -40,8 +39,8 @@ class OtherConfigFragment : PreferenceFragmentCompat(), override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { putPrefBoolean("process_text", isProcessTextEnabled()) addPreferencesFromResource(R.xml.pref_config_other) - bindPreferenceSummaryToValue(findPreference(PreferKey.downloadPath)) - bindPreferenceSummaryToValue(findPreference(PreferKey.threadCount)) + upPreferenceSummary(findPreference(PreferKey.downloadPath), BookHelp.downloadPath) + upPreferenceSummary(findPreference(PreferKey.threadCount), AppConfig.threadCount.toString()) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -84,7 +83,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), when (key) { PreferKey.downloadPath -> { BookHelp.upDownloadPath() - findPreference(key)?.summary = getPreferenceString(key).toString() + findPreference(key)?.summary = BookHelp.downloadPath } PreferKey.recordLog -> LogUtils.upLevel() PreferKey.processText -> sharedPreferences?.let { @@ -94,38 +93,16 @@ class OtherConfigFragment : PreferenceFragmentCompat(), } } - override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean { - val stringValue = newValue.toString() - when { - preference is ListPreference -> { - val index = preference.findIndexOfValue(stringValue) + private fun upPreferenceSummary(preference: Preference?, value: String) { + when (preference) { + is ListPreference -> { + val index = preference.findIndexOfValue(value) // Set the summary to reflect the new value. - preference.setSummary(if (index >= 0) preference.entries[index] else null) + preference.summary = if (index >= 0) preference.entries[index] else null + } + else -> { + preference?.summary = value } - preference?.key == PreferKey.threadCount -> preference.summary = - getString(R.string.threads_num, stringValue) - else -> preference?.summary = stringValue - } - return false - } - - private fun bindPreferenceSummaryToValue(preference: Preference?) { - preference?.let { - preference.onPreferenceChangeListener = this - onPreferenceChange( - preference, - getPreferenceString(preference.key) - ) - } - } - - private fun getPreferenceString(key: String): Any { - return when (key) { - PreferKey.downloadPath -> getPrefString(PreferKey.downloadPath) - ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath - ?: App.INSTANCE.cacheDir.absolutePath - PreferKey.threadCount -> AppConfig.threadCount - else -> getPrefString(key) ?: "" } } @@ -166,7 +143,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), childFragmentManager, requestCodeDownloadPath, mode = FileChooserDialog.DIRECTORY, - initPath = getPreferenceString(PreferKey.downloadPath).toString() + initPath = BookHelp.downloadPath ) } .request()