Skip to content

Commit

Permalink
ability to add min/max hours, minutes and seconds for MaterialDateTim…
Browse files Browse the repository at this point in the history
…ePicker
  • Loading branch information
2math committed May 16, 2019
1 parent 48d5622 commit c47a590
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
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;
import com.futurist_labs.android.base_library.ui.BaseFragmentWithRepository;
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<Repository> {


Expand All @@ -24,7 +30,7 @@ public class LoginFragment extends BaseFragmentWithRepository<Repository> {
private FontTextInputEditTextView etUsername;
private FontTextInputLayout ilPass;
private FontTextInputEditTextView etPass;
private FontTextView tvLogin;
private FontTextView tvLogin, tvTestButton;

public LoginFragment() {
// Required empty public constructor
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
app:TvType="bold_italic"/>

<com.futurist_labs.android.base_library.views.font_views.FontTextView
android:id="@+id/tvTestButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ public abstract class BaseDateTimePickerController {
int styleDate = 0, styleTime = 0;//0 is default style
TextView titleDateView, titleTimeView;
long minDate, maxDate;
int minHour = -1, minMinute = -1, minSeconds = -1, maxHour = -1, maxMinute = -1, maxSeconds = -1;

public BaseDateTimePickerController(Activity activity, Callback callback) {
this.activity = activity;
this.callback = callback;
}

abstract void datePicker();


abstract void timePicker();

/**
* Show first date and then time picker with this date and time
*/
Expand Down Expand Up @@ -96,10 +102,12 @@ public void showTimePicker(int mHour, int mMinute) {
}

int accentColor;
public BaseDateTimePickerController setAccentColor(int color){

public BaseDateTimePickerController setAccentColor(int color) {
accentColor = color;
return this;
}

public BaseDateTimePickerController setIs24(boolean is24) {
this.is24 = is24;
return this;
Expand Down Expand Up @@ -130,6 +138,86 @@ public BaseDateTimePickerController setMaxDate(long maxDate) {
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 setMinHour(int minHour) {
if (minHour >= 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;

Expand Down Expand Up @@ -189,11 +277,6 @@ void onSetTime(int hourOfDay, int minute) {
}
}

abstract void datePicker() ;


abstract void timePicker() ;

public interface Callback {
/**
* @param year
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

0 comments on commit c47a590

Please sign in to comment.