From 305322aec8da6b0b9c9b4c590f5a1d51e6030a3e Mon Sep 17 00:00:00 2001 From: simonpoole Date: Mon, 5 Aug 2024 15:28:13 +0200 Subject: [PATCH] Migrate to hard fork of NumberPickerView Maintainer AWOL and the artifact was still on JCenter --- build.gradle | 2 - lib/build.gradle | 2 +- .../pickers/DateRangePicker.java | 810 +++++++++--------- .../pickers/OccurrenceInMonthPicker.java | 2 +- .../pickers/RangePicker.java | 260 +++--- .../pickers/TimeRangePicker.java | 388 ++++----- .../pickers/ValuePicker.java | 210 ++--- lib/src/main/res/layout/datepicker.xml | 6 +- lib/src/main/res/layout/daterangepicker.xml | 16 +- .../res/layout/occurrence_in_month_picker.xml | 8 +- lib/src/main/res/layout/rangepicker.xml | 4 +- lib/src/main/res/layout/timerangepicker.xml | 8 +- lib/src/main/res/layout/valuepicker.xml | 2 +- testapp/build.gradle | 4 +- 14 files changed, 859 insertions(+), 863 deletions(-) diff --git a/build.gradle b/build.gradle index f386616..bc3e8d2 100644 --- a/build.gradle +++ b/build.gradle @@ -14,9 +14,7 @@ buildscript { allprojects { repositories { mavenCentral() - maven { url "https://maven.google.com" } mavenLocal() google() - jcenter() } } diff --git a/lib/build.gradle b/lib/build.gradle index fbbc685..1eb4405 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -113,7 +113,7 @@ dependencies { implementation "androidx.core:core:$androidxVersion" implementation "ch.poole:OpeningHoursParser:0.26.0" implementation "ch.poole.android:rangebar:0.1.6" - implementation 'cn.carbswang.android:NumberPickerView:1.2.0' + implementation 'ch.poole.android:numberpickerview:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' // Instrumentation tests diff --git a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/DateRangePicker.java b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/DateRangePicker.java index 37af049..d984373 100644 --- a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/DateRangePicker.java +++ b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/DateRangePicker.java @@ -1,405 +1,405 @@ -package ch.poole.openinghoursfragment.pickers; - -import android.annotation.SuppressLint; -import android.content.DialogInterface; -import android.content.DialogInterface.OnClickListener; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AlertDialog.Builder; -import androidx.appcompat.app.AppCompatDialog; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import ch.poole.openinghoursfragment.CancelableDialogFragment; -import ch.poole.openinghoursfragment.R; -import ch.poole.openinghoursparser.DateWithOffset; -import ch.poole.openinghoursparser.Month; -import ch.poole.openinghoursparser.VarDate; -import ch.poole.openinghoursparser.YearRange; -import cn.carbswang.android.numberpickerview.library.NumberPickerView; -import cn.carbswang.android.numberpickerview.library.NumberPickerView.OnValueChangeListener; - -/** - * Display a dialog allowing the user to select values for a start date and optionally an end date - * - */ -public class DateRangePicker extends CancelableDialogFragment { - private static final int MAX_YEAR = 2100; - - public static final int NOTHING_SELECTED = Integer.MIN_VALUE; - - private static final String TITLE = "title"; - private static final String START_YEAR = "startYear"; - private static final String START_MONTH = "startMonth"; - private static final String START_DAY = "startDay"; - private static final String START_VARDATE = "startVarDate"; - private static final String START_ONLY = "startOnly"; - private static final String END_YEAR = "endYear"; - private static final String END_MONTH = "endMonth"; - private static final String END_DAY = "endDay"; - private static final String END_VARDATE = "endVarDate"; - - private static final String TAG = "fragment_daterangepicker"; - - /** - * Show the DateRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startYear initial start year - * @param startMonth initial start month - * @param startDay initial start day of the month - * @param endYear initial end year - * @param endMonth initial end month - * @param endDay initial end day of the month - */ - public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull Month startMonth, int startDay, int endYear, - @Nullable Month endMonth, int endDay) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - DateRangePicker datePickerFragment = newInstance(title, startYear, startMonth, startDay, null, false, endYear, endMonth, endDay, null); - datePickerFragment.show(fm, TAG); - } - - /** - * Show the DateRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startYear initial start year - * @param startVarDate initial start variable date ie easter - * @param endYear initial end year - * @param endMonth initial end month - * @param endDay initial end day of the month - */ - public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull VarDate startVarDate, int endYear, @Nullable Month endMonth, - int endDay) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - DateRangePicker datePickerFragment = newInstance(title, startYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, startVarDate, false, endYear, endMonth, - endDay, null); - datePickerFragment.show(fm, TAG); - } - - /** - * Show the DateRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startYear initial start year - * @param startMonth initial start month - * @param startDay initial start day of the month - * @param endYear initial end year - * @param endVarDate initial end variable date ie easter - */ - public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull Month startMonth, int startDay, int endYear, - @NonNull VarDate endVarDate) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - DateRangePicker datePickerFragment = newInstance(title, startYear, startMonth, startDay, null, false, endYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, - endVarDate); - datePickerFragment.show(fm, TAG); - } - - /** - * Show the DateRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startYear initial start year - * @param startVarDate initial start variable date ie easter - * @param endYear initial end year - * @param endVarDate initial end variable date ie easter - */ - public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull VarDate startVarDate, int endYear, @NonNull VarDate endVarDate) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - DateRangePicker datePickerFragment = newInstance(title, startYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, startVarDate, false, endYear, null, - DateWithOffset.UNDEFINED_MONTH_DAY, endVarDate); - datePickerFragment.show(fm, TAG); - } - - /** - * Show the DateRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startYear initial start year - * @param startMonth initial start month - * @param startDay initial start day of the month - */ - public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull Month startMonth, int startDay) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - DateRangePicker datePickerFragment = newInstance(title, startYear, startMonth, startDay, null, true, YearRange.UNDEFINED_YEAR, null, - DateWithOffset.UNDEFINED_MONTH_DAY, null); - datePickerFragment.show(fm, TAG); - } - - /** - * Show the DateRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startYear initial start year - * @param startVarDate initial start variable date ie easter - */ - public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull VarDate startVarDate) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - DateRangePicker datePickerFragment = newInstance(title, startYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, startVarDate, true, - YearRange.UNDEFINED_YEAR, null, DateWithOffset.UNDEFINED_MONTH_DAY, null); - datePickerFragment.show(fm, TAG); - } - - /** - * Create a new instance of DateRangePicker - * - * @param title resource id for the title to display - * @param startYear initial start year - * @param startMonth initial start month or null - * @param startDay initial start day of the month - * @param startVarDate initial start variable date ie easter or null - * @param startOnly only show a picker for one date - * @param endYear initial end year - * @param endMonth initial end month or null - * @param endDay initial end day of the month - * @param endVarDate initial end variable date ie easter or null - * @return an instance of DateRangePicker - */ - private static DateRangePicker newInstance(int title, int startYear, @Nullable Month startMonth, int startDay, @Nullable VarDate startVarDate, - boolean startOnly, int endYear, @Nullable Month endMonth, int endDay, @Nullable VarDate endVarDate) { - DateRangePicker f = new DateRangePicker(); - Bundle args = new Bundle(); - args.putInt(TITLE, title); - args.putInt(START_YEAR, startYear); - args.putSerializable(START_MONTH, startMonth); - args.putInt(START_DAY, startDay); - args.putSerializable(START_VARDATE, startVarDate); - args.putBoolean(START_ONLY, startOnly); - args.putInt(END_YEAR, endYear); - args.putSerializable(END_MONTH, endMonth); - args.putInt(END_DAY, endDay); - args.putSerializable(END_VARDATE, endVarDate); - - f.setArguments(args); - f.setShowsDialog(true); - - return f; - } - - @NonNull - @SuppressLint("InflateParams") - @Override - public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { - int title = getArguments().getInt(TITLE); - - VarDate startVarDate = (VarDate) getArguments().getSerializable(START_VARDATE); - VarDate endVarDate = (VarDate) getArguments().getSerializable(END_VARDATE); - - int startYear = getArguments().getInt(START_YEAR); - Month startMonth = (Month) getArguments().getSerializable(START_MONTH); - int startDay = getArguments().getInt(START_DAY); - int endYear = getArguments().getInt(END_YEAR); - Month endMonth = (Month) getArguments().getSerializable(END_MONTH); - int endDay = getArguments().getInt(END_DAY); - - boolean startOnly = getArguments().getBoolean(START_ONLY); - - final SetDateRangeListener listener = (SetDateRangeListener) getParentFragment(); - - Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(title); - - final LayoutInflater inflater = getActivity().getLayoutInflater(); - - View layout = inflater.inflate(R.layout.daterangepicker, null); - builder.setView(layout); - - String[] yearValues = new String[MAX_YEAR - YearRange.FIRST_VALID_YEAR + 2]; - yearValues[0] = "-"; - for (int i = YearRange.FIRST_VALID_YEAR; i <= MAX_YEAR; i++) { - yearValues[i - YearRange.FIRST_VALID_YEAR + 1] = Integer.toString(i); - } - final NumberPickerView npvStartYear = (NumberPickerView) layout.findViewById(R.id.startYear); - npvStartYear.setDisplayedValues(yearValues); - npvStartYear.setMinValue(YearRange.FIRST_VALID_YEAR - 1); - npvStartYear.setMaxValue(MAX_YEAR); - npvStartYear.setValue(startYear != YearRange.UNDEFINED_YEAR ? startYear : YearRange.FIRST_VALID_YEAR - 1); - - String[] monthEntries = getActivity().getResources().getStringArray(R.array.months_entries); - String[] dayValues = new String[32]; - dayValues[0] = "-"; - for (int i = 1; i <= 31; i++) { - dayValues[i] = Integer.toString(i); - } - - String[] varDateEntries = getActivity().getResources().getStringArray(R.array.vardate_entries); - - final NumberPickerView npvStartVarDate = (NumberPickerView) layout.findViewById(R.id.startVarDate); - final NumberPickerView npvStartMonth = (NumberPickerView) layout.findViewById(R.id.startMonth); - final NumberPickerView npvStartDay = (NumberPickerView) layout.findViewById(R.id.startDay); - if (startVarDate == null) { - npvStartVarDate.setVisibility(View.GONE); - npvStartMonth.setVisibility(View.VISIBLE); - npvStartDay.setVisibility(View.VISIBLE); - - npvStartMonth.setDisplayedValues(monthEntries); - npvStartMonth.setMinValue(1); - npvStartMonth.setMaxValue(12); - npvStartMonth.setValue(startMonth.ordinal() + 1); - - npvStartDay.setDisplayedValues(dayValues); - npvStartDay.setMinValue(0); - npvStartDay.setMaxValue(31); - npvStartDay.setValue(startDay != DateWithOffset.UNDEFINED_MONTH_DAY ? startDay : 0); - } else { - npvStartVarDate.setVisibility(View.VISIBLE); - npvStartMonth.setVisibility(View.GONE); - npvStartDay.setVisibility(View.GONE); - - npvStartVarDate.setDisplayedValues(varDateEntries); - npvStartVarDate.setMinValue(1); - npvStartVarDate.setMaxValue(VarDate.values().length); - npvStartVarDate.setValue(startVarDate.ordinal() + 1); - } - - final NumberPickerView npvEndYear = (NumberPickerView) layout.findViewById(R.id.endYear); - npvEndYear.setDisplayedValues(yearValues); - npvEndYear.setMinValue(YearRange.FIRST_VALID_YEAR - 1); - npvEndYear.setMaxValue(MAX_YEAR); - npvEndYear.setValue(endYear != YearRange.UNDEFINED_YEAR ? endYear : YearRange.FIRST_VALID_YEAR - 1); - OnValueChangeListener startYearChangeListener = (picker, oldVal, newVal) -> { - int endY = npvEndYear.getValue(); - if (newVal >= endY && endY >= YearRange.FIRST_VALID_YEAR) { - npvEndYear.smoothScrollToValue(newVal); - } - }; - - final NumberPickerView npvEndVarDate = (NumberPickerView) layout.findViewById(R.id.endVarDate); - final NumberPickerView npvEndMonth = (NumberPickerView) layout.findViewById(R.id.endMonth); - final NumberPickerView npvEndDay = (NumberPickerView) layout.findViewById(R.id.endDay); - - if (startOnly) { - npvEndYear.setVisibility(View.GONE); - npvEndVarDate.setVisibility(View.GONE); - npvEndMonth.setVisibility(View.GONE); - npvEndDay.setVisibility(View.GONE); - } else { - if (endVarDate == null) { - npvEndVarDate.setVisibility(View.GONE); - npvEndMonth.setVisibility(View.VISIBLE); - npvEndDay.setVisibility(View.VISIBLE); - - String[] tempMonthValues = new String[monthEntries.length + 1]; - tempMonthValues[0] = "-"; - for (int i = 0; i < monthEntries.length; i++) { - tempMonthValues[i + 1] = monthEntries[i]; - } - npvEndMonth.setDisplayedValues(tempMonthValues); - npvEndMonth.setMinValue(0); - npvEndMonth.setMaxValue(12); - npvEndMonth.setValue(endMonth != null ? endMonth.ordinal() + 1 : 0); - - npvEndDay.setDisplayedValues(dayValues); - npvEndDay.setMinValue(0); - npvEndDay.setMaxValue(31); - npvEndDay.setValue(endDay != DateWithOffset.UNDEFINED_MONTH_DAY ? endDay : 0); - - npvStartYear.setOnValueChangedListener(startYearChangeListener); - - if (npvStartMonth.getVisibility() == View.VISIBLE) { - npvStartMonth.setOnValueChangedListener((picker, oldVal, newVal) -> { - int endM = npvEndMonth.getValue(); - if (npvStartYear.getValue() == npvEndYear.getValue() && newVal >= endM && endM != 0) { - npvEndMonth.smoothScrollToValue(newVal); - } - }); - } - - if (npvStartDay.getVisibility() == View.VISIBLE) { - npvStartDay.setOnValueChangedListener((NumberPickerView picker, int oldVal, int newVal) -> { - int endD = npvEndDay.getValue(); - if (npvStartYear.getValue() == npvEndYear.getValue() && npvStartMonth.getValue() == npvEndMonth.getValue() && newVal >= endD - && endD != 0) { - npvEndDay.smoothScrollToValue(newVal); - } - }); - } - - } else { - npvEndVarDate.setVisibility(View.VISIBLE); - npvEndMonth.setVisibility(View.GONE); - npvEndDay.setVisibility(View.GONE); - - npvEndVarDate.setDisplayedValues(varDateEntries); - npvEndVarDate.setMinValue(1); - npvEndVarDate.setMaxValue(VarDate.values().length); - npvEndVarDate.setValue(endVarDate.ordinal() + 1); - npvStartYear.setOnValueChangedListener(startYearChangeListener); - } - } - - builder.setPositiveButton(android.R.string.ok, new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - int startYearValue = getYearValue(npvStartYear); - Month startMonthValue = getMonthValue(npvStartMonth); - int startDayValue = getDayValue(npvStartDay); - int endYearValue = getYearValue(npvEndYear); - Month endMonthValue = getMonthValue(npvEndMonth); - int endDayValue = getDayValue(npvEndDay); - - VarDate startVarDateValue = getVarDateValue(npvStartVarDate); - VarDate endVarDateValue = getVarDateValue(npvEndVarDate); - - listener.setDateRange(startYearValue, startMonthValue, null, startDayValue, startVarDateValue, endYearValue, endMonthValue, null, endDayValue, - endVarDateValue); - } - - private Month getMonthValue(final NumberPickerView npvMonth) { - Month monthValue = null; - if (npvMonth.getValue() != 0) { - monthValue = Month.values()[npvMonth.getValue() - 1]; - } - return monthValue; - } - - private int getYearValue(final NumberPickerView npvYear) { - int yearValue = npvYear.getValue(); - if (yearValue == YearRange.FIRST_VALID_YEAR - 1) { - yearValue = NOTHING_SELECTED; - } - return yearValue; - } - - private int getDayValue(final NumberPickerView npvEndDay) { - int dayValue = npvEndDay.getValue(); - if (dayValue == 0) { - dayValue = NOTHING_SELECTED; - } - return dayValue; - } - - private VarDate getVarDateValue(final NumberPickerView npvVarDate) { - VarDate varDateValue = null; - if (npvVarDate.getValue() != 0) { - varDateValue = VarDate.values()[npvVarDate.getValue() - 1]; - } - return varDateValue; - } - }); - builder.setNeutralButton(R.string.spd_ohf_cancel, null); - - return builder.create(); - } -} +package ch.poole.openinghoursfragment.pickers; + +import android.annotation.SuppressLint; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AlertDialog.Builder; +import androidx.appcompat.app.AppCompatDialog; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import ch.poole.openinghoursfragment.CancelableDialogFragment; +import ch.poole.openinghoursfragment.R; +import ch.poole.openinghoursparser.DateWithOffset; +import ch.poole.openinghoursparser.Month; +import ch.poole.openinghoursparser.VarDate; +import ch.poole.openinghoursparser.YearRange; +import ch.poole.android.numberpickerview.library.NumberPickerView; +import ch.poole.android.numberpickerview.library.NumberPickerView.OnValueChangeListener; + +/** + * Display a dialog allowing the user to select values for a start date and optionally an end date + * + */ +public class DateRangePicker extends CancelableDialogFragment { + private static final int MAX_YEAR = 2100; + + public static final int NOTHING_SELECTED = Integer.MIN_VALUE; + + private static final String TITLE = "title"; + private static final String START_YEAR = "startYear"; + private static final String START_MONTH = "startMonth"; + private static final String START_DAY = "startDay"; + private static final String START_VARDATE = "startVarDate"; + private static final String START_ONLY = "startOnly"; + private static final String END_YEAR = "endYear"; + private static final String END_MONTH = "endMonth"; + private static final String END_DAY = "endDay"; + private static final String END_VARDATE = "endVarDate"; + + private static final String TAG = "fragment_daterangepicker"; + + /** + * Show the DateRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startYear initial start year + * @param startMonth initial start month + * @param startDay initial start day of the month + * @param endYear initial end year + * @param endMonth initial end month + * @param endDay initial end day of the month + */ + public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull Month startMonth, int startDay, int endYear, + @Nullable Month endMonth, int endDay) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + DateRangePicker datePickerFragment = newInstance(title, startYear, startMonth, startDay, null, false, endYear, endMonth, endDay, null); + datePickerFragment.show(fm, TAG); + } + + /** + * Show the DateRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startYear initial start year + * @param startVarDate initial start variable date ie easter + * @param endYear initial end year + * @param endMonth initial end month + * @param endDay initial end day of the month + */ + public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull VarDate startVarDate, int endYear, @Nullable Month endMonth, + int endDay) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + DateRangePicker datePickerFragment = newInstance(title, startYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, startVarDate, false, endYear, endMonth, + endDay, null); + datePickerFragment.show(fm, TAG); + } + + /** + * Show the DateRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startYear initial start year + * @param startMonth initial start month + * @param startDay initial start day of the month + * @param endYear initial end year + * @param endVarDate initial end variable date ie easter + */ + public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull Month startMonth, int startDay, int endYear, + @NonNull VarDate endVarDate) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + DateRangePicker datePickerFragment = newInstance(title, startYear, startMonth, startDay, null, false, endYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, + endVarDate); + datePickerFragment.show(fm, TAG); + } + + /** + * Show the DateRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startYear initial start year + * @param startVarDate initial start variable date ie easter + * @param endYear initial end year + * @param endVarDate initial end variable date ie easter + */ + public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull VarDate startVarDate, int endYear, @NonNull VarDate endVarDate) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + DateRangePicker datePickerFragment = newInstance(title, startYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, startVarDate, false, endYear, null, + DateWithOffset.UNDEFINED_MONTH_DAY, endVarDate); + datePickerFragment.show(fm, TAG); + } + + /** + * Show the DateRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startYear initial start year + * @param startMonth initial start month + * @param startDay initial start day of the month + */ + public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull Month startMonth, int startDay) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + DateRangePicker datePickerFragment = newInstance(title, startYear, startMonth, startDay, null, true, YearRange.UNDEFINED_YEAR, null, + DateWithOffset.UNDEFINED_MONTH_DAY, null); + datePickerFragment.show(fm, TAG); + } + + /** + * Show the DateRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startYear initial start year + * @param startVarDate initial start variable date ie easter + */ + public static void showDialog(Fragment parentFragment, int title, int startYear, @NonNull VarDate startVarDate) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + DateRangePicker datePickerFragment = newInstance(title, startYear, null, DateWithOffset.UNDEFINED_MONTH_DAY, startVarDate, true, + YearRange.UNDEFINED_YEAR, null, DateWithOffset.UNDEFINED_MONTH_DAY, null); + datePickerFragment.show(fm, TAG); + } + + /** + * Create a new instance of DateRangePicker + * + * @param title resource id for the title to display + * @param startYear initial start year + * @param startMonth initial start month or null + * @param startDay initial start day of the month + * @param startVarDate initial start variable date ie easter or null + * @param startOnly only show a picker for one date + * @param endYear initial end year + * @param endMonth initial end month or null + * @param endDay initial end day of the month + * @param endVarDate initial end variable date ie easter or null + * @return an instance of DateRangePicker + */ + private static DateRangePicker newInstance(int title, int startYear, @Nullable Month startMonth, int startDay, @Nullable VarDate startVarDate, + boolean startOnly, int endYear, @Nullable Month endMonth, int endDay, @Nullable VarDate endVarDate) { + DateRangePicker f = new DateRangePicker(); + Bundle args = new Bundle(); + args.putInt(TITLE, title); + args.putInt(START_YEAR, startYear); + args.putSerializable(START_MONTH, startMonth); + args.putInt(START_DAY, startDay); + args.putSerializable(START_VARDATE, startVarDate); + args.putBoolean(START_ONLY, startOnly); + args.putInt(END_YEAR, endYear); + args.putSerializable(END_MONTH, endMonth); + args.putInt(END_DAY, endDay); + args.putSerializable(END_VARDATE, endVarDate); + + f.setArguments(args); + f.setShowsDialog(true); + + return f; + } + + @NonNull + @SuppressLint("InflateParams") + @Override + public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { + int title = getArguments().getInt(TITLE); + + VarDate startVarDate = (VarDate) getArguments().getSerializable(START_VARDATE); + VarDate endVarDate = (VarDate) getArguments().getSerializable(END_VARDATE); + + int startYear = getArguments().getInt(START_YEAR); + Month startMonth = (Month) getArguments().getSerializable(START_MONTH); + int startDay = getArguments().getInt(START_DAY); + int endYear = getArguments().getInt(END_YEAR); + Month endMonth = (Month) getArguments().getSerializable(END_MONTH); + int endDay = getArguments().getInt(END_DAY); + + boolean startOnly = getArguments().getBoolean(START_ONLY); + + final SetDateRangeListener listener = (SetDateRangeListener) getParentFragment(); + + Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(title); + + final LayoutInflater inflater = getActivity().getLayoutInflater(); + + View layout = inflater.inflate(R.layout.daterangepicker, null); + builder.setView(layout); + + String[] yearValues = new String[MAX_YEAR - YearRange.FIRST_VALID_YEAR + 2]; + yearValues[0] = "-"; + for (int i = YearRange.FIRST_VALID_YEAR; i <= MAX_YEAR; i++) { + yearValues[i - YearRange.FIRST_VALID_YEAR + 1] = Integer.toString(i); + } + final NumberPickerView npvStartYear = (NumberPickerView) layout.findViewById(R.id.startYear); + npvStartYear.setDisplayedValues(yearValues); + npvStartYear.setMinValue(YearRange.FIRST_VALID_YEAR - 1); + npvStartYear.setMaxValue(MAX_YEAR); + npvStartYear.setValue(startYear != YearRange.UNDEFINED_YEAR ? startYear : YearRange.FIRST_VALID_YEAR - 1); + + String[] monthEntries = getActivity().getResources().getStringArray(R.array.months_entries); + String[] dayValues = new String[32]; + dayValues[0] = "-"; + for (int i = 1; i <= 31; i++) { + dayValues[i] = Integer.toString(i); + } + + String[] varDateEntries = getActivity().getResources().getStringArray(R.array.vardate_entries); + + final NumberPickerView npvStartVarDate = (NumberPickerView) layout.findViewById(R.id.startVarDate); + final NumberPickerView npvStartMonth = (NumberPickerView) layout.findViewById(R.id.startMonth); + final NumberPickerView npvStartDay = (NumberPickerView) layout.findViewById(R.id.startDay); + if (startVarDate == null) { + npvStartVarDate.setVisibility(View.GONE); + npvStartMonth.setVisibility(View.VISIBLE); + npvStartDay.setVisibility(View.VISIBLE); + + npvStartMonth.setDisplayedValues(monthEntries); + npvStartMonth.setMinValue(1); + npvStartMonth.setMaxValue(12); + npvStartMonth.setValue(startMonth.ordinal() + 1); + + npvStartDay.setDisplayedValues(dayValues); + npvStartDay.setMinValue(0); + npvStartDay.setMaxValue(31); + npvStartDay.setValue(startDay != DateWithOffset.UNDEFINED_MONTH_DAY ? startDay : 0); + } else { + npvStartVarDate.setVisibility(View.VISIBLE); + npvStartMonth.setVisibility(View.GONE); + npvStartDay.setVisibility(View.GONE); + + npvStartVarDate.setDisplayedValues(varDateEntries); + npvStartVarDate.setMinValue(1); + npvStartVarDate.setMaxValue(VarDate.values().length); + npvStartVarDate.setValue(startVarDate.ordinal() + 1); + } + + final NumberPickerView npvEndYear = (NumberPickerView) layout.findViewById(R.id.endYear); + npvEndYear.setDisplayedValues(yearValues); + npvEndYear.setMinValue(YearRange.FIRST_VALID_YEAR - 1); + npvEndYear.setMaxValue(MAX_YEAR); + npvEndYear.setValue(endYear != YearRange.UNDEFINED_YEAR ? endYear : YearRange.FIRST_VALID_YEAR - 1); + OnValueChangeListener startYearChangeListener = (picker, oldVal, newVal) -> { + int endY = npvEndYear.getValue(); + if (newVal >= endY && endY >= YearRange.FIRST_VALID_YEAR) { + npvEndYear.smoothScrollToValue(newVal); + } + }; + + final NumberPickerView npvEndVarDate = (NumberPickerView) layout.findViewById(R.id.endVarDate); + final NumberPickerView npvEndMonth = (NumberPickerView) layout.findViewById(R.id.endMonth); + final NumberPickerView npvEndDay = (NumberPickerView) layout.findViewById(R.id.endDay); + + if (startOnly) { + npvEndYear.setVisibility(View.GONE); + npvEndVarDate.setVisibility(View.GONE); + npvEndMonth.setVisibility(View.GONE); + npvEndDay.setVisibility(View.GONE); + } else { + if (endVarDate == null) { + npvEndVarDate.setVisibility(View.GONE); + npvEndMonth.setVisibility(View.VISIBLE); + npvEndDay.setVisibility(View.VISIBLE); + + String[] tempMonthValues = new String[monthEntries.length + 1]; + tempMonthValues[0] = "-"; + for (int i = 0; i < monthEntries.length; i++) { + tempMonthValues[i + 1] = monthEntries[i]; + } + npvEndMonth.setDisplayedValues(tempMonthValues); + npvEndMonth.setMinValue(0); + npvEndMonth.setMaxValue(12); + npvEndMonth.setValue(endMonth != null ? endMonth.ordinal() + 1 : 0); + + npvEndDay.setDisplayedValues(dayValues); + npvEndDay.setMinValue(0); + npvEndDay.setMaxValue(31); + npvEndDay.setValue(endDay != DateWithOffset.UNDEFINED_MONTH_DAY ? endDay : 0); + + npvStartYear.setOnValueChangedListener(startYearChangeListener); + + if (npvStartMonth.getVisibility() == View.VISIBLE) { + npvStartMonth.setOnValueChangedListener((picker, oldVal, newVal) -> { + int endM = npvEndMonth.getValue(); + if (npvStartYear.getValue() == npvEndYear.getValue() && newVal >= endM && endM != 0) { + npvEndMonth.smoothScrollToValue(newVal); + } + }); + } + + if (npvStartDay.getVisibility() == View.VISIBLE) { + npvStartDay.setOnValueChangedListener((NumberPickerView picker, int oldVal, int newVal) -> { + int endD = npvEndDay.getValue(); + if (npvStartYear.getValue() == npvEndYear.getValue() && npvStartMonth.getValue() == npvEndMonth.getValue() && newVal >= endD + && endD != 0) { + npvEndDay.smoothScrollToValue(newVal); + } + }); + } + + } else { + npvEndVarDate.setVisibility(View.VISIBLE); + npvEndMonth.setVisibility(View.GONE); + npvEndDay.setVisibility(View.GONE); + + npvEndVarDate.setDisplayedValues(varDateEntries); + npvEndVarDate.setMinValue(1); + npvEndVarDate.setMaxValue(VarDate.values().length); + npvEndVarDate.setValue(endVarDate.ordinal() + 1); + npvStartYear.setOnValueChangedListener(startYearChangeListener); + } + } + + builder.setPositiveButton(android.R.string.ok, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + int startYearValue = getYearValue(npvStartYear); + Month startMonthValue = getMonthValue(npvStartMonth); + int startDayValue = getDayValue(npvStartDay); + int endYearValue = getYearValue(npvEndYear); + Month endMonthValue = getMonthValue(npvEndMonth); + int endDayValue = getDayValue(npvEndDay); + + VarDate startVarDateValue = getVarDateValue(npvStartVarDate); + VarDate endVarDateValue = getVarDateValue(npvEndVarDate); + + listener.setDateRange(startYearValue, startMonthValue, null, startDayValue, startVarDateValue, endYearValue, endMonthValue, null, endDayValue, + endVarDateValue); + } + + private Month getMonthValue(final NumberPickerView npvMonth) { + Month monthValue = null; + if (npvMonth.getValue() != 0) { + monthValue = Month.values()[npvMonth.getValue() - 1]; + } + return monthValue; + } + + private int getYearValue(final NumberPickerView npvYear) { + int yearValue = npvYear.getValue(); + if (yearValue == YearRange.FIRST_VALID_YEAR - 1) { + yearValue = NOTHING_SELECTED; + } + return yearValue; + } + + private int getDayValue(final NumberPickerView npvEndDay) { + int dayValue = npvEndDay.getValue(); + if (dayValue == 0) { + dayValue = NOTHING_SELECTED; + } + return dayValue; + } + + private VarDate getVarDateValue(final NumberPickerView npvVarDate) { + VarDate varDateValue = null; + if (npvVarDate.getValue() != 0) { + varDateValue = VarDate.values()[npvVarDate.getValue() - 1]; + } + return varDateValue; + } + }); + builder.setNeutralButton(R.string.spd_ohf_cancel, null); + + return builder.create(); + } +} diff --git a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/OccurrenceInMonthPicker.java b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/OccurrenceInMonthPicker.java index 1d755ca..c4e257d 100644 --- a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/OccurrenceInMonthPicker.java +++ b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/OccurrenceInMonthPicker.java @@ -18,7 +18,7 @@ import ch.poole.openinghoursparser.Month; import ch.poole.openinghoursparser.WeekDay; import ch.poole.openinghoursparser.YearRange; -import cn.carbswang.android.numberpickerview.library.NumberPickerView; +import ch.poole.android.numberpickerview.library.NumberPickerView; /** * Display a dialog allowing the user to select values for a start date and optionally an end date diff --git a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/RangePicker.java b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/RangePicker.java index 37e3267..d4cde55 100644 --- a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/RangePicker.java +++ b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/RangePicker.java @@ -1,130 +1,130 @@ -package ch.poole.openinghoursfragment.pickers; - -import android.annotation.SuppressLint; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AlertDialog.Builder; -import androidx.appcompat.app.AppCompatDialog; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import ch.poole.openinghoursfragment.CancelableDialogFragment; -import ch.poole.openinghoursfragment.R; -import cn.carbswang.android.numberpickerview.library.NumberPickerView; - -/** - * Display a dialog allowing the user to set a numeric range - * - */ -public class RangePicker extends CancelableDialogFragment { - public static final int NOTHING_SELECTED = Integer.MIN_VALUE; - - private static final String TITLE = "title"; - private static final String MIN = "min"; - private static final String MAX = "max"; - private static final String START_CURRENT = "startCurrent"; - private static final String END_CURRENT = "endCurrent"; - - private static final String TAG = "fragment_rangepicker"; - - /** - * Show the range picker dialog - * - * @param parentFragment Fragment that called this - * @param title resource id for the title to display - * @param min minimum range value - * @param max maximum range value - * @param startCurrent initial start value - * @param endCurrent initial end value - */ - public static void showDialog(Fragment parentFragment, int title, int min, int max, int startCurrent, int endCurrent) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - RangePicker rangePickerFragment = newInstance(title, min, max, startCurrent, endCurrent); - rangePickerFragment.show(fm, TAG); - } - - /** - * Create a new instance of RangePicker - * - * @param title resource id for the title to display - * @param min minimum range value - * @param max maximum range value - * @param startCurrent initial start value - * @param endCurrent initial end value - * @return an instance of RangePicker - */ - private static RangePicker newInstance(int title, int min, int max, int startCurrent, int endCurrent) { - RangePicker f = new RangePicker(); - Bundle args = new Bundle(); - args.putInt(TITLE, title); - args.putInt(MIN, min); - args.putInt(MAX, max); - args.putInt(START_CURRENT, startCurrent); - args.putInt(END_CURRENT, endCurrent); - - f.setArguments(args); - f.setShowsDialog(true); - - return f; - } - - @NonNull - @SuppressLint("InflateParams") - @Override - public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { - int title = getArguments().getInt(TITLE); - final int min = getArguments().getInt(MIN); - final int max = getArguments().getInt(MAX); - int startCurrent = getArguments().getInt(START_CURRENT); - int endCurrent = getArguments().getInt(END_CURRENT); - final SetRangeListener listener = (SetRangeListener) getParentFragment(); - - Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(title); - - final LayoutInflater inflater = getActivity().getLayoutInflater(); - - View layout = inflater.inflate(R.layout.rangepicker, null); - builder.setView(layout); - - String[] startValues = new String[max - min + 1]; - for (int i = min; i <= max; i++) { - startValues[i - min] = Integer.toString(i); - } - final NumberPickerView npvStart = (NumberPickerView) layout.findViewById(R.id.start); - npvStart.setDisplayedValues(startValues); - npvStart.setMinValue(min); - npvStart.setMaxValue(max); - npvStart.setValue(startCurrent); - - String[] endValues = new String[max - min + 2]; - endValues[0] = "-"; - for (int i = min; i <= max; i++) { - endValues[i - min + 1] = Integer.toString(i); - } - final NumberPickerView npvEnd = (NumberPickerView) layout.findViewById(R.id.end); - npvEnd.setDisplayedValues(endValues); - npvEnd.setMinValue(min - 1); - npvEnd.setMaxValue(max); - if (endCurrent == NOTHING_SELECTED) { - endCurrent = min - 1; - } - npvEnd.setValue(endCurrent); - - builder.setPositiveButton(R.string.spd_ohf_ok, (DialogInterface dialog, int which) -> { - int endValue = npvEnd.getValue(); - if (endValue == min - 1) { - endValue = NOTHING_SELECTED; - } - listener.setRange(npvStart.getValue(), endValue); - }); - builder.setNeutralButton(R.string.spd_ohf_cancel, null); - - return builder.create(); - } -} +package ch.poole.openinghoursfragment.pickers; + +import android.annotation.SuppressLint; +import android.content.DialogInterface; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AlertDialog.Builder; +import androidx.appcompat.app.AppCompatDialog; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import ch.poole.openinghoursfragment.CancelableDialogFragment; +import ch.poole.openinghoursfragment.R; +import ch.poole.android.numberpickerview.library.NumberPickerView; + +/** + * Display a dialog allowing the user to set a numeric range + * + */ +public class RangePicker extends CancelableDialogFragment { + public static final int NOTHING_SELECTED = Integer.MIN_VALUE; + + private static final String TITLE = "title"; + private static final String MIN = "min"; + private static final String MAX = "max"; + private static final String START_CURRENT = "startCurrent"; + private static final String END_CURRENT = "endCurrent"; + + private static final String TAG = "fragment_rangepicker"; + + /** + * Show the range picker dialog + * + * @param parentFragment Fragment that called this + * @param title resource id for the title to display + * @param min minimum range value + * @param max maximum range value + * @param startCurrent initial start value + * @param endCurrent initial end value + */ + public static void showDialog(Fragment parentFragment, int title, int min, int max, int startCurrent, int endCurrent) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + RangePicker rangePickerFragment = newInstance(title, min, max, startCurrent, endCurrent); + rangePickerFragment.show(fm, TAG); + } + + /** + * Create a new instance of RangePicker + * + * @param title resource id for the title to display + * @param min minimum range value + * @param max maximum range value + * @param startCurrent initial start value + * @param endCurrent initial end value + * @return an instance of RangePicker + */ + private static RangePicker newInstance(int title, int min, int max, int startCurrent, int endCurrent) { + RangePicker f = new RangePicker(); + Bundle args = new Bundle(); + args.putInt(TITLE, title); + args.putInt(MIN, min); + args.putInt(MAX, max); + args.putInt(START_CURRENT, startCurrent); + args.putInt(END_CURRENT, endCurrent); + + f.setArguments(args); + f.setShowsDialog(true); + + return f; + } + + @NonNull + @SuppressLint("InflateParams") + @Override + public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { + int title = getArguments().getInt(TITLE); + final int min = getArguments().getInt(MIN); + final int max = getArguments().getInt(MAX); + int startCurrent = getArguments().getInt(START_CURRENT); + int endCurrent = getArguments().getInt(END_CURRENT); + final SetRangeListener listener = (SetRangeListener) getParentFragment(); + + Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(title); + + final LayoutInflater inflater = getActivity().getLayoutInflater(); + + View layout = inflater.inflate(R.layout.rangepicker, null); + builder.setView(layout); + + String[] startValues = new String[max - min + 1]; + for (int i = min; i <= max; i++) { + startValues[i - min] = Integer.toString(i); + } + final NumberPickerView npvStart = (NumberPickerView) layout.findViewById(R.id.start); + npvStart.setDisplayedValues(startValues); + npvStart.setMinValue(min); + npvStart.setMaxValue(max); + npvStart.setValue(startCurrent); + + String[] endValues = new String[max - min + 2]; + endValues[0] = "-"; + for (int i = min; i <= max; i++) { + endValues[i - min + 1] = Integer.toString(i); + } + final NumberPickerView npvEnd = (NumberPickerView) layout.findViewById(R.id.end); + npvEnd.setDisplayedValues(endValues); + npvEnd.setMinValue(min - 1); + npvEnd.setMaxValue(max); + if (endCurrent == NOTHING_SELECTED) { + endCurrent = min - 1; + } + npvEnd.setValue(endCurrent); + + builder.setPositiveButton(R.string.spd_ohf_ok, (DialogInterface dialog, int which) -> { + int endValue = npvEnd.getValue(); + if (endValue == min - 1) { + endValue = NOTHING_SELECTED; + } + listener.setRange(npvStart.getValue(), endValue); + }); + builder.setNeutralButton(R.string.spd_ohf_cancel, null); + + return builder.create(); + } +} diff --git a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/TimeRangePicker.java b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/TimeRangePicker.java index 1347d68..df30be0 100644 --- a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/TimeRangePicker.java +++ b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/TimeRangePicker.java @@ -1,194 +1,194 @@ -package ch.poole.openinghoursfragment.pickers; - -import android.annotation.SuppressLint; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AlertDialog.Builder; -import androidx.appcompat.app.AppCompatDialog; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import ch.poole.openinghoursfragment.CancelableDialogFragment; -import ch.poole.openinghoursfragment.R; -import cn.carbswang.android.numberpickerview.library.NumberPickerView; - -/** - * Display a dialog allowing the user to select values for a start date and optionally an end date - * - */ -public class TimeRangePicker extends CancelableDialogFragment { - public static final int NOTHING_SELECTED = Integer.MIN_VALUE; - - private static final String TITLE = "title"; - private static final String START_HOUR = "startHour"; - private static final String START_MINUTE = "startMinute"; - private static final String START_ONLY = "startOnly"; - private static final String END_HOUR = "endHour"; - private static final String END_MINUTE = "endMinute"; - private static final String INCREMENT = "increment"; - - private static final String TAG = "fragment_timepicker"; - - private static final int MAX_MINUTES = 59; - private static final int MAX_EXTENDED_HOURS = 48; - - /** - * Show the TimeRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startHour initial start hour - * @param startMinute initial start minute - * @param endHour initial end hour - * @param endMinute initial end minute - * @param increment minute tick size - */ - public static void showDialog(@NonNull Fragment parentFragment, int title, int startHour, int startMinute, int endHour, int endMinute, int increment) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - TimeRangePicker timePickerFragment = newInstance(title, startHour, startMinute, false, endHour, endMinute, increment); - timePickerFragment.show(fm, TAG); - } - - /** - * Show the TimeRangePicker dialog - * - * @param parentFragment Fragment calling this - * @param title resource id for the title to display - * @param startHour initial start hour - * @param startMinute initial start minute - * @param increment minute tick size - */ - public static void showDialog(@NonNull Fragment parentFragment, int title, int startHour, int startMinute, int increment) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - TimeRangePicker timePickerFragment = newInstance(title, startHour, startMinute, true, NOTHING_SELECTED, NOTHING_SELECTED, increment); - timePickerFragment.show(fm, TAG); - } - - /** - * Create a new instance of DateRangePicker - * - * @param title resource id for the title to display - * @param startHour initial start hour - * @param startMinute initial start minute - * @param startOnly only show a picker for one time - * @param endHour initial end hour - * @param endMinute initial end minute - * @param increment minute tick size - * @return an instance of TimeRangePicker - */ - private static TimeRangePicker newInstance(int title, int startHour, int startMinute, boolean startOnly, int endHour, int endMinute, int increment) { - TimeRangePicker f = new TimeRangePicker(); - Bundle args = new Bundle(); - args.putInt(TITLE, title); - args.putInt(START_HOUR, startHour); - args.putInt(START_MINUTE, startMinute); - args.putBoolean(START_ONLY, startOnly); - args.putInt(END_HOUR, endHour); - args.putInt(END_MINUTE, endMinute); - args.putInt(INCREMENT, increment); - - f.setArguments(args); - f.setShowsDialog(true); - - return f; - } - - @NonNull - @SuppressLint("InflateParams") - @Override - public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { - - int title = getArguments().getInt(TITLE); - - int startHour = getArguments().getInt(START_HOUR); - int startMinute = getArguments().getInt(START_MINUTE); - int endHour = getArguments().getInt(END_HOUR); - int endMinute = getArguments().getInt(END_MINUTE); - int increment = getArguments().getInt(INCREMENT, 1); - boolean startOnly = getArguments().getBoolean(START_ONLY); - - final SetTimeRangeListener listener = (SetTimeRangeListener) getParentFragment(); - - Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(title); - - final LayoutInflater inflater = getActivity().getLayoutInflater(); - - View layout = inflater.inflate(R.layout.timerangepicker, null); - builder.setView(layout); - - String[] hourValues = new String[MAX_EXTENDED_HOURS + 1]; - for (int i = 0; i <= MAX_EXTENDED_HOURS; i++) { - hourValues[i] = String.format("%02d", i); - } - final NumberPickerView npvStartHour = (NumberPickerView) layout.findViewById(R.id.startHour); - npvStartHour.setDisplayedValues(hourValues); - npvStartHour.setMinValue(0); - npvStartHour.setMaxValue(23); - npvStartHour.setValue(startHour); - - int ticks = 60 / increment; - String[] minValues = new String[ticks]; - for (int i = 0; i < ticks; i++) { - minValues[i] = String.format("%02d", i * increment); - } - - final NumberPickerView npvStartMinute = (NumberPickerView) layout.findViewById(R.id.startMinute); - - int maxMinutes = MAX_MINUTES / increment; - npvStartMinute.setDisplayedValues(minValues); - npvStartMinute.setMinValue(0); - npvStartMinute.setMaxValue(maxMinutes); - npvStartMinute.setValue(startMinute / increment); - - final NumberPickerView npvEndHour = (NumberPickerView) layout.findViewById(R.id.endHour); - final NumberPickerView npvEndMinute = (NumberPickerView) layout.findViewById(R.id.endMinute); - if (startOnly) { - npvEndHour.setVisibility(View.GONE); - npvEndMinute.setVisibility(View.GONE); - } else { - npvStartHour.setOnValueChangedListener((picker, oldVal, newVal) -> { - if (newVal >= npvEndHour.getValue()) { - npvEndHour.smoothScrollToValue(newVal); - } - }); - npvEndHour.setVisibility(View.VISIBLE); - npvEndMinute.setVisibility(View.VISIBLE); - npvEndHour.setDisplayedValues(hourValues); - npvEndHour.setMinValue(0); - npvEndHour.setMaxValue(MAX_EXTENDED_HOURS); - npvEndHour.setValue(endHour); - npvEndHour.setOnValueChangedListener((picker, oldVal, newVal) -> { - if (newVal == MAX_EXTENDED_HOURS) { - npvEndMinute.smoothScrollToValue(0); - npvEndMinute.setMaxValue(0); - } else { - npvEndMinute.setMaxValue(maxMinutes); - } - }); - - npvEndMinute.setDisplayedValues(minValues); - npvEndMinute.setMinValue(0); - npvEndMinute.setMaxValue(maxMinutes); - npvEndMinute.setValue(endMinute / increment); - } - - builder.setPositiveButton(R.string.spd_ohf_ok, (dialog, which) -> { - int startHourValue = npvStartHour.getValue(); - int startMinuteValue = npvStartMinute.getValue() * increment; - int endHourValue = npvEndHour.getValue(); - int endMinuteValue = npvEndMinute.getValue() * increment; - - listener.setTimeRange(startHourValue, startMinuteValue, endHourValue, endMinuteValue); - }); - builder.setNeutralButton(R.string.spd_ohf_cancel, null); - - return builder.create(); - } -} +package ch.poole.openinghoursfragment.pickers; + +import android.annotation.SuppressLint; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AlertDialog.Builder; +import androidx.appcompat.app.AppCompatDialog; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import ch.poole.openinghoursfragment.CancelableDialogFragment; +import ch.poole.openinghoursfragment.R; +import ch.poole.android.numberpickerview.library.NumberPickerView; + +/** + * Display a dialog allowing the user to select values for a start date and optionally an end date + * + */ +public class TimeRangePicker extends CancelableDialogFragment { + public static final int NOTHING_SELECTED = Integer.MIN_VALUE; + + private static final String TITLE = "title"; + private static final String START_HOUR = "startHour"; + private static final String START_MINUTE = "startMinute"; + private static final String START_ONLY = "startOnly"; + private static final String END_HOUR = "endHour"; + private static final String END_MINUTE = "endMinute"; + private static final String INCREMENT = "increment"; + + private static final String TAG = "fragment_timepicker"; + + private static final int MAX_MINUTES = 59; + private static final int MAX_EXTENDED_HOURS = 48; + + /** + * Show the TimeRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startHour initial start hour + * @param startMinute initial start minute + * @param endHour initial end hour + * @param endMinute initial end minute + * @param increment minute tick size + */ + public static void showDialog(@NonNull Fragment parentFragment, int title, int startHour, int startMinute, int endHour, int endMinute, int increment) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + TimeRangePicker timePickerFragment = newInstance(title, startHour, startMinute, false, endHour, endMinute, increment); + timePickerFragment.show(fm, TAG); + } + + /** + * Show the TimeRangePicker dialog + * + * @param parentFragment Fragment calling this + * @param title resource id for the title to display + * @param startHour initial start hour + * @param startMinute initial start minute + * @param increment minute tick size + */ + public static void showDialog(@NonNull Fragment parentFragment, int title, int startHour, int startMinute, int increment) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + TimeRangePicker timePickerFragment = newInstance(title, startHour, startMinute, true, NOTHING_SELECTED, NOTHING_SELECTED, increment); + timePickerFragment.show(fm, TAG); + } + + /** + * Create a new instance of DateRangePicker + * + * @param title resource id for the title to display + * @param startHour initial start hour + * @param startMinute initial start minute + * @param startOnly only show a picker for one time + * @param endHour initial end hour + * @param endMinute initial end minute + * @param increment minute tick size + * @return an instance of TimeRangePicker + */ + private static TimeRangePicker newInstance(int title, int startHour, int startMinute, boolean startOnly, int endHour, int endMinute, int increment) { + TimeRangePicker f = new TimeRangePicker(); + Bundle args = new Bundle(); + args.putInt(TITLE, title); + args.putInt(START_HOUR, startHour); + args.putInt(START_MINUTE, startMinute); + args.putBoolean(START_ONLY, startOnly); + args.putInt(END_HOUR, endHour); + args.putInt(END_MINUTE, endMinute); + args.putInt(INCREMENT, increment); + + f.setArguments(args); + f.setShowsDialog(true); + + return f; + } + + @NonNull + @SuppressLint("InflateParams") + @Override + public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { + + int title = getArguments().getInt(TITLE); + + int startHour = getArguments().getInt(START_HOUR); + int startMinute = getArguments().getInt(START_MINUTE); + int endHour = getArguments().getInt(END_HOUR); + int endMinute = getArguments().getInt(END_MINUTE); + int increment = getArguments().getInt(INCREMENT, 1); + boolean startOnly = getArguments().getBoolean(START_ONLY); + + final SetTimeRangeListener listener = (SetTimeRangeListener) getParentFragment(); + + Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(title); + + final LayoutInflater inflater = getActivity().getLayoutInflater(); + + View layout = inflater.inflate(R.layout.timerangepicker, null); + builder.setView(layout); + + String[] hourValues = new String[MAX_EXTENDED_HOURS + 1]; + for (int i = 0; i <= MAX_EXTENDED_HOURS; i++) { + hourValues[i] = String.format("%02d", i); + } + final NumberPickerView npvStartHour = (NumberPickerView) layout.findViewById(R.id.startHour); + npvStartHour.setDisplayedValues(hourValues); + npvStartHour.setMinValue(0); + npvStartHour.setMaxValue(23); + npvStartHour.setValue(startHour); + + int ticks = 60 / increment; + String[] minValues = new String[ticks]; + for (int i = 0; i < ticks; i++) { + minValues[i] = String.format("%02d", i * increment); + } + + final NumberPickerView npvStartMinute = (NumberPickerView) layout.findViewById(R.id.startMinute); + + int maxMinutes = MAX_MINUTES / increment; + npvStartMinute.setDisplayedValues(minValues); + npvStartMinute.setMinValue(0); + npvStartMinute.setMaxValue(maxMinutes); + npvStartMinute.setValue(startMinute / increment); + + final NumberPickerView npvEndHour = (NumberPickerView) layout.findViewById(R.id.endHour); + final NumberPickerView npvEndMinute = (NumberPickerView) layout.findViewById(R.id.endMinute); + if (startOnly) { + npvEndHour.setVisibility(View.GONE); + npvEndMinute.setVisibility(View.GONE); + } else { + npvStartHour.setOnValueChangedListener((picker, oldVal, newVal) -> { + if (newVal >= npvEndHour.getValue()) { + npvEndHour.smoothScrollToValue(newVal); + } + }); + npvEndHour.setVisibility(View.VISIBLE); + npvEndMinute.setVisibility(View.VISIBLE); + npvEndHour.setDisplayedValues(hourValues); + npvEndHour.setMinValue(0); + npvEndHour.setMaxValue(MAX_EXTENDED_HOURS); + npvEndHour.setValue(endHour); + npvEndHour.setOnValueChangedListener((picker, oldVal, newVal) -> { + if (newVal == MAX_EXTENDED_HOURS) { + npvEndMinute.smoothScrollToValue(0); + npvEndMinute.setMaxValue(0); + } else { + npvEndMinute.setMaxValue(maxMinutes); + } + }); + + npvEndMinute.setDisplayedValues(minValues); + npvEndMinute.setMinValue(0); + npvEndMinute.setMaxValue(maxMinutes); + npvEndMinute.setValue(endMinute / increment); + } + + builder.setPositiveButton(R.string.spd_ohf_ok, (dialog, which) -> { + int startHourValue = npvStartHour.getValue(); + int startMinuteValue = npvStartMinute.getValue() * increment; + int endHourValue = npvEndHour.getValue(); + int endMinuteValue = npvEndMinute.getValue() * increment; + + listener.setTimeRange(startHourValue, startMinuteValue, endHourValue, endMinuteValue); + }); + builder.setNeutralButton(R.string.spd_ohf_cancel, null); + + return builder.create(); + } +} diff --git a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/ValuePicker.java b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/ValuePicker.java index 84f2c17..f3164eb 100644 --- a/lib/src/main/java/ch/poole/openinghoursfragment/pickers/ValuePicker.java +++ b/lib/src/main/java/ch/poole/openinghoursfragment/pickers/ValuePicker.java @@ -1,105 +1,105 @@ -package ch.poole.openinghoursfragment.pickers; - -import android.annotation.SuppressLint; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AlertDialog.Builder; -import androidx.appcompat.app.AppCompatDialog; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentManager; -import ch.poole.openinghoursfragment.CancelableDialogFragment; -import ch.poole.openinghoursfragment.R; -import cn.carbswang.android.numberpickerview.library.NumberPickerView; - -/** - * Display a dialog allowing the user to set a single numeric value - * - */ -public class ValuePicker extends CancelableDialogFragment { - public static final int NOTHING_SELECTED = Integer.MIN_VALUE; - - private static final String TITLE_KEY = "title"; - private static final String MIN_KEY = "min"; - private static final String MAX_KEY = "max"; - private static final String CURRENT_KEY = "current"; - - private static final String TAG = "fragment_valuepicker"; - - /** - * Show the range picker dialog - * - * @param parentFragment Fragment that called this - * @param title resource id for the title to display - * @param min minimum range value - * @param max maximum range value - * @param current initial value - */ - public static void showDialog(Fragment parentFragment, int title, int min, int max, int current) { - dismissDialog(parentFragment, TAG); - - FragmentManager fm = parentFragment.getChildFragmentManager(); - ValuePicker valuePickerFragment = newInstance(title, min, max, current); - valuePickerFragment.show(fm, TAG); - } - - /** - * Create a new instance of RangePicker - * - * @param title resource id for the title to display - * @param min minimum range value - * @param max maximum range value - * @param current initial value - * @return an instance of ValuePicker - */ - private static ValuePicker newInstance(int title, int min, int max, int current) { - ValuePicker f = new ValuePicker(); - Bundle args = new Bundle(); - args.putInt(TITLE_KEY, title); - args.putInt(MIN_KEY, min); - args.putInt(MAX_KEY, max); - args.putInt(CURRENT_KEY, current); - - f.setArguments(args); - f.setShowsDialog(true); - - return f; - } - - @NonNull - @SuppressLint("InflateParams") - @Override - public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { - int title = getArguments().getInt(TITLE_KEY); - final int min = getArguments().getInt(MIN_KEY); - final int max = getArguments().getInt(MAX_KEY); - int current = getArguments().getInt(CURRENT_KEY); - final SetRangeListener listener = (SetRangeListener) getParentFragment(); - - Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(title); - - final LayoutInflater inflater = getActivity().getLayoutInflater(); - - View layout = inflater.inflate(R.layout.valuepicker, null); - builder.setView(layout); - - String[] startValues = new String[max - min + 1]; - for (int i = min; i <= max; i++) { - startValues[i - min] = Integer.toString(i); - } - final NumberPickerView npvStart = (NumberPickerView) layout.findViewById(R.id.start); - npvStart.setDisplayedValues(startValues); - npvStart.setMinValue(min); - npvStart.setMaxValue(max); - npvStart.setValue(current); - - builder.setPositiveButton(R.string.spd_ohf_ok, (DialogInterface dialog, int which) -> listener.setRange(npvStart.getValue(), -1)); - builder.setNeutralButton(R.string.spd_ohf_cancel, null); - - return builder.create(); - } -} +package ch.poole.openinghoursfragment.pickers; + +import android.annotation.SuppressLint; +import android.content.DialogInterface; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AlertDialog.Builder; +import androidx.appcompat.app.AppCompatDialog; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import ch.poole.openinghoursfragment.CancelableDialogFragment; +import ch.poole.openinghoursfragment.R; +import ch.poole.android.numberpickerview.library.NumberPickerView; + +/** + * Display a dialog allowing the user to set a single numeric value + * + */ +public class ValuePicker extends CancelableDialogFragment { + public static final int NOTHING_SELECTED = Integer.MIN_VALUE; + + private static final String TITLE_KEY = "title"; + private static final String MIN_KEY = "min"; + private static final String MAX_KEY = "max"; + private static final String CURRENT_KEY = "current"; + + private static final String TAG = "fragment_valuepicker"; + + /** + * Show the range picker dialog + * + * @param parentFragment Fragment that called this + * @param title resource id for the title to display + * @param min minimum range value + * @param max maximum range value + * @param current initial value + */ + public static void showDialog(Fragment parentFragment, int title, int min, int max, int current) { + dismissDialog(parentFragment, TAG); + + FragmentManager fm = parentFragment.getChildFragmentManager(); + ValuePicker valuePickerFragment = newInstance(title, min, max, current); + valuePickerFragment.show(fm, TAG); + } + + /** + * Create a new instance of RangePicker + * + * @param title resource id for the title to display + * @param min minimum range value + * @param max maximum range value + * @param current initial value + * @return an instance of ValuePicker + */ + private static ValuePicker newInstance(int title, int min, int max, int current) { + ValuePicker f = new ValuePicker(); + Bundle args = new Bundle(); + args.putInt(TITLE_KEY, title); + args.putInt(MIN_KEY, min); + args.putInt(MAX_KEY, max); + args.putInt(CURRENT_KEY, current); + + f.setArguments(args); + f.setShowsDialog(true); + + return f; + } + + @NonNull + @SuppressLint("InflateParams") + @Override + public AppCompatDialog onCreateDialog(Bundle savedInstanceState) { + int title = getArguments().getInt(TITLE_KEY); + final int min = getArguments().getInt(MIN_KEY); + final int max = getArguments().getInt(MAX_KEY); + int current = getArguments().getInt(CURRENT_KEY); + final SetRangeListener listener = (SetRangeListener) getParentFragment(); + + Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(title); + + final LayoutInflater inflater = getActivity().getLayoutInflater(); + + View layout = inflater.inflate(R.layout.valuepicker, null); + builder.setView(layout); + + String[] startValues = new String[max - min + 1]; + for (int i = min; i <= max; i++) { + startValues[i - min] = Integer.toString(i); + } + final NumberPickerView npvStart = (NumberPickerView) layout.findViewById(R.id.start); + npvStart.setDisplayedValues(startValues); + npvStart.setMinValue(min); + npvStart.setMaxValue(max); + npvStart.setValue(current); + + builder.setPositiveButton(R.string.spd_ohf_ok, (DialogInterface dialog, int which) -> listener.setRange(npvStart.getValue(), -1)); + builder.setNeutralButton(R.string.spd_ohf_cancel, null); + + return builder.create(); + } +} diff --git a/lib/src/main/res/layout/datepicker.xml b/lib/src/main/res/layout/datepicker.xml index 60cfe99..ec1df89 100644 --- a/lib/src/main/res/layout/datepicker.xml +++ b/lib/src/main/res/layout/datepicker.xml @@ -10,7 +10,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" > - - - - - - - - - - - - - - - - - - - - - -