forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidgetCommunication.ios.js
79 lines (71 loc) · 3.13 KB
/
WidgetCommunication.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { useContext, useEffect } from 'react';
import { BlueStorageContext } from './storage-context';
import DefaultPreference from 'react-native-default-preference';
import RNWidgetCenter from 'react-native-widget-center';
import AsyncStorage from '@react-native-async-storage/async-storage';
function WidgetCommunication() {
WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance';
WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime = 'WidgetCommunicationAllWalletsLatestTransactionTime';
WidgetCommunication.WidgetCommunicationDisplayBalanceAllowed = 'WidgetCommunicationDisplayBalanceAllowed';
WidgetCommunication.LatestTransactionIsUnconfirmed = 'WidgetCommunicationLatestTransactionIsUnconfirmed';
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();
};
WidgetCommunication.reloadAllTimelines = () => {
RNWidgetCenter.reloadAllTimelines();
};
const allWalletsBalanceAndTransactionTime = async () => {
if ((await isStorageEncrypted()) || !(await WidgetCommunication.isBalanceDisplayAllowed())) {
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) {
if (wallet.getTransactions()[0].confirmations === 0) {
latestTransactionTime = WidgetCommunication.LatestTransactionIsUnconfirmed;
} else {
latestTransactionTime = wallet.getLatestTransactionTimeEpoch();
}
}
}
return { allWalletsBalance: balance, latestTransactionTime };
}
};
const setValues = async () => {
await DefaultPreference.setName('group.io.bluewallet.bluewallet');
const { allWalletsBalance, latestTransactionTime } = await allWalletsBalanceAndTransactionTime();
await DefaultPreference.set(WidgetCommunication.WidgetCommunicationAllWalletsSatoshiBalance, JSON.stringify(allWalletsBalance));
await DefaultPreference.set(
WidgetCommunication.WidgetCommunicationAllWalletsLatestTransactionTime,
JSON.stringify(latestTransactionTime),
);
RNWidgetCenter.reloadAllTimelines();
};
useEffect(() => {
if (walletsInitialized) {
setValues();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [wallets, walletsInitialized]);
return null;
}
export default WidgetCommunication;