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.

ProxiwashListItem.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import * as React from 'react';
  2. import {Avatar, Caption, List, ProgressBar, Surface, Text, withTheme} from 'react-native-paper';
  3. import {StyleSheet, View} from "react-native";
  4. import ProxiwashConstants from "../../../constants/ProxiwashConstants";
  5. import i18n from "i18n-js";
  6. import AprilFoolsManager from "../../../managers/AprilFoolsManager";
  7. import * as Animatable from "react-native-animatable";
  8. import type {CustomTheme} from "../../../managers/ThemeManager";
  9. import type {Machine} from "../../../screens/Proxiwash/ProxiwashScreen";
  10. type Props = {
  11. item: Machine,
  12. theme: CustomTheme,
  13. onPress: Function,
  14. isWatched: boolean,
  15. isDryer: boolean,
  16. height: number,
  17. }
  18. const AnimatedIcon = Animatable.createAnimatableComponent(Avatar.Icon);
  19. /**
  20. * Component used to display a proxiwash item, showing machine progression and state
  21. */
  22. class ProxiwashListItem extends React.Component<Props> {
  23. stateColors: Object;
  24. stateStrings: Object;
  25. title: string;
  26. constructor(props) {
  27. super(props);
  28. this.stateColors = {};
  29. this.stateStrings = {};
  30. this.updateStateStrings();
  31. let displayNumber = props.item.number;
  32. if (AprilFoolsManager.getInstance().isAprilFoolsEnabled())
  33. displayNumber = AprilFoolsManager.getProxiwashMachineDisplayNumber(parseInt(props.item.number));
  34. this.title = props.isDryer
  35. ? i18n.t('screens.proxiwash.dryer')
  36. : i18n.t('screens.proxiwash.washer');
  37. this.title += ' n°' + displayNumber;
  38. }
  39. shouldComponentUpdate(nextProps: Props): boolean {
  40. const props = this.props;
  41. return (nextProps.theme.dark !== props.theme.dark)
  42. || (nextProps.item.state !== props.item.state)
  43. || (nextProps.item.donePercent !== props.item.donePercent)
  44. || (nextProps.isWatched !== props.isWatched);
  45. }
  46. updateStateStrings() {
  47. this.stateStrings[ProxiwashConstants.machineStates.AVAILABLE] = i18n.t('screens.proxiwash.states.ready');
  48. this.stateStrings[ProxiwashConstants.machineStates.RUNNING] = i18n.t('screens.proxiwash.states.running');
  49. this.stateStrings[ProxiwashConstants.machineStates.RUNNING_NOT_STARTED] = i18n.t('screens.proxiwash.states.runningNotStarted');
  50. this.stateStrings[ProxiwashConstants.machineStates.FINISHED] = i18n.t('screens.proxiwash.states.finished');
  51. this.stateStrings[ProxiwashConstants.machineStates.UNAVAILABLE] = i18n.t('screens.proxiwash.states.broken');
  52. this.stateStrings[ProxiwashConstants.machineStates.ERROR] = i18n.t('screens.proxiwash.states.error');
  53. this.stateStrings[ProxiwashConstants.machineStates.UNKNOWN] = i18n.t('screens.proxiwash.states.unknown');
  54. }
  55. updateStateColors() {
  56. const colors = this.props.theme.colors;
  57. this.stateColors[ProxiwashConstants.machineStates.AVAILABLE] = colors.proxiwashReadyColor;
  58. this.stateColors[ProxiwashConstants.machineStates.RUNNING] = colors.proxiwashRunningColor;
  59. this.stateColors[ProxiwashConstants.machineStates.RUNNING_NOT_STARTED] = colors.proxiwashRunningNotStartedColor;
  60. this.stateColors[ProxiwashConstants.machineStates.FINISHED] = colors.proxiwashFinishedColor;
  61. this.stateColors[ProxiwashConstants.machineStates.UNAVAILABLE] = colors.proxiwashBrokenColor;
  62. this.stateColors[ProxiwashConstants.machineStates.ERROR] = colors.proxiwashErrorColor;
  63. this.stateColors[ProxiwashConstants.machineStates.UNKNOWN] = colors.proxiwashUnknownColor;
  64. }
  65. onListItemPress = () => this.props.onPress(this.title, this.props.item, this.props.isDryer);
  66. render() {
  67. const props = this.props;
  68. const colors = props.theme.colors;
  69. const machineState = props.item.state;
  70. const isRunning = machineState === ProxiwashConstants.machineStates.RUNNING;
  71. const isReady = machineState === ProxiwashConstants.machineStates.AVAILABLE;
  72. const description = isRunning ? props.item.startTime + '/' + props.item.endTime : '';
  73. const stateIcon = ProxiwashConstants.stateIcons[machineState];
  74. const stateString = this.stateStrings[machineState];
  75. const progress = isRunning
  76. ? props.item.donePercent !== ''
  77. ? parseFloat(props.item.donePercent) / 100
  78. : 0
  79. : 1;
  80. const icon = props.isWatched
  81. ? <AnimatedIcon
  82. icon={'bell-ring'}
  83. animation={"rubberBand"}
  84. useNativeDriver
  85. size={50}
  86. color={colors.primary}
  87. style={styles.icon}
  88. />
  89. : <AnimatedIcon
  90. icon={props.isDryer ? 'tumble-dryer' : 'washing-machine'}
  91. animation={isRunning ? "pulse" : undefined}
  92. iterationCount={"infinite"}
  93. easing={"linear"}
  94. duration={1000}
  95. useNativeDriver
  96. size={40}
  97. color={colors.text}
  98. style={styles.icon}
  99. />;
  100. this.updateStateColors();
  101. return (
  102. <Surface
  103. style={{
  104. ...styles.container,
  105. height: props.height,
  106. borderRadius: 4,
  107. }}
  108. >
  109. {
  110. !isReady
  111. ? <ProgressBar
  112. style={{
  113. ...styles.progressBar,
  114. height: props.height
  115. }}
  116. progress={progress}
  117. color={this.stateColors[machineState]}
  118. />
  119. : null
  120. }
  121. <List.Item
  122. title={this.title}
  123. description={description}
  124. style={{
  125. height: props.height,
  126. justifyContent: 'center',
  127. }}
  128. onPress={this.onListItemPress}
  129. left={() => icon}
  130. right={() => (
  131. <View style={{flexDirection: 'row',}}>
  132. <View style={{justifyContent: 'center',}}>
  133. <Text style={
  134. machineState === ProxiwashConstants.machineStates.FINISHED ?
  135. {fontWeight: 'bold',} : {}
  136. }
  137. >
  138. {stateString}
  139. </Text>
  140. {
  141. machineState === ProxiwashConstants.machineStates.RUNNING
  142. ? <Caption>{props.item.remainingTime} min</Caption>
  143. : null
  144. }
  145. </View>
  146. <View style={{justifyContent: 'center',}}>
  147. <Avatar.Icon
  148. icon={stateIcon}
  149. color={colors.text}
  150. size={30}
  151. style={styles.icon}
  152. />
  153. </View>
  154. </View>)}
  155. />
  156. </Surface>
  157. );
  158. }
  159. }
  160. const styles = StyleSheet.create({
  161. container: {
  162. margin: 5,
  163. justifyContent: 'center',
  164. elevation: 1
  165. },
  166. icon: {
  167. backgroundColor: 'transparent'
  168. },
  169. progressBar: {
  170. position: 'absolute',
  171. left: 0,
  172. borderRadius: 4,
  173. },
  174. });
  175. export default withTheme(ProxiwashListItem);