From 5d8f08b7a85ae2bd3bc985b6270c5164bdd76991 Mon Sep 17 00:00:00 2001 From: marcosrdz Date: Fri, 18 Dec 2020 17:01:11 -0500 Subject: [PATCH] ADD: Privacy settings for iOS 14 widgets --- blue_modules/WidgetCommunication.android.js | 2 + blue_modules/WidgetCommunication.ios.js | 34 ++++++++++++++--- loc/en.json | 5 ++- screen/settings/SettingsPrivacy.js | 41 +++++++++++++++++++-- 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/blue_modules/WidgetCommunication.android.js b/blue_modules/WidgetCommunication.android.js index 4813e2b6f..437788d12 100644 --- a/blue_modules/WidgetCommunication.android.js +++ b/blue_modules/WidgetCommunication.android.js @@ -1,4 +1,6 @@ function WidgetCommunication(props) { + WidgetCommunication.isBalanceDisplayAllowed = false; + WidgetCommunication.setBalanceDisplayAllowed = () => {}; return null; } diff --git a/blue_modules/WidgetCommunication.ios.js b/blue_modules/WidgetCommunication.ios.js index 9bc314d1e..88e229055 100644 --- a/blue_modules/WidgetCommunication.ios.js +++ b/blue_modules/WidgetCommunication.ios.js @@ -2,15 +2,34 @@ import { useContext, useEffect } from 'react'; import { BlueStorageContext } from './storage-context'; import DefaultPreference from 'react-native-default-preference'; import RNWidgetCenter from 'react-native-widget-center'; - -const WidgetCommunicationAllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance'; -const WidgetCommunicationAllWalletsLatestTransactionTime = 'WidgetCommunicationAllWalletsLatestTransactionTime'; +import AsyncStorage from '@react-native-async-storage/async-storage'; function WidgetCommunication() { + WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance'; + WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime = 'WidgetCommunicationAllWalletsLatestTransactionTime'; + WidgetCommunication.WidgetCommunicationDisplayBalanceAllowed = 'WidgetCommunicationDisplayBalanceAllowed'; 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 () => { - if (await isStorageEncrypted()) { + if ((await isStorageEncrypted()) || !(await WidgetCommunication.isBalanceDisplayAllowed())) { return { allWalletsBalance: 0, latestTransactionTime: 0 }; } else { let balance = 0; @@ -30,8 +49,11 @@ function WidgetCommunication() { const setValues = async () => { await DefaultPreference.setName('group.io.bluewallet.bluewallet'); const { allWalletsBalance, latestTransactionTime } = await allWalletsBalanceAndTransactionTime(); - await DefaultPreference.set(WidgetCommunicationAllWalletsSatoshiBalance, JSON.stringify(allWalletsBalance)); - await DefaultPreference.set(WidgetCommunicationAllWalletsLatestTransactionTime, JSON.stringify(latestTransactionTime)); + await DefaultPreference.set(WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance, JSON.stringify(allWalletsBalance)); + await DefaultPreference.set( + WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime, + JSON.stringify(latestTransactionTime), + ); RNWidgetCenter.reloadAllTimelines(); }; diff --git a/loc/en.json b/loc/en.json index 9b189e770..245b4aaab 100644 --- a/loc/en.json +++ b/loc/en.json @@ -293,7 +293,10 @@ "retype_password": "Re-type password", "save": "Save", "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": { "would_you_like_to_receive_notifications": "Would you like to receive notifications when you get incoming payments?", diff --git a/screen/settings/SettingsPrivacy.js b/screen/settings/SettingsPrivacy.js index 3e77df4f8..d59089e03 100644 --- a/screen/settings/SettingsPrivacy.js +++ b/screen/settings/SettingsPrivacy.js @@ -1,18 +1,22 @@ import React, { useContext, useEffect, useState } from 'react'; -import { ScrollView, TouchableWithoutFeedback, StyleSheet, Linking } from 'react-native'; -import { BlueText, BlueSpacing20, BlueListItem, BlueNavigationStyle, BlueCard } from '../../BlueComponents'; +import { ScrollView, TouchableWithoutFeedback, StyleSheet, Linking, Platform } from 'react-native'; +import { BlueText, BlueSpacing20, BlueListItem, BlueNavigationStyle, BlueCard, BlueHeaderDefaultSub } from '../../BlueComponents'; import { useTheme } from '@react-navigation/native'; import loc from '../../loc'; import BlueClipboard from '../../blue_modules/clipboard'; import DeviceQuickActions from '../../class/quick-actions'; import { BlueStorageContext } from '../../blue_modules/storage-context'; +import WidgetCommunication from '../../blue_modules/WidgetCommunication'; + const SettingsPrivacy = () => { const { colors } = useTheme(); 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 [isReadClipboardAllowed, setIsReadClipboardAllowed] = useState(false); + + const [isDisplayWidgetBalanceAllowed, setIsDisplayWidgetBalanceAllowed] = useState(false); const [isQuickActionsEnabled, setIsQuickActionsEnabled] = useState(false); const [storageIsEncrypted, setStorageIsEncrypted] = useState(true); @@ -22,6 +26,7 @@ const SettingsPrivacy = () => { setIsReadClipboardAllowed(await BlueClipboard.isReadClipboardAllowed()); setStorageIsEncrypted(await isStorageEncrypted()); setIsQuickActionsEnabled(await DeviceQuickActions.getEnabled()); + setIsDisplayWidgetBalanceAllowed(await WidgetCommunication.isBalanceDisplayAllowed()); } catch (e) { console.log(e); } @@ -52,6 +57,17 @@ const SettingsPrivacy = () => { 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({ root: { backgroundColor: colors.background, @@ -64,6 +80,7 @@ const SettingsPrivacy = () => { return ( + { )} + {Platform.OS === 'ios' && !storageIsEncrypted && ( + <> + + + + {loc.settings.total_balance_explanation} + + + )}