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

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