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.js 3.7KB

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