diff --git a/android/src/main/java/com/henninghall/date_picker/DatePickerModuleImpl.java b/android/src/main/java/com/henninghall/date_picker/DatePickerModuleImpl.java index c731e668..d874b57c 100644 --- a/android/src/main/java/com/henninghall/date_picker/DatePickerModuleImpl.java +++ b/android/src/main/java/com/henninghall/date_picker/DatePickerModuleImpl.java @@ -4,6 +4,7 @@ import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; +import android.graphics.Color; import android.view.View; import android.widget.LinearLayout; import android.widget.RelativeLayout; @@ -53,9 +54,10 @@ private AlertDialog createDialog( String title = props.getString("title"); String confirmText = props.getString("confirmText"); final String cancelText = props.getString("cancelText"); + String buttonColor = props.getString("buttonColor"); final View pickerWithMargin = withTopMargin(picker); - return new AlertDialog.Builder(DatePickerPackage.context.getCurrentActivity(), getTheme(props)) + AlertDialog dialog = new AlertDialog.Builder(DatePickerPackage.context.getCurrentActivity(), getTheme(props)) .setTitle(title) .setCancelable(true) .setView(pickerWithMargin) @@ -78,6 +80,20 @@ public void onCancel(DialogInterface dialogInterface) { } }) .create(); + + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialoga) { + if(buttonColor != null){ + int color = Color.parseColor(buttonColor); + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color); + dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color); + } + + } + }); + + return dialog; } private int getTheme(ReadableMap props) { diff --git a/android/src/newarch/java/com/henninghall/date_picker/DatePickerModule.java b/android/src/newarch/java/com/henninghall/date_picker/DatePickerModule.java index 408aa6ae..0fce9a78 100644 --- a/android/src/newarch/java/com/henninghall/date_picker/DatePickerModule.java +++ b/android/src/newarch/java/com/henninghall/date_picker/DatePickerModule.java @@ -1,34 +1,20 @@ package com.henninghall.date_picker; -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.graphics.Color; -import android.view.View; -import android.widget.LinearLayout; -import android.widget.RelativeLayout; - -import androidx.annotation.ColorInt; import androidx.annotation.NonNull; -import com.facebook.react.bridge.Callback; -import com.facebook.react.bridge.Dynamic; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableMap; -import com.facebook.react.bridge.ReadableMapKeySetIterator; -import com.henninghall.date_picker.ui.SpinnerState; -import com.henninghall.date_picker.ui.SpinnerStateListener; import net.time4j.android.ApplicationStarter; public class DatePickerModule extends NativeRNDatePickerSpec { - public static final String NAME = "RNDatePicker"; - private AlertDialog dialog; + private final DatePickerModuleImpl module; DatePickerModule(ReactApplicationContext context) { super(context); ApplicationStarter.initialize(context, false); // false = no need to prefetch on time data background tread + module = new DatePickerModuleImpl(context); } @Override @@ -43,134 +29,17 @@ public void removeListeners(double type) { @Override public void openPicker(ReadableMap props){ - PickerView picker = createPicker(props); - Callback onConfirm = new Callback() { - @Override - public void invoke(Object... objects) { - Emitter.onConfirm(picker.getDate(), picker.getPickerId()); - } - }; - - Callback onCancel = new Callback() { - @Override - public void invoke(Object... objects) { - Emitter.onCancel(picker.getPickerId()); - } - }; - - dialog = createDialog(props, picker, onConfirm, onCancel); - dialog.show(); + module.openPicker(props); } - @ReactMethod + @Override public void closePicker(){ - dialog.dismiss(); - } - - private AlertDialog createDialog( - ReadableMap props, final PickerView picker, final Callback onConfirm, final Callback onCancel) { - String title = props.getString("title"); - String confirmText = props.getString("confirmText"); - final String cancelText = props.getString("cancelText"); - String buttonColor = props.getString("buttonColor"); - final View pickerWithMargin = withTopMargin(picker); - - AlertDialog dialog = new AlertDialog.Builder(DatePickerPackage.context.getCurrentActivity(), getTheme(props)) - .setTitle(title) - .setCancelable(true) - .setView(pickerWithMargin) - .setPositiveButton(confirmText, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - onConfirm.invoke(picker.getDate()); - dialog.dismiss(); - } - }) - .setNegativeButton(cancelText, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - onCancel.invoke(); - dialog.dismiss(); - } - }) - .setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialogInterface) { - onCancel.invoke(); - } - }) - .create(); - - dialog.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialoga) { - if(buttonColor != null){ - int color = Color.parseColor(buttonColor); - dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color); - dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color); - } - - } - }); - return dialog; - } - - private int getTheme(ReadableMap props) { - int defaultTheme = 0; - String theme = props.getString("theme"); - if(theme == null) return defaultTheme; - switch (theme){ - case "light": return AlertDialog.THEME_DEVICE_DEFAULT_LIGHT; - case "dark": return AlertDialog.THEME_DEVICE_DEFAULT_DARK; - default: return defaultTheme; - } - } - - private PickerView createPicker(ReadableMap props){ - int height = 180; - LinearLayout.LayoutParams rootLayoutParams = new LinearLayout.LayoutParams( - RelativeLayout.LayoutParams.MATCH_PARENT, - Utils.toDp(height)); - PickerView picker = new PickerView(rootLayoutParams); - ReadableMapKeySetIterator iterator = props.keySetIterator(); - while(iterator.hasNextKey()){ - String key = iterator.nextKey(); - Dynamic value = props.getDynamic(key); - if(!key.equals("style")){ - try{ - picker.updateProp(key, value); - } catch (Exception e){ - // ignore invalid prop - } - } - } - picker.update(); - - picker.addSpinnerStateListener(new SpinnerStateListener() { - @Override - public void onChange(SpinnerState state) { - setEnabledConfirmButton(state == SpinnerState.idle); - } - }); - return picker; - } - - private void setEnabledConfirmButton(boolean enabled) { - dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(enabled); - } - - private View withTopMargin(PickerView view) { - LinearLayout linearLayout = new LinearLayout(DatePickerPackage.context); - linearLayout.setLayoutParams(new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.MATCH_PARENT, - LinearLayout.LayoutParams.WRAP_CONTENT - )); - linearLayout.addView(view); - linearLayout.setPadding(0, Utils.toDp(20),0,0); - return linearLayout; + module.closePicker(); } @Override @NonNull public String getName() { - return NAME; + return DatePickerModuleImpl.NAME; } } \ No newline at end of file diff --git a/src/fabric/NativeRNDatePicker.ts b/src/fabric/NativeRNDatePicker.ts index 2fd94317..1633d7a1 100644 --- a/src/fabric/NativeRNDatePicker.ts +++ b/src/fabric/NativeRNDatePicker.ts @@ -4,6 +4,7 @@ import { Double, UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes' export interface Spec extends TurboModule { readonly getConstants: () => {} + closePicker(): void openPicker(props: UnsafeObject): void removeListeners(type: Double): void addListener(eventName: string): void