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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // @flow
  2. import * as React from 'react';
  3. import {
  4. Body,
  5. Card,
  6. CardItem,
  7. CheckBox,
  8. Container,
  9. Content,
  10. Left,
  11. List,
  12. ListItem,
  13. Picker,
  14. Right,
  15. Text,
  16. } from "native-base";
  17. import CustomHeader from "../components/CustomHeader";
  18. import ThemeManager from '../utils/ThemeManager';
  19. import i18n from "i18n-js";
  20. import {NavigationActions, StackActions} from "react-navigation";
  21. import CustomMaterialIcon from "../components/CustomMaterialIcon";
  22. import AsyncStorageManager from "../utils/AsyncStorageManager";
  23. import Touchable from "react-native-platform-touchable";
  24. import {Platform} from "react-native";
  25. import NotificationsManager from "../utils/NotificationsManager";
  26. type Props = {
  27. navigation: Object,
  28. };
  29. type State = {
  30. nightMode: boolean,
  31. proxiwashNotifPickerSelected: string,
  32. startScreenPickerSelected: string,
  33. };
  34. /**
  35. * Class defining the Settings screen. This screen shows controls to modify app preferences.
  36. */
  37. export default class SettingsScreen extends React.Component<Props, State> {
  38. state = {
  39. nightMode: ThemeManager.getNightMode(),
  40. proxiwashNotifPickerSelected: AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current,
  41. startScreenPickerSelected: AsyncStorageManager.getInstance().preferences.defaultStartScreen.current,
  42. };
  43. /**
  44. * Save the value for the proxiwash reminder notification time
  45. *
  46. * @param value The value to store
  47. */
  48. onProxiwashNotifPickerValueChange(value: string) {
  49. let key = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.key;
  50. AsyncStorageManager.getInstance().savePref(key, value);
  51. this.setState({
  52. proxiwashNotifPickerSelected: value
  53. });
  54. let intVal = 0;
  55. if (value !== 'never')
  56. intVal = parseInt(value);
  57. NotificationsManager.setMachineReminderNotificationTime(intVal);
  58. }
  59. /**
  60. * Save the value for the proxiwash reminder notification time
  61. *
  62. * @param value The value to store
  63. */
  64. onStartScreenPickerValueChange(value: string) {
  65. let key = AsyncStorageManager.getInstance().preferences.defaultStartScreen.key;
  66. AsyncStorageManager.getInstance().savePref(key, value);
  67. this.setState({
  68. startScreenPickerSelected: value
  69. });
  70. }
  71. /**
  72. * Returns a picker allowing the user to select the proxiwash reminder notification time
  73. *
  74. * @returns {React.Node}
  75. */
  76. getProxiwashNotifPicker() {
  77. return (
  78. <Picker
  79. note
  80. mode="dropdown"
  81. style={{width: 120}}
  82. selectedValue={this.state.proxiwashNotifPickerSelected}
  83. onValueChange={(value) => this.onProxiwashNotifPickerValueChange(value)}
  84. >
  85. <Picker.Item label={i18n.t('settingsScreen.proxiwashNotifReminderPicker.never')} value="never"/>
  86. <Picker.Item label={i18n.t('settingsScreen.proxiwashNotifReminderPicker.5')} value="5"/>
  87. <Picker.Item label={i18n.t('settingsScreen.proxiwashNotifReminderPicker.10')} value="10"/>
  88. <Picker.Item label={i18n.t('settingsScreen.proxiwashNotifReminderPicker.20')} value="20"/>
  89. <Picker.Item label={i18n.t('settingsScreen.proxiwashNotifReminderPicker.30')} value="30"/>
  90. </Picker>
  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. <Picker
  101. note
  102. mode="dropdown"
  103. style={{width: 120}}
  104. selectedValue={this.state.startScreenPickerSelected}
  105. onValueChange={(value) => this.onStartScreenPickerValueChange(value)}
  106. >
  107. <Picker.Item label={i18n.t('screens.home')} value="Home"/>
  108. <Picker.Item label={i18n.t('screens.planning')} value="Planning"/>
  109. <Picker.Item label={i18n.t('screens.proxiwash')} value="Proxiwash"/>
  110. <Picker.Item label={i18n.t('screens.proximo')} value="Proximo"/>
  111. <Picker.Item label={'Planex'} value="Planex"/>
  112. </Picker>
  113. );
  114. }
  115. /**
  116. * Toggle night mode and save it to preferences
  117. */
  118. toggleNightMode() {
  119. ThemeManager.getInstance().setNightMode(!this.state.nightMode);
  120. this.setState({nightMode: !this.state.nightMode});
  121. this.resetStack();
  122. }
  123. /**
  124. * Reset react navigation stack to allow for a theme reset
  125. */
  126. resetStack() {
  127. const resetAction = StackActions.reset({
  128. index: 0,
  129. key: null,
  130. actions: [NavigationActions.navigate({routeName: 'Main'})],
  131. });
  132. this.props.navigation.dispatch(resetAction);
  133. // this.props.navigation.navigate('SettingsScreen');
  134. }
  135. /**
  136. * Get 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. * @returns {React.Node}
  143. */
  144. getToggleItem(onPressCallback: Function, icon: string, title: string, subtitle: string) {
  145. return (
  146. <ListItem
  147. button
  148. thumbnail
  149. onPress={onPressCallback}
  150. >
  151. <Left>
  152. <CustomMaterialIcon icon={icon}/>
  153. </Left>
  154. <Body>
  155. <Text>
  156. {title}
  157. </Text>
  158. <Text note>
  159. {subtitle}
  160. </Text>
  161. </Body>
  162. <Right>
  163. <CheckBox
  164. checked={this.state.nightMode}
  165. onPress={() => this.toggleNightMode()}
  166. style={{marginRight: 20}}/>
  167. </Right>
  168. </ListItem>
  169. );
  170. }
  171. /**
  172. * Get a list item using the specified control
  173. *
  174. * @param control The custom control to use
  175. * @param icon The icon name to display on the list item
  176. * @param title The text to display as this list item title
  177. * @param subtitle The text to display as this list item subtitle
  178. * @returns {React.Node}
  179. */
  180. static getGeneralItem(control: React.Node, icon: string, title: string, subtitle: string) {
  181. return (
  182. <ListItem
  183. thumbnail
  184. >
  185. <Left>
  186. <CustomMaterialIcon icon={icon}/>
  187. </Left>
  188. <Body>
  189. <Text>
  190. {title}
  191. </Text>
  192. <Text note>
  193. {subtitle}
  194. </Text>
  195. </Body>
  196. <Right>
  197. {control}
  198. </Right>
  199. </ListItem>
  200. );
  201. }
  202. getRightButton() {
  203. return (
  204. <Touchable
  205. style={{padding: 6}}
  206. onPress={() => this.props.navigation.navigate('AboutScreen')}>
  207. <CustomMaterialIcon
  208. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  209. icon="information"/>
  210. </Touchable>
  211. );
  212. }
  213. render() {
  214. const nav = this.props.navigation;
  215. return (
  216. <Container>
  217. <CustomHeader navigation={nav} title={i18n.t('screens.settings')} hasBackButton={true}
  218. rightButton={this.getRightButton()}/>
  219. <Content padder>
  220. <Card>
  221. <CardItem header>
  222. <Text>{i18n.t('settingsScreen.generalCard')}</Text>
  223. </CardItem>
  224. <List>
  225. {this.getToggleItem(() => this.toggleNightMode(), 'theme-light-dark', i18n.t('settingsScreen.nightMode'), i18n.t('settingsScreen.nightModeSub'))}
  226. {SettingsScreen.getGeneralItem(this.getStartScreenPicker(), 'power', i18n.t('settingsScreen.startScreen'), i18n.t('settingsScreen.startScreenSub'))}
  227. </List>
  228. </Card>
  229. <Card>
  230. <CardItem header>
  231. <Text>Proxiwash</Text>
  232. </CardItem>
  233. <List>
  234. {SettingsScreen.getGeneralItem(this.getProxiwashNotifPicker(), 'washing-machine', i18n.t('settingsScreen.proxiwashNotifReminder'), i18n.t('settingsScreen.proxiwashNotifReminderSub'))}
  235. </List>
  236. </Card>
  237. </Content>
  238. </Container>
  239. );
  240. }
  241. }