Skip to content

Commit

Permalink
Fix changing disabled button color (#6387)
Browse files Browse the repository at this point in the history
When a button was disabled, changing its color once it became enabled did not work.
  • Loading branch information
guyca authored Jul 12, 2020
1 parent 1bba342 commit 1b291c6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import com.reactnativenavigation.utils.*
import com.reactnativenavigation.views.stack.topbar.titlebar.TitleBar

open class ButtonPresenter(private val button: ButtonOptions, private val iconResolver: IconResolver) {
companion object {
const val DISABLED_COLOR = Color.LTGRAY
}

val styledText: SpannableString
get() {
return SpannableString(button.text.get("")).apply {
Expand Down Expand Up @@ -83,8 +87,12 @@ open class ButtonPresenter(private val button: ButtonOptions, private val iconRe
}

private fun applyTextColor(view: View) {
if (view is TextView && button.color.hasValue()) {
view.setTextColor(button.color.get())
if (view is TextView) {
if (button.enabled.isTrueOrUndefined) {
if (button.color.hasValue()) view.setTextColor(button.color.get())
} else {
view.setTextColor(button.disabledColor.get(DISABLED_COLOR))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import android.text.style.MetricAffectingSpan
import com.reactnativenavigation.options.ButtonOptions

class ButtonSpan(private val button: ButtonOptions) : MetricAffectingSpan() {
companion object {
const val DISABLED_COLOR = Color.LTGRAY
}

override fun updateDrawState(drawState: TextPaint) = apply(drawState)

override fun updateMeasureState(paint: TextPaint) = apply(paint)
Expand All @@ -22,7 +18,6 @@ class ButtonSpan(private val button: ButtonOptions) : MetricAffectingSpan() {
if (fakeStyle and Typeface.BOLD != 0) paint.isFakeBoldText = true
if (fakeStyle and Typeface.ITALIC != 0) paint.textSkewX = -0.25f
if (fontSize.hasValue()) paint.textSize = fontSize.get().toFloat()
if (enabled.hasValue()) paint.color = disabledColor.get(DISABLED_COLOR)
paint.typeface = fontFamily
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@
import android.widget.TextView;

import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonPresenter;
import com.reactnativenavigation.fakes.IconResolverFake;
import com.reactnativenavigation.options.ButtonOptions;
import com.reactnativenavigation.options.params.Bool;
import com.reactnativenavigation.options.params.Colour;
import com.reactnativenavigation.options.params.Number;
import com.reactnativenavigation.options.params.Text;
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonController;
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonPresenter;
import com.reactnativenavigation.views.stack.topbar.titlebar.TitleBar;
import com.reactnativenavigation.views.stack.topbar.titlebar.TitleBarButtonCreator;

import org.junit.Test;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadows.ShadowLooper;

import java.util.List;

import androidx.appcompat.widget.ActionMenuView;

import static java.util.Objects.requireNonNull;
Expand All @@ -30,16 +29,19 @@

@LooperMode(LooperMode.Mode.PAUSED)
public class ButtonPresenterTest extends BaseTest {
private static final String BTN_TEXT = "button1";

private TitleBar titleBar;
private ButtonPresenter uut;
private ButtonController buttonController;
private ButtonOptions button;

@Override
public void beforeEach() {
Activity activity = newActivity();
titleBar = new TitleBar(activity);
activity.setContentView(titleBar);
ButtonOptions button = createButton();
button = createButton();

uut = new ButtonPresenter(button, new IconResolverFake(activity));
buttonController = new ButtonController(
Expand All @@ -51,25 +53,47 @@ public void beforeEach() {
);
}

@Test
public void applyOptions_buttonIsAddedToMenu() {
addButtonAndApplyOptions();

assertThat(findButtonView().getText().toString()).isEqualTo(BTN_TEXT);
}

@Test
public void applyOptions_appliesColorOnButtonTextView() {
button.color = new Colour(Color.RED);
addButtonAndApplyOptions();

assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.RED);
}

@Test
public void apply_disabledColor() {
button.enabled = new Bool(false);
addButtonAndApplyOptions();

assertThat(findButtonView().getCurrentTextColor()).isEqualTo(ButtonPresenter.DISABLED_COLOR);
}

private void addButtonAndApplyOptions() {
MenuItem menuItem = buttonController.createAndAddButtonToTitleBar(titleBar, 0);
uut.applyOptions(titleBar, menuItem, buttonController::getView);
}

private TextView findButtonView() {
ShadowLooper.idleMainLooper();
List<TextView> textualButtons = ViewUtils.findChildrenByClass(
return (TextView) ViewUtils.findChildrenByClass(
requireNonNull(ViewUtils.findChildByClass(titleBar, ActionMenuView.class)),
TextView.class,
child -> true
);
assertThat(textualButtons.get(0).getCurrentTextColor()).isEqualTo(Color.RED);
).get(0);
}

private ButtonOptions createButton() {
ButtonOptions b = new ButtonOptions();
b.id = "btn1";
b.text = new Text("button");
b.color = new Colour(Color.RED);
b.text = new Text(BTN_TEXT);
b.showAsAction = new Number(MenuItem.SHOW_AS_ACTION_ALWAYS);
return b;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import android.graphics.Paint;

import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonSpan;
import com.reactnativenavigation.options.params.Bool;
import com.reactnativenavigation.options.ButtonOptions;
import com.reactnativenavigation.options.params.Colour;
import com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonSpan;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import static com.reactnativenavigation.viewcontrollers.stack.topbar.button.ButtonSpan.DISABLED_COLOR;
import static org.assertj.core.api.Java6Assertions.assertThat;

public class ButtonSpanTest extends BaseTest {
Expand All @@ -33,16 +31,6 @@ public void apply_colorIsNotHandled() {
assertThat(paint.getColor()).isNotEqualTo(button.color.get());
}

@Test
public void apply_disabledColor() {
button.enabled = new Bool(false);

Paint paint = new Paint();
uut.apply(paint);

assertThat(paint.getColor()).isEqualTo(DISABLED_COLOR);
}

@NotNull
private ButtonOptions createButton() {
ButtonOptions button = new ButtonOptions();
Expand Down

0 comments on commit 1b291c6

Please sign in to comment.