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

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