Skip to content

Commit

Permalink
Only set elevation values from Options
Browse files Browse the repository at this point in the history
This fixes a bug which occurred when returning from background - sometimes the system would set
an unexpected elevation value which seems like the default elevation.
This commit checks if the new elevation value was specified by RNN before setting it.
  • Loading branch information
guyca committed Jan 13, 2019
1 parent 135c6eb commit 487c1da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
private FrameLayout root;
private View border;
private View component;
private float elevation = -1;

public TopBar(final Context context, StackLayout parentView) {
super(context);
Expand Down Expand Up @@ -195,7 +196,7 @@ public void setTopTabsVisible(boolean visible) {

public void setTopTabsHeight(int height) {
if (topTabs.getLayoutParams().height == height) return;
topTabs.getLayoutParams().height = height > 0 ? (int) UiUtils.dpToPx(getContext(), height) : height;
topTabs.getLayoutParams().height = height > 0 ? UiUtils.dpToPx(getContext(), height) : height;
topTabs.setLayoutParams(topTabs.getLayoutParams());
}

Expand All @@ -212,12 +213,17 @@ public void setRightButtons(List<TitleBarButtonController> rightButtons) {
}

public void setElevation(Double elevation) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
getElevation() != elevation.floatValue()) {
setElevation(UiUtils.dpToPx(getContext(), elevation.floatValue()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && getElevation() != elevation.floatValue()) {
this.elevation = UiUtils.dpToPx(getContext(), elevation.floatValue());
setElevation(this.elevation);
}
}

@Override
public void setElevation(float elevation) {
if (elevation == this.elevation) super.setElevation(elevation);
}

public Toolbar getTitleBar() {
return titleBar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.anim.TopBarAnimator;
import com.reactnativenavigation.parse.AnimationOptions;
import com.reactnativenavigation.utils.UiUtils;
import com.reactnativenavigation.viewcontrollers.topbar.TopBarController;
import com.reactnativenavigation.views.topbar.TopBar;

import org.junit.Test;
import org.robolectric.annotation.Config;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -17,15 +19,17 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@Config(qualifiers = "xxhdpi")
public class TopBarTest extends BaseTest {

private TopBar uut;
private TopBarAnimator animator;
private Activity activity;

@SuppressWarnings("Convert2Lambda")
@Override
public void beforeEach() {
Activity activity = newActivity();
activity = newActivity();
StackLayout parent = new StackLayout(activity, new TopBarController(), null);
uut = new TopBar(activity, parent);
animator = spy(new TopBarAnimator(uut));
Expand Down Expand Up @@ -54,4 +58,14 @@ public void show_animate() {
uut.showAnimate(options);
verify(animator, times(1)).show(options);
}

@Test
public void setElevation_ignoreValuesNotSetByNavigation() {
float initialElevation = uut.getElevation();
uut.setElevation(1f);
assertThat(uut.getElevation()).isEqualTo(initialElevation);

uut.setElevation(Double.valueOf(2));
assertThat(uut.getElevation()).isEqualTo(UiUtils.dpToPx(activity, 2));
}
}

0 comments on commit 487c1da

Please sign in to comment.