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

Button color #3713

Merged
merged 12 commits into from
Aug 9, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public static TopBarOptions parse(TypefaceLoader typefaceLoader, JSONObject json
options.elevation = FractionParser.parse(json, "elevation");
options.buttons = TopBarButtons.parse(typefaceLoader, json);

options.rightButtonColor = ColorParser.parse(json, "rightButtonColor");
options.leftButtonColor = ColorParser.parse(json, "leftButtonColor");

options.validate();
return options;
}
Expand All @@ -61,6 +64,18 @@ public static TopBarOptions parse(TypefaceLoader typefaceLoader, JSONObject json
public Fraction borderHeight = new NullFraction();
public Color borderColor = new NullColor();

// Deprecated
public Color rightButtonColor = new NullColor();
public Color leftButtonColor = new NullColor();
public Color rightButtonDisabledColor = new NullColor();
public Color leftButtonDisabledColor = new NullColor();

public TopBarOptions copy() {
TopBarOptions result = new TopBarOptions();
result.mergeWith(this);
return result;
}

void mergeWith(final TopBarOptions other) {
title.mergeWith(other.title);
subtitle.mergeWith(other.subtitle);
Expand All @@ -75,10 +90,16 @@ void mergeWith(final TopBarOptions other) {
if (other.borderHeight.hasValue()) borderHeight = other.borderHeight;
if (other.borderColor.hasValue()) borderColor = other.borderColor;
if (other.elevation.hasValue()) elevation = other.elevation;

if (other.rightButtonColor.hasValue()) rightButtonColor = other.rightButtonColor;
if (other.leftButtonColor.hasValue()) leftButtonColor = other.leftButtonColor;
if (other.rightButtonDisabledColor.hasValue()) rightButtonDisabledColor = other.rightButtonDisabledColor;
if (other.leftButtonDisabledColor.hasValue()) leftButtonDisabledColor = other.leftButtonDisabledColor;

validate();
}

void mergeWithDefault(TopBarOptions defaultOptions) {
public TopBarOptions mergeWithDefault(TopBarOptions defaultOptions) {
title.mergeWithDefault(defaultOptions.title);
subtitle.mergeWithDefault(defaultOptions.subtitle);
background.mergeWithDefault(defaultOptions.background);
Expand All @@ -92,7 +113,14 @@ void mergeWithDefault(TopBarOptions defaultOptions) {
if (!borderHeight.hasValue()) borderHeight = defaultOptions.borderHeight;
if (!borderColor.hasValue()) borderColor = defaultOptions.borderColor;
if (!elevation.hasValue()) elevation = defaultOptions.elevation;

if (!rightButtonColor.hasValue()) rightButtonColor = defaultOptions.rightButtonColor;
if (!leftButtonColor.hasValue()) leftButtonColor = defaultOptions.leftButtonColor;
if (!rightButtonDisabledColor.hasValue()) rightButtonDisabledColor = defaultOptions.rightButtonDisabledColor;
if (!leftButtonDisabledColor.hasValue()) leftButtonDisabledColor = defaultOptions.leftButtonDisabledColor;

validate();
return this;
}

public void validate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Button {
public Text testId = new NullText();
public Component component = new Component();

protected static Button parseJson(JSONObject json, TypefaceLoader typefaceManager) {
private static Button parseJson(JSONObject json, TypefaceLoader typefaceManager) {
Button button = new Button();
button.id = json.optString("id");
button.text = TextParser.parse(json, "text");
Expand Down Expand Up @@ -78,6 +78,12 @@ private static ArrayList<Button> parseJsonArray(JSONArray jsonArray, TypefaceLoa
return buttons;
}

public Button copy() {
Button button = new Button();
button.mergeWith(this);
return button;
}

public boolean hasComponent() {
return component.hasValue();
}
Expand Down Expand Up @@ -118,6 +124,7 @@ public void mergeWith(Button other) {
if (other.component.hasValue()) component = other.component;
if (other.showAsAction.hasValue()) showAsAction = other.showAsAction;
if (other.icon.hasValue()) icon = other.icon;
if (other.id != null) id = other.id;
}

public void mergeWithDefault(Button defaultOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

Expand All @@ -13,15 +14,20 @@
import com.reactnativenavigation.parse.TopBarOptions;
import com.reactnativenavigation.parse.TopTabOptions;
import com.reactnativenavigation.parse.TopTabsOptions;
import com.reactnativenavigation.parse.params.Button;
import com.reactnativenavigation.utils.UiUtils;
import com.reactnativenavigation.viewcontrollers.IReactView;
import com.reactnativenavigation.views.Component;
import com.reactnativenavigation.views.topbar.TopBar;

import java.util.ArrayList;
import java.util.List;

public class StackOptionsPresenter {
private static final int DEFAULT_TITLE_COLOR = Color.BLACK;
private static final int DEFAULT_SUBTITLE_COLOR = Color.GRAY;
private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
private static final double DEFAULT_ELEVATION = 4d;
private final double defaultTitleFontSize;
private final double defaultSubtitleFontSize;

Expand Down Expand Up @@ -59,12 +65,12 @@ public void applyLayoutParamsOptions(Options options, View view) {
}

public void applyChildOptions(Options options, Component child) {
Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
applyOrientation(withDefaultOptions.layout.orientation);
applyButtons(withDefaultOptions.topBar.buttons);
applyTopBarOptions(withDefaultOptions.topBar, withDefaultOptions.animations, child, options);
applyTopTabsOptions(withDefaultOptions.topTabs);
applyTopTabOptions(withDefaultOptions.topTabOptions);
Options withDefault = options.copy().withDefaultOptions(defaultOptions);
applyOrientation(withDefault.layout.orientation);
applyButtons(withDefault.topBar, withDefault.topBar.rightButtonColor, withDefault.topBar.leftButtonColor, withDefault.topBar.rightButtonDisabledColor, withDefault.topBar.leftButtonDisabledColor);
applyTopBarOptions(withDefault.topBar, withDefault.animations, child, options);
applyTopTabsOptions(withDefault.topTabs);
applyTopTabOptions(withDefault.topTabOptions);
}

public void applyOrientation(OrientationOptions options) {
Expand All @@ -74,7 +80,7 @@ public void applyOrientation(OrientationOptions options) {

private void applyTopBarOptions(TopBarOptions options, AnimationsOptions animationOptions, Component component, Options componentOptions) {
topBar.setHeight(options.height.get(LayoutParams.WRAP_CONTENT));
topBar.setElevation(options.elevation.get(4d));
topBar.setElevation(options.elevation.get(DEFAULT_ELEVATION));

topBar.setTitleHeight(options.title.height.get(LayoutParams.WRAP_CONTENT));
topBar.setTitle(options.title.text.get(""));
Expand Down Expand Up @@ -128,10 +134,12 @@ private void applyTopBarVisibility(TopBarOptions options, AnimationsOptions anim
}
}

private void applyButtons(TopBarButtons buttons) {
topBar.setLeftButtons(buttons.left);
topBar.setRightButtons(buttons.right);
if (buttons.back.visible.isTrue() && !buttons.hasLeftButtons()) topBar.setBackButton(buttons.back);
private void applyButtons(TopBarOptions options, com.reactnativenavigation.parse.params.Color rightButtonColor, com.reactnativenavigation.parse.params.Color leftButtonColor, com.reactnativenavigation.parse.params.Color rightButtonDisabledColor, com.reactnativenavigation.parse.params.Color leftButtonDisabledColor) {
List<Button> rightButtons = mergeButtonsWithColor(options.buttons.right, rightButtonColor, rightButtonDisabledColor);
List<Button> leftButtons = mergeButtonsWithColor(options.buttons.left, leftButtonColor, leftButtonDisabledColor);
topBar.setRightButtons(rightButtons);
topBar.setLeftButtons(leftButtons);
if (options.buttons.back.visible.isTrue() && !options.buttons.hasLeftButtons()) topBar.setBackButton(options.buttons.back);
}

private void applyTopTabsOptions(TopTabsOptions options) {
Expand All @@ -155,9 +163,15 @@ public void onChildWillAppear(Options appearing, Options disappearing) {
}
}

public void mergeChildOptions(Options options, Component child) {
public void mergeChildOptions(Options options, Options childOptions, Component child) {
TopBarOptions topBar = options.copy().mergeWith(childOptions).withDefaultOptions(defaultOptions).topBar;
mergeOrientation(options.layout.orientation);
mergeButtons(options.topBar.buttons);
mergeButtons(options.topBar.buttons,
topBar.rightButtonColor,
topBar.leftButtonColor,
topBar.rightButtonDisabledColor,
topBar.leftButtonDisabledColor
);
mergeTopBarOptions(options.topBar, options.animations, child);
mergeTopTabsOptions(options.topTabs);
mergeTopTabOptions(options.topTabOptions);
Expand All @@ -167,12 +181,29 @@ private void mergeOrientation(OrientationOptions orientationOptions) {
if (orientationOptions.hasValue()) applyOrientation(orientationOptions);
}

private void mergeButtons(TopBarButtons buttons) {
if (buttons.left != null) topBar.setLeftButtons(buttons.left);
if (buttons.right != null) topBar.setRightButtons(buttons.right);
private void mergeButtons(TopBarButtons buttons, com.reactnativenavigation.parse.params.Color rightButtonColor, com.reactnativenavigation.parse.params.Color leftButtonColor, com.reactnativenavigation.parse.params.Color rightButtonDisabledColor, com.reactnativenavigation.parse.params.Color leftButtonDisabledColor) {
List<Button> rightButtons = mergeButtonsWithColor(buttons.right, rightButtonColor, rightButtonDisabledColor);
List<Button> leftButtons = mergeButtonsWithColor(buttons.left, leftButtonColor, leftButtonDisabledColor);
if (buttons.right != null) topBar.setRightButtons(rightButtons);
if (buttons.left != null) topBar.setLeftButtons(leftButtons);
if (buttons.back.hasValue()) topBar.setBackButton(buttons.back);
}

@Nullable
private List<Button> mergeButtonsWithColor(List<Button> buttons, com.reactnativenavigation.parse.params.Color buttonColor, com.reactnativenavigation.parse.params.Color disabledColor) {
List<Button> result = null;
if (buttons != null) {
result = new ArrayList<>();
for (Button button : buttons) {
Button copy = button.copy();
if (!button.color.hasValue()) copy.color = buttonColor;
if (!button.disabledColor.hasValue()) copy.disabledColor = disabledColor;
result.add(copy);
}
}
return result;
}

private void mergeTopBarOptions(TopBarOptions options, AnimationsOptions animationsOptions, Component component) {
if (options.height.hasValue()) topBar.setHeight(options.height.get());
if (options.elevation.hasValue()) topBar.setElevation(options.elevation.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public interface OnClickListener {
private TopBarButtonController.OnClickListener onPressListener;
private Drawable icon;

public TopBarButtonController(Activity activity, NavigationIconResolver navigationIconResolver, ImageLoader imageLoader, ButtonOptionsPresenter optionsPresenter, Button button, ReactViewCreator viewCreator, OnClickListener onClickListener) {
public TopBarButtonController(Activity activity,
NavigationIconResolver navigationIconResolver,
ImageLoader imageLoader,
ButtonOptionsPresenter optionsPresenter,
Button button,
ReactViewCreator viewCreator,
OnClickListener onClickListener) {
super(activity, button.id, new Options());
this.navigationIconResolver = navigationIconResolver;
this.imageLoader = imageLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void applyChildOptions(Options options, Component child) {
@Override
public void mergeChildOptions(Options options, Component child) {
super.mergeChildOptions(options, child);
presenter.mergeChildOptions(options, child);
presenter.mergeChildOptions(options, resolveCurrentOptions(), child);
if (options.fabOptions.hasValue() && child instanceof ReactComponent) {
fabOptionsPresenter.mergeOptions(options.fabOptions, (ReactComponent) child, getView());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.reactnativenavigation.utils.CompatUtils;
import com.reactnativenavigation.utils.ImageLoader;
import com.reactnativenavigation.utils.UiUtils;
import com.reactnativenavigation.utils.ViewUtils;
import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
Expand All @@ -55,8 +54,6 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
private TopBarAnimator animator;
private TopTabs topTabs;
private FrameLayout root;
private LinearLayout content;
private StackLayout parentView;
private TopBarBackgroundViewController topBarBackgroundViewController;
private View border;
private ImageLoader imageLoader;
Expand All @@ -67,7 +64,6 @@ public TopBar(final Context context, ReactViewCreator buttonCreator, TitleBarRea
this.imageLoader = imageLoader;
collapsingBehavior = new TopBarCollapseBehavior(this);
this.topBarBackgroundViewController = topBarBackgroundViewController;
this.parentView = parentView;
topTabs = new TopTabs(getContext());
animator = new TopBarAnimator(this, parentView.getStackId());
createLayout(buttonCreator, titleBarReactViewCreator, onClickListener);
Expand All @@ -78,7 +74,7 @@ private void createLayout(ReactViewCreator buttonCreator, TitleBarReactViewCreat
titleBar = createTitleBar(getContext(), buttonCreator, titleBarReactViewCreator, onClickListener, imageLoader);
topTabs = createTopTabs();
border = createBorder();
content = createContentLayout();
LinearLayout content = createContentLayout();

root = new FrameLayout(getContext());
root.setId(CompatUtils.generateViewId());
Expand Down Expand Up @@ -187,7 +183,7 @@ public void setTitleComponent(Component component) {
public void setBackgroundComponent(Component component) {
if (component.hasValue()) {
topBarBackgroundViewController.setComponent(component);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, ViewUtils.getPreferredHeight(this));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
root.addView(topBarBackgroundViewController.getView(), 0, lp);
}
}
Expand Down
Loading