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.

MascotPopup.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // @flow
  2. import * as React from 'react';
  3. import {
  4. Avatar,
  5. Button,
  6. Card,
  7. Paragraph,
  8. Portal,
  9. withTheme,
  10. } from 'react-native-paper';
  11. import * as Animatable from 'react-native-animatable';
  12. import {
  13. BackHandler,
  14. Dimensions,
  15. ScrollView,
  16. TouchableWithoutFeedback,
  17. View,
  18. } from 'react-native';
  19. import Mascot from './Mascot';
  20. import type {CustomTheme} from '../../managers/ThemeManager';
  21. import SpeechArrow from './SpeechArrow';
  22. import AsyncStorageManager from '../../managers/AsyncStorageManager';
  23. type PropsType = {
  24. theme: CustomTheme,
  25. icon: string,
  26. title: string,
  27. message: string,
  28. buttons: {
  29. action: {
  30. message: string,
  31. icon: string | null,
  32. color: string | null,
  33. onPress?: () => void,
  34. },
  35. cancel: {
  36. message: string,
  37. icon: string | null,
  38. color: string | null,
  39. onPress?: () => void,
  40. },
  41. },
  42. emotion: number,
  43. visible?: boolean,
  44. prefKey?: string,
  45. };
  46. type StateType = {
  47. shouldRenderDialog: boolean, // Used to stop rendering after hide animation
  48. dialogVisible: boolean,
  49. };
  50. /**
  51. * Component used to display a popup with the mascot.
  52. */
  53. class MascotPopup extends React.Component<PropsType, StateType> {
  54. static defaultProps = {
  55. visible: null,
  56. prefKey: null,
  57. };
  58. mascotSize: number;
  59. windowWidth: number;
  60. windowHeight: number;
  61. constructor(props: PropsType) {
  62. super(props);
  63. this.windowWidth = Dimensions.get('window').width;
  64. this.windowHeight = Dimensions.get('window').height;
  65. this.mascotSize = Dimensions.get('window').height / 6;
  66. if (props.visible != null) {
  67. this.state = {
  68. shouldRenderDialog: props.visible,
  69. dialogVisible: props.visible,
  70. };
  71. } else if (props.prefKey != null) {
  72. const visible = AsyncStorageManager.getBool(props.prefKey);
  73. this.state = {
  74. shouldRenderDialog: visible,
  75. dialogVisible: visible,
  76. };
  77. } else {
  78. this.state = {
  79. shouldRenderDialog: false,
  80. dialogVisible: false,
  81. };
  82. }
  83. }
  84. componentDidMount(): * {
  85. BackHandler.addEventListener(
  86. 'hardwareBackPress',
  87. this.onBackButtonPressAndroid,
  88. );
  89. }
  90. shouldComponentUpdate(nextProps: PropsType, nextState: StateType): boolean {
  91. const {props, state} = this;
  92. if (nextProps.visible) {
  93. this.state.shouldRenderDialog = true;
  94. this.state.dialogVisible = true;
  95. } else if (
  96. nextProps.visible !== props.visible ||
  97. (!nextState.dialogVisible &&
  98. nextState.dialogVisible !== state.dialogVisible)
  99. ) {
  100. this.state.dialogVisible = false;
  101. setTimeout(this.onAnimationEnd, 300);
  102. }
  103. return true;
  104. }
  105. onAnimationEnd = () => {
  106. this.setState({
  107. shouldRenderDialog: false,
  108. });
  109. };
  110. onBackButtonPressAndroid = (): boolean => {
  111. const {state, props} = this;
  112. if (state.dialogVisible) {
  113. const {cancel} = props.buttons;
  114. const {action} = props.buttons;
  115. if (cancel != null) this.onDismiss(cancel.onPress);
  116. else this.onDismiss(action.onPress);
  117. return true;
  118. }
  119. return false;
  120. };
  121. getSpeechBubble(): React.Node {
  122. const {state, props} = this;
  123. return (
  124. <Animatable.View
  125. style={{
  126. marginLeft: '10%',
  127. marginRight: '10%',
  128. }}
  129. useNativeDriver
  130. animation={state.dialogVisible ? 'bounceInLeft' : 'bounceOutLeft'}
  131. duration={state.dialogVisible ? 1000 : 300}>
  132. <SpeechArrow
  133. style={{marginLeft: this.mascotSize / 3}}
  134. size={20}
  135. color={props.theme.colors.mascotMessageArrow}
  136. />
  137. <Card
  138. style={{
  139. borderColor: props.theme.colors.mascotMessageArrow,
  140. borderWidth: 4,
  141. borderRadius: 10,
  142. }}>
  143. <Card.Title
  144. title={props.title}
  145. left={
  146. props.icon != null
  147. ? (): React.Node => (
  148. <Avatar.Icon
  149. size={48}
  150. style={{backgroundColor: 'transparent'}}
  151. color={props.theme.colors.primary}
  152. icon={props.icon}
  153. />
  154. )
  155. : null
  156. }
  157. />
  158. <Card.Content
  159. style={{
  160. maxHeight: this.windowHeight / 3,
  161. }}>
  162. <ScrollView>
  163. <Paragraph style={{marginBottom: 10}}>{props.message}</Paragraph>
  164. </ScrollView>
  165. </Card.Content>
  166. <Card.Actions style={{marginTop: 10, marginBottom: 10}}>
  167. {this.getButtons()}
  168. </Card.Actions>
  169. </Card>
  170. </Animatable.View>
  171. );
  172. }
  173. getMascot(): React.Node {
  174. const {props, state} = this;
  175. return (
  176. <Animatable.View
  177. useNativeDriver
  178. animation={state.dialogVisible ? 'bounceInLeft' : 'bounceOutLeft'}
  179. duration={state.dialogVisible ? 1500 : 200}>
  180. <Mascot
  181. style={{width: this.mascotSize}}
  182. animated
  183. emotion={props.emotion}
  184. />
  185. </Animatable.View>
  186. );
  187. }
  188. getButtons(): React.Node {
  189. const {props} = this;
  190. const {action} = props.buttons;
  191. const {cancel} = props.buttons;
  192. return (
  193. <View
  194. style={{
  195. marginLeft: 'auto',
  196. marginRight: 'auto',
  197. marginTop: 'auto',
  198. marginBottom: 'auto',
  199. }}>
  200. {action != null ? (
  201. <Button
  202. style={{
  203. marginLeft: 'auto',
  204. marginRight: 'auto',
  205. marginBottom: 10,
  206. }}
  207. mode="contained"
  208. icon={action.icon}
  209. color={action.color}
  210. onPress={() => {
  211. this.onDismiss(action.onPress);
  212. }}>
  213. {action.message}
  214. </Button>
  215. ) : null}
  216. {cancel != null ? (
  217. <Button
  218. style={{
  219. marginLeft: 'auto',
  220. marginRight: 'auto',
  221. }}
  222. mode="contained"
  223. icon={cancel.icon}
  224. color={cancel.color}
  225. onPress={() => {
  226. this.onDismiss(cancel.onPress);
  227. }}>
  228. {cancel.message}
  229. </Button>
  230. ) : null}
  231. </View>
  232. );
  233. }
  234. getBackground(): React.Node {
  235. const {props, state} = this;
  236. return (
  237. <TouchableWithoutFeedback
  238. onPress={() => {
  239. this.onDismiss(props.buttons.cancel.onPress);
  240. }}>
  241. <Animatable.View
  242. style={{
  243. position: 'absolute',
  244. backgroundColor: 'rgba(0,0,0,0.7)',
  245. width: '100%',
  246. height: '100%',
  247. }}
  248. useNativeDriver
  249. animation={state.dialogVisible ? 'fadeIn' : 'fadeOut'}
  250. duration={state.dialogVisible ? 300 : 300}
  251. />
  252. </TouchableWithoutFeedback>
  253. );
  254. }
  255. onDismiss = (callback?: () => void) => {
  256. const {prefKey} = this.props;
  257. if (prefKey != null) {
  258. AsyncStorageManager.set(prefKey, false);
  259. this.setState({dialogVisible: false});
  260. }
  261. if (callback != null) callback();
  262. };
  263. render(): React.Node {
  264. const {shouldRenderDialog} = this.state;
  265. if (shouldRenderDialog) {
  266. return (
  267. <Portal>
  268. {this.getBackground()}
  269. <View
  270. style={{
  271. marginTop: 'auto',
  272. marginBottom: 'auto',
  273. }}>
  274. <View
  275. style={{
  276. marginTop: -80,
  277. width: '100%',
  278. }}>
  279. {this.getMascot()}
  280. {this.getSpeechBubble()}
  281. </View>
  282. </View>
  283. </Portal>
  284. );
  285. }
  286. return null;
  287. }
  288. }
  289. export default withTheme(MascotPopup);