Browse Source

Fix right buttons not getting applied after push and pop (#5353)

Guy Carmeli 5 years ago
parent
commit
22191ed292
No account linked to committer's email address

+ 13
- 2
lib/android/app/src/main/java/com/reactnativenavigation/presentation/StackPresenter.java View File

@@ -23,6 +23,7 @@ import com.reactnativenavigation.parse.TopTabsOptions;
23 23
 import com.reactnativenavigation.parse.params.Button;
24 24
 import com.reactnativenavigation.parse.params.Colour;
25 25
 import com.reactnativenavigation.utils.ButtonPresenter;
26
+import com.reactnativenavigation.utils.CollectionUtils;
26 27
 import com.reactnativenavigation.utils.ImageLoader;
27 28
 import com.reactnativenavigation.utils.ObjectUtils;
28 29
 import com.reactnativenavigation.utils.UiUtils;
@@ -69,6 +70,7 @@ public class StackPresenter {
69 70
     private Map<Component, TopBarBackgroundViewController> backgroundControllers = new HashMap();
70 71
     private Map<Component, Map<String, TitleBarButtonController>> componentRightButtons = new HashMap();
71 72
     private Map<Component, Map<String, TitleBarButtonController>> componentLeftButtons = new HashMap();
73
+    private List<TitleBarButtonController> currentRightButtons = new ArrayList<>();
72 74
 
73 75
     public StackPresenter(Activity activity,
74 76
                           TitleBarReactViewCreator titleViewCreator,
@@ -278,8 +280,12 @@ public class StackPresenter {
278 280
         if (rightButtons != null) {
279 281
             List<TitleBarButtonController> rightButtonControllers = getOrCreateButtonControllers(componentRightButtons.get(child), rightButtons);
280 282
             componentRightButtons.put(child, keyBy(rightButtonControllers, TitleBarButtonController::getButtonInstanceId));
281
-            topBar.setRightButtons(rightButtonControllers);
283
+            if (!CollectionUtils.equals(currentRightButtons, rightButtonControllers)) {
284
+                currentRightButtons = rightButtonControllers;
285
+                topBar.setRightButtons(rightButtonControllers);
286
+            }
282 287
         } else {
288
+            currentRightButtons = null;
283 289
             topBar.clearRightButtons();
284 290
         }
285 291
 
@@ -370,7 +376,12 @@ public class StackPresenter {
370 376
             if (previousLeftButtons != null) forEach(previousLeftButtons.values(), TitleBarButtonController::destroy);
371 377
         }
372 378
 
373
-        if (buttons.right != null) topBar.setRightButtons(rightButtonControllers);
379
+        if (buttons.right != null) {
380
+            if (!CollectionUtils.equals(currentRightButtons, rightButtonControllers)) {
381
+                currentRightButtons = rightButtonControllers;
382
+                topBar.setRightButtons(rightButtonControllers);
383
+            }
384
+        }
374 385
         if (buttons.left != null) topBar.setLeftButtons(leftButtonControllers);
375 386
         if (buttons.back.hasValue()) topBar.setBackButton(createButtonController(buttons.back));
376 387
 

+ 0
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/topbar/TopBar.java View File

@@ -28,7 +28,6 @@ import com.reactnativenavigation.parse.Alignment;
28 28
 import com.reactnativenavigation.parse.AnimationOptions;
29 29
 import com.reactnativenavigation.parse.params.Colour;
30 30
 import com.reactnativenavigation.parse.params.Number;
31
-import com.reactnativenavigation.utils.CollectionUtils;
32 31
 import com.reactnativenavigation.utils.CompatUtils;
33 32
 import com.reactnativenavigation.utils.UiUtils;
34 33
 import com.reactnativenavigation.viewcontrollers.TitleBarButtonController;
@@ -36,7 +35,6 @@ import com.reactnativenavigation.views.StackLayout;
36 35
 import com.reactnativenavigation.views.titlebar.TitleBar;
37 36
 import com.reactnativenavigation.views.toptabs.TopTabs;
38 37
 
39
-import java.util.ArrayList;
40 38
 import java.util.Collections;
41 39
 import java.util.List;
42 40
 
@@ -53,7 +51,6 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
53 51
     private View border;
54 52
     private View component;
55 53
     private float elevation = -1;
56
-    private List<TitleBarButtonController> rightButtons = new ArrayList<>();
57 54
 
58 55
     public TopBar(final Context context, StackLayout parentView) {
59 56
         super(context);
@@ -220,8 +217,6 @@ public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAw
220 217
     }
221 218
 
222 219
     public void setRightButtons(List<TitleBarButtonController> rightButtons) {
223
-        if (CollectionUtils.equals(this.rightButtons, rightButtons)) return;
224
-        this.rightButtons = rightButtons;
225 220
         titleBar.setRightButtons(rightButtons);
226 221
     }
227 222
 

+ 3
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackPresenterTest.java View File

@@ -178,7 +178,9 @@ public class StackPresenterTest extends BaseTest {
178 178
         verify(topBar, times(0)).setLeftButtons(any());
179 179
 
180 180
         Options options = new Options();
181
-        options.topBar.buttons.right = new ArrayList<>();
181
+        Button button = new Button();
182
+        button.text = new Text("btn");
183
+        options.topBar.buttons.right = new ArrayList<>(Collections.singleton(button));
182 184
         uut.mergeChildOptions(options, EMPTY_OPTIONS, child);
183 185
         verify(topBar, times(1)).setRightButtons(any());
184 186