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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // @flow
  2. import * as React from 'react';
  3. import {ScrollView, View} from "react-native";
  4. import type {CustomTheme} from "../../managers/ThemeManager";
  5. import ThemeManager from '../../managers/ThemeManager';
  6. import i18n from "i18n-js";
  7. import AsyncStorageManager from "../../managers/AsyncStorageManager";
  8. import {Card, List, Switch, ToggleButton, withTheme} from 'react-native-paper';
  9. import {Appearance} from "react-native-appearance";
  10. import CustomSlider from "../../components/Overrides/CustomSlider";
  11. import {StackNavigationProp} from "@react-navigation/stack";
  12. type Props = {
  13. navigation: StackNavigationProp,
  14. theme: CustomTheme,
  15. };
  16. type State = {
  17. nightMode: boolean,
  18. nightModeFollowSystem: boolean,
  19. notificationReminderSelected: number,
  20. startScreenPickerSelected: string,
  21. isDebugUnlocked: boolean,
  22. };
  23. /**
  24. * Class defining the Settings screen. This screen shows controls to modify app preferences.
  25. */
  26. class SettingsScreen extends React.Component<Props, State> {
  27. savedNotificationReminder: number;
  28. /**
  29. * Loads user preferences into state
  30. */
  31. constructor() {
  32. super();
  33. let notifReminder = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current;
  34. this.savedNotificationReminder = parseInt(notifReminder);
  35. if (isNaN(this.savedNotificationReminder))
  36. this.savedNotificationReminder = 0;
  37. this.state = {
  38. nightMode: ThemeManager.getNightMode(),
  39. nightModeFollowSystem: AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.current === '1' &&
  40. Appearance.getColorScheme() !== 'no-preference',
  41. notificationReminderSelected: this.savedNotificationReminder,
  42. startScreenPickerSelected: AsyncStorageManager.getInstance().preferences.defaultStartScreen.current,
  43. isDebugUnlocked: AsyncStorageManager.getInstance().preferences.debugUnlocked.current === '1'
  44. };
  45. }
  46. /**
  47. * Unlocks debug mode and saves its state to user preferences
  48. */
  49. unlockDebugMode = () => {
  50. this.setState({isDebugUnlocked: true});
  51. let key = AsyncStorageManager.getInstance().preferences.debugUnlocked.key;
  52. AsyncStorageManager.getInstance().savePref(key, '1');
  53. }
  54. /**
  55. * Saves the value for the proxiwash reminder notification time
  56. *
  57. * @param value The value to store
  58. */
  59. onProxiwashNotifPickerValueChange = (value: number) => {
  60. let key = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.key;
  61. AsyncStorageManager.getInstance().savePref(key, value.toString());
  62. this.setState({notificationReminderSelected: value})
  63. };
  64. /**
  65. * Saves the value for the proxiwash reminder notification time
  66. *
  67. * @param value The value to store
  68. */
  69. onStartScreenPickerValueChange = (value: string) => {
  70. if (value != null) {
  71. let key = AsyncStorageManager.getInstance().preferences.defaultStartScreen.key;
  72. AsyncStorageManager.getInstance().savePref(key, value);
  73. this.setState({
  74. startScreenPickerSelected: value
  75. });
  76. }
  77. };
  78. /**
  79. * Returns a picker allowing the user to select the proxiwash reminder notification time
  80. *
  81. * @returns {React.Node}
  82. */
  83. getProxiwashNotifPicker() {
  84. return (
  85. <CustomSlider
  86. style={{flex: 1, marginHorizontal: 10, height: 50}}
  87. minimumValue={0}
  88. maximumValue={10}
  89. step={1}
  90. value={this.savedNotificationReminder}
  91. onValueChange={this.onProxiwashNotifPickerValueChange}
  92. thumbTintColor={this.props.theme.colors.primary}
  93. minimumTrackTintColor={this.props.theme.colors.primary}
  94. />
  95. );
  96. }
  97. /**
  98. * Returns a picker allowing the user to select the start screen
  99. *
  100. * @returns {React.Node}
  101. */
  102. getStartScreenPicker() {
  103. return (
  104. <ToggleButton.Row
  105. onValueChange={this.onStartScreenPickerValueChange}
  106. value={this.state.startScreenPickerSelected}
  107. style={{marginLeft: 'auto', marginRight: 'auto'}}
  108. >
  109. <ToggleButton icon="account-circle" value="services"/>
  110. <ToggleButton icon="tshirt-crew" value="proxiwash"/>
  111. <ToggleButton icon="triangle" value="home"/>
  112. <ToggleButton icon="calendar-range" value="planning"/>
  113. <ToggleButton icon="clock" value="planex"/>
  114. </ToggleButton.Row>
  115. );
  116. }
  117. /**
  118. * Toggles night mode and saves it to preferences
  119. */
  120. onToggleNightMode = () => {
  121. ThemeManager.getInstance().setNightMode(!this.state.nightMode);
  122. this.setState({nightMode: !this.state.nightMode});
  123. };
  124. onToggleNightModeFollowSystem = () => {
  125. const value = !this.state.nightModeFollowSystem;
  126. this.setState({nightModeFollowSystem: value});
  127. let key = AsyncStorageManager.getInstance().preferences.nightModeFollowSystem.key;
  128. AsyncStorageManager.getInstance().savePref(key, value ? '1' : '0');
  129. if (value) {
  130. const nightMode = Appearance.getColorScheme() === 'dark';
  131. ThemeManager.getInstance().setNightMode(nightMode);
  132. this.setState({nightMode: nightMode});
  133. }
  134. };
  135. /**
  136. * Gets a list item using a checkbox control
  137. *
  138. * @param onPressCallback The callback when the checkbox state changes
  139. * @param icon The icon name to display on the list item
  140. * @param title The text to display as this list item title
  141. * @param subtitle The text to display as this list item subtitle
  142. * @param state The current state of the switch
  143. * @returns {React.Node}
  144. */
  145. getToggleItem(onPressCallback: Function, icon: string, title: string, subtitle: string, state: boolean) {
  146. return (
  147. <List.Item
  148. title={title}
  149. description={subtitle}
  150. left={props => <List.Icon {...props} icon={icon}/>}
  151. right={() =>
  152. <Switch
  153. value={state}
  154. onValueChange={onPressCallback}
  155. />}
  156. />
  157. );
  158. }
  159. render() {
  160. return (
  161. <ScrollView>
  162. <Card style={{margin: 5}}>
  163. <Card.Title title={i18n.t('screens.settings.generalCard')}/>
  164. <List.Section>
  165. {Appearance.getColorScheme() !== 'no-preference' ? this.getToggleItem(
  166. this.onToggleNightModeFollowSystem,
  167. 'theme-light-dark',
  168. i18n.t('screens.settings.nightModeAuto'),
  169. this.state.nightMode ?
  170. i18n.t('screens.settings.nightModeSubOn') :
  171. i18n.t('screens.settings.nightModeSubOff'),
  172. this.state.nightModeFollowSystem
  173. ) : null}
  174. {
  175. Appearance.getColorScheme() === 'no-preference' || !this.state.nightModeFollowSystem ?
  176. this.getToggleItem(
  177. this.onToggleNightMode,
  178. 'theme-light-dark',
  179. i18n.t('screens.settings.nightMode'),
  180. this.state.nightMode ?
  181. i18n.t('screens.settings.nightModeSubOn') :
  182. i18n.t('screens.settings.nightModeSubOff'),
  183. this.state.nightMode
  184. ) : null
  185. }
  186. <List.Item
  187. title={i18n.t('screens.settings.startScreen')}
  188. subtitle={i18n.t('screens.settings.startScreenSub')}
  189. left={props => <List.Icon {...props} icon="power"/>}
  190. />
  191. {this.getStartScreenPicker()}
  192. </List.Section>
  193. </Card>
  194. <Card style={{margin: 5}}>
  195. <Card.Title title="Proxiwash"/>
  196. <List.Section>
  197. <List.Item
  198. title={i18n.t('screens.settings.proxiwashNotifReminder')}
  199. description={i18n.t('screens.settings.proxiwashNotifReminderSub')}
  200. left={props => <List.Icon {...props} icon="washing-machine"/>}
  201. opened={true}
  202. />
  203. <View style={{marginLeft: 30}}>
  204. {this.getProxiwashNotifPicker()}
  205. </View>
  206. </List.Section>
  207. </Card>
  208. <Card style={{margin: 5}}>
  209. <Card.Title title={i18n.t('screens.settings.information')}/>
  210. <List.Section>
  211. {this.state.isDebugUnlocked
  212. ? <List.Item
  213. title={i18n.t('screens.debug.title')}
  214. left={props => <List.Icon {...props} icon="bug-check"/>}
  215. onPress={() => this.props.navigation.navigate("debug")}
  216. />
  217. : null}
  218. <List.Item
  219. title={i18n.t('screens.about.title')}
  220. description={i18n.t('screens.about.buttonDesc')}
  221. left={props => <List.Icon {...props} icon="information"/>}
  222. onPress={() => this.props.navigation.navigate("about")}
  223. onLongPress={this.unlockDebugMode}
  224. />
  225. <List.Item
  226. title={i18n.t('screens.feedback.homeButtonTitle')}
  227. description={i18n.t('screens.feedback.homeButtonSubtitle')}
  228. left={props => <List.Icon {...props} icon="bug"/>}
  229. onPress={() => this.props.navigation.navigate("feedback")}
  230. />
  231. </List.Section>
  232. </Card>
  233. </ScrollView>
  234. );
  235. }
  236. }
  237. export default withTheme(SettingsScreen);