|
@@ -2,179 +2,216 @@
|
2
|
2
|
|
3
|
3
|
import * as React from 'react';
|
4
|
4
|
import {withTheme} from 'react-native-paper';
|
5
|
|
-import TabIcon from "./TabIcon";
|
6
|
|
-import TabHomeIcon from "./TabHomeIcon";
|
7
|
|
-import {Animated} from 'react-native';
|
8
|
|
-import {Collapsible} from "react-navigation-collapsible";
|
9
|
|
-
|
10
|
|
-type Props = {
|
11
|
|
- state: Object,
|
12
|
|
- descriptors: Object,
|
13
|
|
- navigation: Object,
|
14
|
|
- theme: Object,
|
15
|
|
- collapsibleStack: Object,
|
16
|
|
-}
|
17
|
|
-
|
18
|
|
-type State = {
|
19
|
|
- translateY: AnimatedValue,
|
20
|
|
- barSynced: boolean,
|
21
|
|
-}
|
|
5
|
+import Animated from 'react-native-reanimated';
|
|
6
|
+import {Collapsible} from 'react-navigation-collapsible';
|
|
7
|
+import {StackNavigationProp} from '@react-navigation/stack';
|
|
8
|
+import TabIcon from './TabIcon';
|
|
9
|
+import TabHomeIcon from './TabHomeIcon';
|
|
10
|
+import type {CustomTheme} from '../../managers/ThemeManager';
|
|
11
|
+
|
|
12
|
+type RouteType = {
|
|
13
|
+ name: string,
|
|
14
|
+ key: string,
|
|
15
|
+ params: {collapsible: Collapsible},
|
|
16
|
+ state: {
|
|
17
|
+ index: number,
|
|
18
|
+ routes: Array<RouteType>,
|
|
19
|
+ },
|
|
20
|
+};
|
22
|
21
|
|
23
|
|
-const TAB_ICONS = {
|
24
|
|
- proxiwash: 'tshirt-crew',
|
25
|
|
- services: 'account-circle',
|
26
|
|
- planning: 'calendar-range',
|
27
|
|
- planex: 'clock',
|
|
22
|
+type PropsType = {
|
|
23
|
+ state: {
|
|
24
|
+ index: number,
|
|
25
|
+ routes: Array<RouteType>,
|
|
26
|
+ },
|
|
27
|
+ descriptors: {
|
|
28
|
+ [key: string]: {
|
|
29
|
+ options: {
|
|
30
|
+ tabBarLabel: string,
|
|
31
|
+ title: string,
|
|
32
|
+ },
|
|
33
|
+ },
|
|
34
|
+ },
|
|
35
|
+ navigation: StackNavigationProp,
|
|
36
|
+ theme: CustomTheme,
|
28
|
37
|
};
|
29
|
38
|
|
30
|
|
-class CustomTabBar extends React.Component<Props, State> {
|
|
39
|
+type StateType = {
|
|
40
|
+ // eslint-disable-next-line flowtype/no-weak-types
|
|
41
|
+ translateY: any,
|
|
42
|
+};
|
31
|
43
|
|
32
|
|
- static TAB_BAR_HEIGHT = 48;
|
|
44
|
+const TAB_ICONS = {
|
|
45
|
+ proxiwash: 'tshirt-crew',
|
|
46
|
+ services: 'account-circle',
|
|
47
|
+ planning: 'calendar-range',
|
|
48
|
+ planex: 'clock',
|
|
49
|
+};
|
33
|
50
|
|
34
|
|
- state = {
|
35
|
|
- translateY: new Animated.Value(0),
|
36
|
|
- }
|
|
51
|
+class CustomTabBar extends React.Component<PropsType, StateType> {
|
|
52
|
+ static TAB_BAR_HEIGHT = 48;
|
37
|
53
|
|
38
|
|
- syncTabBar = (route, index) => {
|
39
|
|
- const state = this.props.state;
|
40
|
|
- const isFocused = state.index === index;
|
41
|
|
- if (isFocused) {
|
42
|
|
- const stackState = route.state;
|
43
|
|
- const stackRoute = stackState ? stackState.routes[stackState.index] : undefined;
|
44
|
|
- const params: { collapsible: Collapsible } = stackRoute ? stackRoute.params : undefined;
|
45
|
|
- const collapsible = params ? params.collapsible : undefined;
|
46
|
|
- if (collapsible) {
|
47
|
|
- this.setState({
|
48
|
|
- translateY: Animated.multiply(-1.5, collapsible.translateY), // Hide tab bar faster than header bar
|
49
|
|
- });
|
50
|
|
- }
|
51
|
|
- }
|
|
54
|
+ constructor() {
|
|
55
|
+ super();
|
|
56
|
+ this.state = {
|
|
57
|
+ translateY: new Animated.Value(0),
|
52
|
58
|
};
|
53
|
|
-
|
54
|
|
- /**
|
55
|
|
- * Navigates to the given route if it is different from the current one
|
56
|
|
- *
|
57
|
|
- * @param route Destination route
|
58
|
|
- * @param currentIndex The current route index
|
59
|
|
- * @param destIndex The destination route index
|
60
|
|
- */
|
61
|
|
- onItemPress(route: Object, currentIndex: number, destIndex: number) {
|
62
|
|
- const event = this.props.navigation.emit({
|
63
|
|
- type: 'tabPress',
|
64
|
|
- target: route.key,
|
65
|
|
- canPreventDefault: true,
|
66
|
|
- });
|
67
|
|
- if (currentIndex !== destIndex && !event.defaultPrevented)
|
68
|
|
- this.props.navigation.navigate(route.name);
|
69
|
|
- }
|
70
|
|
-
|
71
|
|
- /**
|
72
|
|
- * Navigates to tetris screen on home button long press
|
73
|
|
- *
|
74
|
|
- * @param route
|
75
|
|
- */
|
76
|
|
- onItemLongPress(route: Object) {
|
77
|
|
- const event = this.props.navigation.emit({
|
78
|
|
- type: 'tabLongPress',
|
79
|
|
- target: route.key,
|
80
|
|
- canPreventDefault: true,
|
81
|
|
- });
|
82
|
|
- if (route.name === "home" && !event.defaultPrevented)
|
83
|
|
- this.props.navigation.navigate('game-start');
|
84
|
|
- }
|
85
|
|
-
|
86
|
|
- /**
|
87
|
|
- * Gets an icon for the given route if it is not the home one as it uses a custom button
|
88
|
|
- *
|
89
|
|
- * @param route
|
90
|
|
- * @param focused
|
91
|
|
- * @returns {null}
|
92
|
|
- */
|
93
|
|
- tabBarIcon = (route, focused) => {
|
94
|
|
- let icon = TAB_ICONS[route.name];
|
95
|
|
- icon = focused ? icon : icon + ('-outline');
|
96
|
|
- if (route.name !== "home")
|
97
|
|
- return icon;
|
98
|
|
- else
|
99
|
|
- return null;
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ /**
|
|
62
|
+ * Navigates to the given route if it is different from the current one
|
|
63
|
+ *
|
|
64
|
+ * @param route Destination route
|
|
65
|
+ * @param currentIndex The current route index
|
|
66
|
+ * @param destIndex The destination route index
|
|
67
|
+ */
|
|
68
|
+ onItemPress(route: RouteType, currentIndex: number, destIndex: number) {
|
|
69
|
+ const {navigation} = this.props;
|
|
70
|
+ const event = navigation.emit({
|
|
71
|
+ type: 'tabPress',
|
|
72
|
+ target: route.key,
|
|
73
|
+ canPreventDefault: true,
|
|
74
|
+ });
|
|
75
|
+ if (currentIndex !== destIndex && !event.defaultPrevented)
|
|
76
|
+ navigation.navigate(route.name);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ /**
|
|
80
|
+ * Navigates to tetris screen on home button long press
|
|
81
|
+ *
|
|
82
|
+ * @param route
|
|
83
|
+ */
|
|
84
|
+ onItemLongPress(route: RouteType) {
|
|
85
|
+ const {navigation} = this.props;
|
|
86
|
+ const event = navigation.emit({
|
|
87
|
+ type: 'tabLongPress',
|
|
88
|
+ target: route.key,
|
|
89
|
+ canPreventDefault: true,
|
|
90
|
+ });
|
|
91
|
+ if (route.name === 'home' && !event.defaultPrevented)
|
|
92
|
+ navigation.navigate('game-start');
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ /**
|
|
96
|
+ * Finds the active route and syncs the tab bar animation with the header bar
|
|
97
|
+ */
|
|
98
|
+ onRouteChange = () => {
|
|
99
|
+ const {props} = this;
|
|
100
|
+ props.state.routes.map(this.syncTabBar);
|
|
101
|
+ };
|
|
102
|
+
|
|
103
|
+ /**
|
|
104
|
+ * Gets an icon for the given route if it is not the home one as it uses a custom button
|
|
105
|
+ *
|
|
106
|
+ * @param route
|
|
107
|
+ * @param focused
|
|
108
|
+ * @returns {null}
|
|
109
|
+ */
|
|
110
|
+ getTabBarIcon = (route: RouteType, focused: boolean): React.Node => {
|
|
111
|
+ let icon = TAB_ICONS[route.name];
|
|
112
|
+ icon = focused ? icon : `${icon}-outline`;
|
|
113
|
+ if (route.name !== 'home') return icon;
|
|
114
|
+ return null;
|
|
115
|
+ };
|
|
116
|
+
|
|
117
|
+ /**
|
|
118
|
+ * Gets a tab icon render.
|
|
119
|
+ * If the given route is focused, it syncs the tab bar and header bar animations together
|
|
120
|
+ *
|
|
121
|
+ * @param route The route for the icon
|
|
122
|
+ * @param index The index of the current route
|
|
123
|
+ * @returns {*}
|
|
124
|
+ */
|
|
125
|
+ getRenderIcon = (route: RouteType, index: number): React.Node => {
|
|
126
|
+ const {props} = this;
|
|
127
|
+ const {state} = props;
|
|
128
|
+ const {options} = props.descriptors[route.key];
|
|
129
|
+ let label;
|
|
130
|
+ if (options.tabBarLabel != null) label = options.tabBarLabel;
|
|
131
|
+ else if (options.title != null) label = options.title;
|
|
132
|
+ else label = route.name;
|
|
133
|
+
|
|
134
|
+ const onPress = () => {
|
|
135
|
+ this.onItemPress(route, state.index, index);
|
100
|
136
|
};
|
101
|
|
-
|
102
|
|
- /**
|
103
|
|
- * Finds the active route and syncs the tab bar animation with the header bar
|
104
|
|
- */
|
105
|
|
- onRouteChange = () => {
|
106
|
|
- this.props.state.routes.map(this.syncTabBar)
|
107
|
|
- }
|
108
|
|
-
|
109
|
|
- /**
|
110
|
|
- * Gets a tab icon render.
|
111
|
|
- * If the given route is focused, it syncs the tab bar and header bar animations together
|
112
|
|
- *
|
113
|
|
- * @param route The route for the icon
|
114
|
|
- * @param index The index of the current route
|
115
|
|
- * @returns {*}
|
116
|
|
- */
|
117
|
|
- renderIcon = (route, index) => {
|
118
|
|
- const state = this.props.state;
|
119
|
|
- const {options} = this.props.descriptors[route.key];
|
120
|
|
- const label =
|
121
|
|
- options.tabBarLabel != null
|
122
|
|
- ? options.tabBarLabel
|
123
|
|
- : options.title != null
|
124
|
|
- ? options.title
|
125
|
|
- : route.name;
|
126
|
|
-
|
127
|
|
- const onPress = () => this.onItemPress(route, state.index, index);
|
128
|
|
- const onLongPress = () => this.onItemLongPress(route);
|
129
|
|
- const isFocused = state.index === index;
|
130
|
|
-
|
131
|
|
- const color = isFocused ? this.props.theme.colors.primary : this.props.theme.colors.tabIcon;
|
132
|
|
- if (route.name !== "home") {
|
133
|
|
- return <TabIcon
|
134
|
|
- onPress={onPress}
|
135
|
|
- onLongPress={onLongPress}
|
136
|
|
- icon={this.tabBarIcon(route, isFocused)}
|
137
|
|
- color={color}
|
138
|
|
- label={label}
|
139
|
|
- focused={isFocused}
|
140
|
|
- extraData={state.index > index}
|
141
|
|
- key={route.key}
|
142
|
|
- />
|
143
|
|
- } else
|
144
|
|
- return <TabHomeIcon
|
145
|
|
- onPress={onPress}
|
146
|
|
- onLongPress={onLongPress}
|
147
|
|
- focused={isFocused}
|
148
|
|
- key={route.key}
|
149
|
|
- tabBarHeight={CustomTabBar.TAB_BAR_HEIGHT}
|
150
|
|
- />
|
|
137
|
+ const onLongPress = () => {
|
|
138
|
+ this.onItemLongPress(route);
|
151
|
139
|
};
|
152
|
|
-
|
153
|
|
- getIcons() {
|
154
|
|
- return this.props.state.routes.map(this.renderIcon);
|
|
140
|
+ const isFocused = state.index === index;
|
|
141
|
+
|
|
142
|
+ const color = isFocused
|
|
143
|
+ ? props.theme.colors.primary
|
|
144
|
+ : props.theme.colors.tabIcon;
|
|
145
|
+ if (route.name !== 'home') {
|
|
146
|
+ return (
|
|
147
|
+ <TabIcon
|
|
148
|
+ onPress={onPress}
|
|
149
|
+ onLongPress={onLongPress}
|
|
150
|
+ icon={this.getTabBarIcon(route, isFocused)}
|
|
151
|
+ color={color}
|
|
152
|
+ label={label}
|
|
153
|
+ focused={isFocused}
|
|
154
|
+ extraData={state.index > index}
|
|
155
|
+ key={route.key}
|
|
156
|
+ />
|
|
157
|
+ );
|
155
|
158
|
}
|
156
|
|
-
|
157
|
|
- render() {
|
158
|
|
- this.props.navigation.addListener('state', this.onRouteChange);
|
159
|
|
- const icons = this.getIcons();
|
160
|
|
- return (
|
161
|
|
- <Animated.View
|
162
|
|
- useNativeDriver
|
163
|
|
- style={{
|
164
|
|
- flexDirection: 'row',
|
165
|
|
- height: CustomTabBar.TAB_BAR_HEIGHT,
|
166
|
|
- width: '100%',
|
167
|
|
- position: 'absolute',
|
168
|
|
- bottom: 0,
|
169
|
|
- left: 0,
|
170
|
|
- backgroundColor: this.props.theme.colors.surface,
|
171
|
|
- transform: [{translateY: this.state.translateY}],
|
172
|
|
- }}
|
173
|
|
- >
|
174
|
|
- {icons}
|
175
|
|
- </Animated.View>
|
176
|
|
- );
|
|
159
|
+ return (
|
|
160
|
+ <TabHomeIcon
|
|
161
|
+ onPress={onPress}
|
|
162
|
+ onLongPress={onLongPress}
|
|
163
|
+ focused={isFocused}
|
|
164
|
+ key={route.key}
|
|
165
|
+ tabBarHeight={CustomTabBar.TAB_BAR_HEIGHT}
|
|
166
|
+ />
|
|
167
|
+ );
|
|
168
|
+ };
|
|
169
|
+
|
|
170
|
+ getIcons(): React.Node {
|
|
171
|
+ const {props} = this;
|
|
172
|
+ return props.state.routes.map(this.getRenderIcon);
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ syncTabBar = (route: RouteType, index: number) => {
|
|
176
|
+ const {state} = this.props;
|
|
177
|
+ const isFocused = state.index === index;
|
|
178
|
+ if (isFocused) {
|
|
179
|
+ const stackState = route.state;
|
|
180
|
+ const stackRoute =
|
|
181
|
+ stackState != null ? stackState.routes[stackState.index] : null;
|
|
182
|
+ const params: {collapsible: Collapsible} | null =
|
|
183
|
+ stackRoute != null ? stackRoute.params : null;
|
|
184
|
+ const collapsible = params != null ? params.collapsible : null;
|
|
185
|
+ if (collapsible != null) {
|
|
186
|
+ this.setState({
|
|
187
|
+ translateY: Animated.multiply(-1.5, collapsible.translateY), // Hide tab bar faster than header bar
|
|
188
|
+ });
|
|
189
|
+ }
|
177
|
190
|
}
|
|
191
|
+ };
|
|
192
|
+
|
|
193
|
+ render(): React.Node {
|
|
194
|
+ const {props, state} = this;
|
|
195
|
+ props.navigation.addListener('state', this.onRouteChange);
|
|
196
|
+ const icons = this.getIcons();
|
|
197
|
+ // $FlowFixMe
|
|
198
|
+ return (
|
|
199
|
+ <Animated.View
|
|
200
|
+ useNativeDriver
|
|
201
|
+ style={{
|
|
202
|
+ flexDirection: 'row',
|
|
203
|
+ height: CustomTabBar.TAB_BAR_HEIGHT,
|
|
204
|
+ width: '100%',
|
|
205
|
+ position: 'absolute',
|
|
206
|
+ bottom: 0,
|
|
207
|
+ left: 0,
|
|
208
|
+ backgroundColor: props.theme.colors.surface,
|
|
209
|
+ transform: [{translateY: state.translateY}],
|
|
210
|
+ }}>
|
|
211
|
+ {icons}
|
|
212
|
+ </Animated.View>
|
|
213
|
+ );
|
|
214
|
+ }
|
178
|
215
|
}
|
179
|
216
|
|
180
|
217
|
export default withTheme(CustomTabBar);
|