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
 import com.reactnativenavigation.parse.params.Button;
23
 import com.reactnativenavigation.parse.params.Button;
24
 import com.reactnativenavigation.parse.params.Colour;
24
 import com.reactnativenavigation.parse.params.Colour;
25
 import com.reactnativenavigation.utils.ButtonPresenter;
25
 import com.reactnativenavigation.utils.ButtonPresenter;
26
+import com.reactnativenavigation.utils.CollectionUtils;
26
 import com.reactnativenavigation.utils.ImageLoader;
27
 import com.reactnativenavigation.utils.ImageLoader;
27
 import com.reactnativenavigation.utils.ObjectUtils;
28
 import com.reactnativenavigation.utils.ObjectUtils;
28
 import com.reactnativenavigation.utils.UiUtils;
29
 import com.reactnativenavigation.utils.UiUtils;
69
     private Map<Component, TopBarBackgroundViewController> backgroundControllers = new HashMap();
70
     private Map<Component, TopBarBackgroundViewController> backgroundControllers = new HashMap();
70
     private Map<Component, Map<String, TitleBarButtonController>> componentRightButtons = new HashMap();
71
     private Map<Component, Map<String, TitleBarButtonController>> componentRightButtons = new HashMap();
71
     private Map<Component, Map<String, TitleBarButtonController>> componentLeftButtons = new HashMap();
72
     private Map<Component, Map<String, TitleBarButtonController>> componentLeftButtons = new HashMap();
73
+    private List<TitleBarButtonController> currentRightButtons = new ArrayList<>();
72
 
74
 
73
     public StackPresenter(Activity activity,
75
     public StackPresenter(Activity activity,
74
                           TitleBarReactViewCreator titleViewCreator,
76
                           TitleBarReactViewCreator titleViewCreator,
278
         if (rightButtons != null) {
280
         if (rightButtons != null) {
279
             List<TitleBarButtonController> rightButtonControllers = getOrCreateButtonControllers(componentRightButtons.get(child), rightButtons);
281
             List<TitleBarButtonController> rightButtonControllers = getOrCreateButtonControllers(componentRightButtons.get(child), rightButtons);
280
             componentRightButtons.put(child, keyBy(rightButtonControllers, TitleBarButtonController::getButtonInstanceId));
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
         } else {
287
         } else {
288
+            currentRightButtons = null;
283
             topBar.clearRightButtons();
289
             topBar.clearRightButtons();
284
         }
290
         }
285
 
291
 
370
             if (previousLeftButtons != null) forEach(previousLeftButtons.values(), TitleBarButtonController::destroy);
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
         if (buttons.left != null) topBar.setLeftButtons(leftButtonControllers);
385
         if (buttons.left != null) topBar.setLeftButtons(leftButtonControllers);
375
         if (buttons.back.hasValue()) topBar.setBackButton(createButtonController(buttons.back));
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
 import com.reactnativenavigation.parse.AnimationOptions;
28
 import com.reactnativenavigation.parse.AnimationOptions;
29
 import com.reactnativenavigation.parse.params.Colour;
29
 import com.reactnativenavigation.parse.params.Colour;
30
 import com.reactnativenavigation.parse.params.Number;
30
 import com.reactnativenavigation.parse.params.Number;
31
-import com.reactnativenavigation.utils.CollectionUtils;
32
 import com.reactnativenavigation.utils.CompatUtils;
31
 import com.reactnativenavigation.utils.CompatUtils;
33
 import com.reactnativenavigation.utils.UiUtils;
32
 import com.reactnativenavigation.utils.UiUtils;
34
 import com.reactnativenavigation.viewcontrollers.TitleBarButtonController;
33
 import com.reactnativenavigation.viewcontrollers.TitleBarButtonController;
36
 import com.reactnativenavigation.views.titlebar.TitleBar;
35
 import com.reactnativenavigation.views.titlebar.TitleBar;
37
 import com.reactnativenavigation.views.toptabs.TopTabs;
36
 import com.reactnativenavigation.views.toptabs.TopTabs;
38
 
37
 
39
-import java.util.ArrayList;
40
 import java.util.Collections;
38
 import java.util.Collections;
41
 import java.util.List;
39
 import java.util.List;
42
 
40
 
53
     private View border;
51
     private View border;
54
     private View component;
52
     private View component;
55
     private float elevation = -1;
53
     private float elevation = -1;
56
-    private List<TitleBarButtonController> rightButtons = new ArrayList<>();
57
 
54
 
58
     public TopBar(final Context context, StackLayout parentView) {
55
     public TopBar(final Context context, StackLayout parentView) {
59
         super(context);
56
         super(context);
220
     }
217
     }
221
 
218
 
222
     public void setRightButtons(List<TitleBarButtonController> rightButtons) {
219
     public void setRightButtons(List<TitleBarButtonController> rightButtons) {
223
-        if (CollectionUtils.equals(this.rightButtons, rightButtons)) return;
224
-        this.rightButtons = rightButtons;
225
         titleBar.setRightButtons(rightButtons);
220
         titleBar.setRightButtons(rightButtons);
226
     }
221
     }
227
 
222
 

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

178
         verify(topBar, times(0)).setLeftButtons(any());
178
         verify(topBar, times(0)).setLeftButtons(any());
179
 
179
 
180
         Options options = new Options();
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
         uut.mergeChildOptions(options, EMPTY_OPTIONS, child);
184
         uut.mergeChildOptions(options, EMPTY_OPTIONS, child);
183
         verify(topBar, times(1)).setRightButtons(any());
185
         verify(topBar, times(1)).setRightButtons(any());
184
 
186