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.tsx 8.1KB

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