FIX: If latest tx was unconfirmed, widget should show "Never" for latest

This commit is contained in:
Marcos Rodriguez Velez 2024-06-04 00:23:53 -04:00
parent 9b6434715e
commit 2469134181
No known key found for this signature in database
GPG Key ID: 6030B2F48CCE86D7
2 changed files with 36 additions and 25 deletions

View File

@ -1,8 +1,9 @@
import React, { useEffect } from 'react'; import React, { useEffect, useCallback } from 'react';
import DefaultPreference from 'react-native-default-preference'; import DefaultPreference from 'react-native-default-preference';
import { TWallet } from '../class/wallets/types'; import { TWallet, Transaction } from '../class/wallets/types';
import { useSettings } from '../hooks/context/useSettings'; import { useSettings } from '../hooks/context/useSettings';
import { useStorage } from '../hooks/context/useStorage'; import { useStorage } from '../hooks/context/useStorage';
import { GROUP_IO_BLUEWALLET } from '../blue_modules/currency';
enum WidgetCommunicationKeys { enum WidgetCommunicationKeys {
AllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance', AllWalletsSatoshiBalance = 'WidgetCommunicationAllWalletsSatoshiBalance',
@ -13,7 +14,7 @@ enum WidgetCommunicationKeys {
export const isBalanceDisplayAllowed = async (): Promise<boolean> => { export const isBalanceDisplayAllowed = async (): Promise<boolean> => {
try { try {
await DefaultPreference.setName('group.io.bluewallet.bluewallet'); await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
const displayBalance = await DefaultPreference.get(WidgetCommunicationKeys.DisplayBalanceAllowed); const displayBalance = await DefaultPreference.get(WidgetCommunicationKeys.DisplayBalanceAllowed);
return displayBalance === '1'; return displayBalance === '1';
} catch { } catch {
@ -23,7 +24,7 @@ export const isBalanceDisplayAllowed = async (): Promise<boolean> => {
}; };
export const setBalanceDisplayAllowed = async (value: boolean): Promise<void> => { export const setBalanceDisplayAllowed = async (value: boolean): Promise<void> => {
await DefaultPreference.setName('group.io.bluewallet.bluewallet'); await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
if (value) { if (value) {
await DefaultPreference.set(WidgetCommunicationKeys.DisplayBalanceAllowed, '1'); await DefaultPreference.set(WidgetCommunicationKeys.DisplayBalanceAllowed, '1');
} else { } else {
@ -31,13 +32,6 @@ export const setBalanceDisplayAllowed = async (value: boolean): Promise<void> =>
} }
}; };
export const syncWidgetBalanceWithWallets = async (wallets: TWallet[], walletsInitialized: boolean): Promise<void> => {
await DefaultPreference.setName('group.io.bluewallet.bluewallet');
const { allWalletsBalance, latestTransactionTime } = await allWalletsBalanceAndTransactionTime(wallets, walletsInitialized);
await DefaultPreference.set(WidgetCommunicationKeys.AllWalletsSatoshiBalance, String(allWalletsBalance));
await DefaultPreference.set(WidgetCommunicationKeys.AllWalletsLatestTransactionTime, String(latestTransactionTime));
};
const allWalletsBalanceAndTransactionTime = async ( const allWalletsBalanceAndTransactionTime = async (
wallets: TWallet[], wallets: TWallet[],
walletsInitialized: boolean, walletsInitialized: boolean,
@ -47,15 +41,23 @@ const allWalletsBalanceAndTransactionTime = async (
} }
let balance = 0; let balance = 0;
let latestTransactionTime: number | string = 0; let latestTransactionTime: number | string = 0;
wallets.forEach((wallet: TWallet) => {
if (wallet.hideBalance) return; for (const wallet of wallets) {
balance += wallet.getBalance(); if (wallet.hideBalance) continue;
const walletLatestTime = wallet.getLatestTransactionTimeEpoch(); balance += await wallet.getBalance();
if (typeof latestTransactionTime === 'number' && walletLatestTime > latestTransactionTime) {
latestTransactionTime = const transactions: Transaction[] = await wallet.getTransactions();
wallet.getTransactions()[0]?.confirmations === 0 ? WidgetCommunicationKeys.LatestTransactionIsUnconfirmed : walletLatestTime; for (const transaction of transactions) {
const transactionTime = await wallet.getLatestTransactionTimeEpoch();
if (transaction.confirmations > 0 && transactionTime > Number(latestTransactionTime)) {
latestTransactionTime = transactionTime;
}
}
if (latestTransactionTime === 0 && transactions[0]?.confirmations === 0) {
latestTransactionTime = WidgetCommunicationKeys.LatestTransactionIsUnconfirmed;
}
} }
});
return { allWalletsBalance: balance, latestTransactionTime }; return { allWalletsBalance: balance, latestTransactionTime };
}; };
@ -64,11 +66,24 @@ const WidgetCommunication: React.FC = () => {
const { wallets, walletsInitialized } = useStorage(); const { wallets, walletsInitialized } = useStorage();
const { isWidgetBalanceDisplayAllowed } = useSettings(); const { isWidgetBalanceDisplayAllowed } = useSettings();
const syncWidgetBalanceWithWallets = useCallback(async (): Promise<void> => {
try {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
const { allWalletsBalance, latestTransactionTime } = await allWalletsBalanceAndTransactionTime(wallets, walletsInitialized);
await Promise.all([
DefaultPreference.set(WidgetCommunicationKeys.AllWalletsSatoshiBalance, String(allWalletsBalance)),
DefaultPreference.set(WidgetCommunicationKeys.AllWalletsLatestTransactionTime, String(latestTransactionTime)),
]);
} catch (error) {
console.error('Failed to sync widget balance with wallets:', error);
}
}, [wallets, walletsInitialized]);
useEffect(() => { useEffect(() => {
if (walletsInitialized) { if (walletsInitialized) {
syncWidgetBalanceWithWallets(wallets, walletsInitialized); syncWidgetBalanceWithWallets();
} }
}, [wallets, walletsInitialized, isWidgetBalanceDisplayAllowed]); }, [wallets, walletsInitialized, isWidgetBalanceDisplayAllowed, syncWidgetBalanceWithWallets]);
return null; return null;
}; };

View File

@ -1,15 +1,11 @@
import React from 'react'; import React from 'react';
import { TWallet } from '../class/wallets/types';
export const isBalanceDisplayAllowed = async (): Promise<boolean> => { export const isBalanceDisplayAllowed = async (): Promise<boolean> => {
return true; return true;
}; };
export const setBalanceDisplayAllowed = async (value: boolean): Promise<void> => {}; export const setBalanceDisplayAllowed = async (value: boolean): Promise<void> => {};
export const syncWidgetBalanceWithWallets = async (_wallets: TWallet[], _walletsInitialized: boolean): Promise<void> => {};
const WidgetCommunication: React.FC = () => { const WidgetCommunication: React.FC = () => {
return null; // This component does not render anything. return null; // This component does not render anything.
}; };