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

Implement BottomTabs mergeChildOptions

Instead of merging child options, the presenter applied all options :(
Guy Carmeli 6 лет назад
Родитель
Сommit
0852ea880c

+ 4
- 0
lib/android/app/src/main/java/com/reactnativenavigation/parse/params/TitleDisplayMode.java Просмотреть файл

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

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

@@ -1,8 +1,10 @@
1 1
 package com.reactnativenavigation.presentation;
2 2
 
3
+import android.graphics.Color;
3 4
 import android.view.ViewGroup;
4 5
 import android.view.ViewGroup.MarginLayoutParams;
5 6
 
7
+import com.aurelhubert.ahbottomnavigation.AHBottomNavigation.TitleState;
6 8
 import com.reactnativenavigation.anim.BottomTabsAnimator;
7 9
 import com.reactnativenavigation.parse.AnimationsOptions;
8 10
 import com.reactnativenavigation.parse.BottomTabsOptions;
@@ -50,13 +52,54 @@ public class BottomTabsOptionsPresenter {
50 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 56
         Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
55 57
         applyBottomTabsOptions(withDefaultOptions.bottomTabsOptions, withDefaultOptions.animations);
56 58
         int tabIndex = bottomTabFinder.findByComponent(child);
57 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 103
     private void applyDrawBehind(BottomTabsOptions options, int tabIndex) {
61 104
         ViewGroup tab = tabs.get(tabIndex).getView();
62 105
         MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
@@ -72,20 +115,29 @@ public class BottomTabsOptionsPresenter {
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 136
         if (options.currentTabIndex.hasValue()) {
83 137
             int tabIndex = options.currentTabIndex.get();
84 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 141
         if (options.currentTabId.hasValue()) {
90 142
             int tabIndex = bottomTabFinder.findByControllerId(options.currentTabId.get());
91 143
             if (tabIndex >= 0) tabSelector.selectTab(tabIndex);

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

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