Skip to content

Commit

Permalink
Emit SideMenu visibility events (#5149)
Browse files Browse the repository at this point in the history
  • Loading branch information
guyca authored May 24, 2019
1 parent 6b190d4 commit 7ee9c12
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class SideMenuController extends ParentController<DrawerLayout> implement
private ViewController left;
private ViewController right;
private SideMenuPresenter presenter;
private float prevLeftSlideOffset = 0;
private float prevRightSlideOffset = 0;

public SideMenuController(Activity activity, ChildControllersRegistry childRegistry, String id, Options initialOptions, SideMenuPresenter sideMenuOptionsPresenter, Presenter presenter) {
super(activity, childRegistry, id, presenter, initialOptions);
Expand Down Expand Up @@ -111,15 +113,30 @@ public Options resolveCurrentOptions() {
@Override
public void onDrawerOpened(@NonNull View drawerView) {
ViewController view = this.getMatchingView(drawerView);
view.mergeOptions(this.getOptionsWithVisability(this.viewIsLeft(drawerView), true));
view.onViewAppeared();
view.mergeOptions(this.getOptionsWithVisibility(this.viewIsLeft(drawerView), true));
}

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

@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
int gravity = getSideMenuGravity(drawerView);
if (gravity == Gravity.LEFT) {
dispatchSideMenuVisibilityEvents(left, prevLeftSlideOffset, slideOffset);
prevLeftSlideOffset = slideOffset;
} else if (gravity == Gravity.RIGHT) {
dispatchSideMenuVisibilityEvents(right, prevRightSlideOffset, slideOffset);
prevRightSlideOffset = slideOffset;
}
}

@Override
public void onDrawerStateChanged(int newState) {

}

@Override
Expand All @@ -135,15 +152,15 @@ public void setCenterController(ViewController centerController) {

public void setLeftController(ViewController controller) {
this.left = controller;
int height = this.getHeight(options.sideMenuRootOptions.left);
int width = this.getWidth(options.sideMenuRootOptions.left);
int height = getHeight(options.sideMenuRootOptions.left);
int width = getWidth(options.sideMenuRootOptions.left);
getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.LEFT));
}

public void setRightController(ViewController controller) {
this.right = controller;
int height = this.getHeight(options.sideMenuRootOptions.right);
int width = this.getWidth(options.sideMenuRootOptions.right);
int height = getHeight(options.sideMenuRootOptions.right);
int width = getWidth(options.sideMenuRootOptions.right);
getView().addView(controller.getView(), new LayoutParams(width, height, Gravity.RIGHT));
}

Expand Down Expand Up @@ -171,7 +188,11 @@ private boolean viewIsLeft (View drawerView) {
return (left != null && drawerView.equals(left.getView()));
}

private Options getOptionsWithVisability ( boolean isLeft, boolean visible ) {
private int getSideMenuGravity(View drawerView) {
return ((LayoutParams) drawerView.getLayoutParams()).gravity;
}

private Options getOptionsWithVisibility(boolean isLeft, boolean visible ) {
Options options = new Options();
if (isLeft) {
options.sideMenuRootOptions.left.visible = new Bool(visible);
Expand All @@ -181,13 +202,11 @@ private Options getOptionsWithVisability ( boolean isLeft, boolean visible ) {
return options;
}

@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

}

@Override
public void onDrawerStateChanged(int newState) {

private void dispatchSideMenuVisibilityEvents(ViewController drawer, float prevOffset, float offset) {
if (prevOffset == 0 && offset> 0) {
drawer.onViewAppeared();
} else if (prevOffset > 0 && offset == 0) {
drawer.onViewDisappear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;

import com.reactnativenavigation.BaseTest;
import com.reactnativenavigation.mocks.SimpleComponentViewController;
import com.reactnativenavigation.parse.*;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.parse.SideMenuOptions;
import com.reactnativenavigation.parse.params.Bool;
import com.reactnativenavigation.parse.params.Number;
import com.reactnativenavigation.parse.params.Text;
import com.reactnativenavigation.presentation.Presenter;
import com.reactnativenavigation.presentation.SideMenuPresenter;
import com.reactnativenavigation.utils.*;
import com.reactnativenavigation.utils.CommandListenerAdapter;
import com.reactnativenavigation.utils.Functions;
import com.reactnativenavigation.viewcontrollers.ChildControllersRegistry;
import com.reactnativenavigation.viewcontrollers.ParentController;
import com.reactnativenavigation.viewcontrollers.ViewController;
Expand All @@ -30,6 +34,7 @@
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SuppressWarnings("MagicNumber")
public class SideMenuControllerTest extends BaseTest {
Expand All @@ -46,7 +51,8 @@ public class SideMenuControllerTest extends BaseTest {

@Override
public void beforeEach() {
activity = newActivity();
activity = createActivity();

childRegistry = new ChildControllersRegistry();
presenter = spy(new SideMenuPresenter());
child = new SimpleComponentViewController(activity, childRegistry, "child", new Options());
Expand Down Expand Up @@ -305,4 +311,12 @@ private void closeRightMenu() {
options.sideMenuRootOptions.right.animate = new Bool(false);
uut.mergeOptions(options);
}

private Activity createActivity() {
Activity activity = spy(newActivity());
Window window = Mockito.mock(Window.class);
when(window.getDecorView()).thenReturn(Mockito.mock(View.class));
when(activity.getWindow()).thenReturn(window);
return activity;
}
}

0 comments on commit 7ee9c12

Please sign in to comment.