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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import i18n from 'i18n-js';
  5. import {Card, List, Switch, ToggleButton, withTheme} from 'react-native-paper';
  6. import {Appearance} from 'react-native-appearance';
  7. import {StackNavigationProp} from '@react-navigation/stack';
  8. import type {CustomThemeType} from '../../../managers/ThemeManager';
  9. import ThemeManager from '../../../managers/ThemeManager';
  10. import AsyncStorageManager from '../../../managers/AsyncStorageManager';
  11. import CustomSlider from '../../../components/Overrides/CustomSlider';
  12. import CollapsibleScrollView from '../../../components/Collapsible/CollapsibleScrollView';
  13. type PropsType = {
  14. navigation: StackNavigationProp,
  15. theme: CustomThemeType,
  16. };
  17. type StateType = {
  18. nightMode: boolean,
  19. nightModeFollowSystem: boolean,
  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<PropsType, StateType> {
  27. savedNotificationReminder: number;
  28. /**
  29. * Loads user preferences into state
  30. */
  31. constructor() {
  32. super();
  33. const notifReminder = AsyncStorageManager.getString(
  34. AsyncStorageManager.PREFERENCES.proxiwashNotifications.key,
  35. );
  36. this.savedNotificationReminder = parseInt(notifReminder, 10);
  37. if (Number.isNaN(this.savedNotificationReminder))
  38. this.savedNotificationReminder = 0;
  39. this.state = {
  40. nightMode: ThemeManager.getNightMode(),
  41. nightModeFollowSystem:
  42. AsyncStorageManager.getBool(
  43. AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key,
  44. ) && Appearance.getColorScheme() !== 'no-preference',
  45. startScreenPickerSelected: AsyncStorageManager.getString(
  46. AsyncStorageManager.PREFERENCES.defaultStartScreen.key,
  47. ),
  48. isDebugUnlocked: AsyncStorageManager.getBool(
  49. AsyncStorageManager.PREFERENCES.debugUnlocked.key,
  50. ),
  51. };
  52. }
  53. /**
  54. * Saves the value for the proxiwash reminder notification time
  55. *
  56. * @param value The value to store
  57. */
  58. onProxiwashNotifPickerValueChange = (value: number) => {
  59. AsyncStorageManager.set(
  60. AsyncStorageManager.PREFERENCES.proxiwashNotifications.key,
  61. value,
  62. );
  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. this.setState({startScreenPickerSelected: value});
  72. AsyncStorageManager.set(
  73. AsyncStorageManager.PREFERENCES.defaultStartScreen.key,
  74. 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(): React.Node {
  84. const {theme} = this.props;
  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={theme.colors.primary}
  94. minimumTrackTintColor={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(): React.Node {
  104. const {startScreenPickerSelected} = this.state;
  105. return (
  106. <ToggleButton.Row
  107. onValueChange={this.onStartScreenPickerValueChange}
  108. value={startScreenPickerSelected}
  109. style={{marginLeft: 'auto', marginRight: 'auto'}}>
  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. const {nightMode} = this.state;
  123. ThemeManager.getInstance().setNightMode(!nightMode);
  124. this.setState({nightMode: !nightMode});
  125. };
  126. onToggleNightModeFollowSystem = () => {
  127. const {nightModeFollowSystem} = this.state;
  128. const value = !nightModeFollowSystem;
  129. this.setState({nightModeFollowSystem: value});
  130. AsyncStorageManager.set(
  131. AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key,
  132. value,
  133. );
  134. if (value) {
  135. const nightMode = Appearance.getColorScheme() === 'dark';
  136. ThemeManager.getInstance().setNightMode(nightMode);
  137. this.setState({nightMode});
  138. }
  139. };
  140. /**
  141. * Gets a list item using a checkbox control
  142. *
  143. * @param onPressCallback The callback when the checkbox state changes
  144. * @param icon The icon name to display on the list item
  145. * @param title The text to display as this list item title
  146. * @param subtitle The text to display as this list item subtitle
  147. * @param state The current state of the switch
  148. * @returns {React.Node}
  149. */
  150. static getToggleItem(
  151. onPressCallback: () => void,
  152. icon: string,
  153. title: string,
  154. subtitle: string,
  155. state: boolean,
  156. ): React.Node {
  157. return (
  158. <List.Item
  159. title={title}
  160. description={subtitle}
  161. left={({size, color}: {size: number, color: number}): React.Node => (
  162. <List.Icon size={size} color={color} icon={icon} />
  163. )}
  164. right={(): React.Node => (
  165. <Switch value={state} onValueChange={onPressCallback} />
  166. )}
  167. />
  168. );
  169. }
  170. getNavigateItem(
  171. route: string,
  172. icon: string,
  173. title: string,
  174. subtitle: string,
  175. onLongPress?: () => void,
  176. ): React.Node {
  177. const {navigation} = this.props;
  178. return (
  179. <List.Item
  180. title={title}
  181. description={subtitle}
  182. onPress={() => {
  183. navigation.navigate(route);
  184. }}
  185. left={({size, color}: {size: number, color: number}): React.Node => (
  186. <List.Icon size={size} color={color} icon={icon} />
  187. )}
  188. right={({size, color}: {size: number, color: number}): React.Node => (
  189. <List.Icon size={size} color={color} icon="chevron-right" />
  190. )}
  191. onLongPress={onLongPress}
  192. />
  193. );
  194. }
  195. /**
  196. * Unlocks debug mode and saves its state to user preferences
  197. */
  198. unlockDebugMode = () => {
  199. this.setState({isDebugUnlocked: true});
  200. AsyncStorageManager.set(
  201. AsyncStorageManager.PREFERENCES.debugUnlocked.key,
  202. true,
  203. );
  204. };
  205. render(): React.Node {
  206. const {nightModeFollowSystem, nightMode, isDebugUnlocked} = this.state;
  207. return (
  208. <CollapsibleScrollView>
  209. <Card style={{margin: 5}}>
  210. <Card.Title title={i18n.t('screens.settings.generalCard')} />
  211. <List.Section>
  212. {Appearance.getColorScheme() !== 'no-preference'
  213. ? SettingsScreen.getToggleItem(
  214. this.onToggleNightModeFollowSystem,
  215. 'theme-light-dark',
  216. i18n.t('screens.settings.nightModeAuto'),
  217. i18n.t('screens.settings.nightModeAutoSub'),
  218. nightModeFollowSystem,
  219. )
  220. : null}
  221. {Appearance.getColorScheme() === 'no-preference' ||
  222. !nightModeFollowSystem
  223. ? SettingsScreen.getToggleItem(
  224. this.onToggleNightMode,
  225. 'theme-light-dark',
  226. i18n.t('screens.settings.nightMode'),
  227. nightMode
  228. ? i18n.t('screens.settings.nightModeSubOn')
  229. : i18n.t('screens.settings.nightModeSubOff'),
  230. nightMode,
  231. )
  232. : null}
  233. <List.Item
  234. title={i18n.t('screens.settings.startScreen')}
  235. description={i18n.t('screens.settings.startScreenSub')}
  236. left={({
  237. size,
  238. color,
  239. }: {
  240. size: number,
  241. color: number,
  242. }): React.Node => (
  243. <List.Icon size={size} color={color} icon="power" />
  244. )}
  245. />
  246. {this.getStartScreenPicker()}
  247. {this.getNavigateItem(
  248. 'dashboard-edit',
  249. 'view-dashboard',
  250. i18n.t('screens.settings.dashboard'),
  251. i18n.t('screens.settings.dashboardSub'),
  252. )}
  253. </List.Section>
  254. </Card>
  255. <Card style={{margin: 5}}>
  256. <Card.Title title="Proxiwash" />
  257. <List.Section>
  258. <List.Item
  259. title={i18n.t('screens.settings.proxiwashNotifReminder')}
  260. description={i18n.t('screens.settings.proxiwashNotifReminderSub')}
  261. left={({
  262. size,
  263. color,
  264. }: {
  265. size: number,
  266. color: number,
  267. }): React.Node => (
  268. <List.Icon size={size} color={color} icon="washing-machine" />
  269. )}
  270. />
  271. <View style={{marginLeft: 30}}>
  272. {this.getProxiwashNotifPicker()}
  273. </View>
  274. </List.Section>
  275. </Card>
  276. <Card style={{margin: 5}}>
  277. <Card.Title title={i18n.t('screens.settings.information')} />
  278. <List.Section>
  279. {isDebugUnlocked
  280. ? this.getNavigateItem(
  281. 'debug',
  282. 'bug-check',
  283. i18n.t('screens.debug.title'),
  284. '',
  285. )
  286. : null}
  287. {this.getNavigateItem(
  288. 'about',
  289. 'information',
  290. i18n.t('screens.about.title'),
  291. i18n.t('screens.about.buttonDesc'),
  292. this.unlockDebugMode,
  293. )}
  294. {this.getNavigateItem(
  295. 'feedback',
  296. 'comment-quote',
  297. i18n.t('screens.feedback.homeButtonTitle'),
  298. i18n.t('screens.feedback.homeButtonSubtitle'),
  299. )}
  300. </List.Section>
  301. </Card>
  302. </CollapsibleScrollView>
  303. );
  304. }
  305. }
  306. export default withTheme(SettingsScreen);