Kaynağa Gözat

Add a way to dynamically enable/disable a side menu for Android (#1326)

Matt Davies 7 yıl önce
ebeveyn
işleme
3176aeb448

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java Dosyayı Görüntüle

@@ -151,6 +151,11 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
151 151
         NavigationCommandsHandler.setSideMenuVisible(animated, visible, Side.fromString(side));
152 152
     }
153 153
 
154
+    @ReactMethod
155
+    public void setSideMenuEnabled(boolean enabled, String side) {
156
+        NavigationCommandsHandler.setSideMenuEnabled(enabled, Side.fromString(side));
157
+    }
158
+
154 159
     @ReactMethod
155 160
     public void toggleTopBarVisible(final ReadableMap params) {
156 161
     }

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java Dosyayı Görüntüle

@@ -314,6 +314,10 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
314 314
         layout.setSideMenuVisible(animated, visible, side);
315 315
     }
316 316
 
317
+    public void setSideMenuEnabled(boolean enabled, Side side) {
318
+        layout.setSideMenuEnabled(enabled, side);
319
+    }
320
+
317 321
     public void selectTopTabByTabIndex(String screenInstanceId, int index) {
318 322
         layout.selectTopTabByTabIndex(screenInstanceId, index);
319 323
         modalController.selectTopTabByTabIndex(screenInstanceId, index);

+ 14
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java Dosyayı Görüntüle

@@ -317,6 +317,20 @@ public class NavigationCommandsHandler {
317 317
         });
318 318
     }
319 319
 
320
+    public static void setSideMenuEnabled(final boolean enabled, final Side side) {
321
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
322
+        if (currentActivity == null) {
323
+            return;
324
+        }
325
+
326
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
327
+            @Override
328
+            public void run() {
329
+                currentActivity.setSideMenuEnabled(enabled, side);
330
+            }
331
+        });
332
+    }
333
+
320 334
     public static void selectTopTabByTabIndex(final String screenInstanceId, final int index) {
321 335
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
322 336
         if (currentActivity == null) {

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java Dosyayı Görüntüle

@@ -3,6 +3,7 @@ package com.reactnativenavigation.layouts;
3 3
 import android.os.Bundle;
4 4
 import android.support.annotation.NonNull;
5 5
 import android.support.annotation.Nullable;
6
+import android.support.v4.widget.DrawerLayout;
6 7
 import android.support.v7.app.AppCompatActivity;
7 8
 import android.view.View;
8 9
 import android.widget.RelativeLayout;
@@ -231,6 +232,13 @@ public class BottomTabsLayout extends BaseLayout implements AHBottomNavigation.O
231 232
         }
232 233
     }
233 234
 
235
+    @Override
236
+    public void setSideMenuEnabled(boolean enabled, Side side) {
237
+        if (sideMenu != null) {
238
+            sideMenu.setDrawerLockMode(enabled ? DrawerLayout.LOCK_MODE_UNLOCKED : DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
239
+        }
240
+    }
241
+
234 242
     @Override
235 243
     public void showSnackbar(SnackbarParams params) {
236 244
         final String eventId = getCurrentScreenStack().peek().getNavigatorEventId();

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/Layout.java Dosyayı Görüntüle

@@ -37,6 +37,8 @@ public interface Layout extends ScreenStackContainer {
37 37
 
38 38
     void setSideMenuVisible(boolean animated, boolean visible, Side side);
39 39
 
40
+    void setSideMenuEnabled(boolean enabled, Side side);
41
+
40 42
     void showSnackbar(SnackbarParams params);
41 43
 
42 44
     void showSlidingOverlay(SlidingOverlayParams params);

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java Dosyayı Görüntüle

@@ -219,6 +219,13 @@ public class SingleScreenLayout extends BaseLayout {
219 219
         }
220 220
     }
221 221
 
222
+    @Override
223
+    public void setSideMenuEnabled(boolean enabled, Side side) {
224
+        if (sideMenu != null) {
225
+            sideMenu.setEnabled(enabled, side);
226
+        }
227
+    }
228
+
222 229
     @Override
223 230
     public void showSnackbar(SnackbarParams params) {
224 231
         final String navigatorEventId = stack.peek().getNavigatorEventId();

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/views/SideMenu.java Dosyayı Görüntüle

@@ -61,6 +61,14 @@ public class SideMenu extends DrawerLayout {
61 61
         }
62 62
     }
63 63
 
64
+    public void setEnabled(boolean enabled, Side side) {
65
+        if (enabled) {
66
+            setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, side.gravity);
67
+        } else {
68
+            setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, side.gravity);
69
+        }
70
+    }
71
+
64 72
     public void openDrawer(Side side) {
65 73
         openDrawer(side.gravity);
66 74
     }

+ 11
- 0
docs/screen-api.md Dosyayı Görüntüle

@@ -173,6 +173,17 @@ this.props.navigator.toggleDrawer({
173 173
 });
174 174
 ```
175 175
 
176
+## setDrawerEnabled(params = {}) (Android only)
177
+
178
+Enables or disables the side menu drawer assuming you have one in your app. Both drawers are enabled by default.
179
+
180
+```js
181
+this.props.navigator.setDrawerEnabled({
182
+  side: 'left', // the side of the drawer since you can have two, 'left' / 'right'
183
+  enabled: false // should the drawer be enabled or disabled (locked closed)
184
+});
185
+```
186
+
176 187
 ## toggleTabs(params = {})
177 188
 
178 189
 Toggle whether the tabs are displayed or not (only in tab-based apps).

+ 4
- 0
src/Screen.js Dosyayı Görüntüle

@@ -96,6 +96,10 @@ class Navigator {
96 96
     return platformSpecific.navigatorToggleDrawer(this, params);
97 97
   }
98 98
 
99
+  setDrawerEnabled(params = {}) {
100
+    return platformSpecific.navigatorSetDrawerEnabled(this, params);
101
+  }
102
+
99 103
   toggleTabs(params = {}) {
100 104
     return platformSpecific.navigatorToggleTabs(this, params);
101 105
   }

+ 5
- 0
src/deprecated/platformSpecificDeprecated.android.js Dosyayı Görüntüle

@@ -388,6 +388,10 @@ function navigatorToggleDrawer(navigator, params) {
388 388
   }
389 389
 }
390 390
 
391
+function navigatorSetDrawerEnabled(navigator, params) {
392
+  newPlatformSpecific.setSideMenuEnabled(params.enabled, params.side);
393
+}
394
+
391 395
 function navigatorToggleNavBar(navigator, params) {
392 396
   const screenInstanceID = navigator.screenInstanceID;
393 397
   const visible = params.to === 'shown' || params.to === 'show';
@@ -693,6 +697,7 @@ export default {
693 697
   navigatorSwitchToTab,
694 698
   navigatorSwitchToTopTab,
695 699
   navigatorToggleDrawer,
700
+  navigatorSetDrawerEnabled,
696 701
   navigatorToggleTabs,
697 702
   navigatorToggleNavBar,
698 703
   showSnackbar,

+ 5
- 0
src/platformSpecific.android.js Dosyayı Görüntüle

@@ -121,6 +121,10 @@ function setSideMenuVisible(animated, visible, side) {
121 121
   NativeReactModule.setSideMenuVisible(animated, visible, side);
122 122
 }
123 123
 
124
+function setSideMenuEnabled(enabled, side) {
125
+  NativeReactModule.setSideMenuEnabled(enabled, side);
126
+}
127
+
124 128
 function selectTopTabByTabIndex(screenInstanceId, index) {
125 129
   NativeReactModule.selectTopTabByTabIndex(screenInstanceId, index);
126 130
 }
@@ -195,6 +199,7 @@ module.exports = {
195 199
   dismissInAppNotification,
196 200
   toggleSideMenuVisible,
197 201
   setSideMenuVisible,
202
+  setSideMenuEnabled,
198 203
   selectBottomTabByNavigatorId,
199 204
   selectBottomTabByTabIndex,
200 205
   setBottomTabBadgeByNavigatorId,