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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // @flow
  2. import * as React from 'react';
  3. import {FlatList, View} from "react-native";
  4. import AsyncStorageManager from "../../managers/AsyncStorageManager";
  5. import CustomModal from "../../components/Overrides/CustomModal";
  6. import {Button, List, Subheading, TextInput, Title, withTheme} from 'react-native-paper';
  7. type Props = {
  8. navigation: Object,
  9. };
  10. type State = {
  11. modalCurrentDisplayItem: Object,
  12. currentPreferences: Array<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. onModalRef: Function;
  22. colors: Object;
  23. constructor(props) {
  24. super(props);
  25. this.onModalRef = this.onModalRef.bind(this);
  26. this.colors = props.theme.colors;
  27. let copy = {...AsyncStorageManager.getInstance().preferences};
  28. let currentPreferences = [];
  29. Object.values(copy).map((object) => {
  30. currentPreferences.push(object);
  31. });
  32. this.state = {
  33. modalCurrentDisplayItem: {},
  34. currentPreferences: currentPreferences
  35. };
  36. }
  37. /**
  38. * Show the edit modal
  39. * @param item
  40. */
  41. showEditModal(item: Object) {
  42. this.setState({
  43. modalCurrentDisplayItem: item
  44. });
  45. if (this.modalRef) {
  46. this.modalRef.open();
  47. }
  48. }
  49. /**
  50. * Gets the edit modal content
  51. *
  52. * @return {*}
  53. */
  54. getModalContent() {
  55. return (
  56. <View style={{
  57. flex: 1,
  58. padding: 20
  59. }}>
  60. <Title>{this.state.modalCurrentDisplayItem.key}</Title>
  61. <Subheading>Default: {this.state.modalCurrentDisplayItem.default}</Subheading>
  62. <Subheading>Current: {this.state.modalCurrentDisplayItem.current}</Subheading>
  63. <TextInput
  64. label='New Value'
  65. onChangeText={(text) => this.modalInputValue = text}
  66. />
  67. <View style={{
  68. flexDirection: 'row',
  69. marginTop: 10,
  70. }}>
  71. <Button
  72. mode="contained"
  73. dark={true}
  74. color={this.colors.success}
  75. onPress={() => this.saveNewPrefs(this.state.modalCurrentDisplayItem.key, this.modalInputValue)}>
  76. Save new value
  77. </Button>
  78. <Button
  79. mode="contained"
  80. dark={true}
  81. color={this.colors.danger}
  82. onPress={() => this.saveNewPrefs(this.state.modalCurrentDisplayItem.key, this.state.modalCurrentDisplayItem.default)}>
  83. Reset to default
  84. </Button>
  85. </View>
  86. </View>
  87. );
  88. }
  89. findIndexOfKey(key: string) {
  90. let index = -1;
  91. for (let i = 0; i < this.state.currentPreferences.length; i++) {
  92. if (this.state.currentPreferences[i].key === key) {
  93. index = i;
  94. break;
  95. }
  96. }
  97. return index;
  98. }
  99. /**
  100. * Saves the new value of the given preference
  101. *
  102. * @param key The pref key
  103. * @param value The pref value
  104. */
  105. saveNewPrefs(key: string, value: string) {
  106. this.setState((prevState) => {
  107. let currentPreferences = [...prevState.currentPreferences];
  108. currentPreferences[this.findIndexOfKey(key)].current = value;
  109. return {currentPreferences};
  110. });
  111. AsyncStorageManager.getInstance().savePref(key, value);
  112. this.modalRef.close();
  113. }
  114. /**
  115. * Callback used when receiving the modal ref
  116. *
  117. * @param ref
  118. */
  119. onModalRef(ref: Object) {
  120. this.modalRef = ref;
  121. }
  122. renderItem = ({item}: Object) => {
  123. return (
  124. <List.Item
  125. title={item.key}
  126. description={'Click to edit'}
  127. onPress={() => this.showEditModal(item)}
  128. />
  129. );
  130. };
  131. render() {
  132. return (
  133. <View>
  134. <CustomModal onRef={this.onModalRef}>
  135. {this.getModalContent()}
  136. </CustomModal>
  137. {/*$FlowFixMe*/}
  138. <FlatList
  139. data={this.state.currentPreferences}
  140. extraData={this.state.currentPreferences}
  141. renderItem={this.renderItem}
  142. />
  143. </View>
  144. );
  145. }
  146. }
  147. export default withTheme(DebugScreen);