-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHijriDatePickerAndroidModule.java
More file actions
322 lines (288 loc) · 14.1 KB
/
Copy pathHijriDatePickerAndroidModule.java
File metadata and controls
322 lines (288 loc) · 14.1 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package com.hijridatepicker;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.widget.DatePicker;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.DialogFragment;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.github.msarhan.ummalqura.calendar.UmmalquraCalendar;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.PatternSyntaxException;
public class HijriDatePickerAndroidModule extends ReactContextBaseJavaModule {
private static final String REACT_CLASS = "HijriDatePickerAndroid";
public static final String ERROR_NO_ACTIVITY = "E_NO_ACTIVITY";
public static final String ERROR_OPEN = "E_OPEN";
public static final String ERROR_PARSING_OPTIONS = "E_PARSING_OPTIONS";
private static final String ERROR_CONVERT = "E_CONVERT";
private static final String ACTION_DATE_SET = "dateSetAction";
private static final String ACTION_DISMISSED = "dismissedAction";
private static final String FRAGMENT_TAG = "DatePickerAndroid";
static final String ARG_DATE = "date";
static final String ARG_MINDATE = "minDate";
static final String ARG_MAXDATE = "maxDate";
static final String ARG_WEEK_DAY_LABELS = "weekDayLabels";
public static final String ARG_MODE = "mode";
public HijriDatePickerAndroidModule(final ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return REACT_CLASS;
}
@Nullable
@Override
public Map<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>();
constants.put(ACTION_DATE_SET, ACTION_DATE_SET);
constants.put(ACTION_DISMISSED, ACTION_DISMISSED);
constants.put(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY);
constants.put(ERROR_OPEN, ERROR_OPEN);
constants.put(ERROR_CONVERT, ERROR_CONVERT);
constants.put(ERROR_PARSING_OPTIONS, ERROR_PARSING_OPTIONS);
return constants;
}
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
* <p>
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
* Show a date picker dialog.
*
* @param options a map containing options. Available keys are:
* <p>
* <ul>
* <li>{@code date} (timestamp in milliseconds) the date to show by default</li>
* <li>
* {@code minDate} (timestamp in milliseconds) the minimum date the user should be allowed
* to select
* </li>
* <li>
* {@code maxDate} (timestamp in milliseconds) the maximum date the user should be allowed
* to select
* </li>
* <li>
* {@code mode} To set the date picker mode to ' no_arrows/default' ,
* currently there's only one mode for HijriDatePicker, so this field works only with mode no_arrows/default
* </li>
* <li>
* {@code weekDayLabels} (array of strings) the day labels that appears on the calendar
* </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(@Nullable final ReadableMap options, Promise promise) {
try {
Activity activity = getCurrentActivity();
if (activity == null) {
promise.reject(ERROR_NO_ACTIVITY, "Tried to open a DatePicker dialog while not attached to an Activity");
return;
}
// We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity
// (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
if (activity instanceof FragmentActivity) {
FragmentManager fragmentManager = ((FragmentActivity) activity).getSupportFragmentManager();
DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null) {
oldFragment.dismiss();
}
SupportHijriDatePickerDialogFragment fragment = new SupportHijriDatePickerDialogFragment();
if (options != null) {
final Bundle args = createFragmentArguments(options, promise);
if (args == null)
return;
fragment.setArguments(args);
}
final DatePickerDialogListener listener = new DatePickerDialogListener(promise);
fragment.setOnDismissListener(listener);
fragment.setOnDateSetListener(listener);
fragment.setOnExceptionListener(listener);
fragment.show(fragmentManager, FRAGMENT_TAG);
} else {
FragmentActivity fragmentActivity = (FragmentActivity) activity;
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
if (oldFragment != null) {
oldFragment.dismiss();
}
HijriDatePickerDialogFragment fragment = new HijriDatePickerDialogFragment();
if (options != null) {
final Bundle args = createFragmentArguments(options, promise);
if (args == null)
return;
fragment.setArguments(args);
}
final DatePickerDialogListener listener = new DatePickerDialogListener(promise);
fragment.setOnDismissListener(listener);
fragment.setOnDateSetListener(listener);
fragment.setOnExceptionListener(listener);
fragment.show(activity.getFragmentManager(), FRAGMENT_TAG);
}
} catch (Exception e) {
promise.reject(ERROR_OPEN, "Exception happened while executing open method, details: " + e.getMessage());
}
}
private Bundle createFragmentArguments(ReadableMap options, Promise promise) {
final Bundle args = new Bundle();
try {
if (options.hasKey(ARG_DATE) && !options.isNull(ARG_DATE)) {
if (!parseOptionsWithKey(ARG_DATE, options, args, promise))
return null;
}
if (options.hasKey(ARG_MINDATE) && !options.isNull(ARG_MINDATE)) {
if (!parseOptionsWithKey(ARG_MINDATE, options, args, promise))
return null;
}
if (options.hasKey(ARG_MAXDATE) && !options.isNull(ARG_MAXDATE)) {
if (!parseOptionsWithKey(ARG_MAXDATE, options, args, promise))
return null;
}
if (options.hasKey(ARG_MODE) && !options.isNull(ARG_MODE)) {
args.putString(ARG_MODE, options.getString(ARG_MODE));
}
if (options.hasKey(ARG_WEEK_DAY_LABELS) && !options.isNull(ARG_WEEK_DAY_LABELS)) {
args.putStringArrayList(ARG_WEEK_DAY_LABELS, toStringArrayList(options.getArray(ARG_WEEK_DAY_LABELS)));
}
} catch (Exception e) {
promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing options, details: " + e.getMessage());
return null;
}
return args;
}
private ArrayList<String> toStringArrayList(ReadableArray readableArray) {
ArrayList<String> stringList = new ArrayList<>();
for (int i = 0; i < readableArray.size(); i++) {
stringList.add(readableArray.getString(i));
}
return stringList;
}
private boolean parseOptionsWithKey(String ARG_KEY, ReadableMap options, Bundle args, Promise promise) {
ReadableType argDateType = options.getType(ARG_KEY);
if (ReadableType.String.equals(argDateType)) {
try {
long milliSeconds = 0;
milliSeconds = (long) convertHijriDateToGregorianMilliseconds(options.getString(ARG_KEY));
args.putLong(ARG_KEY, milliSeconds);
return true;
} catch (PatternSyntaxException | IndexOutOfBoundsException | NumberFormatException e) {
promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing " + ARG_KEY +
" we only accept object of Date or String with the format \"dd-MM-yyyy\" in Hijri");
return false;
}
} else if (ReadableType.Number.equals(argDateType)) {
args.putLong(ARG_KEY, (long) options.getDouble(ARG_KEY));
return true;
} else {
promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing " + ARG_KEY +
" we only accept object of Date or String with the format \"dd-MM-yyyy\" in Hijri");
return false;
}
}
/**
* @param hijriDate must be at the format dd-MM-yyyy
* @return milliseconds of Gregorian equivalent to the given hijriDate
*/
public double convertHijriDateToGregorianMilliseconds(String hijriDate) {
UmmalquraCalendar ummalquraCalendar = new UmmalquraCalendar();
int year = Integer.parseInt(hijriDate.split("-")[2]);
int monthOfYear = Integer.parseInt(hijriDate.split("-")[1]);
int dayOfMonth = Integer.parseInt(hijriDate.split("-")[0]);
ummalquraCalendar.set(UmmalquraCalendar.YEAR, year);
ummalquraCalendar.set(UmmalquraCalendar.MONTH, monthOfYear - 1);
ummalquraCalendar.set(UmmalquraCalendar.DAY_OF_MONTH, dayOfMonth);
return ummalquraCalendar.getTime().getTime();
}
@ReactMethod
public void convertHijriDateStrToGregorianMilliseconds(String hijriDate, Promise promise) {
try {
double milliseconds = convertHijriDateToGregorianMilliseconds(hijriDate);
promise.resolve(milliseconds);
} catch (Exception e) {
promise.reject(ERROR_CONVERT, "Exception while executing convertHijriDateStrToGregorianMilliseconds, Details: " + e.getMessage());
}
}
/**
* @param milliseconds your gregorian date in milliseconds
* @return hijri date at the format of "dd-MM-yyyy"
*/
@ReactMethod
public void convertMillisecondsToHijriDate(double milliseconds, Promise promise) {
try {
UmmalquraCalendar ummalquraCalendar = new UmmalquraCalendar();
ummalquraCalendar.setTimeInMillis((long) milliseconds);
WritableMap result = new WritableNativeMap();
result.putInt("year", ummalquraCalendar.get(Calendar.YEAR));
result.putInt("month", ummalquraCalendar.get(Calendar.MONTH));
result.putInt("day", ummalquraCalendar.get(Calendar.DAY_OF_MONTH));
promise.resolve(result);
} catch (Exception e) {
promise.reject(ERROR_CONVERT, "Exception while executing convertMillisecondsToHijriDate, Details: " + e.getMessage());
}
}
public interface OnExceptionListener {
void onException(String code, String message);
}
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
* <p>
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
private class DatePickerDialogListener implements DatePickerDialog.OnDateSetListener, DialogInterface.OnDismissListener, OnExceptionListener {
private final Promise mPromise;
private boolean mPromiseResolved = false;
public DatePickerDialogListener(final Promise promise) {
mPromise = promise;
}
@Override
public void onDateSet(DatePicker ignored, int year, int month, int day) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveCatalystInstance()) {
WritableMap result = new WritableNativeMap();
result.putString("action", ACTION_DATE_SET);
result.putInt("year", year);
result.putInt("month", month);
result.putInt("day", day);
mPromise.resolve(result);
mPromiseResolved = true;
}
}
@Override
public void onDismiss(DialogInterface dialog) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveCatalystInstance()) {
WritableMap result = new WritableNativeMap();
result.putString("action", ACTION_DISMISSED);
mPromise.resolve(result);
mPromiseResolved = true;
}
}
@Override
public void onException(String code, String message) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveCatalystInstance()) {
mPromise.reject(code, message);
mPromiseResolved = true;
}
}
}
}