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.

ProxiwashListItem.js 7.1KB

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