-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
RNDatePickerDialogFragment.java
189 lines (160 loc) · 6.55 KB
/
RNDatePickerDialogFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package versioned.host.exp.exponent.modules.api.components.datetimepicker;
import host.exp.expoview.R;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import android.widget.DatePicker;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
@SuppressLint("ValidFragment")
public class RNDatePickerDialogFragment extends DialogFragment {
private DatePickerDialog instance;
@Nullable
private OnDateSetListener mOnDateSetListener;
@Nullable
private OnDismissListener mOnDismissListener;
@Nullable
private static OnClickListener mOnNeutralButtonActionListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
instance = createDialog(args, getActivity(), mOnDateSetListener);
return instance;
}
public void update(Bundle args) {
final RNDate date = new RNDate(args);
instance.updateDate(date.year(), date.month(), date.day());
}
static @NonNull
DatePickerDialog getDialog(
Bundle args,
Context activityContext,
@Nullable OnDateSetListener onDateSetListener) {
final RNDate date = new RNDate(args);
final int year = date.year();
final int month = date.month();
final int day = date.day();
RNDatePickerDisplay display = RNDatePickerDisplay.DEFAULT;
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
display = RNDatePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
}
switch (display) {
case CALENDAR:
case SPINNER:
int theme = display == RNDatePickerDisplay.CALENDAR
? R.style.CalendarDatePickerDialog
: R.style.SpinnerDatePickerDialog;
return new RNDismissableDatePickerDialog(
activityContext,
theme,
onDateSetListener,
year,
month,
day,
display
);
default:
return new RNDismissableDatePickerDialog(
activityContext,
onDateSetListener,
year,
month,
day,
display
);
}
}
static DatePickerDialog createDialog(
Bundle args,
Context activityContext,
@Nullable OnDateSetListener onDateSetListener) {
final Calendar c = Calendar.getInstance();
DatePickerDialog dialog = getDialog(args, activityContext, onDateSetListener);
if (args != null && args.containsKey(RNConstants.ARG_NEUTRAL_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, args.getString(RNConstants.ARG_NEUTRAL_BUTTON_LABEL), mOnNeutralButtonActionListener);
}
if (args != null && args.containsKey(RNConstants.ARG_POSITIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_POSITIVE, args.getString(RNConstants.ARG_POSITIVE_BUTTON_LABEL), dialog);
}
if (args != null && args.containsKey(RNConstants.ARG_NEGATIVE_BUTTON_LABEL)) {
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, args.getString(RNConstants.ARG_NEGATIVE_BUTTON_LABEL), dialog);
}
final DatePicker datePicker = dialog.getDatePicker();
Integer timeZoneOffsetInMilliseconds = getTimeZoneOffset(args);
if (timeZoneOffsetInMilliseconds != null) {
c.setTimeZone(TimeZone.getTimeZone("GMT"));
}
if (args != null && args.containsKey(RNConstants.ARG_MINDATE)) {
// Set minDate to the beginning of the day. We need this because of clowniness in datepicker
// that causes it to throw an exception if minDate is greater than the internal timestamp
// that it generates from the y/m/d passed in the constructor.
c.setTimeInMillis(args.getLong(RNConstants.ARG_MINDATE));
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
datePicker.setMinDate(c.getTimeInMillis() - getOffset(c, timeZoneOffsetInMilliseconds));
} else {
// This is to work around a bug in DatePickerDialog where it doesn't display a title showing
// the date under certain conditions.
datePicker.setMinDate(RNConstants.DEFAULT_MIN_DATE);
}
if (args != null && args.containsKey(RNConstants.ARG_MAXDATE)) {
// Set maxDate to the end of the day, same reason as for minDate.
c.setTimeInMillis(args.getLong(RNConstants.ARG_MAXDATE));
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
datePicker.setMaxDate(c.getTimeInMillis() - getOffset(c, timeZoneOffsetInMilliseconds));
}
return dialog;
}
private static Integer getTimeZoneOffset(Bundle args) {
if (args != null && args.containsKey(RNConstants.ARG_TZOFFSET_MINS)) {
long timeZoneOffsetInMinutesFallback = args.getLong(RNConstants.ARG_TZOFFSET_MINS);
int timeZoneOffsetInMinutes = args.getInt(RNConstants.ARG_TZOFFSET_MINS, (int) timeZoneOffsetInMinutesFallback);
return timeZoneOffsetInMinutes * 60000;
}
return null;
}
private static int getOffset(Calendar c, Integer timeZoneOffsetInMilliseconds) {
if (timeZoneOffsetInMilliseconds != null) {
return TimeZone.getDefault().getOffset(c.getTimeInMillis()) - timeZoneOffsetInMilliseconds;
}
return 0;
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
if (mOnDismissListener != null) {
mOnDismissListener.onDismiss(dialog);
}
}
/*package*/ void setOnDateSetListener(@Nullable OnDateSetListener onDateSetListener) {
mOnDateSetListener = onDateSetListener;
}
/*package*/ void setOnDismissListener(@Nullable OnDismissListener onDismissListener) {
mOnDismissListener = onDismissListener;
}
/*package*/ void setOnNeutralButtonActionListener(@Nullable OnClickListener onNeutralButtonActionListener) {
mOnNeutralButtonActionListener = onNeutralButtonActionListener;
}
}