|
1 | 1 | /**
|
2 | 2 | * Copyright (c) Facebook, Inc. and its affiliates.
|
3 |
| - * |
| 3 | + * <p> |
4 | 4 | * This source code is licensed under the MIT license found in the
|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | */
|
|
21 | 21 | import androidx.annotation.NonNull;
|
22 | 22 | import androidx.annotation.Nullable;
|
23 | 23 | import androidx.fragment.app.DialogFragment;
|
| 24 | + |
24 | 25 | import android.widget.DatePicker;
|
25 | 26 |
|
26 | 27 | import java.util.Calendar;
|
27 | 28 | import java.util.Locale;
|
28 | 29 |
|
29 | 30 | @SuppressLint("ValidFragment")
|
30 | 31 | 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; |
67 | 46 | }
|
68 | 47 |
|
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()); |
110 | 51 | }
|
111 |
| - } |
112 |
| - |
113 |
| - static DatePickerDialog createDialog( |
114 |
| - Bundle args, |
115 |
| - Context activityContext, |
116 |
| - @Nullable OnDateSetListener onDateSetListener) { |
117 |
| - |
118 |
| - final Calendar c = Calendar.getInstance(); |
119 | 52 |
|
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 | + } |
124 | 112 | }
|
125 | 113 |
|
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; |
151 | 155 | }
|
152 | 156 |
|
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 | + } |
161 | 163 | }
|
162 |
| - } |
163 | 164 |
|
164 |
| - /*package*/ void setOnDateSetListener(@Nullable OnDateSetListener onDateSetListener) { |
165 |
| - mOnDateSetListener = onDateSetListener; |
166 |
| - } |
| 165 | + /*package*/ void setOnDateSetListener(@Nullable OnDateSetListener onDateSetListener) { |
| 166 | + mOnDateSetListener = onDateSetListener; |
| 167 | + } |
167 | 168 |
|
168 |
| - /*package*/ void setOnDismissListener(@Nullable OnDismissListener onDismissListener) { |
169 |
| - mOnDismissListener = onDismissListener; |
170 |
| - } |
| 169 | + /*package*/ void setOnDismissListener(@Nullable OnDismissListener onDismissListener) { |
| 170 | + mOnDismissListener = onDismissListener; |
| 171 | + } |
171 | 172 |
|
172 |
| - /*package*/ void setOnNeutralButtonActionListener(@Nullable OnClickListener onNeutralButtonActionListener) { |
173 |
| - mOnNeutralButtonActionListener = onNeutralButtonActionListener; |
174 |
| - } |
| 173 | + /*package*/ void setOnNeutralButtonActionListener(@Nullable OnClickListener onNeutralButtonActionListener) { |
| 174 | + mOnNeutralButtonActionListener = onNeutralButtonActionListener; |
| 175 | + } |
175 | 176 | }
|
0 commit comments