diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuController.java b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuController.java index 0cbd5223c0a..30c59d4039f 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuController.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuController.java @@ -31,6 +31,8 @@ public class SideMenuController extends ParentController 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); @@ -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 @@ -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)); } @@ -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); @@ -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(); + } } } diff --git a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuControllerTest.java b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuControllerTest.java index 8e3cb696b6e..111e529c4fd 100644 --- a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuControllerTest.java +++ b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/sidemenu/SideMenuControllerTest.java @@ -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; @@ -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 { @@ -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()); @@ -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; + } }