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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. style={{backgroundColor: "transparent"}}
  113. color={this.props.theme.colors.primary}
  114. icon={this.props.icon}
  115. />
  116. : null}
  117. />
  118. <Card.Content style={{
  119. maxHeight: this.windowHeight / 3
  120. }}>
  121. <ScrollView>
  122. <Paragraph style={{marginBottom: 10}}>
  123. {this.props.message}
  124. </Paragraph>
  125. </ScrollView>
  126. </Card.Content>
  127. <Card.Actions>
  128. {this.getButtons()}
  129. </Card.Actions>
  130. </Card>
  131. </Animatable.View>
  132. );
  133. }
  134. getMascot() {
  135. return (
  136. <Animatable.View
  137. useNativeDriver={true}
  138. animation={this.props.visible ? "bounceInLeft" : "bounceOutLeft"}
  139. duration={this.props.visible ? 1500 : 200}
  140. >
  141. <Mascot
  142. size={this.mascotSize}
  143. animated={true}
  144. emotion={this.props.emotion}
  145. />
  146. </Animatable.View>
  147. );
  148. }
  149. getButtons() {
  150. const action = this.props.buttons.action;
  151. const cancel = this.props.buttons.cancel;
  152. return (
  153. <View style={{
  154. flexDirection: "row",
  155. marginLeft: "auto",
  156. marginRight: "auto",
  157. marginTop: "auto",
  158. marginBottom: "auto",
  159. }}>
  160. {cancel != null
  161. ? <Button
  162. mode={"contained"}
  163. icon={cancel.icon}
  164. color={cancel.color}
  165. onPress={cancel.onPress}
  166. >
  167. {cancel.message}
  168. </Button>
  169. : null}
  170. {action != null
  171. ? <Button
  172. style={{
  173. marginLeft: 20,
  174. }}
  175. mode={"contained"}
  176. icon={action.icon}
  177. color={action.color}
  178. onPress={action.onPress}
  179. >
  180. {action.message}
  181. </Button>
  182. : null}
  183. </View>
  184. );
  185. }
  186. getBackground() {
  187. return (
  188. <TouchableWithoutFeedback onPress={this.props.buttons.cancel.onPress}>
  189. <Animatable.View
  190. style={{
  191. position: "absolute",
  192. backgroundColor: "rgba(0,0,0,0.7)",
  193. width: "100%",
  194. height: "100%",
  195. }}
  196. useNativeDriver={true}
  197. animation={this.props.visible ? "fadeIn" : "fadeOut"}
  198. duration={this.props.visible ? 300 : 300}
  199. />
  200. </TouchableWithoutFeedback>
  201. );
  202. }
  203. render() {
  204. if (this.state.shouldShowDialog) {
  205. return (
  206. <Portal>
  207. {this.getBackground()}
  208. <View style={{
  209. marginTop: "auto",
  210. marginBottom: "auto",
  211. }}>
  212. {this.getMascot()}
  213. {this.getSpeechBubble()}
  214. </View>
  215. </Portal>
  216. );
  217. } else
  218. return null;
  219. }
  220. }
  221. export default withTheme(MascotPopup);