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

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