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.

SettingsScreen.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // @flow
  2. import * as React from 'react';
  3. import {ScrollView} from "react-native";
  4. import ThemeManager from '../../managers/ThemeManager';
  5. import i18n from "i18n-js";
  6. import AsyncStorageManager from "../../managers/AsyncStorageManager";
  7. import {setMachineReminderNotificationTime} from "../../utils/Notifications";
  8. import {Card, List, Switch, ToggleButton} from 'react-native-paper';
  9. import {Appearance} from "react-native-appearance";
  10. import AnimatedAccordion from "../../components/Animations/AnimatedAccordion";
  11. type Props = {
  12. navigation: Object,
  13. };
  14. type State = {
  15. nightMode: boolean,
  16. nightModeFollowSystem: boolean,
  17. proxiwashNotifPickerSelected: string,
  18. startScreenPickerSelected: string,
  19. };
  20. /**
  21. * Class defining the Settings screen. This screen shows controls to modify app preferences.
  22. */
  23. export default class SettingsScreen extends React.Component<Props, State> {
  24. state = {
  25. nightMode: ThemeManager.getNightMode(),
  26. nightModeFollowSystem: AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current === '1' &&
  27. Appearance.getColorScheme() !== 'no-preference',
  28. proxiwashNotifPickerSelected: AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current,
  29. startScreenPickerSelected: AsyncStorageManager.getInstance().preferences.defaultStartScreen.current,
  30. };
  31. constructor() {
  32. super();
  33. }
  34. /**
  35. * Saves the value for the proxiwash reminder notification time
  36. *
  37. * @param value The value to store
  38. */
  39. onProxiwashNotifPickerValueChange = (value: string) => {
  40. if (value != null) {
  41. let key = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.key;
  42. AsyncStorageManager.getInstance().savePref(key, value);
  43. this.setState({
  44. proxiwashNotifPickerSelected: value
  45. });
  46. let intVal = 0;
  47. if (value !== 'never')
  48. intVal = parseInt(value);
  49. setMachineReminderNotificationTime(intVal);
  50. }
  51. };
  52. /**
  53. * Saves the value for the proxiwash reminder notification time
  54. *
  55. * @param value The value to store
  56. */
  57. onStartScreenPickerValueChange = (value: string) => {
  58. if (value != null) {
  59. let key = AsyncStorageManager.getInstance().preferences.defaultStartScreen.key;
  60. AsyncStorageManager.getInstance().savePref(key, value);
  61. this.setState({
  62. startScreenPickerSelected: value
  63. });
  64. }
  65. };
  66. /**
  67. * Returns a picker allowing the user to select the proxiwash reminder notification time
  68. *
  69. * @returns {React.Node}
  70. */
  71. getProxiwashNotifPicker() {
  72. return (
  73. <ToggleButton.Row
  74. onValueChange={this.onProxiwashNotifPickerValueChange}
  75. value={this.state.proxiwashNotifPickerSelected}
  76. style={{marginLeft: 'auto', marginRight: 'auto'}}
  77. >
  78. <ToggleButton icon="close" value="never"/>
  79. <ToggleButton icon="numeric-2" value="2"/>
  80. <ToggleButton icon="numeric-5" value="5"/>
  81. </ToggleButton.Row>
  82. );
  83. }
  84. /**
  85. * Returns a picker allowing the user to select the start screen
  86. *
  87. * @returns {React.Node}
  88. */
  89. getStartScreenPicker() {
  90. return (
  91. <ToggleButton.Row
  92. onValueChange={this.onStartScreenPickerValueChange}
  93. value={this.state.startScreenPickerSelected}
  94. style={{marginLeft: 'auto', marginRight: 'auto'}}
  95. >
  96. <ToggleButton icon="account-circle" value="services"/>
  97. <ToggleButton icon="tshirt-crew" value="proxiwash"/>
  98. <ToggleButton icon="triangle" value="home"/>
  99. <ToggleButton icon="calendar-range" value="planning"/>
  100. <ToggleButton icon="clock" value="planex"/>
  101. </ToggleButton.Row>
  102. );
  103. }
  104. /**
  105. * Toggles night mode and saves it to preferences
  106. */
  107. onToggleNightMode = () => {
  108. ThemeManager.getInstance().setNightMode(!this.state.nightMode);
  109. this.setState({nightMode: !this.state.nightMode});
  110. };
  111. onToggleNightModeFollowSystem = () => {
  112. const value = !this.state.nightModeFollowSystem;
  113. this.setState({nightModeFollowSystem: value});
  114. let key = AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.key;
  115. AsyncStorageManager.getInstance().savePref(key, value ? '1' : '0');
  116. if (value) {
  117. const nightMode = Appearance.getColorScheme() === 'dark';
  118. ThemeManager.getInstance().setNightMode(nightMode);
  119. this.setState({nightMode: nightMode});
  120. }
  121. };
  122. /**
  123. * Gets a list item using a checkbox control
  124. *
  125. * @param onPressCallback The callback when the checkbox state changes
  126. * @param icon The icon name to display on the list item
  127. * @param title The text to display as this list item title
  128. * @param subtitle The text to display as this list item subtitle
  129. * @returns {React.Node}
  130. */
  131. getToggleItem(onPressCallback: Function, icon: string, title: string, subtitle: string, state: boolean) {
  132. return (
  133. <List.Item
  134. title={title}
  135. description={subtitle}
  136. left={props => <List.Icon {...props} icon={icon}/>}
  137. right={props =>
  138. <Switch
  139. value={state}
  140. onValueChange={onPressCallback}
  141. />}
  142. />
  143. );
  144. }
  145. render() {
  146. return (
  147. <ScrollView>
  148. <Card style={{margin: 5}}>
  149. <Card.Title title={i18n.t('settingsScreen.generalCard')}/>
  150. <List.Section>
  151. {Appearance.getColorScheme() !== 'no-preference' ? this.getToggleItem(
  152. this.onToggleNightModeFollowSystem,
  153. 'theme-light-dark',
  154. i18n.t('settingsScreen.nightModeAuto'),
  155. this.state.nightMode ?
  156. i18n.t('settingsScreen.nightModeSubOn') :
  157. i18n.t('settingsScreen.nightModeSubOff'),
  158. this.state.nightModeFollowSystem
  159. ) : null}
  160. {
  161. Appearance.getColorScheme() === 'no-preference' || !this.state.nightModeFollowSystem ?
  162. this.getToggleItem(
  163. this.onToggleNightMode,
  164. 'theme-light-dark',
  165. i18n.t('settingsScreen.nightMode'),
  166. this.state.nightMode ?
  167. i18n.t('settingsScreen.nightModeSubOn') :
  168. i18n.t('settingsScreen.nightModeSubOff'),
  169. this.state.nightMode
  170. ) : null
  171. }
  172. <AnimatedAccordion
  173. title={i18n.t('settingsScreen.startScreen')}
  174. subtitle={i18n.t('settingsScreen.startScreenSub')}
  175. left={props => <List.Icon {...props} icon="power"/>}
  176. >
  177. {this.getStartScreenPicker()}
  178. </AnimatedAccordion>
  179. </List.Section>
  180. </Card>
  181. <Card style={{margin: 5}}>
  182. <Card.Title title="Proxiwash"/>
  183. <List.Section>
  184. <AnimatedAccordion
  185. title={i18n.t('settingsScreen.proxiwashNotifReminder')}
  186. description={i18n.t('settingsScreen.proxiwashNotifReminderSub')}
  187. left={props => <List.Icon {...props} icon="washing-machine"/>}
  188. >
  189. {this.getProxiwashNotifPicker()}
  190. </AnimatedAccordion>
  191. </List.Section>
  192. </Card>
  193. </ScrollView>
  194. );
  195. }
  196. }