// @flow import * as React from 'react'; import {View} from "react-native"; import type {CustomTheme} from "../../../managers/ThemeManager"; import ThemeManager from '../../../managers/ThemeManager'; import i18n from "i18n-js"; import AsyncStorageManager from "../../../managers/AsyncStorageManager"; import {Card, List, Switch, ToggleButton, withTheme} from 'react-native-paper'; import {Appearance} from "react-native-appearance"; import CustomSlider from "../../../components/Overrides/CustomSlider"; import {StackNavigationProp} from "@react-navigation/stack"; import CollapsibleScrollView from "../../../components/Collapsible/CollapsibleScrollView"; type Props = { navigation: StackNavigationProp, theme: CustomTheme, }; type State = { nightMode: boolean, nightModeFollowSystem: boolean, notificationReminderSelected: number, startScreenPickerSelected: string, isDebugUnlocked: boolean, }; /** * Class defining the Settings screen. This screen shows controls to modify app preferences. */ class SettingsScreen extends React.Component { savedNotificationReminder: number; /** * Loads user preferences into state */ constructor() { super(); let notifReminder = AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key); this.savedNotificationReminder = parseInt(notifReminder); if (isNaN(this.savedNotificationReminder)) this.savedNotificationReminder = 0; this.state = { nightMode: ThemeManager.getNightMode(), nightModeFollowSystem: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key) && Appearance.getColorScheme() !== 'no-preference', notificationReminderSelected: this.savedNotificationReminder, startScreenPickerSelected: AsyncStorageManager.getString(AsyncStorageManager.PREFERENCES.defaultStartScreen.key), isDebugUnlocked: AsyncStorageManager.getBool(AsyncStorageManager.PREFERENCES.debugUnlocked.key) }; } /** * Unlocks debug mode and saves its state to user preferences */ unlockDebugMode = () => { this.setState({isDebugUnlocked: true}); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.debugUnlocked.key, true); } /** * Saves the value for the proxiwash reminder notification time * * @param value The value to store */ onProxiwashNotifPickerValueChange = (value: number) => { this.setState({notificationReminderSelected: value}); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.proxiwashNotifications.key, value); }; /** * Saves the value for the proxiwash reminder notification time * * @param value The value to store */ onStartScreenPickerValueChange = (value: string) => { if (value != null) { this.setState({startScreenPickerSelected: value}); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.defaultStartScreen.key, value); } }; /** * Returns a picker allowing the user to select the proxiwash reminder notification time * * @returns {React.Node} */ getProxiwashNotifPicker() { return ( ); } /** * Returns a picker allowing the user to select the start screen * * @returns {React.Node} */ getStartScreenPicker() { return ( ); } /** * Toggles night mode and saves it to preferences */ onToggleNightMode = () => { ThemeManager.getInstance().setNightMode(!this.state.nightMode); this.setState({nightMode: !this.state.nightMode}); }; onToggleNightModeFollowSystem = () => { const value = !this.state.nightModeFollowSystem; this.setState({nightModeFollowSystem: value}); AsyncStorageManager.set(AsyncStorageManager.PREFERENCES.nightModeFollowSystem.key, value); if (value) { const nightMode = Appearance.getColorScheme() === 'dark'; ThemeManager.getInstance().setNightMode(nightMode); this.setState({nightMode: nightMode}); } }; /** * Gets 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 * @param state The current state of the switch * @returns {React.Node} */ getToggleItem(onPressCallback: Function, icon: string, title: string, subtitle: string, state: boolean) { return ( } right={() => } /> ); } getNavigateItem(route: string, icon: string, title: string, subtitle: string, onLongPress?: () => void) { return ( this.props.navigation.navigate(route)} left={props => } right={props => } onLongPress={onLongPress} /> ); } render() { return ( {Appearance.getColorScheme() !== 'no-preference' ? this.getToggleItem( this.onToggleNightModeFollowSystem, 'theme-light-dark', i18n.t('screens.settings.nightModeAuto'), i18n.t('screens.settings.nightModeAutoSub'), this.state.nightModeFollowSystem ) : null} { Appearance.getColorScheme() === 'no-preference' || !this.state.nightModeFollowSystem ? this.getToggleItem( this.onToggleNightMode, 'theme-light-dark', i18n.t('screens.settings.nightMode'), this.state.nightMode ? i18n.t('screens.settings.nightModeSubOn') : i18n.t('screens.settings.nightModeSubOff'), this.state.nightMode ) : null } } /> {this.getStartScreenPicker()} {this.getNavigateItem( "dashboard-edit", "view-dashboard", i18n.t('screens.settings.dashboard'), i18n.t('screens.settings.dashboardSub') )} } opened={true} /> {this.getProxiwashNotifPicker()} {this.state.isDebugUnlocked ? this.getNavigateItem( "debug", "bug-check", i18n.t('screens.debug.title'), "" ) : null} {this.getNavigateItem( "about", "information", i18n.t('screens.about.title'), i18n.t('screens.about.buttonDesc'), this.unlockDebugMode, )} {this.getNavigateItem( "feedback", "comment-quote", i18n.t('screens.feedback.homeButtonTitle'), i18n.t('screens.feedback.homeButtonSubtitle'), )} ); } } export default withTheme(SettingsScreen);