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.

SettingsScreen.js 8.4KB

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