Browse Source

Implement BottomTabs mergeChildOptions

Instead of merging child options, the presenter applied all options :(
Guy Carmeli 6 years ago
parent
commit
0852ea880c

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/TitleDisplayMode.java View File

32
         return state != null;
32
         return state != null;
33
     }
33
     }
34
 
34
 
35
+    public TitleState get(@NonNull TitleState defaultValue) {
36
+        return state == null ? defaultValue : state;
37
+    }
38
+
35
     @NonNull
39
     @NonNull
36
     public TitleState toState() {
40
     public TitleState toState() {
37
         if (state == null) throw new RuntimeException("TitleDisplayMode is undefined");
41
         if (state == null) throw new RuntimeException("TitleDisplayMode is undefined");

+ 61
- 9
lib/android/app/src/main/java/com/reactnativenavigation/presentation/BottomTabsOptionsPresenter.java View File

1
 package com.reactnativenavigation.presentation;
1
 package com.reactnativenavigation.presentation;
2
 
2
 
3
+import android.graphics.Color;
3
 import android.view.ViewGroup;
4
 import android.view.ViewGroup;
4
 import android.view.ViewGroup.MarginLayoutParams;
5
 import android.view.ViewGroup.MarginLayoutParams;
5
 
6
 
7
+import com.aurelhubert.ahbottomnavigation.AHBottomNavigation.TitleState;
6
 import com.reactnativenavigation.anim.BottomTabsAnimator;
8
 import com.reactnativenavigation.anim.BottomTabsAnimator;
7
 import com.reactnativenavigation.parse.AnimationsOptions;
9
 import com.reactnativenavigation.parse.AnimationsOptions;
8
 import com.reactnativenavigation.parse.BottomTabsOptions;
10
 import com.reactnativenavigation.parse.BottomTabsOptions;
50
         applyBottomTabsOptions(withDefaultOptions.bottomTabsOptions, withDefaultOptions.animations);
52
         applyBottomTabsOptions(withDefaultOptions.bottomTabsOptions, withDefaultOptions.animations);
51
     }
53
     }
52
 
54
 
53
-    public void presentChildOptions(Options options, Component child) {
55
+    public void applyChildOptions(Options options, Component child) {
54
         Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
56
         Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
55
         applyBottomTabsOptions(withDefaultOptions.bottomTabsOptions, withDefaultOptions.animations);
57
         applyBottomTabsOptions(withDefaultOptions.bottomTabsOptions, withDefaultOptions.animations);
56
         int tabIndex = bottomTabFinder.findByComponent(child);
58
         int tabIndex = bottomTabFinder.findByComponent(child);
57
         applyDrawBehind(withDefaultOptions.bottomTabsOptions, tabIndex);
59
         applyDrawBehind(withDefaultOptions.bottomTabsOptions, tabIndex);
58
     }
60
     }
59
 
61
 
62
+    public void mergeChildOptions(Options options, Component child) {
63
+        Options withDefault = options.copy().withDefaultOptions(defaultOptions);
64
+        mergeBottomTabsOptions(withDefault.bottomTabsOptions, withDefault.animations);
65
+        int tabIndex = bottomTabFinder.findByComponent(child);
66
+        mergeDrawBehind(withDefault.bottomTabsOptions, tabIndex);
67
+    }
68
+
69
+    private void mergeBottomTabsOptions(BottomTabsOptions options, AnimationsOptions animations) {
70
+        if (options.titleDisplayMode.hasValue()) {
71
+            bottomTabs.setTitleState(options.titleDisplayMode.toState());
72
+        }
73
+        if (options.backgroundColor.hasValue()) {
74
+            bottomTabs.setBackgroundColor(options.backgroundColor.get());
75
+        }
76
+        if (options.currentTabIndex.hasValue()) {
77
+            int tabIndex = options.currentTabIndex.get();
78
+            if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
79
+        }
80
+        if (options.testId.hasValue()) {
81
+            bottomTabs.setTag(options.testId.get());
82
+        }
83
+        if (options.currentTabId.hasValue()) {
84
+            int tabIndex = bottomTabFinder.findByControllerId(options.currentTabId.get());
85
+            if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
86
+        }
87
+        if (options.visible.isTrueOrUndefined()) {
88
+            if (options.animate.isTrueOrUndefined()) {
89
+                animator.show(animations);
90
+            } else {
91
+                bottomTabs.restoreBottomNavigation(false);
92
+            }
93
+        }
94
+        if (options.visible.isFalse()) {
95
+            if (options.animate.isTrueOrUndefined()) {
96
+                animator.hide(animations);
97
+            } else {
98
+                bottomTabs.hideBottomNavigation(false);
99
+            }
100
+        }
101
+    }
102
+
60
     private void applyDrawBehind(BottomTabsOptions options, int tabIndex) {
103
     private void applyDrawBehind(BottomTabsOptions options, int tabIndex) {
61
         ViewGroup tab = tabs.get(tabIndex).getView();
104
         ViewGroup tab = tabs.get(tabIndex).getView();
62
         MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
105
         MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
72
         }
115
         }
73
     }
116
     }
74
 
117
 
75
-    private void applyBottomTabsOptions(BottomTabsOptions options, AnimationsOptions animationsOptions) {
76
-        if (options.titleDisplayMode.hasValue()) {
77
-            bottomTabs.setTitleState(options.titleDisplayMode.toState());
118
+    private void mergeDrawBehind(BottomTabsOptions options, int tabIndex) {
119
+        ViewGroup tab = tabs.get(tabIndex).getView();
120
+        MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
121
+        if (options.drawBehind.isTrue()) {
122
+            lp.bottomMargin = 0;
78
         }
123
         }
79
-        if (options.backgroundColor.hasValue()) {
80
-            bottomTabs.setBackgroundColor(options.backgroundColor.get());
124
+        if (options.visible.isTrue() && options.drawBehind.isFalse()) {
125
+            if (bottomTabs.getHeight() == 0) {
126
+                UiUtils.runOnPreDrawOnce(bottomTabs, () -> lp.bottomMargin = bottomTabs.getHeight());
127
+            } else {
128
+                lp.bottomMargin = bottomTabs.getHeight();
129
+            }
81
         }
130
         }
131
+    }
132
+
133
+    private void applyBottomTabsOptions(BottomTabsOptions options, AnimationsOptions animationsOptions) {
134
+        bottomTabs.setTitleState(options.titleDisplayMode.get(TitleState.SHOW_WHEN_ACTIVE));
135
+        bottomTabs.setBackgroundColor(options.backgroundColor.get(Color.WHITE));
82
         if (options.currentTabIndex.hasValue()) {
136
         if (options.currentTabIndex.hasValue()) {
83
             int tabIndex = options.currentTabIndex.get();
137
             int tabIndex = options.currentTabIndex.get();
84
             if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
138
             if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
85
         }
139
         }
86
-        if (options.testId.hasValue()) {
87
-            bottomTabs.setTag(options.testId.get());
88
-        }
140
+        if (options.testId.hasValue()) bottomTabs.setTag(options.testId.get());
89
         if (options.currentTabId.hasValue()) {
141
         if (options.currentTabId.hasValue()) {
90
             int tabIndex = bottomTabFinder.findByControllerId(options.currentTabId.get());
142
             int tabIndex = bottomTabFinder.findByControllerId(options.currentTabId.get());
91
             if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
143
             if (tabIndex >= 0) tabSelector.selectTab(tabIndex);

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

83
     @Override
83
     @Override
84
     public void applyChildOptions(Options options, Component child) {
84
     public void applyChildOptions(Options options, Component child) {
85
         super.applyChildOptions(options, child);
85
         super.applyChildOptions(options, child);
86
-        presenter.presentChildOptions(this.options, child);
86
+        presenter.applyChildOptions(this.options, child);
87
         applyOnParentController(parentController ->
87
         applyOnParentController(parentController ->
88
                 ((ParentController) parentController).applyChildOptions(this.options.copy().clearBottomTabsOptions().clearBottomTabOptions(), child)
88
                 ((ParentController) parentController).applyChildOptions(this.options.copy().clearBottomTabsOptions().clearBottomTabOptions(), child)
89
         );
89
         );
92
     @Override
92
     @Override
93
     public void mergeChildOptions(Options options, Component child) {
93
     public void mergeChildOptions(Options options, Component child) {
94
         super.mergeChildOptions(options, child);
94
         super.mergeChildOptions(options, child);
95
-        presenter.presentChildOptions(options, child);
95
+        presenter.mergeChildOptions(options, child);
96
         tabPresenter.mergeChildOptions(options, child);
96
         tabPresenter.mergeChildOptions(options, child);
97
         applyOnParentController(parentController ->
97
         applyOnParentController(parentController ->
98
                 ((ParentController) parentController).mergeChildOptions(options.copy().clearBottomTabsOptions(), child)
98
                 ((ParentController) parentController).mergeChildOptions(options.copy().clearBottomTabsOptions(), child)