Skip to content

Commit

Permalink
[ISSUE-135]: Fix warning popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Danswar committed Oct 29, 2024
1 parent 617b241 commit 69afe98
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 26 deletions.
3 changes: 1 addition & 2 deletions BlueApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ class AppStorage {
try {
realm = await this.getRealm();
} catch (error) {
alert(error.message);
console.error(`saveToDisk: getRealm: ${error.message}`);
}
for (const key of this.wallets) {
if (typeof key === 'boolean') continue;
Expand Down Expand Up @@ -669,7 +669,6 @@ class AppStorage {
realmkeyValue.close();
} catch (error) {
console.error('save to disk exception:', error.message);
alert('save to disk exception: ' + error.message);
if (error.message.includes('Realm file decryption failed')) {
console.warn('purging realm key-value database file');
this.purgeRealmKeyValueFile();
Expand Down
12 changes: 8 additions & 4 deletions blue_modules/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ const BlueClipboard = () => {
};

const getClipboardContent = async () => {
const isAllowed = await isReadClipboardAllowed();
if (isAllowed) {
return Clipboard.getString();
} else {
try {
const isAllowed = await isReadClipboardAllowed();
if (isAllowed) {
return Clipboard.getString();
} else {
return '';
}
} catch {
return '';
}
};
Expand Down
63 changes: 55 additions & 8 deletions screen/wallets/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
I18nManager,
useWindowDimensions,
TouchableOpacity,
AppState,
} from 'react-native';
import { Icon } from 'react-native-elements';
import { useRoute, useNavigation, useTheme, useFocusEffect } from '@react-navigation/native';
Expand All @@ -31,7 +32,6 @@ import { BlueStorageContext } from '../../blue_modules/storage-context';
import { isDesktop } from '../../blue_modules/environment';
import BlueClipboard from '../../blue_modules/clipboard';
import { TransactionListItem } from '../../components/TransactionListItem';
import alert from '../../components/Alert';
import { ImageButton } from '../../components/ImageButton';
import { DfxService, useDfxSessionContext } from '../../api/dfx/contexts/session.context';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -75,7 +75,7 @@ const Asset = ({ navigation }) => {
walletTransactionUpdateStatus,
isElectrumDisabled,
isDfxPos,
isDfxSwap
isDfxSwap,
} = useContext(BlueStorageContext);
const { name, params } = useRoute();
const walletID = params.walletID;
Expand All @@ -95,6 +95,8 @@ const Asset = ({ navigation }) => {
const [isHandlingOpenServices, setIsHandlingOpenServices] = useState(false);
const [changeAddress, setChangeAddress] = useState('');
const [fContainerHeight, setFContainerHeight] = useState(0);
const txRefreshInterval = useRef(null);
const elapsedTimeInterval = useRef(null);

const getButtonImages = lang => {
switch (lang) {
Expand Down Expand Up @@ -155,10 +157,22 @@ const Asset = ({ navigation }) => {
return txs.slice(0, lmt);
};

const clearElapsedTimeInterval = () => {
if (elapsedTimeInterval.current) {
clearInterval(elapsedTimeInterval.current);
elapsedTimeInterval.current = null;
}
};

const setElapsedTimeInterval = () => {
clearElapsedTimeInterval();
elapsedTimeInterval.current = setInterval(() => setTimeElapsed(prev => prev + 1), 60000);
};

useEffect(() => {
const interval = setInterval(() => setTimeElapsed(prev => prev + 1), 60000);
setElapsedTimeInterval();
return () => {
clearInterval(interval);
clearElapsedTimeInterval();
};
}, []);

Expand All @@ -170,12 +184,46 @@ const Asset = ({ navigation }) => {
}
}, []);

useEffect(() => {
const refreshingInterval = setInterval(() => {
const clearTxRefreshInterval = () => {
if (txRefreshInterval.current) {
clearInterval(txRefreshInterval.current);
txRefreshInterval.current = null;
}
};

const setTxRefreshInterval = () => {
clearTxRefreshInterval();
txRefreshInterval.current = setInterval(() => {
refreshTransactions();
}, 20 * 1000);
};

useEffect(() => {
setTxRefreshInterval();
return () => {
clearTxRefreshInterval();
};
}, []);

const appState = useRef(AppState.currentState);

useEffect(() => {
const subscription = AppState.addEventListener('change', nextAppState => {
if (appState.current === 'active' && nextAppState.match(/inactive|background/)) {
clearTxRefreshInterval();
clearElapsedTimeInterval();
}

if (appState.current.match(/inactive|background/) && nextAppState === 'active') {
setTxRefreshInterval();
setElapsedTimeInterval();
}

appState.current = nextAppState;
});

return () => {
clearInterval(refreshingInterval);
subscription.remove();
};
}, []);

Expand Down Expand Up @@ -356,7 +404,6 @@ const Asset = ({ navigation }) => {
console.log(wallet.getLabel(), 'fetch tx took', (end - start) / 1000, 'sec');
} catch (err) {
noErr = false;
alert(err.message);
setIsLoading(false);
setTimeElapsed(prev => prev + 1);
}
Expand Down
51 changes: 39 additions & 12 deletions screen/wallets/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
View,
I18nManager,
useWindowDimensions,
AppState,
} from 'react-native';
import { Icon } from 'react-native-elements';
import { useRoute, useNavigation, useTheme } from '@react-navigation/native';
Expand All @@ -26,7 +27,6 @@ import loc, { formatBalance } from '../../loc';
import { FContainer, FButton } from '../../components/FloatButtons';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import BlueClipboard from '../../blue_modules/clipboard';
import alert from '../../components/Alert';
import TransactionsNavigationHeader from '../../components/TransactionsNavigationHeader';
import PropTypes from 'prop-types';
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
Expand Down Expand Up @@ -60,6 +60,7 @@ const WalletHome = ({ navigation }) => {
const { colors, scanImage } = useTheme();
const walletActionButtonsRef = useRef();
const { width } = useWindowDimensions();
const balanceRefreshInterval = useRef(null);

const wallet = useMemo(() => wallets.find(w => w.getID() === walletID), [wallets, walletID]);
const totalWallet = useMemo(() => {
Expand Down Expand Up @@ -108,12 +109,40 @@ const WalletHome = ({ navigation }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [wallets]);

useEffect(() => {
const clearBalanceRefreshInterval = () => {
if (balanceRefreshInterval.current) {
clearInterval(balanceRefreshInterval.current);
balanceRefreshInterval.current = null;
}
};

const setBalanceRefreshInterval = () => {
if (!wallets) return;
const interval = setInterval(() => {
clearBalanceRefreshInterval();
balanceRefreshInterval.current = setInterval(() => {
refreshBalances().catch(console.error);
}, 20 * 1000);
return () => clearInterval(interval);
};

useEffect(() => {
setBalanceRefreshInterval();
return () => {
clearBalanceRefreshInterval();
};
}, []);

const appState = useRef(AppState.currentState);

useEffect(() => {
const subscription = AppState.addEventListener('change', nextAppState => {
if (appState.current === 'active' && nextAppState.match(/inactive|background/)) clearBalanceRefreshInterval();
if (appState.current.match(/inactive|background/) && nextAppState === 'active') setBalanceRefreshInterval();
appState.current = nextAppState;
});

return () => {
subscription.remove();
};
}, []);

/**
Expand All @@ -135,7 +164,6 @@ const WalletHome = ({ navigation }) => {
}
} catch (err) {
noErr = false;
alert(err.message);
setIsLoading(false);
}

Expand All @@ -154,7 +182,7 @@ const WalletHome = ({ navigation }) => {
const oldBalance = w.getBalance();
await w.fetchBalance();
return oldBalance !== w.getBalance();
} catch (_) { }
} catch (_) {}
};

const navigateToSendScreen = () => {
Expand All @@ -175,16 +203,16 @@ const WalletHome = ({ navigation }) => {
params: {
psbtBase64: psbt.toBase64(),
walletID: multisigWallet.getID(),
}
},
});
}
} catch (_) { }
}
} catch (_) {}
};

const onBarScanned = value => {
if (!value) return;

if(BoltCard.isPossiblyBoltcardTapDetails(value)) {
if (BoltCard.isPossiblyBoltcardTapDetails(value)) {
navigate('TappedCardDetails', { tappedCardDetails: value });
return;
}
Expand Down Expand Up @@ -251,7 +279,7 @@ const WalletHome = ({ navigation }) => {
const buttons = [
{
text: loc._.cancel,
onPress: () => { },
onPress: () => {},
style: 'cancel',
},
{
Expand Down Expand Up @@ -299,7 +327,6 @@ const WalletHome = ({ navigation }) => {
scanqrHelper(navigate, name, false).then(d => onBarScanned(d));
};


const navigateToAddMultisig = () => {
navigate('WalletsRoot', {
screen: 'WalletsAddMultisig',
Expand Down

0 comments on commit 69afe98

Please sign in to comment.