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
 import com.facebook.react.bridge.ReadableMap;
7
 import com.facebook.react.bridge.ReadableMap;
8
 import com.reactnativenavigation.controllers.NavigationCommandsHandler;
8
 import com.reactnativenavigation.controllers.NavigationCommandsHandler;
9
 import com.reactnativenavigation.params.TitleBarButtonParams;
9
 import com.reactnativenavigation.params.TitleBarButtonParams;
10
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
10
 import com.reactnativenavigation.params.parsers.TitleBarButtonParamsParser;
11
 import com.reactnativenavigation.params.parsers.TitleBarButtonParamsParser;
12
+import com.reactnativenavigation.params.parsers.TitleBarLeftButtonParamsParser;
11
 
13
 
12
 import java.util.List;
14
 import java.util.List;
13
 
15
 
46
     }
48
     }
47
 
49
 
48
     @ReactMethod
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
     @ReactMethod
74
     @ReactMethod

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

12
 import com.reactnativenavigation.params.ActivityParams;
12
 import com.reactnativenavigation.params.ActivityParams;
13
 import com.reactnativenavigation.params.ScreenParams;
13
 import com.reactnativenavigation.params.ScreenParams;
14
 import com.reactnativenavigation.params.TitleBarButtonParams;
14
 import com.reactnativenavigation.params.TitleBarButtonParams;
15
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
15
 import com.reactnativenavigation.react.JsDevReloadHandler;
16
 import com.reactnativenavigation.react.JsDevReloadHandler;
16
 import com.reactnativenavigation.react.NavigationReactInstance;
17
 import com.reactnativenavigation.react.NavigationReactInstance;
17
 import com.reactnativenavigation.react.RedboxPermission;
18
 import com.reactnativenavigation.react.RedboxPermission;
138
     }
139
     }
139
 
140
 
140
     public void setTitleBarButtons(String screenInstanceId, String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
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
     void showModal(ScreenParams screenParams) {
149
     void showModal(ScreenParams screenParams) {

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

7
 import com.reactnativenavigation.params.ActivityParams;
7
 import com.reactnativenavigation.params.ActivityParams;
8
 import com.reactnativenavigation.params.ScreenParams;
8
 import com.reactnativenavigation.params.ScreenParams;
9
 import com.reactnativenavigation.params.TitleBarButtonParams;
9
 import com.reactnativenavigation.params.TitleBarButtonParams;
10
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
10
 import com.reactnativenavigation.params.parsers.ActivityParamsParser;
11
 import com.reactnativenavigation.params.parsers.ActivityParamsParser;
11
 import com.reactnativenavigation.params.parsers.ScreenParamsParser;
12
 import com.reactnativenavigation.params.parsers.ScreenParamsParser;
12
 
13
 
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
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
144
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
142
         if (currentActivity == null) {
145
         if (currentActivity == null) {
143
             return;
146
             return;
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
     public static void dismissTopModal() {
173
     public static void dismissTopModal() {
155
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
174
         final NavigationActivity currentActivity = NavigationActivity.currentActivity;
156
         if (currentActivity == null) {
175
         if (currentActivity == null) {

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

3
 import android.view.View;
3
 import android.view.View;
4
 
4
 
5
 import com.reactnativenavigation.params.TitleBarButtonParams;
5
 import com.reactnativenavigation.params.TitleBarButtonParams;
6
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
6
 
7
 
7
 import java.util.List;
8
 import java.util.List;
8
 
9
 
15
 
16
 
16
     void setTitleBarTitle(String screenInstanceId, String title);
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
 
7
 import com.reactnativenavigation.params.ScreenParams;
7
 import com.reactnativenavigation.params.ScreenParams;
8
 import com.reactnativenavigation.params.TitleBarButtonParams;
8
 import com.reactnativenavigation.params.TitleBarButtonParams;
9
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
9
 import com.reactnativenavigation.screens.ScreenStack;
10
 import com.reactnativenavigation.screens.ScreenStack;
10
 
11
 
11
 import java.util.List;
12
 import java.util.List;
83
     }
84
     }
84
 
85
 
85
     @Override
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
 import com.balysv.materialmenu.MaterialMenuDrawable;
3
 import com.balysv.materialmenu.MaterialMenuDrawable;
4
 
4
 
5
 public class TitleBarLeftButtonParams extends TitleBarButtonParams {
5
 public class TitleBarLeftButtonParams extends TitleBarButtonParams {
6
-    public MaterialMenuDrawable.IconState initialIconState;
6
+    public MaterialMenuDrawable.IconState iconState;
7
 
7
 
8
     public TitleBarLeftButtonParams(TitleBarButtonParams params) {
8
     public TitleBarLeftButtonParams(TitleBarButtonParams params) {
9
         icon = params.icon;
9
         icon = params.icon;

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

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

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

13
 import com.reactnativenavigation.params.ScreenParams;
13
 import com.reactnativenavigation.params.ScreenParams;
14
 import com.reactnativenavigation.params.ScreenStyleParams;
14
 import com.reactnativenavigation.params.ScreenStyleParams;
15
 import com.reactnativenavigation.params.TitleBarButtonParams;
15
 import com.reactnativenavigation.params.TitleBarButtonParams;
16
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
16
 import com.reactnativenavigation.utils.SdkSupports;
17
 import com.reactnativenavigation.utils.SdkSupports;
17
 import com.reactnativenavigation.utils.ViewUtils;
18
 import com.reactnativenavigation.utils.ViewUtils;
18
 import com.reactnativenavigation.views.ScrollDirectionListener;
19
 import com.reactnativenavigation.views.ScrollDirectionListener;
140
         topBar.setTitle(title);
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
 
7
 import com.reactnativenavigation.params.ScreenParams;
7
 import com.reactnativenavigation.params.ScreenParams;
8
 import com.reactnativenavigation.params.TitleBarButtonParams;
8
 import com.reactnativenavigation.params.TitleBarButtonParams;
9
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
9
 import com.reactnativenavigation.views.TitleBarBackButtonListener;
10
 import com.reactnativenavigation.views.TitleBarBackButtonListener;
10
 
11
 
11
 import java.util.List;
12
 import java.util.List;
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
         Screen screen = findScreenByScreenInstanceId(screenInstanceId);
117
         Screen screen = findScreenByScreenInstanceId(screenInstanceId);
117
         if (screen != null) {
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
             pop();
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
 
34
 
35
     private void setInitialState() {
35
     private void setInitialState() {
36
         if (params != null) {
36
         if (params != null) {
37
-            setIconState(params.initialIconState);
37
+            setIconState(params.iconState);
38
         } else {
38
         } else {
39
             setVisible(false);
39
             setVisible(false);
40
         }
40
         }

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

14
 
14
 
15
     private boolean hideOnScroll = false;
15
     private boolean hideOnScroll = false;
16
     private VisibilityAnimator visibilityAnimator;
16
     private VisibilityAnimator visibilityAnimator;
17
+    private LeftButton leftButton;
17
 
18
 
18
     public TitleBar(Context context) {
19
     public TitleBar(Context context) {
19
         super(context);
20
         super(context);
44
 
45
 
45
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
46
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
46
                                TitleBarBackButtonListener titleBarBackButtonListener, String navigatorEventId) {
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
         setNavigationOnClickListener(leftButton);
57
         setNavigationOnClickListener(leftButton);
49
         setNavigationIcon(leftButton);
58
         setNavigationIcon(leftButton);
50
     }
59
     }

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

43
         titleBar.setVisibility(styleParams.titleBarHidden ? GONE : VISIBLE);
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
         titleBar.setRightButtons(titleBarButtons, navigatorEventId);
47
         titleBar.setRightButtons(titleBarButtons, navigatorEventId);
48
     }
48
     }
49
 
49
 
52
         addView(tabLayout);
52
         addView(tabLayout);
53
         return tabLayout;
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
     });
148
     });
149
   }
149
   }
150
   if (params.leftButton) {
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
   newPlatformSpecific.setScreenTitleBarButtons(navigator.screenInstanceID, navigatorEventID, params.rightButtons, params.leftButton);
158
   newPlatformSpecific.setScreenTitleBarButtons(navigator.screenInstanceID, navigatorEventID, params.rightButtons, params.leftButton);
161
 }
159
 }