Browse Source

Set left button dynamically

Guy Carmeli 8 years ago
parent
commit
f17b1f024c

+ 22
- 4
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java View File

@@ -7,7 +7,9 @@ import com.facebook.react.bridge.ReadableArray;
7 7
 import com.facebook.react.bridge.ReadableMap;
8 8
 import com.reactnativenavigation.controllers.NavigationCommandsHandler;
9 9
 import com.reactnativenavigation.params.TitleBarButtonParams;
10
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
10 11
 import com.reactnativenavigation.params.parsers.TitleBarButtonParamsParser;
12
+import com.reactnativenavigation.params.parsers.TitleBarLeftButtonParamsParser;
11 13
 
12 14
 import java.util.List;
13 15
 
@@ -46,11 +48,27 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
46 48
     }
47 49
 
48 50
     @ReactMethod
49
-    public void setScreenTitleBarButtons(String screenInstanceId, String navigatorEventId, ReadableArray rightButtons, ReadableMap leftButton) {
50
-        List<TitleBarButtonParams> titleBarButtons = new TitleBarButtonParamsParser().
51
-                parseButtons(BundleConverter.toBundle(rightButtons));
51
+    public void setScreenTitleBarButtons(String screenInstanceId, String navigatorEventId,
52
+                                         ReadableArray rightButtonsParams, ReadableMap leftButtonParams) {
53
+        if (rightButtonsParams != null) {
54
+            setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, rightButtonsParams);
55
+        }
52 56
 
53
-        NavigationCommandsHandler.setScreenTitleBarButtons(screenInstanceId, navigatorEventId, titleBarButtons);
57
+        if (leftButtonParams != null) {
58
+            setScreenTitleBarLeftButton(screenInstanceId, navigatorEventId, leftButtonParams);
59
+        }
60
+    }
61
+
62
+    private void setScreenTitleBarRightButtons(String screenInstanceId, String navigatorEventId, ReadableArray rightButtonsParams) {
63
+        List<TitleBarButtonParams> rightButtons = new TitleBarButtonParamsParser()
64
+                .parseButtons(BundleConverter.toBundle(rightButtonsParams));
65
+        NavigationCommandsHandler.setScreenTitleBarRightButtons(screenInstanceId, navigatorEventId, rightButtons);
66
+    }
67
+
68
+    private void setScreenTitleBarLeftButton(String screenInstanceId, String navigatorEventId, ReadableMap leftButtonParams) {
69
+        TitleBarLeftButtonParams leftButton = new TitleBarLeftButtonParamsParser()
70
+                .parseSingleButton(BundleConverter.toBundle(leftButtonParams));
71
+        NavigationCommandsHandler.setScreenTitleBarLeftButtons(screenInstanceId, navigatorEventId, leftButton);
54 72
     }
55 73
 
56 74
     @ReactMethod

+ 6
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -12,6 +12,7 @@ import com.reactnativenavigation.layouts.LayoutFactory;
12 12
 import com.reactnativenavigation.params.ActivityParams;
13 13
 import com.reactnativenavigation.params.ScreenParams;
14 14
 import com.reactnativenavigation.params.TitleBarButtonParams;
15
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
15 16
 import com.reactnativenavigation.react.JsDevReloadHandler;
16 17
 import com.reactnativenavigation.react.NavigationReactInstance;
17 18
 import com.reactnativenavigation.react.RedboxPermission;
@@ -138,7 +139,11 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
138 139
     }
139 140
 
140 141
     public void setTitleBarButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
141
-        layout.setTitleBarButtons(screenInstanceId, navigatorEventId, titleBarButtons);
142
+        layout.setTitleBarRightButtons(screenInstanceId, navigatorEventId, titleBarButtons);
143
+    }
144
+
145
+    public void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButton) {
146
+        layout.setTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarLeftButton);
142 147
     }
143 148
 
144 149
     void showModal(ScreenParams screenParams) {

+ 20
- 1
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

@@ -7,6 +7,7 @@ import com.reactnativenavigation.NavigationApplication;
7 7
 import com.reactnativenavigation.params.ActivityParams;
8 8
 import com.reactnativenavigation.params.ScreenParams;
9 9
 import com.reactnativenavigation.params.TitleBarButtonParams;
10
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
10 11
 import com.reactnativenavigation.params.parsers.ActivityParamsParser;
11 12
 import com.reactnativenavigation.params.parsers.ScreenParamsParser;
12 13
 
@@ -137,7 +138,9 @@ public class NavigationCommandsHandler {
137 138
         });
138 139
     }
139 140
 
140
-    public static void setScreenTitleBarButtons(final String screenInstanceId, final String navigatorEventId, final List<TitleBarButtonParams> titleBarButtons) {
141
+    public static void setScreenTitleBarRightButtons(final String screenInstanceId,
142
+                                                     final String navigatorEventId,
143
+                                                     final List<TitleBarButtonParams> titleBarButtons) {
141 144
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
142 145
         if (currentActivity == null) {
143 146
             return;
@@ -151,6 +154,22 @@ public class NavigationCommandsHandler {
151 154
         });
152 155
     }
153 156
 
157
+    public static void setScreenTitleBarLeftButtons(final String screenInstanceId,
158
+                                                     final String navigatorEventId,
159
+                                                     final TitleBarLeftButtonParams titleBarButtons) {
160
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
161
+        if (currentActivity == null) {
162
+            return;
163
+        }
164
+
165
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
166
+            @Override
167
+            public void run() {
168
+                currentActivity.setTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarButtons);
169
+            }
170
+        });
171
+    }
172
+
154 173
     public static void dismissTopModal() {
155 174
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
156 175
         if (currentActivity == null) {

+ 4
- 1
android/app/src/main/java/com/reactnativenavigation/layouts/Layout.java View File

@@ -3,6 +3,7 @@ package com.reactnativenavigation.layouts;
3 3
 import android.view.View;
4 4
 
5 5
 import com.reactnativenavigation.params.TitleBarButtonParams;
6
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
6 7
 
7 8
 import java.util.List;
8 9
 
@@ -15,5 +16,7 @@ public interface Layout extends ScreenStackContainer {
15 16
 
16 17
     void setTitleBarTitle(String screenInstanceId, String title);
17 18
 
18
-    void setTitleBarButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons);
19
+    void setTitleBarRightButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons);
20
+
21
+    void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButtonParams);
19 22
 }

+ 9
- 2
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

@@ -6,6 +6,7 @@ import android.widget.FrameLayout;
6 6
 
7 7
 import com.reactnativenavigation.params.ScreenParams;
8 8
 import com.reactnativenavigation.params.TitleBarButtonParams;
9
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
9 10
 import com.reactnativenavigation.screens.ScreenStack;
10 11
 
11 12
 import java.util.List;
@@ -83,7 +84,13 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
83 84
     }
84 85
 
85 86
     @Override
86
-    public void setTitleBarButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
87
-        stack.setTitleBarButtons(screenInstanceId, navigatorEventId, titleBarButtons);
87
+    public void setTitleBarRightButtons(String screenInstanceId, String navigatorEventId,
88
+                                        List<TitleBarButtonParams> titleBarRightButtons) {
89
+        stack.setTitleBarRightButtons(screenInstanceId, navigatorEventId, titleBarRightButtons);
90
+    }
91
+
92
+    @Override
93
+    public void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButtonParams) {
94
+        stack.setTitleBarLeftButton(screenInstanceId, navigatorEventId, titleBarLeftButtonParams);
88 95
     }
89 96
 }

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/params/TitleBarLeftButtonParams.java View File

@@ -3,7 +3,7 @@ package com.reactnativenavigation.params;
3 3
 import com.balysv.materialmenu.MaterialMenuDrawable;
4 4
 
5 5
 public class TitleBarLeftButtonParams extends TitleBarButtonParams {
6
-    public MaterialMenuDrawable.IconState initialIconState;
6
+    public MaterialMenuDrawable.IconState iconState;
7 7
 
8 8
     public TitleBarLeftButtonParams(TitleBarButtonParams params) {
9 9
         icon = params.icon;

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/params/parsers/TitleBarLeftButtonParamsParser.java View File

@@ -9,7 +9,7 @@ public class TitleBarLeftButtonParamsParser extends TitleBarButtonParamsParser {
9 9
 
10 10
     public TitleBarLeftButtonParams parseSingleButton(Bundle params) {
11 11
         TitleBarLeftButtonParams backButtonParams = new TitleBarLeftButtonParams(super.parseSingleButton(params));
12
-        backButtonParams.initialIconState = getIconStateFromId(backButtonParams.eventId);
12
+        backButtonParams.iconState = getIconStateFromId(backButtonParams.eventId);
13 13
         return backButtonParams;
14 14
     }
15 15
 

+ 8
- 2
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java View File

@@ -13,6 +13,7 @@ import com.reactnativenavigation.animation.VisibilityAnimator;
13 13
 import com.reactnativenavigation.params.ScreenParams;
14 14
 import com.reactnativenavigation.params.ScreenStyleParams;
15 15
 import com.reactnativenavigation.params.TitleBarButtonParams;
16
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
16 17
 import com.reactnativenavigation.utils.SdkSupports;
17 18
 import com.reactnativenavigation.utils.ViewUtils;
18 19
 import com.reactnativenavigation.views.ScrollDirectionListener;
@@ -140,7 +141,12 @@ public abstract class Screen extends RelativeLayout implements ScrollDirectionLi
140 141
         topBar.setTitle(title);
141 142
     }
142 143
 
143
-    public void setTitleBarButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
144
-        topBar.setTitleBarButtons(navigatorEventId, titleBarButtons);
144
+    public void setTitleBarRightButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
145
+        topBar.setTitleBarRightButtons(navigatorEventId, titleBarButtons);
146
+    }
147
+
148
+    public void setTitleBarLeftButton(String navigatorEventId, TitleBarBackButtonListener backButtonListener,
149
+                                      TitleBarLeftButtonParams titleBarLeftButtonParams) {
150
+        topBar.setTitleBarRightButton(navigatorEventId, backButtonListener, titleBarLeftButtonParams);
145 151
     }
146 152
 }

+ 10
- 21
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

@@ -6,6 +6,7 @@ import android.widget.FrameLayout;
6 6
 
7 7
 import com.reactnativenavigation.params.ScreenParams;
8 8
 import com.reactnativenavigation.params.TitleBarButtonParams;
9
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
9 10
 import com.reactnativenavigation.views.TitleBarBackButtonListener;
10 11
 
11 12
 import java.util.List;
@@ -112,10 +113,17 @@ public class ScreenStack extends FrameLayout implements TitleBarBackButtonListen
112 113
         }
113 114
     }
114 115
 
115
-    public void setTitleBarButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
116
+    public void setTitleBarRightButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
116 117
         Screen screen = findScreenByScreenInstanceId(screenInstanceId);
117 118
         if (screen != null) {
118
-            screen.setTitleBarButtons(navigatorEventId, titleBarButtons);
119
+            screen.setTitleBarRightButtons(navigatorEventId, titleBarButtons);
120
+        }
121
+    }
122
+
123
+    public void setTitleBarLeftButton(String screenInstanceId, String navigatorEventId, TitleBarLeftButtonParams titleBarLeftButtonParams) {
124
+        Screen screen = findScreenByScreenInstanceId(screenInstanceId);
125
+        if (screen != null) {
126
+            screen.setTitleBarLeftButton(navigatorEventId, this, titleBarLeftButtonParams);
119 127
         }
120 128
     }
121 129
 
@@ -125,23 +133,4 @@ public class ScreenStack extends FrameLayout implements TitleBarBackButtonListen
125 133
             pop();
126 134
         }
127 135
     }
128
-
129
-    //    /**
130
-//     * Remove the ScreenStack from {@code parent} while preventing all child react views from getting unmounted
131
-//        -= USE WHEN SWITCHING TABS =-
132
-//     */
133
-//    public void removeFromScreen(ViewGroup parent) {
134
-//        mStack.peek().view.onTemporallyRemovedFromScreen();
135
-//
136
-//        parent.removeView(this);
137
-//    }
138
-//    /**
139
-//     * Add ScreenStack to {@code parent}
140
-//       -= USE WHEN SWITCHING TABS =-
141
-//     */
142
-//    public void addToScreen(ViewGroup parent) {
143
-//        mStack.peek().view.onReAddToScreen();
144
-//
145
-//        parent.addView(this, new CoordinatorLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
146
-//    }
147 136
 }

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/views/LeftButton.java View File

@@ -34,7 +34,7 @@ public class LeftButton extends MaterialMenuDrawable implements View.OnClickList
34 34
 
35 35
     private void setInitialState() {
36 36
         if (params != null) {
37
-            setIconState(params.initialIconState);
37
+            setIconState(params.iconState);
38 38
         } else {
39 39
             setVisible(false);
40 40
         }

+ 10
- 1
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

@@ -14,6 +14,7 @@ public class TitleBar extends Toolbar {
14 14
 
15 15
     private boolean hideOnScroll = false;
16 16
     private VisibilityAnimator visibilityAnimator;
17
+    private LeftButton leftButton;
17 18
 
18 19
     public TitleBar(Context context) {
19 20
         super(context);
@@ -44,7 +45,15 @@ public class TitleBar extends Toolbar {
44 45
 
45 46
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
46 47
                                TitleBarBackButtonListener titleBarBackButtonListener, String navigatorEventId) {
47
-        LeftButton leftButton = new LeftButton(getContext(), leftButtonParams, titleBarBackButtonListener, navigatorEventId);
48
+        if (leftButton == null) {
49
+            createAndSetLeftButton(leftButtonParams, titleBarBackButtonListener, navigatorEventId);
50
+        } else {
51
+            leftButton.animateIconState(leftButtonParams.iconState);
52
+        }
53
+    }
54
+
55
+    private void createAndSetLeftButton(TitleBarLeftButtonParams leftButtonParams, TitleBarBackButtonListener titleBarBackButtonListener, String navigatorEventId) {
56
+        leftButton = new LeftButton(getContext(), leftButtonParams, titleBarBackButtonListener, navigatorEventId);
48 57
         setNavigationOnClickListener(leftButton);
49 58
         setNavigationIcon(leftButton);
50 59
     }

+ 7
- 1
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

@@ -43,7 +43,7 @@ public class TopBar extends AppBarLayout {
43 43
         titleBar.setVisibility(styleParams.titleBarHidden ? GONE : VISIBLE);
44 44
     }
45 45
 
46
-    public void setTitleBarButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
46
+    public void setTitleBarRightButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
47 47
         titleBar.setRightButtons(titleBarButtons, navigatorEventId);
48 48
     }
49 49
 
@@ -52,4 +52,10 @@ public class TopBar extends AppBarLayout {
52 52
         addView(tabLayout);
53 53
         return tabLayout;
54 54
     }
55
+
56
+    public void setTitleBarRightButton(String navigatorEventId,
57
+                                       TitleBarBackButtonListener titleBarBackButtonListener,
58
+                                       TitleBarLeftButtonParams titleBarLeftButtonParams) {
59
+        titleBar.setLeftButton(titleBarLeftButtonParams, titleBarBackButtonListener, navigatorEventId);
60
+    }
55 61
 }

+ 5
- 7
src/deprecated/platformSpecificDeprecated.android.js View File

@@ -148,14 +148,12 @@ function navigatorSetButtons(navigator, navigatorEventID, params) {
148 148
     });
149 149
   }
150 150
   if (params.leftButton) {
151
-    params.leftButton.forEach(function(button) {
152
-      if (button.icon) {
153
-        const icon = resolveAssetSource(button.icon);
154
-        if (icon) {
155
-          button.icon = icon.uri;
156
-        }
151
+    if (params.leftButton.icon) {
152
+      const icon = resolveAssetSource(params.leftButton.icon);
153
+      if (icon) {
154
+        params.leftButton.icon = icon.uri;
157 155
       }
158
-    });
156
+    }
159 157
   }
160 158
   newPlatformSpecific.setScreenTitleBarButtons(navigator.screenInstanceID, navigatorEventID, params.rightButtons, params.leftButton);
161 159
 }