|
18 | 18 | import androidx.fragment.app.FragmentManager;
|
19 | 19 |
|
20 | 20 | import com.facebook.react.bridge.Promise;
|
| 21 | +import com.facebook.react.bridge.ReadableMap; |
| 22 | +import com.facebook.react.util.RNLog; |
21 | 23 |
|
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Calendar; |
| 26 | +import java.util.HashSet; |
22 | 27 | import java.util.Locale;
|
| 28 | +import java.util.SimpleTimeZone; |
| 29 | +import java.util.TimeZone; |
23 | 30 |
|
24 | 31 | public class Common {
|
25 | 32 |
|
@@ -63,21 +70,18 @@ public static int getDefaultDialogButtonTextColor(@NonNull Context activity) {
|
63 | 70 |
|
64 | 71 | @NonNull
|
65 | 72 | public static DialogInterface.OnShowListener setButtonTextColor(@NonNull final Context activityContext, final AlertDialog dialog, final Bundle args, final boolean needsColorOverride) {
|
66 |
| - return new DialogInterface.OnShowListener() { |
67 |
| - @Override |
68 |
| - public void onShow(DialogInterface dialogInterface) { |
69 |
| - // change text color only if custom color is set or if spinner mode is set |
70 |
| - // because spinner suffers from https://github.com/react-native-datetimepicker/datetimepicker/issues/543 |
71 |
| - |
72 |
| - Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); |
73 |
| - Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE); |
74 |
| - Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); |
75 |
| - |
76 |
| - int textColorPrimary = getDefaultDialogButtonTextColor(activityContext); |
77 |
| - setTextColor(positiveButton, POSITIVE, args, needsColorOverride, textColorPrimary); |
78 |
| - setTextColor(negativeButton, NEGATIVE, args, needsColorOverride, textColorPrimary); |
79 |
| - setTextColor(neutralButton, NEUTRAL, args, needsColorOverride, textColorPrimary); |
80 |
| - } |
| 73 | + return dialogInterface -> { |
| 74 | + // change text color only if custom color is set or if spinner mode is set |
| 75 | + // because spinner suffers from https://github.com/react-native-datetimepicker/datetimepicker/issues/543 |
| 76 | + |
| 77 | + Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); |
| 78 | + Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE); |
| 79 | + Button neutralButton = dialog.getButton(AlertDialog.BUTTON_NEUTRAL); |
| 80 | + |
| 81 | + int textColorPrimary = getDefaultDialogButtonTextColor(activityContext); |
| 82 | + setTextColor(positiveButton, POSITIVE, args, needsColorOverride, textColorPrimary); |
| 83 | + setTextColor(negativeButton, NEGATIVE, args, needsColorOverride, textColorPrimary); |
| 84 | + setTextColor(neutralButton, NEUTRAL, args, needsColorOverride, textColorPrimary); |
81 | 85 | };
|
82 | 86 | }
|
83 | 87 |
|
@@ -139,4 +143,63 @@ private static void setButtonLabel(Bundle buttonConfig, AlertDialog dialog, int
|
139 | 143 | }
|
140 | 144 | dialog.setButton(whichButton, buttonConfig.getString(LABEL), listener);
|
141 | 145 | }
|
| 146 | + |
| 147 | + public static TimeZone getTimeZone(Bundle args) { |
| 148 | + if (args != null && args.containsKey(RNConstants.ARG_TZOFFSET_MINS)) { |
| 149 | + return new SimpleTimeZone((int)args.getLong(RNConstants.ARG_TZOFFSET_MINS) * 60 * 1000, "GMT"); |
| 150 | + } |
| 151 | + |
| 152 | + if (args != null && args.containsKey(RNConstants.ARG_TZ_NAME)) { |
| 153 | + String timeZoneName = args.getString(RNConstants.ARG_TZ_NAME); |
| 154 | + if ("GMT".equals(timeZoneName)) { |
| 155 | + return TimeZone.getTimeZone("GMT"); |
| 156 | + } else if (!"GMT".equals(TimeZone.getTimeZone(timeZoneName).getID())) { |
| 157 | + return TimeZone.getTimeZone(timeZoneName); |
| 158 | + } |
| 159 | + RNLog.w(null, "'" + timeZoneName + "' does not exist in TimeZone.getAvailableIDs() fallback to TimeZone.getDefault()=" + TimeZone.getDefault().getID()); |
| 160 | + } |
| 161 | + |
| 162 | + return TimeZone.getDefault(); |
| 163 | + } |
| 164 | + |
| 165 | + public static long maxDateWithTimeZone(Bundle args) { |
| 166 | + if (!args.containsKey(RNConstants.ARG_MAXDATE)) { |
| 167 | + return Long.MAX_VALUE; |
| 168 | + } |
| 169 | + |
| 170 | + Calendar maxDate = Calendar.getInstance(getTimeZone(args)); |
| 171 | + maxDate.setTimeInMillis(args.getLong(RNConstants.ARG_MAXDATE)); |
| 172 | + maxDate.set(Calendar.HOUR_OF_DAY, 23); |
| 173 | + maxDate.set(Calendar.MINUTE, 59); |
| 174 | + maxDate.set(Calendar.SECOND, 59); |
| 175 | + maxDate.set(Calendar.MILLISECOND, 999); |
| 176 | + return maxDate.getTimeInMillis(); |
| 177 | + } |
| 178 | + |
| 179 | + public static long minDateWithTimeZone(Bundle args) { |
| 180 | + if (!args.containsKey(RNConstants.ARG_MINDATE)) { |
| 181 | + return 0; |
| 182 | + } |
| 183 | + |
| 184 | + Calendar minDate = Calendar.getInstance(getTimeZone(args)); |
| 185 | + minDate.setTimeInMillis(args.getLong(RNConstants.ARG_MINDATE)); |
| 186 | + minDate.set(Calendar.HOUR_OF_DAY, 0); |
| 187 | + minDate.set(Calendar.MINUTE, 0); |
| 188 | + minDate.set(Calendar.SECOND, 0); |
| 189 | + minDate.set(Calendar.MILLISECOND, 0); |
| 190 | + return minDate.getTimeInMillis(); |
| 191 | + } |
| 192 | + |
| 193 | + public static Bundle createFragmentArguments(ReadableMap options) { |
| 194 | + final Bundle args = new Bundle(); |
| 195 | + |
| 196 | + if (options.hasKey(RNConstants.ARG_VALUE) && !options.isNull(RNConstants.ARG_VALUE)) { |
| 197 | + args.putLong(RNConstants.ARG_VALUE, (long) options.getDouble(RNConstants.ARG_VALUE)); |
| 198 | + } |
| 199 | + if (options.hasKey(RNConstants.ARG_TZ_NAME) && !options.isNull(RNConstants.ARG_TZ_NAME)) { |
| 200 | + args.putString(RNConstants.ARG_TZ_NAME, options.getString(RNConstants.ARG_TZ_NAME)); |
| 201 | + } |
| 202 | + |
| 203 | + return args; |
| 204 | + } |
142 | 205 | }
|
0 commit comments