Skip to content

Commit cd4e24c

Browse files
committed
wait datepicker to be visible with timeout
1 parent 697c7e4 commit cd4e24c

12 files changed

+649
-633
lines changed
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
package com.reactcommunity.rndatetimepicker;
22

33
public final class RNConstants {
4-
public static final String ERROR_NO_ACTIVITY = "E_NO_ACTIVITY";
5-
public static final String ARG_VALUE = "value";
6-
public static final String ARG_MINDATE = "minimumDate";
7-
public static final String ARG_MAXDATE = "maximumDate";
8-
public static final String ARG_INTERVAL = "minuteInterval";
9-
public static final String ARG_IS24HOUR = "is24Hour";
10-
public static final String ARG_DISPLAY = "display";
11-
public static final String ARG_NEUTRAL_BUTTON_LABEL = "neutralButtonLabel";
12-
public static final String ACTION_DATE_SET = "dateSetAction";
13-
public static final String ACTION_TIME_SET = "timeSetAction";
14-
public static final String ACTION_DISMISSED = "dismissedAction";
15-
public static final String ACTION_NEUTRAL_BUTTON = "neutralButtonAction";
4+
public static final String ERROR_NO_ACTIVITY = "E_NO_ACTIVITY";
5+
public static final String ARG_VALUE = "value";
6+
public static final String ARG_MINDATE = "minimumDate";
7+
public static final String ARG_MAXDATE = "maximumDate";
8+
public static final String ARG_INTERVAL = "minuteInterval";
9+
public static final String ARG_IS24HOUR = "is24Hour";
10+
public static final String ARG_DISPLAY = "display";
11+
public static final String ARG_NEUTRAL_BUTTON_LABEL = "neutralButtonLabel";
12+
public static final String ACTION_DATE_SET = "dateSetAction";
13+
public static final String ACTION_TIME_SET = "timeSetAction";
14+
public static final String ACTION_DISMISSED = "dismissedAction";
15+
public static final String ACTION_NEUTRAL_BUTTON = "neutralButtonAction";
1616

17-
/**
18-
* Minimum date supported by {@link DatePicker}, 01 Jan 1900
19-
*/
20-
public static final long DEFAULT_MIN_DATE = -2208988800001l;
17+
/**
18+
* Minimum date supported by {@link DatePicker}, 01 Jan 1900
19+
*/
20+
public static final long DEFAULT_MIN_DATE = -2208988800001l;
2121

22-
/**
23-
* Minimum and default time picker minute interval
24-
*/
25-
public static final int DEFAULT_TIME_PICKER_INTERVAL = 1;
22+
/**
23+
* Minimum and default time picker minute interval
24+
*/
25+
public static final int DEFAULT_TIME_PICKER_INTERVAL = 1;
2626
}
Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
package com.reactcommunity.rndatetimepicker;
22

33
import java.util.Calendar;
4+
45
import android.os.Bundle;
56

67
public class RNDate {
7-
private Calendar now;
8+
private Calendar now;
9+
10+
public RNDate(Bundle args) {
11+
now = Calendar.getInstance();
12+
13+
if (args != null && args.containsKey(RNConstants.ARG_VALUE)) {
14+
set(args.getLong(RNConstants.ARG_VALUE));
15+
}
16+
}
817

9-
public RNDate(Bundle args) {
10-
now = Calendar.getInstance();
18+
public void set(long value) {
19+
now.setTimeInMillis(value);
20+
}
1121

12-
if (args != null && args.containsKey(RNConstants.ARG_VALUE)) {
13-
set(args.getLong(RNConstants.ARG_VALUE));
22+
public int year() {
23+
return now.get(Calendar.YEAR);
1424
}
15-
}
1625

17-
public void set(long value) {
18-
now.setTimeInMillis(value);
19-
}
26+
public int month() {
27+
return now.get(Calendar.MONTH);
28+
}
2029

21-
public int year() { return now.get(Calendar.YEAR); }
22-
public int month() { return now.get(Calendar.MONTH); }
23-
public int day() { return now.get(Calendar.DAY_OF_MONTH); }
24-
public int hour() { return now.get(Calendar.HOUR_OF_DAY); }
25-
public int minute() { return now.get(Calendar.MINUTE); }
30+
public int day() {
31+
return now.get(Calendar.DAY_OF_MONTH);
32+
}
33+
34+
public int hour() {
35+
return now.get(Calendar.HOUR_OF_DAY);
36+
}
37+
38+
public int minute() {
39+
return now.get(Calendar.MINUTE);
40+
}
2641
}
Lines changed: 134 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Copyright (c) Facebook, Inc. and its affiliates.
3-
*
3+
* <p>
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
@@ -21,155 +21,156 @@
2121
import androidx.annotation.NonNull;
2222
import androidx.annotation.Nullable;
2323
import androidx.fragment.app.DialogFragment;
24+
2425
import android.widget.DatePicker;
2526

2627
import java.util.Calendar;
2728
import java.util.Locale;
2829

2930
@SuppressLint("ValidFragment")
3031
public class RNDatePickerDialogFragment extends DialogFragment {
31-
private DatePickerDialog instance;
32-
33-
@Nullable
34-
private OnDateSetListener mOnDateSetListener;
35-
@Nullable
36-
private OnDismissListener mOnDismissListener;
37-
@Nullable
38-
private static OnClickListener mOnNeutralButtonActionListener;
39-
40-
@Override
41-
public Dialog onCreateDialog(Bundle savedInstanceState) {
42-
Bundle args = getArguments();
43-
instance = createDialog(args, getActivity(), mOnDateSetListener);
44-
return instance;
45-
}
46-
47-
public void update(Bundle args) {
48-
final RNDate date = new RNDate(args);
49-
instance.updateDate(date.year(), date.month(), date.day());
50-
}
51-
52-
static @NonNull
53-
DatePickerDialog getDialog(
54-
Bundle args,
55-
Context activityContext,
56-
@Nullable OnDateSetListener onDateSetListener) {
57-
58-
final RNDate date = new RNDate(args);
59-
final int year = date.year();
60-
final int month = date.month();
61-
final int day = date.day();
62-
63-
RNDatePickerDisplay display = RNDatePickerDisplay.DEFAULT;
64-
65-
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
66-
display = RNDatePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
32+
private DatePickerDialog instance;
33+
34+
@Nullable
35+
private OnDateSetListener mOnDateSetListener;
36+
@Nullable
37+
private OnDismissListener mOnDismissListener;
38+
@Nullable
39+
private static OnClickListener mOnNeutralButtonActionListener;
40+
41+
@Override
42+
public Dialog onCreateDialog(Bundle savedInstanceState) {
43+
Bundle args = getArguments();
44+
instance = createDialog(args, getActivity(), mOnDateSetListener);
45+
return instance;
6746
}
6847

69-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
70-
switch (display) {
71-
case CALENDAR:
72-
case SPINNER:
73-
String resourceName = display == RNDatePickerDisplay.CALENDAR
74-
? "CalendarDatePickerDialog"
75-
: "SpinnerDatePickerDialog";
76-
return new RNDismissableDatePickerDialog(
77-
activityContext,
78-
activityContext.getResources().getIdentifier(
79-
resourceName,
80-
"style",
81-
activityContext.getPackageName()),
82-
onDateSetListener,
83-
year,
84-
month,
85-
day,
86-
display
87-
);
88-
default:
89-
return new RNDismissableDatePickerDialog(
90-
activityContext,
91-
onDateSetListener,
92-
year,
93-
month,
94-
day,
95-
display
96-
);
97-
}
98-
} else {
99-
DatePickerDialog dialog = new RNDismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day, display);
100-
switch (display) {
101-
case CALENDAR:
102-
dialog.getDatePicker().setCalendarViewShown(true);
103-
dialog.getDatePicker().setSpinnersShown(false);
104-
break;
105-
case SPINNER:
106-
dialog.getDatePicker().setCalendarViewShown(false);
107-
break;
108-
}
109-
return dialog;
48+
public void update(Bundle args) {
49+
final RNDate date = new RNDate(args);
50+
instance.updateDate(date.year(), date.month(), date.day());
11051
}
111-
}
112-
113-
static DatePickerDialog createDialog(
114-
Bundle args,
115-
Context activityContext,
116-
@Nullable OnDateSetListener onDateSetListener) {
117-
118-
final Calendar c = Calendar.getInstance();
11952

120-
DatePickerDialog dialog = getDialog(args, activityContext, onDateSetListener);
121-
122-
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
123-
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
53+
static @NonNull
54+
DatePickerDialog getDialog(
55+
Bundle args,
56+
Context activityContext,
57+
@Nullable OnDateSetListener onDateSetListener) {
58+
59+
final RNDate date = new RNDate(args);
60+
final int year = date.year();
61+
final int month = date.month();
62+
final int day = date.day();
63+
64+
RNDatePickerDisplay display = RNDatePickerDisplay.DEFAULT;
65+
66+
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
67+
display = RNDatePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
68+
}
69+
70+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
71+
switch (display) {
72+
case CALENDAR:
73+
case SPINNER:
74+
String resourceName = display == RNDatePickerDisplay.CALENDAR
75+
? "CalendarDatePickerDialog"
76+
: "SpinnerDatePickerDialog";
77+
return new RNDismissableDatePickerDialog(
78+
activityContext,
79+
activityContext.getResources().getIdentifier(
80+
resourceName,
81+
"style",
82+
activityContext.getPackageName()),
83+
onDateSetListener,
84+
year,
85+
month,
86+
day,
87+
display
88+
);
89+
default:
90+
return new RNDismissableDatePickerDialog(
91+
activityContext,
92+
onDateSetListener,
93+
year,
94+
month,
95+
day,
96+
display
97+
);
98+
}
99+
} else {
100+
DatePickerDialog dialog = new RNDismissableDatePickerDialog(activityContext, onDateSetListener, year, month, day, display);
101+
switch (display) {
102+
case CALENDAR:
103+
dialog.getDatePicker().setCalendarViewShown(true);
104+
dialog.getDatePicker().setSpinnersShown(false);
105+
break;
106+
case SPINNER:
107+
dialog.getDatePicker().setCalendarViewShown(false);
108+
break;
109+
}
110+
return dialog;
111+
}
124112
}
125113

126-
final DatePicker datePicker = dialog.getDatePicker();
127-
128-
if (args != null && args.containsKey(RNConstants.ARG_MINDATE)) {
129-
// Set minDate to the beginning of the day. We need this because of clowniness in datepicker
130-
// that causes it to throw an exception if minDate is greater than the internal timestamp
131-
// that it generates from the y/m/d passed in the constructor.
132-
c.setTimeInMillis(args.getLong(RNConstants.ARG_MINDATE));
133-
c.set(Calendar.HOUR_OF_DAY, 0);
134-
c.set(Calendar.MINUTE, 0);
135-
c.set(Calendar.SECOND, 0);
136-
c.set(Calendar.MILLISECOND, 0);
137-
datePicker.setMinDate(c.getTimeInMillis());
138-
} else {
139-
// This is to work around a bug in DatePickerDialog where it doesn't display a title showing
140-
// the date under certain conditions.
141-
datePicker.setMinDate(RNConstants.DEFAULT_MIN_DATE);
142-
}
143-
if (args != null && args.containsKey(RNConstants.ARG_MAXDATE)) {
144-
// Set maxDate to the end of the day, same reason as for minDate.
145-
c.setTimeInMillis(args.getLong(RNConstants.ARG_MAXDATE));
146-
c.set(Calendar.HOUR_OF_DAY, 23);
147-
c.set(Calendar.MINUTE, 59);
148-
c.set(Calendar.SECOND, 59);
149-
c.set(Calendar.MILLISECOND, 999);
150-
datePicker.setMaxDate(c.getTimeInMillis());
114+
static DatePickerDialog createDialog(
115+
Bundle args,
116+
Context activityContext,
117+
@Nullable OnDateSetListener onDateSetListener) {
118+
119+
final Calendar c = Calendar.getInstance();
120+
121+
DatePickerDialog dialog = getDialog(args, activityContext, onDateSetListener);
122+
123+
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
124+
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
125+
}
126+
127+
final DatePicker datePicker = dialog.getDatePicker();
128+
129+
if (args != null && args.containsKey(RNConstants.ARG_MINDATE)) {
130+
// Set minDate to the beginning of the day. We need this because of clowniness in datepicker
131+
// that causes it to throw an exception if minDate is greater than the internal timestamp
132+
// that it generates from the y/m/d passed in the constructor.
133+
c.setTimeInMillis(args.getLong(RNConstants.ARG_MINDATE));
134+
c.set(Calendar.HOUR_OF_DAY, 0);
135+
c.set(Calendar.MINUTE, 0);
136+
c.set(Calendar.SECOND, 0);
137+
c.set(Calendar.MILLISECOND, 0);
138+
datePicker.setMinDate(c.getTimeInMillis());
139+
} else {
140+
// This is to work around a bug in DatePickerDialog where it doesn't display a title showing
141+
// the date under certain conditions.
142+
datePicker.setMinDate(RNConstants.DEFAULT_MIN_DATE);
143+
}
144+
if (args != null && args.containsKey(RNConstants.ARG_MAXDATE)) {
145+
// Set maxDate to the end of the day, same reason as for minDate.
146+
c.setTimeInMillis(args.getLong(RNConstants.ARG_MAXDATE));
147+
c.set(Calendar.HOUR_OF_DAY, 23);
148+
c.set(Calendar.MINUTE, 59);
149+
c.set(Calendar.SECOND, 59);
150+
c.set(Calendar.MILLISECOND, 999);
151+
datePicker.setMaxDate(c.getTimeInMillis());
152+
}
153+
154+
return dialog;
151155
}
152156

153-
return dialog;
154-
}
155-
156-
@Override
157-
public void onDismiss(DialogInterface dialog) {
158-
super.onDismiss(dialog);
159-
if (mOnDismissListener != null) {
160-
mOnDismissListener.onDismiss(dialog);
157+
@Override
158+
public void onDismiss(DialogInterface dialog) {
159+
super.onDismiss(dialog);
160+
if (mOnDismissListener != null) {
161+
mOnDismissListener.onDismiss(dialog);
162+
}
161163
}
162-
}
163164

164-
/*package*/ void setOnDateSetListener(@Nullable OnDateSetListener onDateSetListener) {
165-
mOnDateSetListener = onDateSetListener;
166-
}
165+
/*package*/ void setOnDateSetListener(@Nullable OnDateSetListener onDateSetListener) {
166+
mOnDateSetListener = onDateSetListener;
167+
}
167168

168-
/*package*/ void setOnDismissListener(@Nullable OnDismissListener onDismissListener) {
169-
mOnDismissListener = onDismissListener;
170-
}
169+
/*package*/ void setOnDismissListener(@Nullable OnDismissListener onDismissListener) {
170+
mOnDismissListener = onDismissListener;
171+
}
171172

172-
/*package*/ void setOnNeutralButtonActionListener(@Nullable OnClickListener onNeutralButtonActionListener) {
173-
mOnNeutralButtonActionListener = onNeutralButtonActionListener;
174-
}
173+
/*package*/ void setOnNeutralButtonActionListener(@Nullable OnClickListener onNeutralButtonActionListener) {
174+
mOnNeutralButtonActionListener = onNeutralButtonActionListener;
175+
}
175176
}

0 commit comments

Comments
 (0)