Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improved theming and colors #852

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Dynamic;
Expand Down Expand Up @@ -53,14 +56,13 @@ public void closePicker(){

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 String buttonColor = props.getString("buttonColor");
final View pickerWithMargin = withTopMargin(picker);

AlertDialog dialog = new AlertDialog.Builder(DatePickerPackage.context.getCurrentActivity(), getTheme(props))
.setTitle(title)
AlertDialog dialog = new AlertDialogBuilder(DatePickerPackage.context.getCurrentActivity(), getTheme(props))
.setColoredTitle(props)
.setCancelable(true)
.setView(pickerWithMargin)
.setPositiveButton(confirmText, new DialogInterface.OnClickListener() {
Expand Down Expand Up @@ -154,4 +156,31 @@ private View withTopMargin(PickerView view) {
return linearLayout;
}

static class AlertDialogBuilder extends AlertDialog.Builder {
public AlertDialogBuilder(Context context, int themeResId) {
super(context, themeResId);
}
public AlertDialogBuilder setColoredTitle(ReadableMap props){
String textColor = props.getString("textColor");
String title = props.getString("title");
if(textColor == null){
this.setTitle(title);
return this;
}
TextView coloredTitle = new TextView(DatePickerPackage.context.getCurrentActivity());
coloredTitle.setText(title);
TypedValue typedValue = new TypedValue();
Resources.Theme theme = DatePickerPackage.context.getCurrentActivity().getTheme();
theme.resolveAttribute(R.attr.dialogPreferredPadding, typedValue, true);
int paddingInPixels = TypedValue.complexToDimensionPixelSize(typedValue.data, DatePickerPackage.context.getResources().getDisplayMetrics());
coloredTitle.setPadding(paddingInPixels, paddingInPixels, paddingInPixels, 0);
coloredTitle.setTextSize(20F);
int color = Color.parseColor(textColor);
coloredTitle.setTextColor(color);
this.setCustomTitle(coloredTitle);
return this;
}
}


}
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const DatePickerWrapper = (props) => {
<DatePicker
{...props}
textColor={colorToHex(getTextColor(props))}
dividerColor={colorToHex(props.dividerColor)}
dividerColor={colorToHex(getDividerColor(props))}
theme={getTheme(props)}
title={getTitle(props)}
confirmText={props.confirmText ? props.confirmText : 'Confirm'}
Expand All @@ -43,10 +43,20 @@ const getTheme = (props) => {
return scheme
}

/** @param {Props} props **/
const getDividerColor = (props) => {
if (props.dividerColor) return props.dividerColor
const theme = getTheme(props)
if (theme === 'dark') return 'white'
if (theme === 'light') return 'black'
return undefined
}

/** @param {Props} props **/
const getTextColor = (props) => {
const darkTheme = getTheme(props) === 'dark'
if (darkTheme) return 'white'
const theme = getTheme(props)
if (theme === 'dark') return 'white'
if (theme === 'light') return 'black'
return undefined
}

Expand Down
Loading