forked from wix/react-native-navigation
/
BottomTabsPresenter.java
160 lines (143 loc) · 6.58 KB
/
BottomTabsPresenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.reactnativenavigation.presentation;
import android.graphics.Color;
import android.support.annotation.IntRange;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation.TitleState;
import com.reactnativenavigation.anim.BottomTabsAnimator;
import com.reactnativenavigation.parse.AnimationsOptions;
import com.reactnativenavigation.parse.BottomTabsOptions;
import com.reactnativenavigation.parse.Options;
import com.reactnativenavigation.viewcontrollers.ViewController;
import com.reactnativenavigation.viewcontrollers.bottomtabs.BottomTabFinder;
import com.reactnativenavigation.viewcontrollers.bottomtabs.TabSelector;
import com.reactnativenavigation.views.BottomTabs;
import com.reactnativenavigation.views.Component;
import java.util.List;
import static com.reactnativenavigation.utils.ViewUtils.getHeight;
public class BottomTabsPresenter {
private final BottomTabFinder bottomTabFinder;
private final List<ViewController> tabs;
private Options defaultOptions;
private BottomTabs bottomTabs;
private BottomTabsAnimator animator;
private TabSelector tabSelector;
public BottomTabsPresenter(List<ViewController> tabs, Options defaultOptions) {
this.tabs = tabs;
this.defaultOptions = defaultOptions;
this.bottomTabFinder = new BottomTabFinder(tabs);
}
public void setDefaultOptions(Options defaultOptions) {
this.defaultOptions = defaultOptions;
}
public void bindView(BottomTabs bottomTabs, TabSelector tabSelector) {
this.bottomTabs = bottomTabs;
this.tabSelector = tabSelector;
animator = new BottomTabsAnimator(bottomTabs);
}
public void applyLayoutParamsOptions(Options options, int tabIndex) {
Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
applyDrawBehind(withDefaultOptions.bottomTabsOptions, tabIndex);
}
public void mergeOptions(Options options) {
mergeBottomTabsOptions(options.bottomTabsOptions, options.animations);
}
public void applyOptions(Options options) {
Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
applyBottomTabsOptions(withDefaultOptions.bottomTabsOptions, withDefaultOptions.animations);
}
public void applyChildOptions(Options options, Component child) {
int tabIndex = bottomTabFinder.findByComponent(child);
if (tabIndex >= 0) {
Options withDefaultOptions = options.copy().withDefaultOptions(defaultOptions);
applyBottomTabsOptions(withDefaultOptions.bottomTabsOptions, withDefaultOptions.animations);
applyDrawBehind(withDefaultOptions.bottomTabsOptions, tabIndex);
}
}
public void mergeChildOptions(Options options, Component child) {
mergeBottomTabsOptions(options.bottomTabsOptions, options.animations);
int tabIndex = bottomTabFinder.findByComponent(child);
if (tabIndex >= 0) mergeDrawBehind(options.bottomTabsOptions, tabIndex);
}
private void mergeBottomTabsOptions(BottomTabsOptions options, AnimationsOptions animations) {
if (options.titleDisplayMode.hasValue()) {
bottomTabs.setTitleState(options.titleDisplayMode.toState());
}
if (options.backgroundColor.hasValue()) {
bottomTabs.setBackgroundColor(options.backgroundColor.get());
}
if (options.currentTabIndex.hasValue()) {
int tabIndex = options.currentTabIndex.get();
if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
}
if (options.testId.hasValue()) {
bottomTabs.setTag(options.testId.get());
}
if (options.currentTabId.hasValue()) {
int tabIndex = bottomTabFinder.findByControllerId(options.currentTabId.get());
if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
}
if (options.visible.isTrue()) {
if (options.animate.isTrueOrUndefined()) {
animator.show(animations);
} else {
bottomTabs.restoreBottomNavigation(false);
}
}
if (options.visible.isFalse()) {
if (options.animate.isTrueOrUndefined()) {
animator.hide(animations);
} else {
bottomTabs.hideBottomNavigation(false);
}
}
}
private void applyDrawBehind(BottomTabsOptions options, @IntRange(from = 0) int tabIndex) {
ViewGroup tab = tabs.get(tabIndex).getView();
MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
if (options.drawBehind.isTrue()) {
lp.bottomMargin = 0;
} else if (options.visible.isTrueOrUndefined()) {
lp.bottomMargin = getHeight(bottomTabs);
}
}
private void mergeDrawBehind(BottomTabsOptions options, int tabIndex) {
ViewGroup tab = tabs.get(tabIndex).getView();
MarginLayoutParams lp = (MarginLayoutParams) tab.getLayoutParams();
if (options.drawBehind.isTrue()) {
lp.bottomMargin = 0;
} else if (options.visible.isTrue() && options.drawBehind.isFalse()) {
lp.bottomMargin = getHeight(bottomTabs);
}
}
private void applyBottomTabsOptions(BottomTabsOptions options, AnimationsOptions animationsOptions) {
bottomTabs.setTitleState(options.titleDisplayMode.get(TitleState.SHOW_WHEN_ACTIVE));
bottomTabs.setBackgroundColor(options.backgroundColor.get(Color.WHITE));
if (options.currentTabIndex.hasValue()) {
int tabIndex = options.currentTabIndex.get();
if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
}
if (options.testId.hasValue()) bottomTabs.setTag(options.testId.get());
if (options.currentTabId.hasValue()) {
int tabIndex = bottomTabFinder.findByControllerId(options.currentTabId.get());
if (tabIndex >= 0) tabSelector.selectTab(tabIndex);
}
if (options.visible.isTrueOrUndefined()) {
if (options.animate.isTrueOrUndefined()) {
animator.show(animationsOptions);
} else {
bottomTabs.restoreBottomNavigation(false);
}
}
if (options.visible.isFalse()) {
if (options.animate.isTrueOrUndefined()) {
animator.hide(animationsOptions);
} else {
bottomTabs.hideBottomNavigation(false);
}
}
if (options.elevation.hasValue()) {
bottomTabs.setUseElevation(true, options.elevation.get().floatValue());
}
}
}