Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.

GroupListItem.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 { List, TouchableRipple, withTheme } from 'react-native-paper';
  21. import * as Animatable from 'react-native-animatable';
  22. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  23. import type { PlanexGroupType } from '../../../screens/Planex/GroupSelectionScreen';
  24. import { StyleSheet, View } from 'react-native';
  25. import { getPrettierPlanexGroupName } from '../../../utils/Utils';
  26. type PropsType = {
  27. theme: ReactNativePaper.Theme;
  28. onPress: () => void;
  29. onStarPress: () => void;
  30. item: PlanexGroupType;
  31. favorites: Array<PlanexGroupType>;
  32. height: number;
  33. };
  34. const styles = StyleSheet.create({
  35. item: {
  36. justifyContent: 'center',
  37. },
  38. icon: {
  39. padding: 10,
  40. },
  41. iconContainer: {
  42. marginRight: 10,
  43. marginLeft: 'auto',
  44. marginTop: 'auto',
  45. marginBottom: 'auto',
  46. },
  47. });
  48. class GroupListItem extends React.Component<PropsType> {
  49. isFav: boolean;
  50. starRef: { current: null | (Animatable.View & View) };
  51. constructor(props: PropsType) {
  52. super(props);
  53. this.starRef = React.createRef();
  54. this.isFav = this.isGroupInFavorites(props.favorites);
  55. }
  56. shouldComponentUpdate(nextProps: PropsType): boolean {
  57. const { favorites } = this.props;
  58. const favChanged = favorites.length !== nextProps.favorites.length;
  59. let newFavState = this.isFav;
  60. if (favChanged) {
  61. newFavState = this.isGroupInFavorites(nextProps.favorites);
  62. }
  63. const shouldUpdate = this.isFav !== newFavState;
  64. this.isFav = newFavState;
  65. return shouldUpdate;
  66. }
  67. onStarPress = () => {
  68. const { props } = this;
  69. const ref = this.starRef;
  70. if (ref.current && ref.current.rubberBand && ref.current.swing) {
  71. if (this.isFav) {
  72. ref.current.rubberBand();
  73. } else {
  74. ref.current.swing();
  75. }
  76. }
  77. props.onStarPress();
  78. };
  79. isGroupInFavorites(favorites: Array<PlanexGroupType>): boolean {
  80. const { item } = this.props;
  81. for (let i = 0; i < favorites.length; i += 1) {
  82. if (favorites[i].id === item.id) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. render() {
  89. const { props } = this;
  90. const { colors } = props.theme;
  91. return (
  92. <List.Item
  93. title={getPrettierPlanexGroupName(props.item.name)}
  94. onPress={props.onPress}
  95. left={(iconProps) => (
  96. <List.Icon
  97. color={iconProps.color}
  98. style={iconProps.style}
  99. icon={'chevron-right'}
  100. />
  101. )}
  102. right={(iconProps) => (
  103. <Animatable.View ref={this.starRef} useNativeDriver>
  104. <TouchableRipple
  105. onPress={this.onStarPress}
  106. style={styles.iconContainer}
  107. >
  108. <MaterialCommunityIcons
  109. size={30}
  110. style={styles.icon}
  111. name="star"
  112. color={this.isFav ? colors.tetrisScore : iconProps.color}
  113. />
  114. </TouchableRipple>
  115. </Animatable.View>
  116. )}
  117. style={{
  118. height: props.height,
  119. ...styles.item,
  120. }}
  121. />
  122. );
  123. }
  124. }
  125. export default withTheme(GroupListItem);