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.

CustomIntroSlider.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // @flow
  2. import * as React from 'react';
  3. import {Platform, StatusBar, StyleSheet, View} from 'react-native';
  4. import type {MaterialCommunityIconsGlyphs} from 'react-native-vector-icons/MaterialCommunityIcons';
  5. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  6. import i18n from 'i18n-js';
  7. import AppIntroSlider from 'react-native-app-intro-slider';
  8. import LinearGradient from 'react-native-linear-gradient';
  9. import * as Animatable from 'react-native-animatable';
  10. import {Card} from 'react-native-paper';
  11. import Update from '../../constants/Update';
  12. import ThemeManager from '../../managers/ThemeManager';
  13. import Mascot, {MASCOT_STYLE} from '../Mascot/Mascot';
  14. type PropsType = {
  15. onDone: () => void,
  16. isUpdate: boolean,
  17. isAprilFools: boolean,
  18. };
  19. type StateType = {
  20. currentSlide: number,
  21. };
  22. type IntroSlideType = {
  23. key: string,
  24. title: string,
  25. text: string,
  26. view: () => React.Node,
  27. mascotStyle: number,
  28. colors: [string, string],
  29. };
  30. const styles = StyleSheet.create({
  31. mainContent: {
  32. paddingBottom: 100,
  33. },
  34. text: {
  35. color: 'rgba(255, 255, 255, 0.8)',
  36. backgroundColor: 'transparent',
  37. textAlign: 'center',
  38. paddingHorizontal: 16,
  39. },
  40. title: {
  41. fontSize: 22,
  42. color: 'white',
  43. backgroundColor: 'transparent',
  44. textAlign: 'center',
  45. marginBottom: 16,
  46. },
  47. center: {
  48. marginTop: 'auto',
  49. marginBottom: 'auto',
  50. marginRight: 'auto',
  51. marginLeft: 'auto',
  52. },
  53. });
  54. /**
  55. * Class used to create intro slides
  56. */
  57. export default class CustomIntroSlider extends React.Component<
  58. PropsType,
  59. StateType,
  60. > {
  61. sliderRef: {current: null | AppIntroSlider};
  62. introSlides: Array<IntroSlideType>;
  63. updateSlides: Array<IntroSlideType>;
  64. aprilFoolsSlides: Array<IntroSlideType>;
  65. currentSlides: Array<IntroSlideType>;
  66. /**
  67. * Generates intro slides
  68. */
  69. constructor() {
  70. super();
  71. this.state = {
  72. currentSlide: 0,
  73. };
  74. this.sliderRef = React.createRef();
  75. this.introSlides = [
  76. {
  77. key: '0', // Mascot
  78. title: i18n.t('intro.slideMain.title'),
  79. text: i18n.t('intro.slideMain.text'),
  80. view: this.getWelcomeView,
  81. mascotStyle: MASCOT_STYLE.NORMAL,
  82. colors: ['#be1522', '#57080e'],
  83. },
  84. {
  85. key: '1',
  86. title: i18n.t('intro.slidePlanex.title'),
  87. text: i18n.t('intro.slidePlanex.text'),
  88. view: (): React.Node => CustomIntroSlider.getIconView('calendar-clock'),
  89. mascotStyle: MASCOT_STYLE.INTELLO,
  90. colors: ['#be1522', '#57080e'],
  91. },
  92. {
  93. key: '2',
  94. title: i18n.t('intro.slideEvents.title'),
  95. text: i18n.t('intro.slideEvents.text'),
  96. view: (): React.Node => CustomIntroSlider.getIconView('calendar-star'),
  97. mascotStyle: MASCOT_STYLE.HAPPY,
  98. colors: ['#be1522', '#57080e'],
  99. },
  100. {
  101. key: '3',
  102. title: i18n.t('intro.slideServices.title'),
  103. text: i18n.t('intro.slideServices.text'),
  104. view: (): React.Node =>
  105. CustomIntroSlider.getIconView('view-dashboard-variant'),
  106. mascotStyle: MASCOT_STYLE.CUTE,
  107. colors: ['#be1522', '#57080e'],
  108. },
  109. {
  110. key: '4',
  111. title: i18n.t('intro.slideDone.title'),
  112. text: i18n.t('intro.slideDone.text'),
  113. view: (): React.Node => this.getEndView(),
  114. mascotStyle: MASCOT_STYLE.COOL,
  115. colors: ['#9c165b', '#3e042b'],
  116. },
  117. ];
  118. // $FlowFixMe
  119. this.updateSlides = [];
  120. for (let i = 0; i < Update.slidesNumber; i += 1) {
  121. this.updateSlides.push({
  122. key: i.toString(),
  123. title: Update.getInstance().titleList[i],
  124. text: Update.getInstance().descriptionList[i],
  125. icon: Update.iconList[i],
  126. colors: Update.colorsList[i],
  127. });
  128. }
  129. this.aprilFoolsSlides = [
  130. {
  131. key: '1',
  132. title: i18n.t('intro.aprilFoolsSlide.title'),
  133. text: i18n.t('intro.aprilFoolsSlide.text'),
  134. view: (): React.Node => <View />,
  135. mascotStyle: MASCOT_STYLE.NORMAL,
  136. colors: ['#e01928', '#be1522'],
  137. },
  138. ];
  139. }
  140. /**
  141. * Render item to be used for the intro introSlides
  142. *
  143. * @param item The item to be displayed
  144. * @param dimensions Dimensions of the item
  145. */
  146. getIntroRenderItem = ({
  147. item,
  148. dimensions,
  149. }: {
  150. item: IntroSlideType,
  151. dimensions: {width: number, height: number},
  152. }): React.Node => {
  153. const {state} = this;
  154. const index = parseInt(item.key, 10);
  155. return (
  156. <LinearGradient
  157. style={[styles.mainContent, dimensions]}
  158. colors={item.colors}
  159. start={{x: 0, y: 0.1}}
  160. end={{x: 0.1, y: 1}}>
  161. {state.currentSlide === index ? (
  162. <View style={{height: '100%', flex: 1}}>
  163. <View style={{flex: 1}}>{item.view()}</View>
  164. <Animatable.View animation="fadeIn">
  165. {index !== 0 && index !== this.introSlides.length - 1 ? (
  166. <Mascot
  167. style={{
  168. marginLeft: 30,
  169. marginBottom: 0,
  170. width: 100,
  171. marginTop: -30,
  172. }}
  173. emotion={item.mascotStyle}
  174. animated
  175. entryAnimation={{
  176. animation: 'slideInLeft',
  177. duration: 500,
  178. }}
  179. loopAnimation={{
  180. animation: 'pulse',
  181. iterationCount: 'infinite',
  182. duration: 2000,
  183. }}
  184. />
  185. ) : null}
  186. <View
  187. style={{
  188. marginLeft: 50,
  189. width: 0,
  190. height: 0,
  191. borderLeftWidth: 20,
  192. borderRightWidth: 0,
  193. borderBottomWidth: 20,
  194. borderStyle: 'solid',
  195. backgroundColor: 'transparent',
  196. borderLeftColor: 'transparent',
  197. borderRightColor: 'transparent',
  198. borderBottomColor: 'rgba(0,0,0,0.60)',
  199. }}
  200. />
  201. <Card
  202. style={{
  203. backgroundColor: 'rgba(0,0,0,0.38)',
  204. marginHorizontal: 20,
  205. borderColor: 'rgba(0,0,0,0.60)',
  206. borderWidth: 4,
  207. borderRadius: 10,
  208. }}>
  209. <Card.Content>
  210. <Animatable.Text
  211. animation="fadeIn"
  212. delay={100}
  213. style={styles.title}>
  214. {item.title}
  215. </Animatable.Text>
  216. <Animatable.Text
  217. animation="fadeIn"
  218. delay={200}
  219. style={styles.text}>
  220. {item.text}
  221. </Animatable.Text>
  222. </Card.Content>
  223. </Card>
  224. </Animatable.View>
  225. </View>
  226. ) : null}
  227. </LinearGradient>
  228. );
  229. };
  230. getEndView = (): React.Node => {
  231. return (
  232. <View style={{flex: 1}}>
  233. <Mascot
  234. style={{
  235. ...styles.center,
  236. height: '80%',
  237. }}
  238. emotion={MASCOT_STYLE.COOL}
  239. animated
  240. entryAnimation={{
  241. animation: 'slideInDown',
  242. duration: 2000,
  243. }}
  244. loopAnimation={{
  245. animation: 'pulse',
  246. duration: 2000,
  247. iterationCount: 'infinite',
  248. }}
  249. />
  250. </View>
  251. );
  252. };
  253. getWelcomeView = (): React.Node => {
  254. return (
  255. <View style={{flex: 1}}>
  256. <Mascot
  257. style={{
  258. ...styles.center,
  259. height: '80%',
  260. }}
  261. emotion={MASCOT_STYLE.NORMAL}
  262. animated
  263. entryAnimation={{
  264. animation: 'bounceIn',
  265. duration: 2000,
  266. }}
  267. />
  268. <Animatable.Text
  269. useNativeDriver
  270. animation="fadeInUp"
  271. duration={500}
  272. style={{
  273. color: '#fff',
  274. textAlign: 'center',
  275. fontSize: 25,
  276. }}>
  277. PABLO
  278. </Animatable.Text>
  279. <Animatable.View
  280. useNativeDriver
  281. animation="fadeInUp"
  282. duration={500}
  283. delay={200}
  284. style={{
  285. position: 'absolute',
  286. bottom: 30,
  287. right: '20%',
  288. width: 50,
  289. height: 50,
  290. }}>
  291. <MaterialCommunityIcons
  292. style={{
  293. ...styles.center,
  294. transform: [{rotateZ: '70deg'}],
  295. }}
  296. name="undo"
  297. color="#fff"
  298. size={40}
  299. />
  300. </Animatable.View>
  301. </View>
  302. );
  303. };
  304. static getIconView(icon: MaterialCommunityIconsGlyphs): React.Node {
  305. return (
  306. <View style={{flex: 1}}>
  307. <Animatable.View style={styles.center} animation="fadeIn">
  308. <MaterialCommunityIcons name={icon} color="#fff" size={200} />
  309. </Animatable.View>
  310. </View>
  311. );
  312. }
  313. static setStatusBarColor(color: string) {
  314. if (Platform.OS === 'android') StatusBar.setBackgroundColor(color, true);
  315. }
  316. onSlideChange = (index: number) => {
  317. CustomIntroSlider.setStatusBarColor(this.currentSlides[index].colors[0]);
  318. this.setState({currentSlide: index});
  319. };
  320. onSkip = () => {
  321. CustomIntroSlider.setStatusBarColor(
  322. this.currentSlides[this.currentSlides.length - 1].colors[0],
  323. );
  324. if (this.sliderRef.current != null)
  325. this.sliderRef.current.goToSlide(this.currentSlides.length - 1);
  326. };
  327. onDone = () => {
  328. const {props} = this;
  329. CustomIntroSlider.setStatusBarColor(
  330. ThemeManager.getCurrentTheme().colors.surface,
  331. );
  332. props.onDone();
  333. };
  334. getRenderNextButton = (): React.Node => {
  335. return (
  336. <Animatable.View
  337. animation="fadeIn"
  338. style={{
  339. borderRadius: 25,
  340. padding: 5,
  341. backgroundColor: 'rgba(0,0,0,0.2)',
  342. }}>
  343. <MaterialCommunityIcons name="arrow-right" color="#fff" size={40} />
  344. </Animatable.View>
  345. );
  346. };
  347. getRenderDoneButton = (): React.Node => {
  348. return (
  349. <Animatable.View
  350. animation="bounceIn"
  351. style={{
  352. borderRadius: 25,
  353. padding: 5,
  354. backgroundColor: 'rgb(190,21,34)',
  355. }}>
  356. <MaterialCommunityIcons name="check" color="#fff" size={40} />
  357. </Animatable.View>
  358. );
  359. };
  360. render(): React.Node {
  361. const {props, state} = this;
  362. this.currentSlides = this.introSlides;
  363. if (props.isUpdate) this.currentSlides = this.updateSlides;
  364. else if (props.isAprilFools) this.currentSlides = this.aprilFoolsSlides;
  365. CustomIntroSlider.setStatusBarColor(this.currentSlides[0].colors[0]);
  366. return (
  367. <AppIntroSlider
  368. ref={this.sliderRef}
  369. data={this.currentSlides}
  370. extraData={state.currentSlide}
  371. renderItem={this.getIntroRenderItem}
  372. renderNextButton={this.getRenderNextButton}
  373. renderDoneButton={this.getRenderDoneButton}
  374. onDone={this.onDone}
  375. onSlideChange={this.onSlideChange}
  376. onSkip={this.onSkip}
  377. />
  378. );
  379. }
  380. }