11package com .reactcommunity .rndatetimepicker ;
22
33import android .app .AlertDialog ;
4- import android .app .DatePickerDialog ;
54import android .content .Context ;
65import android .content .DialogInterface ;
76import android .content .res .Resources ;
7+ import android .graphics .Color ;
88import android .os .Bundle ;
99import android .util .TypedValue ;
1010import android .widget .Button ;
2323
2424public class Common {
2525
26+ public static final String POSITIVE = "positive" ;
27+ public static final String NEUTRAL = "neutral" ;
28+ public static final String NEGATIVE = "negative" ;
29+ public static final String LABEL = "label" ;
30+ public static final String TEXT_COLOR = "textColor" ;
31+
2632 public static void dismissDialog (FragmentActivity activity , String fragmentTag , Promise promise ) {
2733 if (activity == null ) {
2834 promise .reject (
@@ -56,29 +62,51 @@ public static int getDefaultDialogButtonTextColor(@NonNull Context activity) {
5662 }
5763
5864 @ NonNull
59- public static DialogInterface .OnShowListener setButtonTextColor (@ NonNull Context activityContext , final AlertDialog dialog ) {
65+ public static DialogInterface .OnShowListener setButtonTextColor (@ NonNull Context activityContext , final AlertDialog dialog , final Bundle args , final boolean needsColorOverride ) {
6066 return new DialogInterface .OnShowListener () {
6167 @ Override
6268 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+
6372 Button positiveButton = dialog .getButton (AlertDialog .BUTTON_POSITIVE );
6473 Button negativeButton = dialog .getButton (AlertDialog .BUTTON_NEGATIVE );
6574 Button neutralButton = dialog .getButton (AlertDialog .BUTTON_NEUTRAL );
6675
6776 int textColorPrimary = getDefaultDialogButtonTextColor (activityContext );
68-
69- if (positiveButton != null ) {
70- positiveButton .setTextColor (textColorPrimary );
71- }
72- if (negativeButton != null ) {
73- negativeButton .setTextColor (textColorPrimary );
74- }
75- if (neutralButton != null ) {
76- neutralButton .setTextColor (textColorPrimary );
77- }
77+ setTextColor (positiveButton , POSITIVE , args , needsColorOverride , textColorPrimary );
78+ setTextColor (negativeButton , NEGATIVE , args , needsColorOverride , textColorPrimary );
79+ setTextColor (neutralButton , NEUTRAL , args , needsColorOverride , textColorPrimary );
7880 }
7981 };
8082 }
8183
84+ private static void setTextColor (Button button , String buttonKey , final Bundle args , final boolean needsColorOverride , int textColorPrimary ) {
85+ if (button == null ) return ;
86+
87+ Integer color = getButtonColor (args , buttonKey );
88+ if (needsColorOverride || color != null ) {
89+ button .setTextColor (color != null ? color : textColorPrimary );
90+ }
91+ }
92+
93+ private static Integer getButtonColor (final Bundle args , String buttonKey ) {
94+ Bundle buttons = args .getBundle (RNConstants .ARG_DIALOG_BUTTONS );
95+ if (buttons == null ) {
96+ return null ;
97+ }
98+ Bundle buttonParams = buttons .getBundle (buttonKey );
99+ if (buttonParams == null ) {
100+ return null ;
101+ }
102+ // yes, this cast is safe. the color is passed as int from JS (RN.processColor)
103+ int color = (int ) buttonParams .getDouble (TEXT_COLOR , Color .TRANSPARENT );
104+ if (color == Color .TRANSPARENT ) {
105+ return null ;
106+ }
107+ return color ;
108+ }
109+
82110 public static RNTimePickerDisplay getDisplayTime (Bundle args ) {
83111 RNTimePickerDisplay display = RNTimePickerDisplay .DEFAULT ;
84112 if (args != null && args .getString (RNConstants .ARG_DISPLAY , null ) != null ) {
@@ -94,4 +122,21 @@ public static RNDatePickerDisplay getDisplayDate(Bundle args) {
94122 }
95123 return display ;
96124 }
125+
126+ public static void setButtonTitles (@ NonNull Bundle args , AlertDialog dialog , DialogInterface .OnClickListener onNeutralButtonActionListener ) {
127+ Bundle buttons = args .getBundle (RNConstants .ARG_DIALOG_BUTTONS );
128+ if (buttons == null ) {
129+ return ;
130+ }
131+ setButtonLabel (buttons .getBundle (NEUTRAL ), dialog , AlertDialog .BUTTON_NEUTRAL , onNeutralButtonActionListener );
132+ setButtonLabel (buttons .getBundle (POSITIVE ), dialog , AlertDialog .BUTTON_POSITIVE , (DialogInterface .OnClickListener ) dialog );
133+ setButtonLabel (buttons .getBundle (NEGATIVE ), dialog , AlertDialog .BUTTON_NEGATIVE , (DialogInterface .OnClickListener ) dialog );
134+ }
135+
136+ private static void setButtonLabel (Bundle buttonConfig , AlertDialog dialog , int whichButton , DialogInterface .OnClickListener listener ) {
137+ if (buttonConfig == null || buttonConfig .getString (LABEL ) == null ) {
138+ return ;
139+ }
140+ dialog .setButton (whichButton , buttonConfig .getString (LABEL ), listener );
141+ }
97142}
0 commit comments