// @flow import * as React from 'react'; import { Body, Card, CardItem, CheckBox, Container, Content, Left, List, ListItem, Picker, Right, Text, } from "native-base"; import CustomHeader from "../components/CustomHeader"; import ThemeManager from '../utils/ThemeManager'; import i18n from "i18n-js"; import {NavigationActions, StackActions} from "react-navigation"; import CustomMaterialIcon from "../components/CustomMaterialIcon"; import AsyncStorageManager from "../utils/AsyncStorageManager"; import NotificationsManager from "../utils/NotificationsManager"; type Props = { navigation: Object, }; type State = { nightMode: boolean, proxiwashNotifPickerSelected: string, startScreenPickerSelected: string, }; /** * Class defining the Settings screen. This screen shows controls to modify app preferences. */ export default class SettingsScreen extends React.Component { state = { nightMode: ThemeManager.getNightMode(), proxiwashNotifPickerSelected: AsyncStorageManager.getInstance().preferences.proxiwashNotifications.current, startScreenPickerSelected: AsyncStorageManager.getInstance().preferences.defaultStartScreen.current, }; /** * Get a list item using the specified control * * @param control The custom control to use * @param icon The icon name to display on the list item * @param title The text to display as this list item title * @param subtitle The text to display as this list item subtitle * @returns {React.Node} */ static getGeneralItem(control: React.Node, icon: string, title: string, subtitle: string) { return ( {title} {subtitle} {control} ); } /** * Save the value for the proxiwash reminder notification time * * @param value The value to store */ onProxiwashNotifPickerValueChange(value: string) { let key = AsyncStorageManager.getInstance().preferences.proxiwashNotifications.key; AsyncStorageManager.getInstance().savePref(key, value); this.setState({ proxiwashNotifPickerSelected: value }); let intVal = 0; if (value !== 'never') intVal = parseInt(value); NotificationsManager.setMachineReminderNotificationTime(intVal); } /** * Save the value for the proxiwash reminder notification time * * @param value The value to store */ onStartScreenPickerValueChange(value: string) { let key = AsyncStorageManager.getInstance().preferences.defaultStartScreen.key; AsyncStorageManager.getInstance().savePref(key, value); this.setState({ startScreenPickerSelected: value }); } /** * Returns a picker allowing the user to select the proxiwash reminder notification time * * @returns {React.Node} */ getProxiwashNotifPicker() { return ( this.onProxiwashNotifPickerValueChange(value)} > ); } /** * Returns a picker allowing the user to select the start screen * * @returns {React.Node} */ getStartScreenPicker() { return ( this.onStartScreenPickerValueChange(value)} > ); } /** * Toggle night mode and save it to preferences */ toggleNightMode() { ThemeManager.getInstance().setNightMode(!this.state.nightMode); this.setState({nightMode: !this.state.nightMode}); this.resetStack(); } /** * Reset react navigation stack to allow for a theme reset */ resetStack() { const resetAction = StackActions.reset({ index: 0, key: null, actions: [NavigationActions.navigate({routeName: 'Main'})], }); this.props.navigation.dispatch(resetAction); } /** * Get a list item using a checkbox control * * @param onPressCallback The callback when the checkbox state changes * @param icon The icon name to display on the list item * @param title The text to display as this list item title * @param subtitle The text to display as this list item subtitle * @returns {React.Node} */ getToggleItem(onPressCallback: Function, icon: string, title: string, subtitle: string) { return ( {title} {subtitle} this.toggleNightMode()} style={{marginRight: 20}}/> ); } render() { const nav = this.props.navigation; return ( {i18n.t('settingsScreen.generalCard')} {this.getToggleItem(() => this.toggleNightMode(), 'theme-light-dark', i18n.t('settingsScreen.nightMode'), i18n.t('settingsScreen.nightModeSub'))} {SettingsScreen.getGeneralItem(this.getStartScreenPicker(), 'power', i18n.t('settingsScreen.startScreen'), i18n.t('settingsScreen.startScreenSub'))} Proxiwash {SettingsScreen.getGeneralItem(this.getProxiwashNotifPicker(), 'washing-machine', i18n.t('settingsScreen.proxiwashNotifReminder'), i18n.t('settingsScreen.proxiwashNotifReminderSub'))} ); } }