24
24
import androidx .fragment .app .DialogFragment ;
25
25
import android .widget .DatePicker ;
26
26
27
- class Date {
28
- private int year ;
29
- private int month ;
30
- private int day ;
31
-
32
- public Date (Bundle args ) {
33
- final Calendar c = Calendar .getInstance ();
34
-
35
- if (args != null && args .containsKey (RNDatePickerDialogModule .ARG_DATE )) {
36
- c .setTimeInMillis (args .getLong (RNDatePickerDialogModule .ARG_DATE ));
37
-
38
- this .year = c .get (Calendar .YEAR );
39
- this .month = c .get (Calendar .MONTH );
40
- this .day = c .get (Calendar .DAY_OF_MONTH );
41
- }
42
- }
43
-
44
- public int getYear () { return this .year ; }
45
- public int getMonth () { return this .month ; }
46
- public int getDay () { return this .day ; }
47
- }
48
-
49
27
@ SuppressLint ("ValidFragment" )
50
28
public class RNDatePickerDialogFragment extends DialogFragment {
51
29
private DatePickerDialog instance ;
52
30
53
- /**
54
- * Minimum date supported by {@link DatePicker}, 01 Jan 1900
55
- */
56
- private static final long DEFAULT_MIN_DATE = -2208988800001l ;
57
-
58
31
@ Nullable
59
32
private OnDateSetListener mOnDateSetListener ;
60
33
@ Nullable
@@ -67,43 +40,43 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
67
40
return instance ;
68
41
}
69
42
70
- public void updateDate (Bundle args ) {
71
- final Date date = new Date (args );
72
- instance .updateDate (date .getYear (), date .getMonth (), date .getDay ());
43
+ public void update (Bundle args ) {
44
+ final RNDate date = new RNDate (args );
45
+ instance .updateDate (date .year (), date .month (), date .day ());
73
46
}
74
47
75
48
static DatePickerDialog createDialog (Bundle args , Context activityContext , @ Nullable OnDateSetListener onDateSetListener ) {
76
- RNDatePickerMode mode = RNDatePickerMode .DEFAULT ;
77
-
78
49
final Calendar c = Calendar .getInstance ();
79
- final Date date = new Date (args );
80
-
81
- if (args != null && args .getString (RNDatePickerDialogModule .ARG_MODE , null ) != null ) {
82
- mode = RNDatePickerMode .valueOf (args .getString (RNDatePickerDialogModule .ARG_MODE ).toUpperCase (Locale .US ));
83
- }
50
+ final RNDate date = new RNDate (args );
51
+ final int year = date .year ();
52
+ final int month = date .month ();
53
+ final int day = date .day ();
84
54
55
+ RNDatePickerMode mode = RNDatePickerMode .DEFAULT ;
85
56
DatePickerDialog dialog = null ;
86
57
58
+ if (args != null && args .getString (RNConstants .ARG_MODE , null ) != null ) {
59
+ mode = RNDatePickerMode .valueOf (args .getString (RNConstants .ARG_MODE ).toUpperCase (Locale .US ));
60
+ }
61
+
87
62
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
88
63
switch (mode ) {
89
64
case CALENDAR :
90
65
dialog = new RNDismissableDatePickerDialog (activityContext ,
91
66
activityContext .getResources ().getIdentifier ("CalendarDatePickerDialog" , "style" , activityContext .getPackageName ()),
92
- onDateSetListener , date . getYear (), date . getMonth (), date . getDay () );
67
+ onDateSetListener , year , month , day );
93
68
break ;
94
69
case SPINNER :
95
70
dialog = new RNDismissableDatePickerDialog (activityContext ,
96
71
activityContext .getResources ().getIdentifier ("SpinnerDatePickerDialog" , "style" , activityContext .getPackageName ()),
97
- onDateSetListener , date . getYear (), date . getMonth (), date . getDay () );
72
+ onDateSetListener , year , month , day );
98
73
break ;
99
74
case DEFAULT :
100
- dialog = new RNDismissableDatePickerDialog (activityContext , onDateSetListener ,
101
- date .getYear (), date .getMonth (), date .getDay ());
75
+ dialog = new RNDismissableDatePickerDialog (activityContext , onDateSetListener , year , month , day );
102
76
break ;
103
77
}
104
78
} else {
105
- dialog = new RNDismissableDatePickerDialog (activityContext , onDateSetListener ,
106
- date .getYear (), date .getMonth (), date .getDay ());
79
+ dialog = new RNDismissableDatePickerDialog (activityContext , onDateSetListener , year , month , day );
107
80
108
81
switch (mode ) {
109
82
case CALENDAR :
@@ -118,11 +91,11 @@ static DatePickerDialog createDialog(Bundle args, Context activityContext, @Null
118
91
119
92
final DatePicker datePicker = dialog .getDatePicker ();
120
93
121
- if (args != null && args .containsKey (RNDatePickerDialogModule .ARG_MINDATE )) {
94
+ if (args != null && args .containsKey (RNConstants .ARG_MINDATE )) {
122
95
// Set minDate to the beginning of the day. We need this because of clowniness in datepicker
123
96
// that causes it to throw an exception if minDate is greater than the internal timestamp
124
97
// that it generates from the y/m/d passed in the constructor.
125
- c .setTimeInMillis (args .getLong (RNDatePickerDialogModule .ARG_MINDATE ));
98
+ c .setTimeInMillis (args .getLong (RNConstants .ARG_MINDATE ));
126
99
c .set (Calendar .HOUR_OF_DAY , 0 );
127
100
c .set (Calendar .MINUTE , 0 );
128
101
c .set (Calendar .SECOND , 0 );
@@ -131,11 +104,11 @@ static DatePickerDialog createDialog(Bundle args, Context activityContext, @Null
131
104
} else {
132
105
// This is to work around a bug in DatePickerDialog where it doesn't display a title showing
133
106
// the date under certain conditions.
134
- datePicker .setMinDate (DEFAULT_MIN_DATE );
107
+ datePicker .setMinDate (RNConstants . DEFAULT_MIN_DATE );
135
108
}
136
- if (args != null && args .containsKey (RNDatePickerDialogModule .ARG_MAXDATE )) {
109
+ if (args != null && args .containsKey (RNConstants .ARG_MAXDATE )) {
137
110
// Set maxDate to the end of the day, same reason as for minDate.
138
- c .setTimeInMillis (args .getLong (RNDatePickerDialogModule .ARG_MAXDATE ));
111
+ c .setTimeInMillis (args .getLong (RNConstants .ARG_MAXDATE ));
139
112
c .set (Calendar .HOUR_OF_DAY , 23 );
140
113
c .set (Calendar .MINUTE , 59 );
141
114
c .set (Calendar .SECOND , 59 );
0 commit comments