import React, { useCallback, useEffect, useState } from 'react'; import { ScrollView, TouchableWithoutFeedback, I18nManager, StyleSheet, Linking, View, TextInput } from 'react-native'; import { Button as ButtonRNElements } from 'react-native-elements'; import navigationStyle from '../../components/navigationStyle'; import { BlueCard, BlueCopyToClipboardButton, BlueLoading, BlueSpacing20, BlueText } from '../../BlueComponents'; import loc from '../../loc'; import { BlueCurrentTheme, useTheme } from '../../components/themes'; import Notifications from '../../blue_modules/notifications'; import presentAlert from '../../components/Alert'; import { Button } from '../../components/Button'; import ListItem from '../../components/ListItem'; const NotificationSettings = () => { const [isLoading, setIsLoading] = useState(true); const [isNotificationsEnabled, setNotificationsEnabled] = useState(false); const [isShowTokenInfo, setShowTokenInfo] = useState(0); const [tokenInfo, setTokenInfo] = useState(''); const [URI, setURI] = useState(); const { colors } = useTheme(); const onNotificationsSwitch = async value => { setNotificationsEnabled(value); // so the slider is not 'jumpy' if (value) { // user is ENABLING notifications await Notifications.cleanUserOptOutFlag(); if (await Notifications.getPushToken()) { // we already have a token, so we just need to reenable ALL level on groundcontrol: await Notifications.setLevels(true); } else { // ok, we dont have a token. we need to try to obtain permissions, configure callbacks and save token locally: await Notifications.tryToObtainPermissions(); } } else { // user is DISABLING notifications await Notifications.setLevels(false); } setNotificationsEnabled(await Notifications.isNotificationsEnabled()); }; useEffect(() => { (async () => { setNotificationsEnabled(await Notifications.isNotificationsEnabled()); setURI(await Notifications.getSavedUri()); setTokenInfo( 'token: ' + JSON.stringify(await Notifications.getPushToken()) + ' permissions: ' + JSON.stringify(await Notifications.checkPermissions()) + ' stored notifications: ' + JSON.stringify(await Notifications.getStoredNotifications()), ); setIsLoading(false); })(); }, []); const stylesWithThemeHook = { root: { ...styles.root, backgroundColor: colors.background, }, scroll: { ...styles.scroll, backgroundColor: colors.background, }, scrollBody: { ...styles.scrollBody, backgroundColor: colors.background, }, }; const save = useCallback(async () => { setIsLoading(true); try { if (URI) { // validating only if its not empty. empty means use default if (await Notifications.isGroundControlUriValid(URI)) { await Notifications.saveUri(URI); presentAlert({ message: loc.settings.saved }); } else { presentAlert({ message: loc.settings.not_a_valid_uri }); } } else { await Notifications.saveUri(''); presentAlert({ message: loc.settings.saved }); } } catch (error) { console.warn(error); } setIsLoading(false); }, [URI]); return isLoading ? ( ) : ( {loc.settings.groundcontrol_explanation} Linking.openURL('https://github.com/BlueWallet/GroundControl')} titleStyle={{ color: colors.buttonAlternativeTextColor }} title="github.com/BlueWallet/GroundControl" color={colors.buttonTextColor} buttonStyle={styles.buttonStyle} /> setShowTokenInfo(isShowTokenInfo + 1)}> ♪ Ground Control to Major Tom ♪ setShowTokenInfo(isShowTokenInfo + 1)}> ♪ Commencing countdown, engines on ♪ {isShowTokenInfo >= 9 && ( )}