pull/78/head
kunfei 5 years ago
parent 7b080b9f16
commit ecaac00432
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 5
      app/src/main/java/io/legado/app/help/PrefExtensions.kt
  3. 15
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  4. 66
      app/src/main/java/io/legado/app/ui/widget/number/NumberPickerDialog.kt
  5. 24
      app/src/main/res/layout/dialog_number_picker.xml

@ -20,4 +20,5 @@ object PreferKey {
const val readBookFont = "readBookFont"
const val fontFolder = "fontFolder"
const val backupPath = "backupUri"
const val threadCount = "threadCount"
}

@ -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)
get() = getPrefInt(PreferKey.threadCount, 16)

@ -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) ?: ""
}
}

@ -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
}
}
}
}

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
tools:ignore="UselessParent">
<NumberPicker
android:id="@+id/number_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</NumberPicker>
</LinearLayout>
</LinearLayout>
Loading…
Cancel
Save