Skip to content

Commit

Permalink
Update SideMenu enabled in parent (#6196)
Browse files Browse the repository at this point in the history
When a menu is opened its resolved options are applied. These options set the enabled property of both left and right menus. If a menu is disabled, opening the other menu will unintentionally enable it when it is opened therefore we need to update the enabled properties of both menus in the actual SideMenu layout.

Closes #6182

Co-authored-by: Yogev Ben David <yogev132@gmail.com>
  • Loading branch information
guyca and yogevbd authored May 18, 2020
1 parent 4d8d2ae commit 67191e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.view.View;

import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.parse.SideMenuRootOptions;
import com.reactnativenavigation.parse.params.Bool;
import com.reactnativenavigation.presentation.Presenter;
import com.reactnativenavigation.presentation.SideMenuPresenter;
Expand All @@ -24,6 +25,8 @@
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.drawerlayout.widget.DrawerLayout.LayoutParams;

import static com.reactnativenavigation.utils.ObjectUtils.perform;

public class SideMenuController extends ParentController<SideMenuRoot> implements DrawerLayout.DrawerListener {

private ViewController center;
Expand Down Expand Up @@ -94,6 +97,7 @@ public void applyChildOptions(Options options, ViewController child) {
public void mergeChildOptions(Options options, ViewController child) {
super.mergeChildOptions(options, child);
presenter.mergeOptions(options.sideMenuRootOptions);
mergeLockMode(this.initialOptions, options.sideMenuRootOptions);
performOnParentController(parent -> parent.mergeChildOptions(options, child));
}

Expand Down Expand Up @@ -126,13 +130,13 @@ private boolean isDrawerOpen(int gravity) {
@Override
public void onDrawerOpened(@NonNull View drawerView) {
ViewController view = this.getMatchingView(drawerView);
view.mergeOptions(this.getOptionsWithVisibility(this.viewIsLeft(drawerView), true));
view.mergeOptions(this.getOptionsWithVisibility(isLeftMenu(drawerView), true));
}

@Override
public void onDrawerClosed(@NonNull View drawerView) {
ViewController view = this.getMatchingView(drawerView);
view.mergeOptions(this.getOptionsWithVisibility(this.viewIsLeft(drawerView), false));
view.mergeOptions(this.getOptionsWithVisibility(isLeftMenu(drawerView), false));
}

@Override
Expand Down Expand Up @@ -179,10 +183,10 @@ public void setRightController(ViewController controller) {
}

private ViewController getMatchingView (View drawerView) {
return this.viewIsLeft(drawerView) ? left : right;
return this.isLeftMenu(drawerView) ? left : right;
}

private boolean viewIsLeft (View drawerView) {
private boolean isLeftMenu(View drawerView) {
return (left != null && drawerView.equals(left.getView()));
}

Expand All @@ -208,6 +212,11 @@ private void dispatchSideMenuVisibilityEvents(ViewController drawer, float prevO
}
}

private void mergeLockMode(Options out, SideMenuRootOptions sideMenu) {
perform(sideMenu.left.enabled.get(null), enabled -> out.sideMenuRootOptions.left.enabled = new Bool(enabled));
perform(sideMenu.right.enabled.get(null), enabled -> out.sideMenuRootOptions.right.enabled = new Bool(enabled));
}

@RestrictTo(RestrictTo.Scope.TESTS)
SideMenu getSideMenu() {
return presenter.getSideMenu();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ public void resolveCurrentOptions_centerOptionsAreMergedEvenIfDrawerIsOpen() {
assertThat(uut.resolveCurrentOptions().topBar.title.text.hasValue()).isTrue();
}

@Test
public void mergeChildOptions_lockModeIsUpdatedInInitialOptions() {
setLeftRight(left, right);

Options leftDisabled = new Options();
leftDisabled.sideMenuRootOptions.left.enabled = new Bool(false);
left.mergeOptions(leftDisabled);
assertThat(uut.resolveCurrentOptions().sideMenuRootOptions.left.enabled.get()).isFalse();

Options rightVisible = new Options();
rightVisible.sideMenuRootOptions.right.visible = new Bool(true);
right.mergeOptions(rightVisible);
assertThat(uut.resolveCurrentOptions().sideMenuRootOptions.left.enabled.get()).isFalse();
}

@Test
public void setLeftController_matchesParentByDefault() {
SideMenuOptions options = new SideMenuOptions();
Expand Down Expand Up @@ -389,5 +404,7 @@ private Activity createActivity() {
private void setLeftRight(ViewController left, ViewController right) {
uut.setLeftController(left);
uut.setRightController(right);
left.setParentController(uut);
right.setParentController(uut);
}
}

0 comments on commit 67191e9

Please sign in to comment.