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.6KB

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