2020-11-02 14:11:28 +01:00
|
|
|
import { useContext, useEffect } from 'react';
|
|
|
|
import { BlueStorageContext } from './storage-context';
|
|
|
|
import DefaultPreference from 'react-native-default-preference';
|
|
|
|
import RNWidgetCenter from 'react-native-widget-center';
|
2020-12-18 23:01:11 +01:00
|
|
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
2020-11-02 14:11:28 +01:00
|
|
|
|
|
|
|
function WidgetCommunication() {
|
2020-12-18 23:01:11 +01:00
|
|
|
WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance';
|
|
|
|
WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime = 'WidgetCommunicationAllWalletsLatestTransactionTime';
|
|
|
|
WidgetCommunication.WidgetCommunicationDisplayBalanceAllowed = 'WidgetCommunicationDisplayBalanceAllowed';
|
2021-02-01 00:39:43 +01:00
|
|
|
WidgetCommunication.LatestTransactionIsUnconfirmed = 'WidgetCommunicationLatestTransactionIsUnconfirmed';
|
2020-11-02 14:11:28 +01:00
|
|
|
const { wallets, walletsInitialized, isStorageEncrypted } = useContext(BlueStorageContext);
|
|
|
|
|
2020-12-18 23:01:11 +01:00
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2021-08-03 18:43:00 +02:00
|
|
|
WidgetCommunication.reloadAllTimelines = () => {
|
|
|
|
RNWidgetCenter.reloadAllTimelines();
|
2021-08-03 18:28:47 +02:00
|
|
|
};
|
|
|
|
|
2020-11-02 14:11:28 +01:00
|
|
|
const allWalletsBalanceAndTransactionTime = async () => {
|
2020-12-18 23:01:11 +01:00
|
|
|
if ((await isStorageEncrypted()) || !(await WidgetCommunication.isBalanceDisplayAllowed())) {
|
2020-11-02 14:11:28 +01:00
|
|
|
return { allWalletsBalance: 0, latestTransactionTime: 0 };
|
|
|
|
} else {
|
|
|
|
let balance = 0;
|
|
|
|
let latestTransactionTime = 0;
|
|
|
|
for (const wallet of wallets) {
|
|
|
|
if (wallet.hideBalance) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
balance += wallet.getBalance();
|
|
|
|
if (wallet.getLatestTransactionTimeEpoch() > latestTransactionTime) {
|
2021-02-01 00:39:43 +01:00
|
|
|
if (wallet.getTransactions()[0].confirmations === 0) {
|
|
|
|
latestTransactionTime = WidgetCommunication.LatestTransactionIsUnconfirmed;
|
|
|
|
} else {
|
|
|
|
latestTransactionTime = wallet.getLatestTransactionTimeEpoch();
|
|
|
|
}
|
2020-11-02 14:11:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return { allWalletsBalance: balance, latestTransactionTime };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const setValues = async () => {
|
|
|
|
await DefaultPreference.setName('group.io.bluewallet.bluewallet');
|
|
|
|
const { allWalletsBalance, latestTransactionTime } = await allWalletsBalanceAndTransactionTime();
|
2020-12-18 23:01:11 +01:00
|
|
|
await DefaultPreference.set(WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance, JSON.stringify(allWalletsBalance));
|
|
|
|
await DefaultPreference.set(
|
|
|
|
WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime,
|
|
|
|
JSON.stringify(latestTransactionTime),
|
|
|
|
);
|
2020-11-02 14:11:28 +01:00
|
|
|
RNWidgetCenter.reloadAllTimelines();
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (walletsInitialized) {
|
|
|
|
setValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [wallets, walletsInitialized]);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WidgetCommunication;
|