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.

DebugScreen.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // @flow
  2. import * as React from 'react';
  3. import {
  4. Body,
  5. Card,
  6. CardItem,
  7. Container,
  8. Content,
  9. H1,
  10. H3,
  11. Left,
  12. List,
  13. ListItem,
  14. Right,
  15. Text,
  16. Form,
  17. Item,
  18. Label,
  19. Input,
  20. Button
  21. } from "native-base";
  22. import CustomHeader from "../components/CustomHeader";
  23. import ThemeManager from '../utils/ThemeManager';
  24. import i18n from "i18n-js";
  25. import CustomMaterialIcon from "../components/CustomMaterialIcon";
  26. import Touchable from "react-native-platform-touchable";
  27. import {Alert, View, Clipboard, Image} from "react-native";
  28. import AsyncStorageManager from "../utils/AsyncStorageManager";
  29. import NotificationsManager from "../utils/NotificationsManager";
  30. import {Modalize} from "react-native-modalize";
  31. type Props = {
  32. navigation: Object,
  33. };
  34. type State = {
  35. modalCurrentDisplayItem: Object,
  36. currentPreferences: Object,
  37. }
  38. /**
  39. * Class defining the Debug screen. This screen allows the user to get detailed information on the app/device.
  40. */
  41. export default class DebugScreen extends React.Component<Props, State> {
  42. modalRef: { current: null | Modalize };
  43. modalInputValue = '';
  44. constructor(props: any) {
  45. super(props);
  46. this.modalRef = React.createRef();
  47. }
  48. state = {
  49. modalCurrentDisplayItem: {},
  50. currentPreferences: JSON.parse(JSON.stringify(AsyncStorageManager.getInstance().preferences))
  51. };
  52. alertCurrentExpoToken() {
  53. let token = AsyncStorageManager.getInstance().preferences.expoToken.current;
  54. console.log(token);
  55. Alert.alert(
  56. 'Expo Token',
  57. token,
  58. [
  59. {text: 'Copy', onPress: () => Clipboard.setString(token)},
  60. {text: 'OK'}
  61. ]
  62. );
  63. }
  64. async forceExpoTokenUpdate() {
  65. await NotificationsManager.forceExpoTokenUpdate();
  66. this.alertCurrentExpoToken();
  67. }
  68. static getGeneralItem(onPressCallback: Function, icon: ?string, title: string, subtitle: string) {
  69. return (
  70. <ListItem
  71. button
  72. thumbnail
  73. onPress={onPressCallback}
  74. >
  75. {icon !== undefined ?
  76. <Left>
  77. <CustomMaterialIcon icon={icon}/>
  78. </Left>
  79. : <View/>
  80. }
  81. <Body>
  82. <Text>
  83. {title}
  84. </Text>
  85. <Text note>
  86. {subtitle}
  87. </Text>
  88. </Body>
  89. <Right/>
  90. </ListItem>
  91. );
  92. }
  93. showEditModal(item: Object) {
  94. this.setState({
  95. modalCurrentDisplayItem: item
  96. });
  97. if (this.modalRef.current) {
  98. this.modalRef.current.open();
  99. }
  100. }
  101. getModalContent() {
  102. return (
  103. <View style={{
  104. flex: 1,
  105. padding: 20
  106. }}>
  107. <H1>{this.state.modalCurrentDisplayItem.key}</H1>
  108. <H3>Default: {this.state.modalCurrentDisplayItem.default}</H3>
  109. <H3>Current: {this.state.modalCurrentDisplayItem.current}</H3>
  110. <Form>
  111. <Item floatingLabel>
  112. <Label>New Value</Label>
  113. <Input onChangeText={(text) => this.modalInputValue = text}/>
  114. </Item>
  115. </Form>
  116. <View style={{
  117. flexDirection: 'row',
  118. marginTop: 10,
  119. }}>
  120. <Button success
  121. onPress={() => this.saveNewPrefs(this.state.modalCurrentDisplayItem.key, this.modalInputValue)}>
  122. <Text>Save new value</Text>
  123. </Button>
  124. <Button
  125. onPress={() => this.saveNewPrefs(this.state.modalCurrentDisplayItem.key, this.state.modalCurrentDisplayItem.default)}>
  126. <Text>Reset to default</Text>
  127. </Button>
  128. </View>
  129. </View>
  130. );
  131. }
  132. saveNewPrefs(key: string, value: string) {
  133. this.setState((prevState) => {
  134. let currentPreferences = {...prevState.currentPreferences};
  135. currentPreferences[key].current = value;
  136. return {currentPreferences};
  137. });
  138. AsyncStorageManager.getInstance().savePref(key, value);
  139. }
  140. render() {
  141. const nav = this.props.navigation;
  142. return (
  143. <Container>
  144. <Modalize
  145. ref={this.modalRef}
  146. adjustToContentHeight
  147. modalStyle={{backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor}}>
  148. {this.getModalContent()}
  149. </Modalize>
  150. <CustomHeader navigation={nav} title={i18n.t('screens.debug')} hasBackButton={true}/>
  151. <Content padder>
  152. <Card>
  153. <CardItem header>
  154. <Text>
  155. Notifications
  156. </Text>
  157. </CardItem>
  158. <List>
  159. {DebugScreen.getGeneralItem(() => this.alertCurrentExpoToken(), 'bell', 'Get current Expo Token', '')}
  160. {DebugScreen.getGeneralItem(() => this.forceExpoTokenUpdate(), 'bell-ring', 'Force Expo token update', '')}
  161. </List>
  162. </Card>
  163. <Card>
  164. <CardItem header>
  165. <Text>
  166. Preferences
  167. </Text>
  168. </CardItem>
  169. <List>
  170. {Object.values(this.state.currentPreferences).map((object) =>
  171. <View>
  172. {DebugScreen.getGeneralItem(() => this.showEditModal(object), undefined, object.key, 'Click to edit')}
  173. </View>
  174. )}
  175. </List>
  176. </Card>
  177. </Content>
  178. </Container>
  179. );
  180. }
  181. }