// @flow import * as React from 'react'; import {FlatList, View} from "react-native"; import AsyncStorageManager from "../../managers/AsyncStorageManager"; import CustomModal from "../../components/Overrides/CustomModal"; import {Button, List, Subheading, TextInput, Title, withTheme} from 'react-native-paper'; import {StackNavigationProp} from "@react-navigation/stack"; import {Modalize} from "react-native-modalize"; import type {CustomTheme} from "../../managers/ThemeManager"; type PreferenceItem = { key: string, default: string, current: string, } type Props = { navigation: StackNavigationProp, theme: CustomTheme }; type State = { modalCurrentDisplayItem: PreferenceItem, currentPreferences: Array, } /** * Class defining the Debug screen. * This screen allows the user to get and modify information on the app/device. */ class DebugScreen extends React.Component { modalRef: Modalize; modalInputValue: string; /** * Copies user preferences to state for easier manipulation * * @param props */ constructor(props) { super(props); this.modalInputValue = ""; let copy = {...AsyncStorageManager.getInstance().preferences}; let currentPreferences : Array = []; Object.values(copy).map((object: any) => { currentPreferences.push(object); }); this.state = { modalCurrentDisplayItem: {}, currentPreferences: currentPreferences }; } /** * Shows the edit modal * * @param item */ showEditModal(item: PreferenceItem) { this.setState({ modalCurrentDisplayItem: item }); if (this.modalRef) { this.modalRef.open(); } } /** * Gets the edit modal content * * @return {*} */ getModalContent() { return ( {this.state.modalCurrentDisplayItem.key} Default: {this.state.modalCurrentDisplayItem.default} Current: {this.state.modalCurrentDisplayItem.current} this.modalInputValue = text} /> ); } /** * Finds the index of the given key in the preferences array * * @param key THe key to find the index of * @returns {number} */ findIndexOfKey(key: string) { let index = -1; for (let i = 0; i < this.state.currentPreferences.length; i++) { if (this.state.currentPreferences[i].key === key) { index = i; break; } } return index; } /** * Saves the new value of the given preference * * @param key The pref key * @param value The pref value */ saveNewPrefs(key: string, value: string) { this.setState((prevState) => { let currentPreferences = [...prevState.currentPreferences]; currentPreferences[this.findIndexOfKey(key)].current = value; return {currentPreferences}; }); AsyncStorageManager.getInstance().savePref(key, value); this.modalRef.close(); } /** * Callback used when receiving the modal ref * * @param ref */ onModalRef = (ref: Modalize) => { this.modalRef = ref; } renderItem = ({item}: {item: PreferenceItem}) => { return ( this.showEditModal(item)} /> ); }; render() { return ( {this.getModalContent()} {/*$FlowFixMe*/} ); } } export default withTheme(DebugScreen);