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.tsx 7.7KB

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