Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomTabBar.tsx 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import {Animated} from 'react-native';
  21. import {withTheme} from 'react-native-paper';
  22. import {Collapsible} from 'react-navigation-collapsible';
  23. import TabIcon from './TabIcon';
  24. import TabHomeIcon from './TabHomeIcon';
  25. import {BottomTabBarProps} from '@react-navigation/bottom-tabs';
  26. import {NavigationState} from '@react-navigation/native';
  27. import {
  28. PartialState,
  29. Route,
  30. } from '@react-navigation/routers/lib/typescript/src/types';
  31. type RouteType = Route<string> & {
  32. state?: NavigationState | PartialState<NavigationState>;
  33. };
  34. interface PropsType extends BottomTabBarProps {
  35. theme: ReactNativePaper.Theme;
  36. }
  37. type StateType = {
  38. translateY: any;
  39. };
  40. type validRoutes = 'proxiwash' | 'services' | 'planning' | 'planex';
  41. const TAB_ICONS = {
  42. proxiwash: 'tshirt-crew',
  43. services: 'account-circle',
  44. planning: 'calendar-range',
  45. planex: 'clock',
  46. };
  47. class CustomTabBar extends React.Component<PropsType, StateType> {
  48. static TAB_BAR_HEIGHT = 48;
  49. constructor(props: PropsType) {
  50. super(props);
  51. this.state = {
  52. translateY: new Animated.Value(0),
  53. };
  54. }
  55. /**
  56. * Navigates to the given route if it is different from the current one
  57. *
  58. * @param route Destination route
  59. * @param currentIndex The current route index
  60. * @param destIndex The destination route index
  61. */
  62. onItemPress(route: RouteType, currentIndex: number, destIndex: number) {
  63. const {navigation} = this.props;
  64. if (currentIndex !== destIndex) {
  65. navigation.navigate(route.name);
  66. }
  67. }
  68. /**
  69. * Navigates to tetris screen on home button long press
  70. *
  71. * @param route
  72. */
  73. onItemLongPress(route: RouteType) {
  74. const {navigation} = this.props;
  75. if (route.name === 'home') {
  76. navigation.navigate('game-start');
  77. }
  78. }
  79. /**
  80. * Finds the active route and syncs the tab bar animation with the header bar
  81. */
  82. onRouteChange = () => {
  83. const {props} = this;
  84. props.state.routes.map(this.syncTabBar);
  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. getTabBarIcon = (route: RouteType, focused: boolean) => {
  94. let icon = TAB_ICONS[route.name as validRoutes];
  95. icon = focused ? icon : `${icon}-outline`;
  96. if (route.name !== 'home') {
  97. return icon;
  98. }
  99. return '';
  100. };
  101. /**
  102. * Gets a tab icon render.
  103. * If the given route is focused, it syncs the tab bar and header bar animations together
  104. *
  105. * @param route The route for the icon
  106. * @param index The index of the current route
  107. * @returns {*}
  108. */
  109. getRenderIcon = (route: RouteType, index: number) => {
  110. const {props} = this;
  111. const {state} = props;
  112. const {options} = props.descriptors[route.key];
  113. let label;
  114. if (options.tabBarLabel != null) {
  115. label = options.tabBarLabel;
  116. } else if (options.title != null) {
  117. label = options.title;
  118. } else {
  119. label = route.name;
  120. }
  121. const onPress = () => {
  122. this.onItemPress(route, state.index, index);
  123. };
  124. const onLongPress = () => {
  125. this.onItemLongPress(route);
  126. };
  127. const isFocused = state.index === index;
  128. const color = isFocused
  129. ? props.theme.colors.primary
  130. : props.theme.colors.tabIcon;
  131. if (route.name !== 'home') {
  132. return (
  133. <TabIcon
  134. onPress={onPress}
  135. onLongPress={onLongPress}
  136. icon={this.getTabBarIcon(route, isFocused)}
  137. color={color}
  138. label={label as string}
  139. focused={isFocused}
  140. extraData={state.index > index}
  141. key={route.key}
  142. />
  143. );
  144. }
  145. return (
  146. <TabHomeIcon
  147. onPress={onPress}
  148. onLongPress={onLongPress}
  149. focused={isFocused}
  150. key={route.key}
  151. tabBarHeight={CustomTabBar.TAB_BAR_HEIGHT}
  152. />
  153. );
  154. };
  155. getIcons() {
  156. const {props} = this;
  157. return props.state.routes.map(this.getRenderIcon);
  158. }
  159. syncTabBar = (route: RouteType, index: number) => {
  160. const {state} = this.props;
  161. const isFocused = state.index === index;
  162. if (isFocused) {
  163. const stackState = route.state;
  164. const stackRoute =
  165. stackState && stackState.index
  166. ? stackState.routes[stackState.index]
  167. : null;
  168. const params: {collapsible: Collapsible} | null | undefined = stackRoute
  169. ? (stackRoute.params as {collapsible: Collapsible})
  170. : null;
  171. const collapsible = params != null ? params.collapsible : null;
  172. if (collapsible != null) {
  173. this.setState({
  174. translateY: Animated.multiply(-1.5, collapsible.translateY), // Hide tab bar faster than header bar
  175. });
  176. }
  177. }
  178. };
  179. render() {
  180. const {props, state} = this;
  181. props.navigation.addListener('state', this.onRouteChange);
  182. const icons = this.getIcons();
  183. return (
  184. <Animated.View
  185. style={{
  186. flexDirection: 'row',
  187. height: CustomTabBar.TAB_BAR_HEIGHT,
  188. width: '100%',
  189. position: 'absolute',
  190. bottom: 0,
  191. left: 0,
  192. backgroundColor: props.theme.colors.surface,
  193. transform: [{translateY: state.translateY}],
  194. }}>
  195. {icons}
  196. </Animated.View>
  197. );
  198. }
  199. }
  200. export default withTheme(CustomTabBar);