From c47a5905f026d68c68eadd9c5bd4faeafb051995 Mon Sep 17 00:00:00 2001 From: Galeen <2math@mail.bg> Date: Thu, 16 May 2019 10:19:54 +0300 Subject: [PATCH] ability to add min/max hours, minutes and seconds for MaterialDateTimePicker --- .../coreapp/ui/login/LoginFragment.java | 31 +++++- app/src/main/res/layout/fragment_login.xml | 1 + .../BaseDateTimePickerController.java | 95 +++++++++++++++++-- .../MaterialDateTimePickerController.java | 43 ++++++--- 4 files changed, 151 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/criapp_studio/coreapp/ui/login/LoginFragment.java b/app/src/main/java/com/criapp_studio/coreapp/ui/login/LoginFragment.java index 7b6a8d9..130fc8f 100644 --- a/app/src/main/java/com/criapp_studio/coreapp/ui/login/LoginFragment.java +++ b/app/src/main/java/com/criapp_studio/coreapp/ui/login/LoginFragment.java @@ -5,6 +5,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Toast; import com.criapp_studio.coreapp.R; import com.criapp_studio.coreapp.repository.Repository; @@ -12,10 +13,15 @@ import com.futurist_labs.android.base_library.utils.EtValidator; import com.futurist_labs.android.base_library.utils.RippleUtils; import com.futurist_labs.android.base_library.utils.SystemUtils; +import com.futurist_labs.android.base_library.utils.date_time.BaseDateTimePickerController; +import com.futurist_labs.android.base_library.utils.date_time.MaterialDateTimePickerController; import com.futurist_labs.android.base_library.views.font_views.FontTextInputEditTextView; import com.futurist_labs.android.base_library.views.font_views.FontTextInputLayout; import com.futurist_labs.android.base_library.views.font_views.FontTextView; +import java.util.Calendar; +import java.util.GregorianCalendar; + public class LoginFragment extends BaseFragmentWithRepository { @@ -24,7 +30,7 @@ public class LoginFragment extends BaseFragmentWithRepository { private FontTextInputEditTextView etUsername; private FontTextInputLayout ilPass; private FontTextInputEditTextView etPass; - private FontTextView tvLogin; + private FontTextView tvLogin, tvTestButton; public LoginFragment() { // Required empty public constructor @@ -75,6 +81,28 @@ public void onClick(View view) { } }); + tvTestButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Calendar min = new GregorianCalendar(); + min.setTimeInMillis(System.currentTimeMillis()); + min.add(Calendar.MINUTE, 60); + new MaterialDateTimePickerController(getActivity(), + new BaseDateTimePickerController.Callback() { + @Override + public void onDateTimeSelected(int year, int month, int day, int hour, int minutes) { + Toast.makeText(getContext(), "year : "+year, Toast.LENGTH_SHORT).show(); + } + }) + .setMinDate(min.getTimeInMillis()) + .setMinHour(9) + .setMinMinute(0) + .setMaxHour(20) + .setMaxMinute(0) + .setIs24(true) + .showDateTimePicker(); + } + }); RippleUtils.setRippleEffectSquare(tvLogin); } @@ -84,6 +112,7 @@ private void initView(View view) { ilPass = view.findViewById(R.id.ilPass); etPass = view.findViewById(R.id.etPass); tvLogin = view.findViewById(R.id.tvLogin); + tvTestButton = view.findViewById(R.id.tvTestButton); SystemUtils.setStrikeLineTextView(tvLogin); } diff --git a/app/src/main/res/layout/fragment_login.xml b/app/src/main/res/layout/fragment_login.xml index 379509a..20325d5 100644 --- a/app/src/main/res/layout/fragment_login.xml +++ b/app/src/main/res/layout/fragment_login.xml @@ -47,6 +47,7 @@ app:TvType="bold_italic"/> = 0) { + this.minHour = minHour; + } + return this; + } + + /** + * This option is available only for MaterialDateTimePickerController, for NativeDateTimePickerController is + * ignored and only MinDate and MaxDate works. + * min max hours,minutes and seconds are with priority over min max Date, for setting min/max hours,minutes and + * seconds + */ + public BaseDateTimePickerController setMinMinute(int minMinute) { + if (minMinute >= 0) { + this.minMinute = minMinute; + } + return this; + } + + /** + * This option is available only for MaterialDateTimePickerController, for NativeDateTimePickerController is + * ignored and only MinDate and MaxDate works. + * min max hours,minutes and seconds are with priority over min max Date, for setting min/max hours,minutes and + * seconds + */ + public BaseDateTimePickerController setMinSeconds(int minSeconds) { + if (minSeconds >= 0) { + this.minSeconds = minSeconds; + } + return this; + } + + /** + * 24-hour clock + * This option is available only for MaterialDateTimePickerController, for NativeDateTimePickerController is + * ignored and only MinDate and MaxDate works. + * min max hours,minutes and seconds are with priority over min max Date, for setting min/max hours,minutes and + * seconds + */ + public BaseDateTimePickerController setMaxHour(int maxHour) { + if (maxHour >= 0) { + this.maxHour = maxHour; + } + return this; + } + + /** + * This option is available only for MaterialDateTimePickerController, for NativeDateTimePickerController is + * ignored and only MinDate and MaxDate works. + * min max hours,minutes and seconds are with priority over min max Date, for setting min/max hours,minutes and + * seconds + */ + public BaseDateTimePickerController setMaxMinute(int maxMinute) { + if (maxMinute >= 0) { + this.maxMinute = maxMinute; + } + return this; + } + + /** + * This option is available only for MaterialDateTimePickerController, for NativeDateTimePickerController is + * ignored and only MinDate and MaxDate works. + * min max hours,minutes and seconds are with priority over min max Date, for setting min/max hours,minutes and + * seconds + */ + public BaseDateTimePickerController setMaxSeconds(int maxSeconds) { + if (maxSeconds >= 0) { + this.maxSeconds = maxSeconds; + } + return this; + } + //work around for pre-Lollipop, select date is called twice on dismiss boolean isDateSet = false, isTimeSet = false; @@ -189,11 +277,6 @@ void onSetTime(int hourOfDay, int minute) { } } - abstract void datePicker() ; - - - abstract void timePicker() ; - public interface Callback { /** * @param year diff --git a/core_library/src/main/java/com/futurist_labs/android/base_library/utils/date_time/MaterialDateTimePickerController.java b/core_library/src/main/java/com/futurist_labs/android/base_library/utils/date_time/MaterialDateTimePickerController.java index 8dae04d..6675160 100644 --- a/core_library/src/main/java/com/futurist_labs/android/base_library/utils/date_time/MaterialDateTimePickerController.java +++ b/core_library/src/main/java/com/futurist_labs/android/base_library/utils/date_time/MaterialDateTimePickerController.java @@ -72,23 +72,37 @@ public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int seco } else if (titleTime != EMPTY) { timePickerDialog.setTitle(activity.getString(titleTime)); } + Calendar minCal = null; if (minDate > 0) { - Calendar min = new GregorianCalendar(); - min.setTimeInMillis(minDate); - if (min.get(Calendar.YEAR) == year && min.get(Calendar.MONTH) == month - && min.get(Calendar.DAY_OF_MONTH) == day) { -// we have selected minimum day - timePickerDialog.setMinTime(min.get(Calendar.HOUR_OF_DAY), min.get(Calendar.MINUTE), 0); + minCal = new GregorianCalendar(); + minCal.setTimeInMillis(minDate); + } + if (minHour >= 0 && minMinute >= 0) { + if (isToday(minCal)) { + timePickerDialog.setMinTime(minCal.get(Calendar.HOUR_OF_DAY) > minHour ? minCal.get(Calendar.HOUR_OF_DAY) : minHour, + minCal.get(Calendar.MINUTE) > minMinute ? minCal.get(Calendar.MINUTE) : minMinute, + minSeconds < 0 ? 0 : minSeconds); + } else { + timePickerDialog.setMinTime(minHour, minMinute, minSeconds < 0 ? 0 : minSeconds); } + } else if (isToday(minCal)) { +// we have selected minimum day + timePickerDialog.setMinTime(minCal.get(Calendar.HOUR_OF_DAY), minCal.get(Calendar.MINUTE), 0); } - if (maxDate > 0) { - Calendar max = new GregorianCalendar(); - max.setTimeInMillis(maxDate); - if (max.get(Calendar.YEAR) == year && max.get(Calendar.MONTH) == month - && max.get(Calendar.DAY_OF_MONTH) == day) { - timePickerDialog.setMaxTime(max.get(Calendar.HOUR_OF_DAY), max.get(Calendar.MINUTE), 0); + Calendar maxCal = new GregorianCalendar(); + maxCal.setTimeInMillis(maxDate); + if (maxHour >= 0 && maxMinute >= 0) { + if (isToday(maxCal)) { + timePickerDialog.setMaxTime(maxCal.get(Calendar.HOUR_OF_DAY) > maxHour ? maxHour : + maxCal.get(Calendar.HOUR_OF_DAY), + maxCal.get(Calendar.MINUTE) > maxMinute ? maxMinute : maxCal.get(Calendar.MINUTE), + maxSeconds < 0 ? 0 : maxSeconds); + } else { + timePickerDialog.setMaxTime(maxHour, maxMinute, maxSeconds < 0 ? 0 : maxSeconds); } + } else if (isToday(maxCal)) { + timePickerDialog.setMaxTime(maxCal.get(Calendar.HOUR_OF_DAY), maxCal.get(Calendar.MINUTE), 0); } timePickerDialog.setVersion(TimePickerDialog.Version.VERSION_1); @@ -97,4 +111,9 @@ public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int seco } timePickerDialog.show(((FragmentActivity) activity).getSupportFragmentManager(), "timePickerDialog"); } + + private boolean isToday(Calendar date) { + return date != null && date.get(Calendar.YEAR) == year && date.get(Calendar.MONTH) == month + && date.get(Calendar.DAY_OF_MONTH) == day; + } }