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
@@ -28,6 +35,7 @@ public class Common {
28
35
public static final String NEGATIVE = "negative" ;
29
36
public static final String LABEL = "label" ;
30
37
public static final String TEXT_COLOR = "textColor" ;
38
+ private static final HashSet TIMEZONE_IDS = new HashSet <>(Arrays .asList (TimeZone .getAvailableIDs ()));
31
39
32
40
public static void dismissDialog (FragmentActivity activity , String fragmentTag , Promise promise ) {
33
41
if (activity == null ) {
@@ -63,21 +71,18 @@ public static int getDefaultDialogButtonTextColor(@NonNull Context activity) {
63
71
64
72
@ NonNull
65
73
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
- }
74
+ return dialogInterface -> {
75
+ // change text color only if custom color is set or if spinner mode is set
76
+ // because spinner suffers from https://github.com/react-native-datetimepicker/datetimepicker/issues/543
77
+
78
+ Button positiveButton = dialog .getButton (AlertDialog .BUTTON_POSITIVE );
79
+ Button negativeButton = dialog .getButton (AlertDialog .BUTTON_NEGATIVE );
80
+ Button neutralButton = dialog .getButton (AlertDialog .BUTTON_NEUTRAL );
81
+
82
+ int textColorPrimary = getDefaultDialogButtonTextColor (activityContext );
83
+ setTextColor (positiveButton , POSITIVE , args , needsColorOverride , textColorPrimary );
84
+ setTextColor (negativeButton , NEGATIVE , args , needsColorOverride , textColorPrimary );
85
+ setTextColor (neutralButton , NEUTRAL , args , needsColorOverride , textColorPrimary );
81
86
};
82
87
}
83
88
@@ -139,4 +144,65 @@ private static void setButtonLabel(Bundle buttonConfig, AlertDialog dialog, int
139
144
}
140
145
dialog .setButton (whichButton , buttonConfig .getString (LABEL ), listener );
141
146
}
147
+
148
+ public static TimeZone getTimeZone (Bundle args ) {
149
+ if (args != null && args .containsKey (RNConstants .ARG_TZOFFSET_MINS )) {
150
+ return new SimpleTimeZone (args .getInt (RNConstants .ARG_TZOFFSET_MINS ) * 60 * 1000 , "GMT" );
151
+ }
152
+
153
+ if (args != null && args .containsKey (RNConstants .ARG_TZ_NAME )) {
154
+ String timeZoneName = args .getString (RNConstants .ARG_TZ_NAME );
155
+ if (TIMEZONE_IDS .contains (timeZoneName )) {
156
+ return TimeZone .getTimeZone (timeZoneName );
157
+ }
158
+ RNLog .w (null , "'" + timeZoneName + "' does not exist in TimeZone.getAvailableIDs() fallback to TimeZone.getDefault()=" + TimeZone .getDefault ().getID ());
159
+ }
160
+
161
+ return TimeZone .getDefault ();
162
+ }
163
+
164
+ public static long clamp (final long value , final long min , final long max ) {
165
+ return Math .min (Math .max (value , min ), max );
166
+ }
167
+
168
+ public static long maxDateWithTimeZone (Bundle args ) {
169
+ if (!args .containsKey (RNConstants .ARG_MAXDATE )) {
170
+ return Long .MAX_VALUE ;
171
+ }
172
+
173
+ Calendar maxDate = Calendar .getInstance (getTimeZone (args ));
174
+ maxDate .setTimeInMillis (args .getLong (RNConstants .ARG_MAXDATE ));
175
+ maxDate .set (Calendar .HOUR_OF_DAY , 23 );
176
+ maxDate .set (Calendar .MINUTE , 59 );
177
+ maxDate .set (Calendar .SECOND , 59 );
178
+ maxDate .set (Calendar .MILLISECOND , 999 );
179
+ return maxDate .getTimeInMillis ();
180
+ }
181
+
182
+ public static long minDateWithTimeZone (Bundle args ) {
183
+ if (!args .containsKey (RNConstants .ARG_MINDATE )) {
184
+ return 0 ;
185
+ }
186
+
187
+ Calendar minDate = Calendar .getInstance (getTimeZone (args ));
188
+ minDate .setTimeInMillis (args .getLong (RNConstants .ARG_MINDATE ));
189
+ minDate .set (Calendar .HOUR_OF_DAY , 0 );
190
+ minDate .set (Calendar .MINUTE , 0 );
191
+ minDate .set (Calendar .SECOND , 0 );
192
+ minDate .set (Calendar .MILLISECOND , 0 );
193
+ return minDate .getTimeInMillis ();
194
+ }
195
+
196
+ public static Bundle createFragmentArguments (ReadableMap options ) {
197
+ final Bundle args = new Bundle ();
198
+
199
+ if (options .hasKey (RNConstants .ARG_VALUE ) && !options .isNull (RNConstants .ARG_VALUE )) {
200
+ args .putLong (RNConstants .ARG_VALUE , (long ) options .getDouble (RNConstants .ARG_VALUE ));
201
+ }
202
+ if (options .hasKey (RNConstants .ARG_TZ_NAME ) && !options .isNull (RNConstants .ARG_TZ_NAME )) {
203
+ args .putString (RNConstants .ARG_TZ_NAME , options .getString (RNConstants .ARG_TZ_NAME ));
204
+ }
205
+
206
+ return args ;
207
+ }
142
208
}
0 commit comments