Просмотр исходного кода

Apply BottomTabs visibility only if child is visible (#5835)

When calling mergeOptions to update BottomTabs visibility, apply the visibility only if the child is visible.
Guy Carmeli 4 лет назад
Родитель
Сommit
6ffb3011f7
No account linked to committer's email address

+ 19
- 17
lib/android/app/src/main/java/com/reactnativenavigation/presentation/BottomTabsPresenter.java Просмотреть файл

@@ -35,14 +35,14 @@ public class BottomTabsPresenter {
35 35
         this.defaultOptions = defaultOptions;
36 36
     }
37 37
 
38
-    public void bindView(BottomTabs bottomTabs, TabSelector tabSelector) {
38
+    public void bindView(BottomTabs bottomTabs, TabSelector tabSelector, BottomTabsAnimator animator) {
39 39
         this.bottomTabs = bottomTabs;
40 40
         this.tabSelector = tabSelector;
41
-        animator = new BottomTabsAnimator(bottomTabs);
41
+        this.animator = animator;
42 42
     }
43 43
 
44
-    public void mergeOptions(Options options) {
45
-        mergeBottomTabsOptions(options);
44
+    public void mergeOptions(Options options, ViewController view) {
45
+        mergeBottomTabsOptions(options, view);
46 46
     }
47 47
 
48 48
     public void applyOptions(Options options) {
@@ -58,12 +58,12 @@ public class BottomTabsPresenter {
58 58
     }
59 59
 
60 60
     public void mergeChildOptions(Options options, ViewController child) {
61
-        mergeBottomTabsOptions(options);
61
+        mergeBottomTabsOptions(options, child);
62 62
         int tabIndex = bottomTabFinder.findByControllerId(child.getId());
63 63
         if (tabIndex >= 0) mergeDrawBehind(tabIndex);
64 64
     }
65 65
 
66
-    private void mergeBottomTabsOptions(Options options) {
66
+    private void mergeBottomTabsOptions(Options options, ViewController view) {
67 67
         BottomTabsOptions bottomTabsOptions = options.bottomTabsOptions;
68 68
         AnimationsOptions animations = options.animations;
69 69
 
@@ -86,18 +86,20 @@ public class BottomTabsPresenter {
86 86
             int tabIndex = bottomTabFinder.findByControllerId(bottomTabsOptions.currentTabId.get());
87 87
             if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
88 88
         }
89
-        if (bottomTabsOptions.visible.isTrue()) {
90
-            if (bottomTabsOptions.animate.isTrueOrUndefined()) {
91
-                animator.show(animations);
92
-            } else {
93
-                bottomTabs.restoreBottomNavigation(false);
89
+        if (view.isViewShown()) {
90
+            if (bottomTabsOptions.visible.isTrue()) {
91
+                if (bottomTabsOptions.animate.isTrueOrUndefined()) {
92
+                    animator.show(animations);
93
+                } else {
94
+                    bottomTabs.restoreBottomNavigation(false);
95
+                }
94 96
             }
95
-        }
96
-        if (bottomTabsOptions.visible.isFalse()) {
97
-            if (bottomTabsOptions.animate.isTrueOrUndefined()) {
98
-                animator.hide(animations);
99
-            } else {
100
-                bottomTabs.hideBottomNavigation(false);
97
+            if (bottomTabsOptions.visible.isFalse()) {
98
+                if (bottomTabsOptions.animate.isTrueOrUndefined()) {
99
+                    animator.hide(animations);
100
+                } else {
101
+                    bottomTabs.hideBottomNavigation(false);
102
+                }
101 103
             }
102 104
         }
103 105
     }

+ 3
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsController.java Просмотреть файл

@@ -7,6 +7,7 @@ import android.view.ViewGroup;
7 7
 
8 8
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
9 9
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
10
+import com.reactnativenavigation.anim.BottomTabsAnimator;
10 11
 import com.reactnativenavigation.parse.BottomTabOptions;
11 12
 import com.reactnativenavigation.parse.Options;
12 13
 import com.reactnativenavigation.presentation.BottomTabPresenter;
@@ -68,7 +69,7 @@ public class BottomTabsController extends ParentController<BottomTabsLayout> imp
68 69
 
69 70
         bottomTabs = createBottomTabs();
70 71
         tabsAttacher.init(root, resolveCurrentOptions());
71
-        presenter.bindView(bottomTabs, this);
72
+        presenter.bindView(bottomTabs, this, new BottomTabsAnimator(bottomTabs));
72 73
         tabPresenter.bindView(bottomTabs);
73 74
         bottomTabs.setOnTabSelectedListener(this);
74 75
         CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
@@ -98,7 +99,7 @@ public class BottomTabsController extends ParentController<BottomTabsLayout> imp
98 99
 
99 100
     @Override
100 101
     public void mergeOptions(Options options) {
101
-        presenter.mergeOptions(options);
102
+        presenter.mergeOptions(options, this);
102 103
         tabPresenter.mergeOptions(options);
103 104
         super.mergeOptions(options);
104 105
         this.options.bottomTabsOptions.clearOneTimeOptions();

+ 22
- 1
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/BottomTabsPresenterTest.java Просмотреть файл

@@ -3,6 +3,7 @@ package com.reactnativenavigation.viewcontrollers;
3 3
 import android.app.Activity;
4 4
 
5 5
 import com.reactnativenavigation.BaseTest;
6
+import com.reactnativenavigation.anim.BottomTabsAnimator;
6 7
 import com.reactnativenavigation.mocks.SimpleViewController;
7 8
 import com.reactnativenavigation.parse.Options;
8 9
 import com.reactnativenavigation.parse.params.Bool;
@@ -17,7 +18,10 @@ import org.mockito.Mockito;
17 18
 import java.util.Arrays;
18 19
 import java.util.List;
19 20
 
21
+import static org.assertj.core.api.Java6Assertions.assertThat;
22
+import static org.mockito.ArgumentMatchers.any;
20 23
 import static org.mockito.Mockito.spy;
24
+import static org.mockito.Mockito.times;
21 25
 import static org.mockito.Mockito.verify;
22 26
 import static org.mockito.Mockito.verifyNoMoreInteractions;
23 27
 
@@ -25,6 +29,7 @@ public class BottomTabsPresenterTest extends BaseTest {
25 29
     private List<ViewController> tabs;
26 30
     private BottomTabsPresenter uut;
27 31
     private BottomTabs bottomTabs;
32
+    private BottomTabsAnimator animator;
28 33
 
29 34
     @Override
30 35
     public void beforeEach() {
@@ -35,7 +40,8 @@ public class BottomTabsPresenterTest extends BaseTest {
35 40
         tabs = Arrays.asList(child1, child2);
36 41
         uut = new BottomTabsPresenter(tabs, new Options());
37 42
         bottomTabs = Mockito.mock(BottomTabs.class);
38
-        uut.bindView(bottomTabs, Mockito.mock(TabSelector.class));
43
+        animator = spy(new BottomTabsAnimator(bottomTabs));
44
+        uut.bindView(bottomTabs, Mockito.mock(TabSelector.class), animator);
39 45
     }
40 46
 
41 47
     @Test
@@ -50,4 +56,19 @@ public class BottomTabsPresenterTest extends BaseTest {
50 56
         verify(bottomTabs).setBackgroundColor(options.bottomTabsOptions.backgroundColor.get());
51 57
         verifyNoMoreInteractions(bottomTabs);
52 58
     }
59
+
60
+    @Test
61
+    public void mergeChildOptions_visibilityIsAppliedOnlyIsChildIsShown() {
62
+        assertThat(tabs.get(0).isViewShown()).isFalse();
63
+        assertThat(bottomTabs.isHidden()).isFalse();
64
+
65
+        Options options = new Options();
66
+        options.bottomTabsOptions.visible = new Bool(false);
67
+        uut.mergeChildOptions(options, tabs.get(0));
68
+        verify(animator, times(0)).hide(any());
69
+
70
+        Mockito.when(tabs.get(0).isViewShown()).thenAnswer(ignored -> true);
71
+        uut.mergeChildOptions(options, tabs.get(0));
72
+        verify(animator).hide(any());
73
+    }
53 74
 }