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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 {CustomTheme} from '../../../managers/ThemeManager';
  18. import type {ProxiwashMachineType} from '../../../screens/Proxiwash/ProxiwashScreen';
  19. type PropsType = {
  20. item: ProxiwashMachineType,
  21. theme: CustomTheme,
  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. constructor(props: PropsType) {
  55. super(props);
  56. this.stateColors = {};
  57. this.stateStrings = {};
  58. this.updateStateStrings();
  59. let displayNumber = props.item.number;
  60. if (AprilFoolsManager.getInstance().isAprilFoolsEnabled())
  61. displayNumber = AprilFoolsManager.getProxiwashMachineDisplayNumber(
  62. parseInt(props.item.number, 10),
  63. );
  64. this.title = props.isDryer
  65. ? i18n.t('screens.proxiwash.dryer')
  66. : i18n.t('screens.proxiwash.washer');
  67. this.title += ` n°${displayNumber}`;
  68. }
  69. shouldComponentUpdate(nextProps: PropsType): boolean {
  70. const {props} = this;
  71. return (
  72. nextProps.theme.dark !== props.theme.dark ||
  73. nextProps.item.state !== props.item.state ||
  74. nextProps.item.donePercent !== props.item.donePercent ||
  75. nextProps.isWatched !== props.isWatched
  76. );
  77. }
  78. onListItemPress = () => {
  79. const {props} = this;
  80. props.onPress(this.title, props.item, props.isDryer);
  81. };
  82. updateStateStrings() {
  83. this.stateStrings[ProxiwashConstants.machineStates.AVAILABLE] = i18n.t(
  84. 'screens.proxiwash.states.ready',
  85. );
  86. this.stateStrings[ProxiwashConstants.machineStates.RUNNING] = i18n.t(
  87. 'screens.proxiwash.states.running',
  88. );
  89. this.stateStrings[
  90. ProxiwashConstants.machineStates.RUNNING_NOT_STARTED
  91. ] = i18n.t('screens.proxiwash.states.runningNotStarted');
  92. this.stateStrings[ProxiwashConstants.machineStates.FINISHED] = i18n.t(
  93. 'screens.proxiwash.states.finished',
  94. );
  95. this.stateStrings[ProxiwashConstants.machineStates.UNAVAILABLE] = i18n.t(
  96. 'screens.proxiwash.states.broken',
  97. );
  98. this.stateStrings[ProxiwashConstants.machineStates.ERROR] = i18n.t(
  99. 'screens.proxiwash.states.error',
  100. );
  101. this.stateStrings[ProxiwashConstants.machineStates.UNKNOWN] = i18n.t(
  102. 'screens.proxiwash.states.unknown',
  103. );
  104. }
  105. updateStateColors() {
  106. const {props} = this;
  107. const {colors} = props.theme;
  108. this.stateColors[ProxiwashConstants.machineStates.AVAILABLE] =
  109. colors.proxiwashReadyColor;
  110. this.stateColors[ProxiwashConstants.machineStates.RUNNING] =
  111. colors.proxiwashRunningColor;
  112. this.stateColors[ProxiwashConstants.machineStates.RUNNING_NOT_STARTED] =
  113. colors.proxiwashRunningNotStartedColor;
  114. this.stateColors[ProxiwashConstants.machineStates.FINISHED] =
  115. colors.proxiwashFinishedColor;
  116. this.stateColors[ProxiwashConstants.machineStates.UNAVAILABLE] =
  117. colors.proxiwashBrokenColor;
  118. this.stateColors[ProxiwashConstants.machineStates.ERROR] =
  119. colors.proxiwashErrorColor;
  120. this.stateColors[ProxiwashConstants.machineStates.UNKNOWN] =
  121. colors.proxiwashUnknownColor;
  122. }
  123. render(): React.Node {
  124. const {props} = this;
  125. const {colors} = props.theme;
  126. const machineState = props.item.state;
  127. const isRunning = machineState === ProxiwashConstants.machineStates.RUNNING;
  128. const isReady = machineState === ProxiwashConstants.machineStates.AVAILABLE;
  129. const description = isRunning
  130. ? `${props.item.startTime}/${props.item.endTime}`
  131. : '';
  132. const stateIcon = ProxiwashConstants.stateIcons[machineState];
  133. const stateString = this.stateStrings[machineState];
  134. let progress;
  135. if (isRunning && props.item.donePercent !== '')
  136. progress = parseFloat(props.item.donePercent) / 100;
  137. else if (isRunning) progress = 0;
  138. else progress = 1;
  139. const icon = props.isWatched ? (
  140. <AnimatedIcon
  141. icon="bell-ring"
  142. animation="rubberBand"
  143. useNativeDriver
  144. size={50}
  145. color={colors.primary}
  146. style={styles.icon}
  147. />
  148. ) : (
  149. <AnimatedIcon
  150. icon={props.isDryer ? 'tumble-dryer' : 'washing-machine'}
  151. animation={isRunning ? 'pulse' : undefined}
  152. iterationCount="infinite"
  153. easing="linear"
  154. duration={1000}
  155. useNativeDriver
  156. size={40}
  157. color={colors.text}
  158. style={styles.icon}
  159. />
  160. );
  161. this.updateStateColors();
  162. return (
  163. <Surface
  164. style={{
  165. ...styles.container,
  166. height: props.height,
  167. borderRadius: 4,
  168. }}>
  169. {!isReady ? (
  170. <ProgressBar
  171. style={{
  172. ...styles.progressBar,
  173. height: props.height,
  174. }}
  175. progress={progress}
  176. color={this.stateColors[machineState]}
  177. />
  178. ) : null}
  179. <List.Item
  180. title={this.title}
  181. description={description}
  182. style={{
  183. height: props.height,
  184. justifyContent: 'center',
  185. }}
  186. onPress={this.onListItemPress}
  187. left={(): React.Node => icon}
  188. right={(): React.Node => (
  189. <View style={{flexDirection: 'row'}}>
  190. <View style={{justifyContent: 'center'}}>
  191. <Text
  192. style={
  193. machineState === ProxiwashConstants.machineStates.FINISHED
  194. ? {fontWeight: 'bold'}
  195. : {}
  196. }>
  197. {stateString}
  198. </Text>
  199. {machineState === ProxiwashConstants.machineStates.RUNNING ? (
  200. <Caption>{props.item.remainingTime} min</Caption>
  201. ) : null}
  202. </View>
  203. <View style={{justifyContent: 'center'}}>
  204. <Avatar.Icon
  205. icon={stateIcon}
  206. color={colors.text}
  207. size={30}
  208. style={styles.icon}
  209. />
  210. </View>
  211. </View>
  212. )}
  213. />
  214. </Surface>
  215. );
  216. }
  217. }
  218. export default withTheme(ProxiwashListItem);