From 9fee2110c792ace57bfc82a70f1b41dc50905b4b Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Wed, 26 Sep 2018 16:59:51 +0200 Subject: [PATCH] Implemented number picker preference --- .../preferences/NumberPickerPreference.java | 106 ++++++++++++++++++ .../preferences/PreferenceController.java | 14 +-- app/src/main/res/layout/numberpicker.xml | 16 +++ app/src/main/res/xml/preferences_network.xml | 10 +- 4 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/preferences/NumberPickerPreference.java create mode 100644 app/src/main/res/layout/numberpicker.xml diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/NumberPickerPreference.java b/app/src/main/java/de/danoeh/antennapod/preferences/NumberPickerPreference.java new file mode 100644 index 000000000..20b07e486 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/preferences/NumberPickerPreference.java @@ -0,0 +1,106 @@ +package de.danoeh.antennapod.preferences; + +import android.app.AlertDialog; +import android.content.Context; +import android.support.v7.preference.Preference; +import android.text.InputFilter; +import android.util.AttributeSet; +import android.view.View; +import android.view.WindowManager; +import android.widget.EditText; +import de.danoeh.antennapod.R; + +public class NumberPickerPreference extends Preference { + private Context context; + private int defaultValue = 0; + private int minValue = 0; + private int maxValue = Integer.MAX_VALUE; + + public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + init(context, attrs); + } + + public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(context, attrs); + } + + public NumberPickerPreference(Context context, AttributeSet attrs) { + super(context, attrs); + init(context, attrs); + } + + public NumberPickerPreference(Context context) { + super(context); + this.context = context; + } + + private void init(Context context, AttributeSet attrs) { + this.context = context; + + for (int i = 0; i < attrs.getAttributeCount(); i++) { + String name = attrs.getAttributeName(i); + String value = attrs.getAttributeValue(i); + switch (name) { + case "defaultValue": + defaultValue = Integer.parseInt(value); + break; + case "minValue": + minValue = Integer.parseInt(value); + break; + case "maxValue": + maxValue = Integer.parseInt(value); + break; + } + } + } + + @Override + protected void onClick() { + super.onClick(); + + View view = View.inflate(context, R.layout.numberpicker, null); + EditText number = view.findViewById(R.id.number); + number.setText(getSharedPreferences().getString(getKey(), ""+defaultValue)); + number.setFilters(new InputFilter[]{(source, start, end, dest, dstart, dend) -> { + try { + String newVal = dest.toString().substring(0, dstart) + dest.toString().substring(dend); + newVal = newVal.substring(0, dstart) + source.toString() + newVal.substring(dstart); + int input = Integer.parseInt(newVal); + if (input >= minValue && input <= maxValue) { + return null; + } + } catch (NumberFormatException nfe) { + nfe.printStackTrace(); + } + return ""; + }}); + + AlertDialog dialog = new AlertDialog.Builder(context) + .setTitle(getTitle()) + .setView(view) + .setNegativeButton(android.R.string.cancel, null) + .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { + try { + String numberString = number.getText().toString(); + int value = Integer.parseInt(numberString); + + if (value < minValue || value > maxValue) { + return; + } + + getSharedPreferences().edit().putString(getKey(), "" + value).apply(); + + if (getOnPreferenceChangeListener() != null) { + getOnPreferenceChangeListener().onPreferenceChange(this, value); + } + } catch (NumberFormatException e) { + // Do not set value + } + }) + .create(); + dialog.show(); + dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + } +} diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java index fc00fc784..c55117ecc 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java @@ -462,18 +462,10 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc ui.findPreference(UserPreferences.PREF_PARALLEL_DOWNLOADS) .setOnPreferenceChangeListener( (preference, o) -> { - if (o instanceof String) { - try { - int value = Integer.parseInt((String) o); - if (1 <= value && value <= 50) { - setParallelDownloadsText(value); - return true; - } - } catch (NumberFormatException e) { - return false; - } + if (o instanceof Integer) { + setParallelDownloadsText((Integer) o); } - return false; + return true; } ); // validate and set correct value: number of downloads between 1 and 50 (inclusive) diff --git a/app/src/main/res/layout/numberpicker.xml b/app/src/main/res/layout/numberpicker.xml new file mode 100644 index 000000000..813326bd6 --- /dev/null +++ b/app/src/main/res/layout/numberpicker.xml @@ -0,0 +1,16 @@ + + + + + + diff --git a/app/src/main/res/xml/preferences_network.xml b/app/src/main/res/xml/preferences_network.xml index a40e54d31..c37a99465 100644 --- a/app/src/main/res/xml/preferences_network.xml +++ b/app/src/main/res/xml/preferences_network.xml @@ -1,7 +1,7 @@ - + android:title="@string/pref_parallel_downloads_title"/>