Browse Source

Fix updating button options with mergeOptions (#6219)

Updating button options with mergeOptions stopped working in 6.5.0. The regression was introduced by #6090. When updating buttons, RNN didn't take button options into account when checking if two buttons are equal, instead it checked only by button id.

closes #6205

Co-authored-by: Yogev Ben David <yogev132@gmail.com>
Guy Carmeli 4 years ago
parent
commit
b2df65a5fb
No account linked to committer's email address

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

306
         return new ArrayList<>(result.values());
306
         return new ArrayList<>(result.values());
307
     }
307
     }
308
 
308
 
309
-    private List<TitleBarButtonController> getOrCreateButtonControllersById(@Nullable Map<String, TitleBarButtonController> currentButtons,@NonNull List<Button> buttons) {
309
+    private List<TitleBarButtonController> getOrCreateButtonControllers(@Nullable Map<String, TitleBarButtonController> currentButtons, @NonNull List<Button> buttons) {
310
         ArrayList result = new ArrayList<TitleBarButtonController>();
310
         ArrayList result = new ArrayList<TitleBarButtonController>();
311
         for (Button b : buttons) {
311
         for (Button b : buttons) {
312
-            result.add(take(first(perform(currentButtons, null, Map::values), button -> button.getId().equals(b.id)), createButtonController(b)));
312
+            result.add(take(first(perform(currentButtons, null, Map::values), button -> button.getButton().equals(b)), createButtonController(b)));
313
         }
313
         }
314
         return result;
314
         return result;
315
     }
315
     }
369
     private void mergeRightButtons(TopBarOptions options, TopBarButtons buttons, View child) {
369
     private void mergeRightButtons(TopBarOptions options, TopBarButtons buttons, View child) {
370
         if (buttons.right == null) return;
370
         if (buttons.right == null) return;
371
         List<Button> rightButtons = mergeButtonsWithColor(buttons.right, options.rightButtonColor, options.rightButtonDisabledColor);
371
         List<Button> rightButtons = mergeButtonsWithColor(buttons.right, options.rightButtonColor, options.rightButtonDisabledColor);
372
-        List<TitleBarButtonController> toMerge = getOrCreateButtonControllersById(componentRightButtons.get(child), rightButtons);
372
+        List<TitleBarButtonController> toMerge = getOrCreateButtonControllers(componentRightButtons.get(child), rightButtons);
373
         List<TitleBarButtonController> toRemove = difference(currentRightButtons, toMerge, TitleBarButtonController::equals);
373
         List<TitleBarButtonController> toRemove = difference(currentRightButtons, toMerge, TitleBarButtonController::equals);
374
         forEach(toRemove, TitleBarButtonController::destroy);
374
         forEach(toRemove, TitleBarButtonController::destroy);
375
 
375
 
383
     private void mergeLeftButton(TopBarOptions options, TopBarButtons buttons, View child) {
383
     private void mergeLeftButton(TopBarOptions options, TopBarButtons buttons, View child) {
384
         if (buttons.left == null) return;
384
         if (buttons.left == null) return;
385
         List<Button> leftButtons = mergeButtonsWithColor(buttons.left, options.leftButtonColor, options.leftButtonDisabledColor);
385
         List<Button> leftButtons = mergeButtonsWithColor(buttons.left, options.leftButtonColor, options.leftButtonDisabledColor);
386
-        List<TitleBarButtonController> toMerge = getOrCreateButtonControllersById(componentLeftButtons.get(child), leftButtons);
386
+        List<TitleBarButtonController> toMerge = getOrCreateButtonControllers(componentLeftButtons.get(child), leftButtons);
387
         componentLeftButtons.put(child, keyBy(toMerge, TitleBarButtonController::getButtonInstanceId));
387
         componentLeftButtons.put(child, keyBy(toMerge, TitleBarButtonController::getButtonInstanceId));
388
         topBarController.setLeftButtons(toMerge);
388
         topBarController.setLeftButtons(toMerge);
389
     }
389
     }

+ 0
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/TitleBarButtonController.java View File

29
 
29
 
30
 import androidx.annotation.NonNull;
30
 import androidx.annotation.NonNull;
31
 import androidx.annotation.Nullable;
31
 import androidx.annotation.Nullable;
32
-import androidx.annotation.RestrictTo;
33
 import androidx.appcompat.widget.ActionMenuView;
32
 import androidx.appcompat.widget.ActionMenuView;
34
 import androidx.appcompat.widget.Toolbar;
33
 import androidx.appcompat.widget.Toolbar;
35
 import androidx.core.view.MenuItemCompat;
34
 import androidx.core.view.MenuItemCompat;
48
     private TitleBarButtonController.OnClickListener onPressListener;
47
     private TitleBarButtonController.OnClickListener onPressListener;
49
     private Drawable icon;
48
     private Drawable icon;
50
 
49
 
51
-    @RestrictTo(RestrictTo.Scope.TESTS)
52
     public Button getButton() {
50
     public Button getButton() {
53
         return button;
51
         return button;
54
     }
52
     }

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

2
 
2
 
3
 import android.app.Activity;
3
 import android.app.Activity;
4
 import android.content.Context;
4
 import android.content.Context;
5
+import android.graphics.Color;
5
 import android.graphics.Typeface;
6
 import android.graphics.Typeface;
6
 import android.view.Gravity;
7
 import android.view.Gravity;
7
 import android.view.View;
8
 import android.view.View;
12
 import com.reactnativenavigation.mocks.ImageLoaderMock;
13
 import com.reactnativenavigation.mocks.ImageLoaderMock;
13
 import com.reactnativenavigation.mocks.Mocks;
14
 import com.reactnativenavigation.mocks.Mocks;
14
 import com.reactnativenavigation.mocks.SimpleViewController;
15
 import com.reactnativenavigation.mocks.SimpleViewController;
16
+import com.reactnativenavigation.mocks.TitleBarButtonCreatorMock;
15
 import com.reactnativenavigation.mocks.TitleBarReactViewCreatorMock;
17
 import com.reactnativenavigation.mocks.TitleBarReactViewCreatorMock;
16
 import com.reactnativenavigation.mocks.TopBarBackgroundViewCreatorMock;
18
 import com.reactnativenavigation.mocks.TopBarBackgroundViewCreatorMock;
17
-import com.reactnativenavigation.mocks.TitleBarButtonCreatorMock;
18
 import com.reactnativenavigation.parse.Alignment;
19
 import com.reactnativenavigation.parse.Alignment;
19
 import com.reactnativenavigation.parse.Component;
20
 import com.reactnativenavigation.parse.Component;
20
 import com.reactnativenavigation.parse.Options;
21
 import com.reactnativenavigation.parse.Options;
244
     @Test
245
     @Test
245
     public void mergeRightButtons_buttonsAreCreatedOnlyIfNeeded() {
246
     public void mergeRightButtons_buttonsAreCreatedOnlyIfNeeded() {
246
         Options toApply = new Options();
247
         Options toApply = new Options();
248
+        textBtn1.color = new Colour(Color.GREEN);
247
         toApply.topBar.buttons.right = new ArrayList<>(asList(textBtn1, componentBtn1));
249
         toApply.topBar.buttons.right = new ArrayList<>(asList(textBtn1, componentBtn1));
248
         uut.applyChildOptions(toApply, parent, child);
250
         uut.applyChildOptions(toApply, parent, child);
249
 
251
 
254
 
256
 
255
         Options toMerge = new Options();
257
         Options toMerge = new Options();
256
         toMerge.topBar.buttons.right = new ArrayList(requireNonNull(map(toApply.topBar.buttons.right, Button::copy)));
258
         toMerge.topBar.buttons.right = new ArrayList(requireNonNull(map(toApply.topBar.buttons.right, Button::copy)));
259
+        toMerge.topBar.buttons.right.get(0).color = new Colour(Color.RED);
257
         toMerge.topBar.buttons.right.add(1, componentBtn2);
260
         toMerge.topBar.buttons.right.add(1, componentBtn2);
258
         uut.mergeChildOptions(toMerge, Options.EMPTY, parent, child);
261
         uut.mergeChildOptions(toMerge, Options.EMPTY, parent, child);
259
 
262
 
262
         verify(topBarController).mergeRightButtons(captor2.capture(), any());
265
         verify(topBarController).mergeRightButtons(captor2.capture(), any());
263
         List<TitleBarButtonController> mergedButtons = captor2.getValue();
266
         List<TitleBarButtonController> mergedButtons = captor2.getValue();
264
         assertThat(mergedButtons).hasSize(3);
267
         assertThat(mergedButtons).hasSize(3);
265
-        assertThat(appliedButtons.get(0)).isEqualTo(mergedButtons.get(0));
268
+        assertThat(appliedButtons.get(0)).isNotEqualTo(mergedButtons.get(0));
266
         assertThat(appliedButtons.get(1)).isEqualTo(mergedButtons.get(2));
269
         assertThat(appliedButtons.get(1)).isEqualTo(mergedButtons.get(2));
267
     }
270
     }
268
 
271