Browse Source

Added setButtons to navigator fixed bug where old buttons wouldn't be cleared

Yedidya Kennard 8 years ago
parent
commit
f7191db17b

+ 9
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java View File

19
 import com.facebook.react.ReactPackage;
19
 import com.facebook.react.ReactPackage;
20
 import com.facebook.react.ReactRootView;
20
 import com.facebook.react.ReactRootView;
21
 import com.facebook.react.bridge.Arguments;
21
 import com.facebook.react.bridge.Arguments;
22
+import com.facebook.react.bridge.ReadableMap;
22
 import com.facebook.react.bridge.WritableMap;
23
 import com.facebook.react.bridge.WritableMap;
23
 import com.facebook.react.common.ReactConstants;
24
 import com.facebook.react.common.ReactConstants;
24
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
25
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
318
     public void invokeDefaultOnBackPressed() {
319
     public void invokeDefaultOnBackPressed() {
319
         super.onBackPressed();
320
         super.onBackPressed();
320
     }
321
     }
322
+
323
+    public void setNavigationButtons(ReadableMap buttons){
324
+        if (mToolbar == null) {
325
+            return;
326
+        }
327
+        getCurrentScreen().setButtons(buttons);
328
+        mToolbar.setupToolbarButtonsAsync(getCurrentScreen());
329
+    }
321
 }
330
 }

+ 2
- 1
android/app/src/main/java/com/reactnativenavigation/activities/BottomTabActivity.java View File

138
         mContentFrame.removeAllViews();
138
         mContentFrame.removeAllViews();
139
         mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
139
         mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
140
         mCurrentStackPosition = position;
140
         mCurrentStackPosition = position;
141
-        setNavigationStyle(mScreenStacks.get(mCurrentStackPosition).peek());
141
+        mToolbar.setupToolbarButtonsAsync(getCurrentScreen());
142
+        setNavigationStyle(getCurrentScreen());
142
     }
143
     }
143
 
144
 
144
     private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {
145
     private static class SetupTabsTask extends AsyncTask<Void, Void, Map<Screen, Drawable>> {

+ 5
- 1
android/app/src/main/java/com/reactnativenavigation/core/objects/Screen.java View File

49
     public final String navigatorId;
49
     public final String navigatorId;
50
     public final String navigatorEventId;
50
     public final String navigatorEventId;
51
     public final String icon;
51
     public final String icon;
52
-    public final ArrayList<Button> buttons;
52
+    public ArrayList<Button> buttons;
53
     public HashMap<String, Object> passedProps = new HashMap<>();
53
     public HashMap<String, Object> passedProps = new HashMap<>();
54
 
54
 
55
     // Navigation styling
55
     // Navigation styling
82
         setToolbarStyle(screen);
82
         setToolbarStyle(screen);
83
     }
83
     }
84
 
84
 
85
+    public void setButtons(ReadableMap params) {
86
+        this.buttons = getButtons(params);
87
+    }
88
+
85
     private ArrayList<Button> getButtons(ReadableMap screen) {
89
     private ArrayList<Button> getButtons(ReadableMap screen) {
86
         ArrayList<Button> ret = new ArrayList<>();
90
         ArrayList<Button> ret = new ArrayList<>();
87
         if (screen.hasKey(KEY_RIGHT_BUTTONS)) {
91
         if (screen.hasKey(KEY_RIGHT_BUTTONS)) {

+ 20
- 6
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java View File

77
         }
77
         }
78
     }
78
     }
79
 
79
 
80
+    @ReactMethod
81
+    public void setNavigatorButtons(final ReadableMap buttons) {
82
+        final BaseReactActivity context = ContextProvider.getActivityContext();
83
+        if (context == null || context.isFinishing()) {
84
+            return;
85
+        }
86
+        context.runOnUiThread(new Runnable() {
87
+            @Override
88
+            public void run() {
89
+                context.setNavigationButtons(buttons);
90
+            }
91
+        });
92
+    }
93
+
80
     @ReactMethod
94
     @ReactMethod
81
     public void navigatorPush(final ReadableMap skreen) {
95
     public void navigatorPush(final ReadableMap skreen) {
82
         final Screen screen = new Screen(skreen);
96
         final Screen screen = new Screen(skreen);
101
         }
115
         }
102
 
116
 
103
         // No Modal is displayed, Push to activity
117
         // No Modal is displayed, Push to activity
104
-         context.runOnUiThread(new Runnable() {
105
-                 @Override
106
-                 public void run() {
107
-                     context.push(screen);
108
-                 }
109
-             });
118
+        context.runOnUiThread(new Runnable() {
119
+            @Override
120
+            public void run() {
121
+                context.push(screen);
122
+            }
123
+        });
110
     }
124
     }
111
 
125
 
112
     @ReactMethod
126
     @ReactMethod

+ 8
- 3
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java View File

134
 
134
 
135
         public SetupToolbarButtonsTask(RnnToolBar toolBar, Screen oldScreen, Screen newScreen) {
135
         public SetupToolbarButtonsTask(RnnToolBar toolBar, Screen oldScreen, Screen newScreen) {
136
             mToolbarWR = new WeakReference<>(toolBar);
136
             mToolbarWR = new WeakReference<>(toolBar);
137
-            mOldButtons = oldScreen == null ? Collections.EMPTY_LIST : oldScreen.getButtons();
137
+            mOldButtons = oldScreen == null ? null : oldScreen.getButtons();
138
             mNewButtons = newScreen.getButtons();
138
             mNewButtons = newScreen.getButtons();
139
             mTintColor = newScreen.buttonsTintColor;
139
             mTintColor = newScreen.buttonsTintColor;
140
         }
140
         }
174
             }
174
             }
175
 
175
 
176
             // Remove prev screen buttons
176
             // Remove prev screen buttons
177
-            for (Button btn : mOldButtons) {
178
-                menu.removeItem(btn.getItemId());
177
+            if(mOldButtons == null) {
178
+                menu.clear();
179
+            }
180
+            else {
181
+                for (Button btn : mOldButtons) {
182
+                    menu.removeItem(btn.getItemId());
183
+                }
179
             }
184
             }
180
 
185
 
181
             // Add new screen buttons
186
             // Add new screen buttons

+ 16
- 1
src/platformSpecific.android.js View File

51
   RctActivity.navigatorPush(params);
51
   RctActivity.navigatorPush(params);
52
 }
52
 }
53
 
53
 
54
+function navigatorSetButtons(navigator, navigatorEventID, params) {
55
+  if (params.rightButtons) {
56
+    params.rightButtons.forEach(function(button) {
57
+      if (button.icon) {
58
+        const icon = resolveAssetSource(button.icon);
59
+        if (icon) {
60
+          button.icon = icon.uri;
61
+        }
62
+      }
63
+    });
64
+  }
65
+  RctActivity.setNavigatorButtons(params);
66
+}
67
+
54
 function navigatorPop(navigator, params) {
68
 function navigatorPop(navigator, params) {
55
   RctActivity.navigatorPop(navigator);
69
   RctActivity.navigatorPop(navigator);
56
 }
70
 }
105
   navigatorPop,
119
   navigatorPop,
106
   showModal,
120
   showModal,
107
   dismissModal,
121
   dismissModal,
108
-  dismissAllModals
122
+  dismissAllModals,
123
+  navigatorSetButtons
109
 }
124
 }