Browse Source

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 years ago
parent
commit
6ffb3011f7
No account linked to committer's email address

+ 19
- 17
lib/android/app/src/main/java/com/reactnativenavigation/presentation/BottomTabsPresenter.java View File

35
         this.defaultOptions = defaultOptions;
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
         this.bottomTabs = bottomTabs;
39
         this.bottomTabs = bottomTabs;
40
         this.tabSelector = tabSelector;
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
     public void applyOptions(Options options) {
48
     public void applyOptions(Options options) {
58
     }
58
     }
59
 
59
 
60
     public void mergeChildOptions(Options options, ViewController child) {
60
     public void mergeChildOptions(Options options, ViewController child) {
61
-        mergeBottomTabsOptions(options);
61
+        mergeBottomTabsOptions(options, child);
62
         int tabIndex = bottomTabFinder.findByControllerId(child.getId());
62
         int tabIndex = bottomTabFinder.findByControllerId(child.getId());
63
         if (tabIndex >= 0) mergeDrawBehind(tabIndex);
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
         BottomTabsOptions bottomTabsOptions = options.bottomTabsOptions;
67
         BottomTabsOptions bottomTabsOptions = options.bottomTabsOptions;
68
         AnimationsOptions animations = options.animations;
68
         AnimationsOptions animations = options.animations;
69
 
69
 
86
             int tabIndex = bottomTabFinder.findByControllerId(bottomTabsOptions.currentTabId.get());
86
             int tabIndex = bottomTabFinder.findByControllerId(bottomTabsOptions.currentTabId.get());
87
             if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
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 View File

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

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

3
 import android.app.Activity;
3
 import android.app.Activity;
4
 
4
 
5
 import com.reactnativenavigation.BaseTest;
5
 import com.reactnativenavigation.BaseTest;
6
+import com.reactnativenavigation.anim.BottomTabsAnimator;
6
 import com.reactnativenavigation.mocks.SimpleViewController;
7
 import com.reactnativenavigation.mocks.SimpleViewController;
7
 import com.reactnativenavigation.parse.Options;
8
 import com.reactnativenavigation.parse.Options;
8
 import com.reactnativenavigation.parse.params.Bool;
9
 import com.reactnativenavigation.parse.params.Bool;
17
 import java.util.Arrays;
18
 import java.util.Arrays;
18
 import java.util.List;
19
 import java.util.List;
19
 
20
 
21
+import static org.assertj.core.api.Java6Assertions.assertThat;
22
+import static org.mockito.ArgumentMatchers.any;
20
 import static org.mockito.Mockito.spy;
23
 import static org.mockito.Mockito.spy;
24
+import static org.mockito.Mockito.times;
21
 import static org.mockito.Mockito.verify;
25
 import static org.mockito.Mockito.verify;
22
 import static org.mockito.Mockito.verifyNoMoreInteractions;
26
 import static org.mockito.Mockito.verifyNoMoreInteractions;
23
 
27
 
25
     private List<ViewController> tabs;
29
     private List<ViewController> tabs;
26
     private BottomTabsPresenter uut;
30
     private BottomTabsPresenter uut;
27
     private BottomTabs bottomTabs;
31
     private BottomTabs bottomTabs;
32
+    private BottomTabsAnimator animator;
28
 
33
 
29
     @Override
34
     @Override
30
     public void beforeEach() {
35
     public void beforeEach() {
35
         tabs = Arrays.asList(child1, child2);
40
         tabs = Arrays.asList(child1, child2);
36
         uut = new BottomTabsPresenter(tabs, new Options());
41
         uut = new BottomTabsPresenter(tabs, new Options());
37
         bottomTabs = Mockito.mock(BottomTabs.class);
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
     @Test
47
     @Test
50
         verify(bottomTabs).setBackgroundColor(options.bottomTabsOptions.backgroundColor.get());
56
         verify(bottomTabs).setBackgroundColor(options.bottomTabsOptions.backgroundColor.get());
51
         verifyNoMoreInteractions(bottomTabs);
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
 }