Преглед изворни кода

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

Yedidya Kennard пре 8 година
родитељ
комит
f7191db17b

+ 9
- 0
android/app/src/main/java/com/reactnativenavigation/activities/BaseReactActivity.java Прегледај датотеку

@@ -19,6 +19,7 @@ import com.facebook.react.ReactInstanceManager;
19 19
 import com.facebook.react.ReactPackage;
20 20
 import com.facebook.react.ReactRootView;
21 21
 import com.facebook.react.bridge.Arguments;
22
+import com.facebook.react.bridge.ReadableMap;
22 23
 import com.facebook.react.bridge.WritableMap;
23 24
 import com.facebook.react.common.ReactConstants;
24 25
 import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
@@ -318,4 +319,12 @@ public abstract class BaseReactActivity extends AppCompatActivity implements Def
318 319
     public void invokeDefaultOnBackPressed() {
319 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 Прегледај датотеку

@@ -138,7 +138,8 @@ public class BottomTabActivity extends BaseReactActivity implements AHBottomNavi
138 138
         mContentFrame.removeAllViews();
139 139
         mContentFrame.addView(mScreenStacks.get(position), new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
140 140
         mCurrentStackPosition = position;
141
-        setNavigationStyle(mScreenStacks.get(mCurrentStackPosition).peek());
141
+        mToolbar.setupToolbarButtonsAsync(getCurrentScreen());
142
+        setNavigationStyle(getCurrentScreen());
142 143
     }
143 144
 
144 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 Прегледај датотеку

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

+ 20
- 6
android/app/src/main/java/com/reactnativenavigation/modules/RctActivityModule.java Прегледај датотеку

@@ -77,6 +77,20 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
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 94
     @ReactMethod
81 95
     public void navigatorPush(final ReadableMap skreen) {
82 96
         final Screen screen = new Screen(skreen);
@@ -101,12 +115,12 @@ public class RctActivityModule extends ReactContextBaseJavaModule {
101 115
         }
102 116
 
103 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 126
     @ReactMethod

+ 8
- 3
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java Прегледај датотеку

@@ -134,7 +134,7 @@ public class RnnToolBar extends Toolbar {
134 134
 
135 135
         public SetupToolbarButtonsTask(RnnToolBar toolBar, Screen oldScreen, Screen newScreen) {
136 136
             mToolbarWR = new WeakReference<>(toolBar);
137
-            mOldButtons = oldScreen == null ? Collections.EMPTY_LIST : oldScreen.getButtons();
137
+            mOldButtons = oldScreen == null ? null : oldScreen.getButtons();
138 138
             mNewButtons = newScreen.getButtons();
139 139
             mTintColor = newScreen.buttonsTintColor;
140 140
         }
@@ -174,8 +174,13 @@ public class RnnToolBar extends Toolbar {
174 174
             }
175 175
 
176 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 186
             // Add new screen buttons

+ 16
- 1
src/platformSpecific.android.js Прегледај датотеку

@@ -51,6 +51,20 @@ function navigatorPush(navigator, params) {
51 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 68
 function navigatorPop(navigator, params) {
55 69
   RctActivity.navigatorPop(navigator);
56 70
 }
@@ -105,5 +119,6 @@ export default {
105 119
   navigatorPop,
106 120
   showModal,
107 121
   dismissModal,
108
-  dismissAllModals
122
+  dismissAllModals,
123
+  navigatorSetButtons
109 124
 }