forked from react-native-datetimepicker/datetimepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatePickerModule.java
189 lines (162 loc) · 6.9 KB
/
DatePickerModule.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 com.reactcommunity.rndatetimepicker;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.DatePicker;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import com.facebook.react.bridge.*;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.module.annotations.ReactModule;
import static com.reactcommunity.rndatetimepicker.Common.dismissDialog;
import java.util.Calendar;
/**
* {@link NativeModule} that allows JS to show a native date picker dialog and get called back when
* the user selects a date.
*/
@ReactModule(name = DatePickerModule.NAME)
public class DatePickerModule extends NativeModuleDatePickerSpec {
@VisibleForTesting
public static final String NAME = "RNDatePicker";
public DatePickerModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@NonNull
@Override
public String getName() {
return NAME;
}
private class DatePickerDialogListener implements OnDateSetListener, OnDismissListener, OnClickListener {
private final Promise mPromise;
private final Bundle mArgs;
private boolean mPromiseResolved = false;
public DatePickerDialogListener(final Promise promise, Bundle arguments) {
mPromise = promise;
mArgs = arguments;
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) {
final RNDate date = new RNDate(mArgs);
Calendar calendar = Calendar.getInstance(Common.getTimeZone(mArgs));
calendar.set(year, month, day, date.hour(), date.minute(), 0);
calendar.set(Calendar.MILLISECOND, 0);
WritableMap result = new WritableNativeMap();
result.putString("action", RNConstants.ACTION_DATE_SET);
result.putDouble("timestamp", calendar.getTimeInMillis());
result.putDouble("utcOffset", calendar.getTimeZone().getOffset(calendar.getTimeInMillis()) / 1000 / 60);
mPromise.resolve(result);
mPromiseResolved = true;
}
}
@Override
public void onDismiss(DialogInterface dialog) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) {
WritableMap result = new WritableNativeMap();
result.putString("action", RNConstants.ACTION_DISMISSED);
mPromise.resolve(result);
mPromiseResolved = true;
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) {
WritableMap result = new WritableNativeMap();
result.putString("action", RNConstants.ACTION_NEUTRAL_BUTTON);
mPromise.resolve(result);
mPromiseResolved = true;
}
}
}
@ReactMethod
public void dismiss(Promise promise) {
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
dismissDialog(activity, NAME, promise);
}
/**
* Show a date picker dialog.
*
* @param options a map containing options. Available keys are:
*
* <ul>
* <li>{@code date} (timestamp in milliseconds) the date to show by default</li>
* <li>
* {@code minimumDate} (timestamp in milliseconds) the minimum date the user should be allowed
* to select
* </li>
* <li>
* {@code maximumDate} (timestamp in milliseconds) the maximum date the user should be allowed
* to select
* </li>
* <li>
* {@code display} To set the date picker display to 'calendar/spinner/default'
* </li>
* <li>
* {@code testID} testID for testing with e.g. detox.
* </li>
* </ul>
*
* @param promise This will be invoked with parameters action, year,
* month (0-11), day, where action is {@code dateSetAction} or
* {@code dismissedAction}, depending on what the user did. If the action is
* dismiss, year, month and date are undefined.
*/
@ReactMethod
public void open(final ReadableMap options, final Promise promise) {
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
if (activity == null) {
promise.reject(
RNConstants.ERROR_NO_ACTIVITY,
"Tried to open a DatePicker dialog while not attached to an Activity");
return;
}
final FragmentManager fragmentManager = activity.getSupportFragmentManager();
UiThreadUtil.runOnUiThread(() -> {
RNDatePickerDialogFragment oldFragment =
(RNDatePickerDialogFragment) fragmentManager.findFragmentByTag(NAME);
Bundle arguments = createFragmentArguments(options);
if (oldFragment != null) {
oldFragment.update(arguments);
return;
}
RNDatePickerDialogFragment fragment = new RNDatePickerDialogFragment();
fragment.setArguments(arguments);
final DatePickerDialogListener listener = new DatePickerDialogListener(promise, arguments);
fragment.setOnDismissListener(listener);
fragment.setOnDateSetListener(listener);
fragment.setOnNeutralButtonActionListener(listener);
fragment.show(fragmentManager, NAME);
});
}
private Bundle createFragmentArguments(ReadableMap options) {
final Bundle args = Common.createFragmentArguments(options);
if (options.hasKey(RNConstants.ARG_MINDATE) && !options.isNull(RNConstants.ARG_MINDATE)) {
args.putLong(RNConstants.ARG_MINDATE, (long) options.getDouble(RNConstants.ARG_MINDATE));
}
if (options.hasKey(RNConstants.ARG_MAXDATE) && !options.isNull(RNConstants.ARG_MAXDATE)) {
args.putLong(RNConstants.ARG_MAXDATE, (long) options.getDouble(RNConstants.ARG_MAXDATE));
}
if (options.hasKey(RNConstants.ARG_DISPLAY) && !options.isNull(RNConstants.ARG_DISPLAY)) {
args.putString(RNConstants.ARG_DISPLAY, options.getString(RNConstants.ARG_DISPLAY));
}
if (options.hasKey(RNConstants.ARG_DIALOG_BUTTONS) && !options.isNull(RNConstants.ARG_DIALOG_BUTTONS)) {
args.putBundle(RNConstants.ARG_DIALOG_BUTTONS, Arguments.toBundle(options.getMap(RNConstants.ARG_DIALOG_BUTTONS)));
}
if (options.hasKey(RNConstants.ARG_TZOFFSET_MINS) && !options.isNull(RNConstants.ARG_TZOFFSET_MINS)) {
args.putLong(RNConstants.ARG_TZOFFSET_MINS, (long) options.getDouble(RNConstants.ARG_TZOFFSET_MINS));
}
if (options.hasKey(RNConstants.ARG_TESTID) && !options.isNull(RNConstants.ARG_TESTID)) {
args.putString(RNConstants.ARG_TESTID, options.getString(RNConstants.ARG_TESTID));
}
return args;
}
}