Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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 8.3KB

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