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 8.8KB

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