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.

DebugScreen.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // @flow
  2. import * as React from 'react';
  3. import {ScrollView, View} from "react-native";
  4. import AsyncStorageManager from "../../utils/AsyncStorageManager";
  5. import CustomModal from "../../components/CustomModal";
  6. import {Button, Card, List, Subheading, TextInput, Title, withTheme} from 'react-native-paper';
  7. type Props = {
  8. navigation: Object,
  9. };
  10. type State = {
  11. modalCurrentDisplayItem: Object,
  12. currentPreferences: Object,
  13. }
  14. /**
  15. * Class defining the Debug screen.
  16. * This screen allows the user to get and modify information on the app/device.
  17. */
  18. class DebugScreen extends React.Component<Props, State> {
  19. modalRef: Object;
  20. modalInputValue = '';
  21. state = {
  22. modalCurrentDisplayItem: {},
  23. currentPreferences: JSON.parse(JSON.stringify(AsyncStorageManager.getInstance().preferences))
  24. };
  25. onModalRef: Function;
  26. colors: Object;
  27. constructor(props) {
  28. super(props);
  29. this.onModalRef = this.onModalRef.bind(this);
  30. this.colors = props.theme.colors;
  31. }
  32. /**
  33. * Gets a clickable list item
  34. *
  35. * @param onPressCallback The function to call when clicking on the item
  36. * @param icon The item's icon
  37. * @param title The item's title
  38. * @param subtitle The item's subtitle
  39. * @return {*}
  40. */
  41. static getGeneralItem(onPressCallback: Function, icon: ?string, title: string, subtitle: string) {
  42. if (icon !== undefined) {
  43. return (
  44. <List.Item
  45. title={title}
  46. description={subtitle}
  47. left={() => <List.Icon icon={icon}/>}
  48. onPress={onPressCallback}
  49. />
  50. );
  51. } else {
  52. return (
  53. <List.Item
  54. title={title}
  55. description={subtitle}
  56. onPress={onPressCallback}
  57. />
  58. );
  59. }
  60. }
  61. /**
  62. * Show the
  63. * @param item
  64. */
  65. showEditModal(item: Object) {
  66. this.setState({
  67. modalCurrentDisplayItem: item
  68. });
  69. if (this.modalRef) {
  70. this.modalRef.open();
  71. }
  72. }
  73. /**
  74. * Gets the edit modal content
  75. *
  76. * @return {*}
  77. */
  78. getModalContent() {
  79. return (
  80. <View style={{
  81. flex: 1,
  82. padding: 20
  83. }}>
  84. <Title>{this.state.modalCurrentDisplayItem.key}</Title>
  85. <Subheading>Default: {this.state.modalCurrentDisplayItem.default}</Subheading>
  86. <Subheading>Current: {this.state.modalCurrentDisplayItem.current}</Subheading>
  87. <TextInput
  88. label='New Value'
  89. onChangeText={(text) => this.modalInputValue = text}
  90. />
  91. <View style={{
  92. flexDirection: 'row',
  93. marginTop: 10,
  94. }}>
  95. <Button
  96. mode="contained"
  97. dark={true}
  98. color={this.colors.success}
  99. onPress={() => this.saveNewPrefs(this.state.modalCurrentDisplayItem.key, this.modalInputValue)}>
  100. Save new value
  101. </Button>
  102. <Button
  103. mode="contained"
  104. dark={true}
  105. color={this.colors.danger}
  106. onPress={() => this.saveNewPrefs(this.state.modalCurrentDisplayItem.key, this.state.modalCurrentDisplayItem.default)}>
  107. Reset to default
  108. </Button>
  109. </View>
  110. </View>
  111. );
  112. }
  113. /**
  114. * Saves the new value of the given preference
  115. *
  116. * @param key The pref key
  117. * @param value The pref value
  118. */
  119. saveNewPrefs(key: string, value: string) {
  120. this.setState((prevState) => {
  121. let currentPreferences = {...prevState.currentPreferences};
  122. currentPreferences[key].current = value;
  123. return {currentPreferences};
  124. });
  125. AsyncStorageManager.getInstance().savePref(key, value);
  126. }
  127. /**
  128. * Callback used when receiving the modal ref
  129. *
  130. * @param ref
  131. */
  132. onModalRef(ref: Object) {
  133. this.modalRef = ref;
  134. }
  135. render() {
  136. return (
  137. <View>
  138. <CustomModal onRef={this.onModalRef}>
  139. {this.getModalContent()}
  140. </CustomModal>
  141. <ScrollView style={{padding: 5}}>
  142. <Card style={{margin: 5}}>
  143. <Card.Title
  144. title={'Preferences'}
  145. />
  146. <Card.Content>
  147. {Object.values(this.state.currentPreferences).map((object) =>
  148. <View>
  149. {DebugScreen.getGeneralItem(
  150. () => this.showEditModal(object),
  151. undefined,
  152. //$FlowFixMe
  153. object.key,
  154. 'Click to edit')}
  155. </View>
  156. )}
  157. </Card.Content>
  158. </Card>
  159. </ScrollView>
  160. </View>
  161. );
  162. }
  163. }
  164. export default withTheme(DebugScreen);