ADD: Privacy settings for iOS 14 widgets

This commit is contained in:
marcosrdz 2020-12-18 17:01:11 -05:00
parent f4cdccf948
commit 5d8f08b7a8
4 changed files with 72 additions and 10 deletions

View file

@ -1,4 +1,6 @@
function WidgetCommunication(props) { function WidgetCommunication(props) {
WidgetCommunication.isBalanceDisplayAllowed = false;
WidgetCommunication.setBalanceDisplayAllowed = () => {};
return null; return null;
} }

View file

@ -2,15 +2,34 @@ import { useContext, useEffect } from 'react';
import { BlueStorageContext } from './storage-context'; import { BlueStorageContext } from './storage-context';
import DefaultPreference from 'react-native-default-preference'; import DefaultPreference from 'react-native-default-preference';
import RNWidgetCenter from 'react-native-widget-center'; import RNWidgetCenter from 'react-native-widget-center';
import AsyncStorage from '@react-native-async-storage/async-storage';
const WidgetCommunicationAllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance';
const WidgetCommunicationAllWalletsLatestTransactionTime = 'WidgetCommunicationAllWalletsLatestTransactionTime';
function WidgetCommunication() { function WidgetCommunication() {
WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance';
WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime = 'WidgetCommunicationAllWalletsLatestTransactionTime';
WidgetCommunication.WidgetCommunicationDisplayBalanceAllowed = 'WidgetCommunicationDisplayBalanceAllowed';
const { wallets, walletsInitialized, isStorageEncrypted } = useContext(BlueStorageContext); const { wallets, walletsInitialized, isStorageEncrypted } = useContext(BlueStorageContext);
WidgetCommunication.isBalanceDisplayAllowed = async () => {
try {
const displayBalance = JSON.parse(await AsyncStorage.getItem(WidgetCommunication.WidgetCommunicationDisplayBalanceAllowed));
if (displayBalance !== null) {
return displayBalance;
} else {
return true;
}
} catch (e) {
return true;
}
};
WidgetCommunication.setBalanceDisplayAllowed = async value => {
await AsyncStorage.setItem(WidgetCommunication.WidgetCommunicationDisplayBalanceAllowed, JSON.stringify(value));
setValues();
};
const allWalletsBalanceAndTransactionTime = async () => { const allWalletsBalanceAndTransactionTime = async () => {
if (await isStorageEncrypted()) { if ((await isStorageEncrypted()) || !(await WidgetCommunication.isBalanceDisplayAllowed())) {
return { allWalletsBalance: 0, latestTransactionTime: 0 }; return { allWalletsBalance: 0, latestTransactionTime: 0 };
} else { } else {
let balance = 0; let balance = 0;
@ -30,8 +49,11 @@ function WidgetCommunication() {
const setValues = async () => { const setValues = async () => {
await DefaultPreference.setName('group.io.bluewallet.bluewallet'); await DefaultPreference.setName('group.io.bluewallet.bluewallet');
const { allWalletsBalance, latestTransactionTime } = await allWalletsBalanceAndTransactionTime(); const { allWalletsBalance, latestTransactionTime } = await allWalletsBalanceAndTransactionTime();
await DefaultPreference.set(WidgetCommunicationAllWalletsSatoshiBalance, JSON.stringify(allWalletsBalance)); await DefaultPreference.set(WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance, JSON.stringify(allWalletsBalance));
await DefaultPreference.set(WidgetCommunicationAllWalletsLatestTransactionTime, JSON.stringify(latestTransactionTime)); await DefaultPreference.set(
WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime,
JSON.stringify(latestTransactionTime),
);
RNWidgetCenter.reloadAllTimelines(); RNWidgetCenter.reloadAllTimelines();
}; };

View file

@ -293,7 +293,10 @@
"retype_password": "Re-type password", "retype_password": "Re-type password",
"save": "Save", "save": "Save",
"saved": "Saved", "saved": "Saved",
"success_transaction_broadcasted" : "Success! You transaction has been broadcasted!" "success_transaction_broadcasted" : "Success! You transaction has been broadcasted!",
"total_balance": "Total Balance",
"total_balance_explanation": "Display the total balance of all your wallets on your home screen widgets.",
"widgets": "Widgets"
}, },
"notifications": { "notifications": {
"would_you_like_to_receive_notifications": "Would you like to receive notifications when you get incoming payments?", "would_you_like_to_receive_notifications": "Would you like to receive notifications when you get incoming payments?",

View file

@ -1,18 +1,22 @@
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import { ScrollView, TouchableWithoutFeedback, StyleSheet, Linking } from 'react-native'; import { ScrollView, TouchableWithoutFeedback, StyleSheet, Linking, Platform } from 'react-native';
import { BlueText, BlueSpacing20, BlueListItem, BlueNavigationStyle, BlueCard } from '../../BlueComponents'; import { BlueText, BlueSpacing20, BlueListItem, BlueNavigationStyle, BlueCard, BlueHeaderDefaultSub } from '../../BlueComponents';
import { useTheme } from '@react-navigation/native'; import { useTheme } from '@react-navigation/native';
import loc from '../../loc'; import loc from '../../loc';
import BlueClipboard from '../../blue_modules/clipboard'; import BlueClipboard from '../../blue_modules/clipboard';
import DeviceQuickActions from '../../class/quick-actions'; import DeviceQuickActions from '../../class/quick-actions';
import { BlueStorageContext } from '../../blue_modules/storage-context'; import { BlueStorageContext } from '../../blue_modules/storage-context';
import WidgetCommunication from '../../blue_modules/WidgetCommunication';
const SettingsPrivacy = () => { const SettingsPrivacy = () => {
const { colors } = useTheme(); const { colors } = useTheme();
const { isStorageEncrypted } = useContext(BlueStorageContext); const { isStorageEncrypted } = useContext(BlueStorageContext);
const sections = Object.freeze({ ALL: 0, CLIPBOARDREAD: 1, QUICKACTION: 2 }); const sections = Object.freeze({ ALL: 0, CLIPBOARDREAD: 1, QUICKACTION: 2, WIDGETS: 3 });
const [isLoading, setIsLoading] = useState(sections.ALL); const [isLoading, setIsLoading] = useState(sections.ALL);
const [isReadClipboardAllowed, setIsReadClipboardAllowed] = useState(false); const [isReadClipboardAllowed, setIsReadClipboardAllowed] = useState(false);
const [isDisplayWidgetBalanceAllowed, setIsDisplayWidgetBalanceAllowed] = useState(false);
const [isQuickActionsEnabled, setIsQuickActionsEnabled] = useState(false); const [isQuickActionsEnabled, setIsQuickActionsEnabled] = useState(false);
const [storageIsEncrypted, setStorageIsEncrypted] = useState(true); const [storageIsEncrypted, setStorageIsEncrypted] = useState(true);
@ -22,6 +26,7 @@ const SettingsPrivacy = () => {
setIsReadClipboardAllowed(await BlueClipboard.isReadClipboardAllowed()); setIsReadClipboardAllowed(await BlueClipboard.isReadClipboardAllowed());
setStorageIsEncrypted(await isStorageEncrypted()); setStorageIsEncrypted(await isStorageEncrypted());
setIsQuickActionsEnabled(await DeviceQuickActions.getEnabled()); setIsQuickActionsEnabled(await DeviceQuickActions.getEnabled());
setIsDisplayWidgetBalanceAllowed(await WidgetCommunication.isBalanceDisplayAllowed());
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
@ -52,6 +57,17 @@ const SettingsPrivacy = () => {
setIsLoading(false); setIsLoading(false);
}; };
const onWidgetsTotalBalanceValueChange = async value => {
setIsLoading(sections.WIDGETS);
try {
await WidgetCommunication.setBalanceDisplayAllowed(value);
setIsDisplayWidgetBalanceAllowed(value);
} catch (e) {
console.log(e);
}
setIsLoading(false);
};
const stylesWithThemeHook = StyleSheet.create({ const stylesWithThemeHook = StyleSheet.create({
root: { root: {
backgroundColor: colors.background, backgroundColor: colors.background,
@ -64,6 +80,7 @@ const SettingsPrivacy = () => {
return ( return (
<ScrollView style={[styles.root, stylesWithThemeHook.root]}> <ScrollView style={[styles.root, stylesWithThemeHook.root]}>
<BlueHeaderDefaultSub leftText={loc.settings.general} rightComponent={null} />
<BlueListItem <BlueListItem
hideChevron hideChevron
title={loc.settings.privacy_read_clipboard} title={loc.settings.privacy_read_clipboard}
@ -87,6 +104,24 @@ const SettingsPrivacy = () => {
</BlueCard> </BlueCard>
</> </>
)} )}
{Platform.OS === 'ios' && !storageIsEncrypted && (
<>
<BlueHeaderDefaultSub leftText={loc.settings.widgets} rightComponent={null} />
<BlueListItem
hideChevron
title={loc.settings.total_balance}
Component={TouchableWithoutFeedback}
switch={{
onValueChange: onWidgetsTotalBalanceValueChange,
value: isDisplayWidgetBalanceAllowed,
disabled: isLoading === sections.ALL,
}}
/>
<BlueCard>
<BlueText>{loc.settings.total_balance_explanation}</BlueText>
</BlueCard>
</>
)}
<BlueSpacing20 /> <BlueSpacing20 />
<BlueListItem title={loc.settings.privacy_system_settings} chevron onPress={openApplicationSettings} /> <BlueListItem title={loc.settings.privacy_system_settings} chevron onPress={openApplicationSettings} />
<BlueSpacing20 /> <BlueSpacing20 />