Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. import React, { PureComponent } from 'react';
  2. import {
  3. Animated,
  4. Text,
  5. TouchableOpacity,
  6. Dimensions,
  7. View,
  8. } from 'react-native';
  9. import { Header } from 'react-navigation';
  10. import { Spinner } from '@components';
  11. import { BalanceMode, InnerContainerMode } from '@utils/enums';
  12. import { WHITE } from '@styles/styles';
  13. import {
  14. widthPercentageToDP,
  15. heightPercentageToDP,
  16. } from '@utils/stylesFunctions';
  17. import {
  18. styles,
  19. IconItem,
  20. InnerView,
  21. HeaderInnerViewContainer,
  22. IconClose,
  23. BalanceHeaderContainer,
  24. CloseIconContainer,
  25. BalanceHeaderText,
  26. BalanceHeaderSubtext,
  27. MenuItemInnerViewContainer,
  28. } from './Menu.styles';
  29.  
  30. const AnimatedTouchable = Animated.createAnimatedComponent(TouchableOpacity);
  31.  
  32. const Saldo = '111.250,50';
  33. const Processing = undefined;
  34. const Blocked = undefined;
  35.  
  36. const SaldoContainer = ({ opacityBalance, saldo, mode }) => {
  37. if (mode === BalanceMode.SHOWBALANCE) {
  38. return (
  39. <Animated.View style={[styles.inner, { opacity: opacityBalance }]}>
  40. <Text style={styles.balanceText}>{saldo || ''}</Text>
  41. <IconItem name="arrow-up" />
  42. </Animated.View>
  43. );
  44. }
  45. if (mode === BalanceMode.ERROR) {
  46. return (
  47. <Animated.View style={[styles.inner, { opacity: opacityBalance }]}>
  48. <IconItem name="alert-circle" fontSize={30} />
  49. </Animated.View>
  50. );
  51. }
  52. if (mode === BalanceMode.LOADING) {
  53. return (
  54. <Animated.View style={[styles.inner, { opacity: opacityBalance }]}>
  55. <Spinner fontSize={30} color={WHITE} />
  56. </Animated.View>
  57. );
  58. }
  59. if (mode === BalanceMode.NONE) {
  60. return (
  61. <Animated.View style={[styles.inner, { opacity: opacityBalance }]} />
  62. );
  63. }
  64. };
  65.  
  66. const HeaderInnerContent = ({ saldo, mode, blocked, processing }) => {
  67. const text = mode === BalanceMode.SHOWBALANCE ? saldo : 'saldo indisponível';
  68. return (
  69. <>
  70. <BalanceHeaderText>{text}</BalanceHeaderText>
  71. {blocked && (
  72. <BalanceHeaderSubtext>Bloqueado R$ {blocked}</BalanceHeaderSubtext>
  73. )}
  74. {processing && (
  75. <BalanceHeaderSubtext>
  76. Em Processamento R$ {processing}
  77. </BalanceHeaderSubtext>
  78. )}
  79. </>
  80. );
  81. };
  82.  
  83. const InnerContainer = ({
  84. mode,
  85. children,
  86. opacityInner,
  87. saldo,
  88. blocked,
  89. processing,
  90. }) => {
  91. return (
  92. <Animated.View style={[styles.innerContainer, { opacity: opacityInner }]}>
  93. {/* header */}
  94. <HeaderInnerViewContainer>
  95. <BalanceHeaderContainer>
  96. <HeaderInnerContent
  97. saldo={saldo}
  98. mode={mode}
  99. blocked={blocked}
  100. processing={processing}
  101. />
  102. </BalanceHeaderContainer>
  103. <CloseIconContainer>
  104. <IconClose name="x" />
  105. </CloseIconContainer>
  106. </HeaderInnerViewContainer>
  107.  
  108. {/* menu itens */}
  109. <MenuItemInnerViewContainer />
  110. </Animated.View>
  111. );
  112. };
  113.  
  114. export class Menu extends PureComponent {
  115. state = {
  116. width: new Animated.Value(widthPercentageToDP(Saldo.length <= 8 ? 45 : 50)),
  117. height: new Animated.Value(heightPercentageToDP(8)),
  118. opacityBalance: new Animated.Value(1),
  119. opacityInner: new Animated.Value(0),
  120. expanded: false,
  121. };
  122.  
  123. animate = (positionRight: number, positionBottom: number) => {
  124. this.setState((prevState: State) => ({
  125. expanded: !prevState.expanded,
  126. }));
  127.  
  128. // pegar do arquivo de config
  129. const widthScreen = Dimensions.get('window').width;
  130. const heightScreen = Dimensions.get('window').height;
  131. const headerHeight = Header.HEIGHT;
  132. const {
  133. width,
  134. height,
  135. expanded,
  136. opacityBalance,
  137. opacityInner,
  138. } = this.state;
  139.  
  140. if (!expanded) {
  141. Animated.sequence([
  142. Animated.timing(opacityBalance, {
  143. toValue: 0,
  144. duration: 1,
  145. }),
  146.  
  147. Animated.parallel([
  148. Animated.timing(width, {
  149. toValue: widthScreen - positionRight * 2,
  150. duration: 300,
  151. }),
  152. Animated.timing(height, {
  153. toValue: heightScreen - positionBottom - headerHeight - 50,
  154. duration: 300,
  155. }),
  156. ]),
  157.  
  158. // view inner
  159. Animated.timing(opacityInner, {
  160. toValue: 1,
  161. duration: 300,
  162. }),
  163. ]).start();
  164. } else {
  165. Animated.sequence([
  166. Animated.parallel([
  167. Animated.timing(width, {
  168. toValue: widthPercentageToDP(Saldo.length <= 8 ? 45 : 50),
  169. duration: 300,
  170. }),
  171. Animated.timing(height, {
  172. toValue: heightPercentageToDP(8),
  173. duration: 300,
  174. }),
  175. ]),
  176. Animated.timing(opacityBalance, {
  177. toValue: 1,
  178. duration: 100,
  179. }),
  180. Animated.timing(opacityInner, {
  181. toValue: 0,
  182. duration: 300,
  183. }),
  184. ]).start();
  185. }
  186. };
  187.  
  188. render() {
  189. const { positionRight, positionBottom } = this.props;
  190. const {
  191. width,
  192. height,
  193. opacityBalance,
  194. showBalance,
  195. opacityInner,
  196. expanded,
  197. } = this.state;
  198. return (
  199. <AnimatedTouchable
  200. style={[
  201. styles.container,
  202. {
  203. width,
  204. height,
  205. bottom: positionBottom,
  206. right: positionRight,
  207. },
  208. ]}
  209. onPress={() => this.animate(positionRight, positionBottom)}
  210. activeOpacity={1}
  211. >
  212. <SaldoContainer
  213. mode={BalanceMode.SHOWBALANCE}
  214. saldo={Saldo}
  215. opacityBalance={opacityBalance}
  216. />
  217. {expanded && (
  218. <InnerContainer
  219. mode={BalanceMode.ERROR}
  220. opacityInner={opacityInner}
  221. blocked={Blocked}
  222. processing={Processing}
  223. saldo={Saldo}
  224. />
  225. )}
  226. </AnimatedTouchable>
  227. );
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement