Skip to content

Commit

Permalink
DrawBehind when tabs are hidden (#5663)
Browse files Browse the repository at this point in the history
When BottomTabs are hidden, draw components behind the tabs even if drawBehind isn't set to true explicitly.
  • Loading branch information
guyca authored Nov 20, 2019
1 parent 8f08254 commit 002b7d8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ void mergeWithDefault(final BottomTabsOptions defaultOptions) {
if (!tabsAttachMode.hasValue()) tabsAttachMode = defaultOptions.tabsAttachMode;
}

public boolean isHiddenOrDrawBehind() {
return visible.isFalse() || drawBehind.isTrue();
}

public void clearOneTimeOptions() {
currentTabId = new NullText();
currentTabIndex = new NullNumber();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public void applyOptions(Options options) {
public void mergeOptions(Options options) {
if (options == Options.EMPTY) return;
if (isViewShown()) presenter.mergeOptions(getView(), options);
performOnParentController(parentController -> parentController.mergeChildOptions(options, this));
super.mergeOptions(options);
performOnParentController(parentController -> parentController.mergeChildOptions(options, this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public boolean onMeasureChild(CoordinatorLayout parent, ViewGroup child, int par

@Override
public int getBottomInset(ViewController child) {
int bottomTabsInset = resolveChildOptions(child).bottomTabsOptions.drawBehind.isTrue() ? 0 : bottomTabs.getHeight();
int bottomTabsInset = resolveChildOptions(child).bottomTabsOptions.isHiddenOrDrawBehind() ? 0 : bottomTabs.getHeight();
return bottomTabsInset + perform(getParentController(), 0, p -> p.getBottomInset(this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.facebook.react.ReactInstanceManager;
import com.reactnativenavigation.interfaces.ScrollEventListener;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.presentation.ComponentPresenterBase;
import com.reactnativenavigation.presentation.Presenter;
import com.reactnativenavigation.react.ReactView;
import com.reactnativenavigation.viewcontrollers.ChildController;
Expand All @@ -20,7 +21,7 @@
import static com.reactnativenavigation.utils.ObjectUtils.perform;

public class SimpleViewController extends ChildController<SimpleViewController.SimpleView> {

private ComponentPresenterBase presenter = new ComponentPresenterBase();

public SimpleViewController(Activity activity, ChildControllersRegistry childRegistry, String id, Options options) {
this(activity, childRegistry, id, new Presenter(activity, new Options()), options);
Expand Down Expand Up @@ -58,6 +59,11 @@ public int getTopInset() {
return statusBarInset + perform(getParentController(), 0, p -> p.getTopInset(this));
}

@Override
public void applyBottomInset() {
if (view != null) presenter.applyBottomInset(view, getBottomInset());
}

public static class SimpleView extends ReactView implements ReactComponent {

public SimpleView(@NonNull Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;

import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.TestUtils;
Expand All @@ -27,6 +28,7 @@
import com.reactnativenavigation.viewcontrollers.ViewController;
import com.reactnativenavigation.viewcontrollers.stack.StackController;
import com.reactnativenavigation.views.BottomTabs;
import com.reactnativenavigation.views.bottomtabs.BottomTabsLayout;

import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -84,7 +86,7 @@ public void superCreateItems() {
child1 = spy(new SimpleViewController(activity, childRegistry, "child1", tabOptions));
child2 = spy(new SimpleViewController(activity, childRegistry, "child2", tabOptions));
child3 = spy(new SimpleViewController(activity, childRegistry, "child3", tabOptions));
child4 = spy(createStack("someStack"));
child4 = spy(createStack());
child5 = spy(new SimpleViewController(activity, childRegistry, "child5", tabOptions));
when(child5.handleBack(any())).thenReturn(true);
tabs = createTabs();
Expand Down Expand Up @@ -208,17 +210,34 @@ public void mergeOptions_currentTabIndex() {

@Test
public void mergeOptions_drawBehind() {
assertThat(uut.getBottomInset()).isEqualTo(uut.getBottomTabs().getHeight());
assertThat(uut.getBottomInset(child1)).isEqualTo(uut.getBottomTabs().getHeight());

Options o1 = new Options();
o1.bottomTabsOptions.drawBehind = new Bool(true);
child1.mergeOptions(o1);
assertThat(uut.getBottomInset()).isEqualTo(0);
assertThat(uut.getBottomInset(child1)).isEqualTo(0);

Options o2 = new Options();
o2.topBar.title.text = new Text("Some text");
child1.mergeOptions(o1);
assertThat(uut.getBottomInset()).isEqualTo(0);
assertThat(uut.getBottomInset(child1)).isEqualTo(0);
}

@Test
public void mergeOptions_drawBehind_stack() {
uut.selectTab(3);

SimpleViewController stackChild = new SimpleViewController(activity, childRegistry, "stackChild", new Options());
disablePushAnimation(stackChild);
child4.push(stackChild, new CommandListenerAdapter());

assertThat(((MarginLayoutParams) stackChild.getView().getLayoutParams()).bottomMargin).isEqualTo(bottomTabs.getHeight());

Options o1 = new Options();
o1.bottomTabsOptions.drawBehind = new Bool(true);
stackChild.mergeOptions(o1);

assertThat(((MarginLayoutParams) stackChild.getView().getLayoutParams()).bottomMargin).isEqualTo(0);
}

@Test
Expand Down Expand Up @@ -369,9 +388,9 @@ private List<ViewController> createTabs() {
return Arrays.asList(child1, child2, child3, child4, child5);
}

private StackController createStack(String id) {
private StackController createStack() {
return TestUtils.newStackController(activity)
.setId(id)
.setId("someStack")
.setInitialOptions(tabOptions)
.build();
}
Expand Down Expand Up @@ -401,6 +420,14 @@ public void ensureViewIsCreated() {
uut.getView().layout(0, 0, 1000, 1000);
}

@NonNull
@Override
protected BottomTabsLayout createView() {
BottomTabsLayout view = super.createView();
bottomTabs.getLayoutParams().height = 100;
return view;
}

@NonNull
@Override
protected BottomTabs createBottomTabs() {
Expand Down

0 comments on commit 002b7d8

Please sign in to comment.