diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index b869927e6..1e6600a61 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -20,4 +20,5 @@ object PreferKey { const val readBookFont = "readBookFont" const val fontFolder = "fontFolder" const val backupPath = "backupUri" + const val threadCount = "threadCount" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/PrefExtensions.kt b/app/src/main/java/io/legado/app/help/PrefExtensions.kt index b9392ef7a..9a41707a1 100644 --- a/app/src/main/java/io/legado/app/help/PrefExtensions.kt +++ b/app/src/main/java/io/legado/app/help/PrefExtensions.kt @@ -1,12 +1,13 @@ package io.legado.app.help import android.content.Context +import io.legado.app.constant.PreferKey import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefInt val Context.isShowRSS: Boolean - get() = getPrefBoolean("showRss", true) + get() = getPrefBoolean(PreferKey.showRss, true) val Context.threadCount: Int - get() = getPrefInt("threadCount", 16) \ No newline at end of file + get() = getPrefInt(PreferKey.threadCount, 16) \ No newline at end of file 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 4795b56bb..47e837ffe 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 @@ -17,6 +17,7 @@ import io.legado.app.help.threadCount import io.legado.app.lib.theme.ATH import io.legado.app.receiver.SharedReceiverActivity import io.legado.app.ui.filechooser.FileChooserDialog +import io.legado.app.ui.widget.number.NumberPickerDialog import io.legado.app.utils.* @@ -36,7 +37,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), putPrefBoolean("process_text", isProcessTextEnabled()) addPreferencesFromResource(R.xml.pref_config_other) bindPreferenceSummaryToValue(findPreference(PreferKey.downloadPath)) - bindPreferenceSummaryToValue(findPreference("threadCount")) + bindPreferenceSummaryToValue(findPreference(PreferKey.threadCount)) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -56,6 +57,14 @@ class OtherConfigFragment : PreferenceFragmentCompat(), override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { + PreferKey.threadCount -> NumberPickerDialog(requireContext()) + .setTitle(getString(R.string.threads_num_title)) + .setMaxValue(999) + .setMinValue(1) + .setValue(requireContext().threadCount) + .show { + requireContext().putPrefInt(PreferKey.threadCount, it) + } PreferKey.downloadPath -> FileChooserDialog.show( childFragmentManager, downloadPath, @@ -92,7 +101,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), // Set the summary to reflect the new value. preference.setSummary(if (index >= 0) preference.entries[index] else null) } - preference?.key == "threadCount" -> preference.summary = + preference?.key == PreferKey.threadCount -> preference.summary = getString(R.string.threads_num, stringValue) else -> preference?.summary = stringValue } @@ -114,7 +123,7 @@ class OtherConfigFragment : PreferenceFragmentCompat(), PreferKey.downloadPath -> getPrefString(PreferKey.downloadPath) ?: App.INSTANCE.getExternalFilesDir(null)?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath - "threadCount" -> requireContext().threadCount + PreferKey.threadCount -> requireContext().threadCount else -> getPrefString(key) ?: "" } } diff --git a/app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt b/app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt new file mode 100644 index 000000000..17fe9ab53 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt @@ -0,0 +1,66 @@ +package io.legado.app.ui.widget.number + +import android.content.Context +import android.widget.NumberPicker +import androidx.appcompat.app.AlertDialog +import io.legado.app.R +import io.legado.app.utils.applyTint +import io.legado.app.utils.hideSoftInput +import kotlinx.android.synthetic.main.dialog_number_picker.* + + +class NumberPickerDialog(context: Context) { + private val builder = AlertDialog.Builder(context) + private var numberPicker: NumberPicker? = null + private var maxValue: Int? = null + private var minValue: Int? = null + private var value: Int? = null + + init { + builder.setView(R.layout.dialog_number_picker) + } + + fun setTitle(title: String): NumberPickerDialog { + builder.setTitle(title) + return this + } + + fun setMaxValue(value: Int): NumberPickerDialog { + maxValue = value + return this + } + + fun setMinValue(value: Int): NumberPickerDialog { + minValue = value + return this + } + + fun setValue(value: Int): NumberPickerDialog { + this.value = value + return this + } + + fun show(callBack: ((value: Int) -> Unit)?) { + builder.setPositiveButton(R.string.ok) { _, _ -> + numberPicker?.let { + it.clearFocus() + it.hideSoftInput() + callBack?.invoke(it.value) + } + } + builder.setNegativeButton(R.string.cancel, null) + val dialog = builder.show().applyTint() + numberPicker = dialog.number_picker + numberPicker?.let { np -> + minValue?.let { + np.minValue = it + } + maxValue?.let { + np.maxValue = it + } + value?.let { + np.value = it + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_number_picker.xml b/app/src/main/res/layout/dialog_number_picker.xml new file mode 100644 index 000000000..ea6582170 --- /dev/null +++ b/app/src/main/res/layout/dialog_number_picker.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + \ No newline at end of file