-
-
Notifications
You must be signed in to change notification settings - Fork 400
/
RNDatePickerDialogFragment.java
168 lines (141 loc) · 5.68 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
/**
* 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 com.reactcommunity.rndatetimepicker;
import static com.reactcommunity.rndatetimepicker.Common.getDisplayDate;
import static com.reactcommunity.rndatetimepicker.Common.setButtonTextColor;
import static com.reactcommunity.rndatetimepicker.Common.setButtonTitles;
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;
@SuppressLint("ValidFragment")
public class RNDatePickerDialogFragment extends DialogFragment {
private DatePickerDialog instance;
@Nullable
private OnDateSetListener mOnDateSetListener;
@Nullable
private OnDismissListener mOnDismissListener;
@Nullable
private OnClickListener mOnNeutralButtonActionListener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = getArguments();
instance = createDialog(args);
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 = getDisplayDate(args);
if (args != null && args.getString(RNConstants.ARG_DISPLAY, null) != null) {
display = RNDatePickerDisplay.valueOf(args.getString(RNConstants.ARG_DISPLAY).toUpperCase(Locale.US));
}
if (display == RNDatePickerDisplay.SPINNER) {
return new RNDismissableDatePickerDialog(
activityContext,
R.style.SpinnerDatePickerDialog,
onDateSetListener,
year,
month,
day,
display
);
}
return new RNDismissableDatePickerDialog(
activityContext,
onDateSetListener,
year,
month,
day,
display
);
}
private DatePickerDialog createDialog(Bundle args) {
Context activityContext = getActivity();
DatePickerDialog dialog = getDialog(args, activityContext, mOnDateSetListener);
if (args != null) {
setButtonTitles(args, dialog, mOnNeutralButtonActionListener);
if (activityContext != null) {
RNDatePickerDisplay display = getDisplayDate(args);
boolean needsColorOverride = display == RNDatePickerDisplay.SPINNER;
dialog.setOnShowListener(setButtonTextColor(activityContext, dialog, args, needsColorOverride));
}
}
final DatePicker datePicker = dialog.getDatePicker();
final long minDate = Common.minDateWithTimeZone(args);
final long maxDate = Common.maxDateWithTimeZone(args);
if (args.containsKey(RNConstants.ARG_MINDATE)) {
datePicker.setMinDate(minDate);
} 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.containsKey(RNConstants.ARG_MAXDATE)) {
datePicker.setMaxDate(maxDate);
}
// Only compatible with SDK 21 and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && args.containsKey(RNConstants.FIRST_DAY_OF_WEEK)) {
final int firstDayOfWeek = args.getInt(RNConstants.FIRST_DAY_OF_WEEK);
datePicker.setFirstDayOfWeek(firstDayOfWeek);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && (args.containsKey(RNConstants.ARG_MAXDATE) || args.containsKey(RNConstants.ARG_MINDATE))) {
datePicker.setOnDateChangedListener((view, year, monthOfYear, dayOfMonth) -> {
Calendar calendar = Calendar.getInstance(Common.getTimeZone(args));
calendar.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
long timestamp = Math.min(Math.max(calendar.getTimeInMillis(), minDate), maxDate);
calendar.setTimeInMillis(timestamp);
if (datePicker.getYear() != calendar.get(Calendar.YEAR) || datePicker.getMonth() != calendar.get(Calendar.MONTH) || datePicker.getDayOfMonth() != calendar.get(Calendar.DAY_OF_MONTH)) {
datePicker.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
}
});
}
if (args.containsKey(RNConstants.ARG_TESTID)) {
datePicker.setTag(args.getString(RNConstants.ARG_TESTID));
}
return dialog;
}
@Override
public void onDismiss(@NonNull 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;
}
}