/* global alert */ import React, { useCallback, useEffect, useState } from 'react'; import { ScrollView, TouchableWithoutFeedback, I18nManager, StyleSheet, Linking, View, TextInput } from 'react-native'; import { useTheme } from '@react-navigation/native'; import { Button } from 'react-native-elements'; import navigationStyle from '../../components/navigationStyle'; import { BlueButton, BlueCard, BlueCopyToClipboardButton, BlueListItem, BlueLoading, BlueSpacing20, BlueText } from '../../BlueComponents'; import loc from '../../loc'; import { BlueCurrentTheme } from '../../components/themes'; import Notifications from '../../blue_modules/notifications'; 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); alert(loc.settings.saved); } else { alert(loc.settings.not_a_valid_uri); } } else { await Notifications.saveUri(''); alert(loc.settings.saved); } } catch (error) { console.warn(error); } setIsLoading(false); }, [URI]); return isLoading ? ( ) : ( {loc.settings.groundcontrol_explanation}