Skip to content

Commit

Permalink
Fix rare race condition when applying bottomTabs.drawBehind (wix#5326)
Browse files Browse the repository at this point in the history
When setting BottomTabs root, if one of the children was a stack with multiple children
and the top child in the stack had `drawBehind: true` while the bottom child had `drawBehind: false`,
sometimes the tabs' bottomMargin would be wrong since it was applied in onPreDraw.
  • Loading branch information
guyca committed Jul 29, 2019
1 parent a58798d commit ecdb691
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
Expand Up @@ -10,7 +10,6 @@
import com.reactnativenavigation.parse.AnimationsOptions;
import com.reactnativenavigation.parse.BottomTabsOptions;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.utils.UiUtils;
import com.reactnativenavigation.viewcontrollers.ViewController;
import com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabFinder;
import com.reactnativenavigation.viewcontrollers.bottomtabs.TabSelector;
Expand All @@ -19,6 +18,8 @@

import java.util.List;

import static com.reactnativenavigation.utils.ViewUtils.getHeight;

public class BottomTabsPresenter {
private final BottomTabFinder bottomTabFinder;
private final List<ViewController> tabs;
Expand Down Expand Up @@ -111,13 +112,8 @@ private void applyDrawBehind(BottomTabsOptions options, @IntRange(from = 0) int
MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
if (options.drawBehind.isTrue()) {
lp.bottomMargin = 0;
}
if (options.visible.isTrueOrUndefined() && options.drawBehind.isFalseOrUndefined()) {
if (bottomTabs.getHeight() == 0) {
UiUtils.runOnPreDrawOnce(bottomTabs, () -> lp.bottomMargin = bottomTabs.getHeight());
} else {
lp.bottomMargin = bottomTabs.getHeight();
}
} else if (options.visible.isTrueOrUndefined()) {
lp.bottomMargin = getHeight(bottomTabs);
}
}

Expand All @@ -126,13 +122,8 @@ private void mergeDrawBehind(BottomTabsOptions options, int tabIndex) {
MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
if (options.drawBehind.isTrue()) {
lp.bottomMargin = 0;
}
if (options.visible.isTrue() && options.drawBehind.isFalse()) {
if (bottomTabs.getHeight() == 0) {
UiUtils.runOnPreDrawOnce(bottomTabs, () -> lp.bottomMargin = bottomTabs.getHeight());
} else {
lp.bottomMargin = bottomTabs.getHeight();
}
} else if (options.visible.isTrue() && options.drawBehind.isFalse()) {
lp.bottomMargin = getHeight(bottomTabs);
}
}

Expand Down
Expand Up @@ -27,10 +27,10 @@
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM;
import static com.reactnativenavigation.utils.CollectionUtils.forEach;
import static com.reactnativenavigation.utils.CollectionUtils.map;
import static com.reactnativenavigation.react.Constants.BOTTOM_TABS_HEIGHT;
import static com.reactnativenavigation.utils.CollectionUtils.*;
import static com.reactnativenavigation.utils.UiUtils.dpToPx;

public class BottomTabsController extends ParentController implements AHBottomNavigation.OnTabSelectedListener, TabSelector {

Expand Down Expand Up @@ -69,7 +69,7 @@ protected ViewGroup createView() {
presenter.bindView(bottomTabs, this);
tabPresenter.bindView(bottomTabs);
bottomTabs.setOnTabSelectedListener(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, dpToPx(getActivity(), BOTTOM_TABS_HEIGHT));
lp.addRule(ALIGN_PARENT_BOTTOM);
root.addView(bottomTabs, lp);
bottomTabs.addItems(createTabs());
Expand Down
Expand Up @@ -402,7 +402,6 @@ private BottomTabsController createBottomTabs() {
public void ensureViewIsCreated() {
super.ensureViewIsCreated();
uut.getView().layout(0, 0, 1000, 1000);
uut.getBottomTabs().layout(0, 0, 1000, 100);
}

@NonNull
Expand Down

0 comments on commit ecdb691

Please sign in to comment.