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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import {
  5. Button,
  6. List,
  7. Subheading,
  8. TextInput,
  9. Title,
  10. withTheme,
  11. } from 'react-native-paper';
  12. import {Modalize} from 'react-native-modalize';
  13. import CustomModal from '../../components/Overrides/CustomModal';
  14. import AsyncStorageManager from '../../managers/AsyncStorageManager';
  15. import type {CustomThemeType} from '../../managers/ThemeManager';
  16. import CollapsibleFlatList from '../../components/Collapsible/CollapsibleFlatList';
  17. type PreferenceItemType = {
  18. key: string,
  19. default: string,
  20. current: string,
  21. };
  22. type PropsType = {
  23. theme: CustomThemeType,
  24. };
  25. type StateType = {
  26. modalCurrentDisplayItem: PreferenceItemType,
  27. currentPreferences: Array<PreferenceItemType>,
  28. };
  29. /**
  30. * Class defining the Debug screen.
  31. * This screen allows the user to get and modify information on the app/device.
  32. */
  33. class DebugScreen extends React.Component<PropsType, StateType> {
  34. modalRef: Modalize;
  35. modalInputValue: string;
  36. /**
  37. * Copies user preferences to state for easier manipulation
  38. *
  39. * @param props
  40. */
  41. constructor(props: PropsType) {
  42. super(props);
  43. this.modalInputValue = '';
  44. const currentPreferences: Array<PreferenceItemType> = [];
  45. // eslint-disable-next-line flowtype/no-weak-types
  46. Object.values(AsyncStorageManager.PREFERENCES).forEach((object: any) => {
  47. const newObject: PreferenceItemType = {...object};
  48. newObject.current = AsyncStorageManager.getString(newObject.key);
  49. currentPreferences.push(newObject);
  50. });
  51. this.state = {
  52. modalCurrentDisplayItem: {},
  53. currentPreferences,
  54. };
  55. }
  56. /**
  57. * Gets the edit modal content
  58. *
  59. * @return {*}
  60. */
  61. getModalContent(): React.Node {
  62. const {props, state} = this;
  63. return (
  64. <View
  65. style={{
  66. flex: 1,
  67. padding: 20,
  68. }}>
  69. <Title>{state.modalCurrentDisplayItem.key}</Title>
  70. <Subheading>
  71. Default: {state.modalCurrentDisplayItem.default}
  72. </Subheading>
  73. <Subheading>
  74. Current: {state.modalCurrentDisplayItem.current}
  75. </Subheading>
  76. <TextInput
  77. label="New Value"
  78. onChangeText={(text: string) => {
  79. this.modalInputValue = text;
  80. }}
  81. />
  82. <View
  83. style={{
  84. flexDirection: 'row',
  85. marginTop: 10,
  86. }}>
  87. <Button
  88. mode="contained"
  89. dark
  90. color={props.theme.colors.success}
  91. onPress={() => {
  92. this.saveNewPrefs(
  93. state.modalCurrentDisplayItem.key,
  94. this.modalInputValue,
  95. );
  96. }}>
  97. Save new value
  98. </Button>
  99. <Button
  100. mode="contained"
  101. dark
  102. color={props.theme.colors.danger}
  103. onPress={() => {
  104. this.saveNewPrefs(
  105. state.modalCurrentDisplayItem.key,
  106. state.modalCurrentDisplayItem.default,
  107. );
  108. }}>
  109. Reset to default
  110. </Button>
  111. </View>
  112. </View>
  113. );
  114. }
  115. getRenderItem = ({item}: {item: PreferenceItemType}): React.Node => {
  116. return (
  117. <List.Item
  118. title={item.key}
  119. description="Click to edit"
  120. onPress={() => {
  121. this.showEditModal(item);
  122. }}
  123. />
  124. );
  125. };
  126. /**
  127. * Callback used when receiving the modal ref
  128. *
  129. * @param ref
  130. */
  131. onModalRef = (ref: Modalize) => {
  132. this.modalRef = ref;
  133. };
  134. /**
  135. * Shows the edit modal
  136. *
  137. * @param item
  138. */
  139. showEditModal(item: PreferenceItemType) {
  140. this.setState({
  141. modalCurrentDisplayItem: item,
  142. });
  143. if (this.modalRef) this.modalRef.open();
  144. }
  145. /**
  146. * Finds the index of the given key in the preferences array
  147. *
  148. * @param key THe key to find the index of
  149. * @returns {number}
  150. */
  151. findIndexOfKey(key: string): number {
  152. const {currentPreferences} = this.state;
  153. let index = -1;
  154. for (let i = 0; i < currentPreferences.length; i += 1) {
  155. if (currentPreferences[i].key === key) {
  156. index = i;
  157. break;
  158. }
  159. }
  160. return index;
  161. }
  162. /**
  163. * Saves the new value of the given preference
  164. *
  165. * @param key The pref key
  166. * @param value The pref value
  167. */
  168. saveNewPrefs(key: string, value: string) {
  169. this.setState((prevState: StateType): {
  170. currentPreferences: Array<PreferenceItemType>,
  171. } => {
  172. const currentPreferences = [...prevState.currentPreferences];
  173. currentPreferences[this.findIndexOfKey(key)].current = value;
  174. return {currentPreferences};
  175. });
  176. AsyncStorageManager.set(key, value);
  177. this.modalRef.close();
  178. }
  179. render(): React.Node {
  180. const {state} = this;
  181. return (
  182. <View>
  183. <CustomModal onRef={this.onModalRef}>
  184. {this.getModalContent()}
  185. </CustomModal>
  186. {/* $FlowFixMe */}
  187. <CollapsibleFlatList
  188. data={state.currentPreferences}
  189. extraData={state.currentPreferences}
  190. renderItem={this.getRenderItem}
  191. />
  192. </View>
  193. );
  194. }
  195. }
  196. export default withTheme(DebugScreen);