Parcourir la source

Update left button navigatorEventId when changing TopTabs (#1269)

Guy Carmeli il y a 7 ans
Parent
révision
8165ab9808

+ 2
- 1
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java Voir le fichier

@@ -79,6 +79,7 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
79 79
         }
80 80
         if (ViewPagerScreenChangedEvent.TYPE.equals(event.getType()) && isShown() ) {
81 81
             topBar.dismissContextualMenu();
82
+            topBar.onViewPagerScreenChanged(getScreenParams());
82 83
         }
83 84
     }
84 85
 
@@ -134,7 +135,7 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
134 135
         topBar.addTitleBarAndSetButtons(screenParams.rightButtons,
135 136
                 screenParams.leftButton,
136 137
                 leftButtonOnClickListener,
137
-                screenParams.getNavigatorEventId(),
138
+                getNavigatorEventId(),
138 139
                 screenParams.overrideBackPressInJs);
139 140
     }
140 141
 

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java Voir le fichier

@@ -326,8 +326,8 @@ public class ScreenStack {
326 326
     public void setScreenTitleBarLeftButton(String screenInstanceId, final String navigatorEventId, final TitleBarLeftButtonParams titleBarLeftButtonParams) {
327 327
         performOnScreen(screenInstanceId, new Task<Screen>() {
328 328
             @Override
329
-            public void run(Screen param) {
330
-                param.setTitleBarLeftButton(navigatorEventId, leftButtonOnClickListener, titleBarLeftButtonParams);
329
+            public void run(Screen screen) {
330
+                screen.setTitleBarLeftButton(navigatorEventId, leftButtonOnClickListener, titleBarLeftButtonParams);
331 331
             }
332 332
         });
333 333
     }

+ 14
- 4
android/app/src/main/java/com/reactnativenavigation/screens/ViewPagerScreen.java Voir le fichier

@@ -9,6 +9,7 @@ import com.reactnativenavigation.params.BaseScreenParams;
9 9
 import com.reactnativenavigation.params.FabParams;
10 10
 import com.reactnativenavigation.params.PageParams;
11 11
 import com.reactnativenavigation.params.ScreenParams;
12
+import com.reactnativenavigation.params.TitleBarLeftButtonParams;
12 13
 import com.reactnativenavigation.views.ContentView;
13 14
 import com.reactnativenavigation.views.LeftButtonOnClickListener;
14 15
 import com.reactnativenavigation.views.TopTabs;
@@ -30,7 +31,12 @@ public class ViewPagerScreen extends Screen {
30 31
 
31 32
     @Override
32 33
     public BaseScreenParams getScreenParams() {
33
-        return screenParams.topTabParams.get(viewPager.getCurrentItem());
34
+        return screenParams.topTabParams.get(getCurrentItem());
35
+    }
36
+
37
+    @Override
38
+    public void setTitleBarLeftButton(String navigatorEventId, LeftButtonOnClickListener backButtonListener, TitleBarLeftButtonParams titleBarLeftButtonParams) {
39
+        super.setTitleBarLeftButton(getNavigatorEventId(), backButtonListener, titleBarLeftButtonParams);
34 40
     }
35 41
 
36 42
     @Override
@@ -41,7 +47,7 @@ public class ViewPagerScreen extends Screen {
41 47
 
42 48
     @Override
43 49
     public ContentView getContentView() {
44
-        return contentViews.get(viewPager.getCurrentItem());
50
+        return contentViews.get(getCurrentItem());
45 51
     }
46 52
 
47 53
     @Override
@@ -120,12 +126,16 @@ public class ViewPagerScreen extends Screen {
120 126
 
121 127
     @Override
122 128
     public String getScreenInstanceId() {
123
-        return screenParams.topTabParams.get(viewPager.getCurrentItem()).navigationParams.screenInstanceId;
129
+        return screenParams.topTabParams.get(getCurrentItem()).navigationParams.screenInstanceId;
124 130
     }
125 131
 
126 132
     @Override
127 133
     public String getNavigatorEventId() {
128
-        return screenParams.topTabParams.get(viewPager.getCurrentItem()).navigationParams.navigatorEventId;
134
+        return screenParams.topTabParams.get(getCurrentItem()).navigationParams.navigatorEventId;
135
+    }
136
+
137
+    private int getCurrentItem() {
138
+        return viewPager == null ? 0 : viewPager.getCurrentItem();
129 139
     }
130 140
 
131 141
     public void selectTopTabByTabIndex(int index) {

+ 5
- 1
android/app/src/main/java/com/reactnativenavigation/views/LeftButton.java Voir le fichier

@@ -20,7 +20,7 @@ class LeftButton extends MaterialMenuDrawable implements View.OnClickListener {
20 20
 
21 21
     private TitleBarLeftButtonParams params;
22 22
     private final LeftButtonOnClickListener onClickListener;
23
-    private final String navigatorEventId;
23
+    private String navigatorEventId;
24 24
     private final boolean overrideBackPressInJs;
25 25
 
26 26
     LeftButton(Context context,
@@ -108,4 +108,8 @@ class LeftButton extends MaterialMenuDrawable implements View.OnClickListener {
108 108
     private void sendClickEvent() {
109 109
         NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(params.eventId, navigatorEventId);
110 110
     }
111
+
112
+    void updateNavigatorEventId(String navigatorEventId) {
113
+        this.navigatorEventId = navigatorEventId;
114
+    }
111 115
 }

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java Voir le fichier

@@ -13,6 +13,7 @@ import android.view.animation.AccelerateDecelerateInterpolator;
13 13
 import android.view.animation.AccelerateInterpolator;
14 14
 import android.widget.TextView;
15 15
 
16
+import com.reactnativenavigation.params.BaseScreenParams;
16 17
 import com.reactnativenavigation.params.BaseTitleBarButtonParams;
17 18
 import com.reactnativenavigation.params.StyleParams;
18 19
 import com.reactnativenavigation.params.TitleBarButtonParams;
@@ -303,4 +304,10 @@ public class TitleBar extends Toolbar {
303 304
     BaseTitleBarButtonParams getButton(int index) {
304 305
         return rightButtons.get(rightButtons.size() - index - 1);
305 306
     }
307
+
308
+    public void onViewPagerScreenChanged(BaseScreenParams screenParams) {
309
+        if (hasLeftButton()) {
310
+            leftButton.updateNavigatorEventId(screenParams.getNavigatorEventId());
311
+        }
312
+    }
306 313
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java Voir le fichier

@@ -8,6 +8,7 @@ import android.view.ViewGroup;
8 8
 import android.widget.FrameLayout;
9 9
 
10 10
 import com.facebook.react.bridge.Callback;
11
+import com.reactnativenavigation.params.BaseScreenParams;
11 12
 import com.reactnativenavigation.params.ContextualMenuParams;
12 13
 import com.reactnativenavigation.params.StyleParams;
13 14
 import com.reactnativenavigation.params.TitleBarButtonParams;
@@ -158,4 +159,8 @@ public class TopBar extends AppBarLayout {
158 159
     public void destroy() {
159 160
 
160 161
     }
162
+
163
+    public void onViewPagerScreenChanged(BaseScreenParams screenParams) {
164
+        titleBar.onViewPagerScreenChanged(screenParams);
165
+    }
161 166
 }