pull/90/head^2
kunfei 5 years ago
parent 3ac6d9807b
commit 21e4fc6e2a
  1. 2
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 47
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt

@ -15,7 +15,7 @@ import kotlin.math.min
object BookHelp { object BookHelp {
private const val cacheFolderName = "book_cache" private const val cacheFolderName = "book_cache"
private var downloadPath: String = var downloadPath: String =
App.INSTANCE.getPrefString(PreferKey.downloadPath) App.INSTANCE.getPrefString(PreferKey.downloadPath)
?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath
?: App.INSTANCE.cacheDir.absolutePath ?: App.INSTANCE.cacheDir.absolutePath

@ -27,7 +27,6 @@ import io.legado.app.utils.*
class OtherConfigFragment : PreferenceFragmentCompat(), class OtherConfigFragment : PreferenceFragmentCompat(),
FileChooserDialog.CallBack, FileChooserDialog.CallBack,
Preference.OnPreferenceChangeListener,
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener {
private val requestCodeDownloadPath = 25324 private val requestCodeDownloadPath = 25324
@ -40,8 +39,8 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
putPrefBoolean("process_text", isProcessTextEnabled()) putPrefBoolean("process_text", isProcessTextEnabled())
addPreferencesFromResource(R.xml.pref_config_other) addPreferencesFromResource(R.xml.pref_config_other)
bindPreferenceSummaryToValue(findPreference(PreferKey.downloadPath)) upPreferenceSummary(findPreference(PreferKey.downloadPath), BookHelp.downloadPath)
bindPreferenceSummaryToValue(findPreference(PreferKey.threadCount)) upPreferenceSummary(findPreference(PreferKey.threadCount), AppConfig.threadCount.toString())
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -84,7 +83,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
when (key) { when (key) {
PreferKey.downloadPath -> { PreferKey.downloadPath -> {
BookHelp.upDownloadPath() BookHelp.upDownloadPath()
findPreference<Preference>(key)?.summary = getPreferenceString(key).toString() findPreference<Preference>(key)?.summary = BookHelp.downloadPath
} }
PreferKey.recordLog -> LogUtils.upLevel() PreferKey.recordLog -> LogUtils.upLevel()
PreferKey.processText -> sharedPreferences?.let { PreferKey.processText -> sharedPreferences?.let {
@ -94,38 +93,16 @@ class OtherConfigFragment : PreferenceFragmentCompat(),
} }
} }
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean { private fun upPreferenceSummary(preference: Preference?, value: String) {
val stringValue = newValue.toString() when (preference) {
when { is ListPreference -> {
preference is ListPreference -> { val index = preference.findIndexOfValue(value)
val index = preference.findIndexOfValue(stringValue)
// Set the summary to reflect the new 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, childFragmentManager,
requestCodeDownloadPath, requestCodeDownloadPath,
mode = FileChooserDialog.DIRECTORY, mode = FileChooserDialog.DIRECTORY,
initPath = getPreferenceString(PreferKey.downloadPath).toString() initPath = BookHelp.downloadPath
) )
} }
.request() .request()

Loading…
Cancel
Save