From 2bdd65ffd138b17c58b9aa94c75cc84576d23a0a Mon Sep 17 00:00:00 2001 From: Marcos Rodriguez Velez Date: Mon, 11 Nov 2024 14:11:59 -0400 Subject: [PATCH] FIX: Small ui fixs --- blue_modules/notifications.js | 77 ++++++++++++------------ screen/wallets/ImportWalletDiscovery.tsx | 31 +++++----- screen/wallets/WalletDetails.tsx | 53 +++++++++------- screen/wallets/WalletsList.tsx | 2 +- 4 files changed, 86 insertions(+), 77 deletions(-) diff --git a/blue_modules/notifications.js b/blue_modules/notifications.js index 4d6739cfa..5c23e8adf 100644 --- a/blue_modules/notifications.js +++ b/blue_modules/notifications.js @@ -172,13 +172,6 @@ function Notifications(props) { }); }; - function _getHeaders() { - return { - 'Access-Control-Allow-Origin': '*', - 'Content-Type': 'application/json', - }; - } - async function _sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } @@ -243,38 +236,6 @@ function Notifications(props) { } }; - /** - * The opposite of `majorTomToGroundControl` call. - * - * @param addresses {string[]} - * @param hashes {string[]} - * @param txids {string[]} - * @returns {Promise} Response object from API rest call - */ - Notifications.unsubscribe = async function (addresses, hashes, txids) { - if (!Array.isArray(addresses) || !Array.isArray(hashes) || !Array.isArray(txids)) - throw new Error('no addresses or hashes or txids provided'); - const pushToken = await Notifications.getPushToken(); - if (!pushToken || !pushToken.token || !pushToken.os) return; - - const response = await fetch(`${baseURI}/unsubscribe`, { - method: 'POST', - headers: _getHeaders(), - body: JSON.stringify({ - addresses, - hashes, - txids, - token: pushToken.token, - os: pushToken.os, - }), - }); - - console.debug('Abandoning notifications Permissions...'); - PushNotification.abandonPermissions(); - console.debug('Abandoned notifications Permissions...'); - return response.json(); - }; - Notifications.isNotificationsEnabled = async function () { const levels = await getLevels(); return !!(await Notifications.getPushToken()) && !!levels.level_all; @@ -511,4 +472,42 @@ function Notifications(props) { export const isNotificationsCapable = hasGmsSync() || hasHmsSync() || Platform.OS !== 'android'; +/** + * The opposite of `majorTomToGroundControl` call. + * + * @param addresses {string[]} + * @param hashes {string[]} + * @param txids {string[]} + * @returns {Promise} Response object from API rest call + */ +export const unsubscribe = async (addresses, hashes, txids) => { + if (!Array.isArray(addresses) || !Array.isArray(hashes) || !Array.isArray(txids)) + throw new Error('no addresses or hashes or txids provided'); + const pushToken = await Notifications.getPushToken(); + if (!pushToken || !pushToken.token || !pushToken.os) return; + + const response = await fetch(`${baseURI}/unsubscribe`, { + method: 'POST', + headers: _getHeaders(), + body: JSON.stringify({ + addresses, + hashes, + txids, + token: pushToken.token, + os: pushToken.os, + }), + }); + + console.debug('Abandoning notifications Permissions...'); + PushNotification.abandonPermissions(); + console.debug('Abandoned notifications Permissions...'); + return response.json(); +}; + +function _getHeaders() { + return { + 'Access-Control-Allow-Origin': '*', + 'Content-Type': 'application/json', + }; +} export default Notifications; diff --git a/screen/wallets/ImportWalletDiscovery.tsx b/screen/wallets/ImportWalletDiscovery.tsx index e93de29d3..f1df13814 100644 --- a/screen/wallets/ImportWalletDiscovery.tsx +++ b/screen/wallets/ImportWalletDiscovery.tsx @@ -2,7 +2,7 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { RouteProp, useRoute } from '@react-navigation/native'; import { ActivityIndicator, FlatList, LayoutAnimation, StyleSheet, View } from 'react-native'; import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback'; -import { BlueButtonLink, BlueFormLabel, BlueSpacing10, BlueSpacing20, BlueText } from '../../BlueComponents'; +import { BlueButtonLink, BlueFormLabel, BlueSpacing10, BlueSpacing20, BlueSpacing40, BlueText } from '../../BlueComponents'; import { HDSegwitBech32Wallet, WatchOnlyWallet } from '../../class'; import startImport, { TImport } from '../../class/wallet-import'; import presentAlert from '../../components/Alert'; @@ -172,11 +172,23 @@ const ImportWalletDiscovery: React.FC = () => { const ListEmptyComponent = useMemo( () => ( - {loc.wallets.import_discovery_no_wallets} - + {loading ? ( + <> + + + + {progress} + + + ) : ( + <> + {loc.wallets.import_discovery_no_wallets} + {' '} + + )} ), - [], + [loading, progress], ); return ( @@ -192,15 +204,6 @@ const ImportWalletDiscovery: React.FC = () => { contentInsetAdjustmentBehavior="always" /> - {loading && ( - <> - - - - {progress} - - - )} {bip39 && ( ; const WalletDetails: React.FC = () => { @@ -146,44 +146,51 @@ const WalletDetails: React.FC = () => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [walletID]); - const navigateToOverviewAndDeleteWallet = () => { + const navigateToOverviewAndDeleteWallet = useCallback(async () => { setIsLoading(true); - let externalAddresses: string[] = []; - try { - if (wallet.getAllExternalAddresses) { - externalAddresses = wallet.getAllExternalAddresses(); - } - } catch (_) {} - // @ts-ignore: ts-ify later - Notifications.unsubscribe(externalAddresses, [], []); - deleteWallet(wallet); - saveToDisk(true); - triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess); - popToTop(); - }; + try { + const externalAddresses = wallet.getAllExternalAddresses(); + if (externalAddresses.length > 0) { + await unsubscribe(externalAddresses, [], []); + } + deleteWallet(wallet); + saveToDisk(true); + triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess); + popToTop(); + } catch (e: unknown) { + console.error(e); + triggerHapticFeedback(HapticFeedbackTypes.NotificationError); + presentAlert({ message: (e as Error).message }); + setIsLoading(false); + } + }, [deleteWallet, saveToDisk, wallet]); const presentWalletHasBalanceAlert = useCallback(async () => { triggerHapticFeedback(HapticFeedbackTypes.NotificationWarning); try { + const balance = formatBalanceWithoutSuffix(wallet.getBalance(), BitcoinUnit.SATS, true); const walletBalanceConfirmation = await prompt( loc.wallets.details_delete_wallet, - loc.formatString(loc.wallets.details_del_wb_q, { balance: wallet.getBalance() }), + loc.formatString(loc.wallets.details_del_wb_q, { balance }), true, - 'plain-text', + 'numeric', true, loc.wallets.details_delete, ); - if (Number(walletBalanceConfirmation) === wallet.getBalance()) { - navigateToOverviewAndDeleteWallet(); + // Remove any non-numeric characters before comparison + const cleanedConfirmation = (walletBalanceConfirmation || '').replace(/[^0-9]/g, ''); + + if (Number(cleanedConfirmation) === wallet.getBalance()) { + await navigateToOverviewAndDeleteWallet(); + triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess); } else { triggerHapticFeedback(HapticFeedbackTypes.NotificationError); setIsLoading(false); presentAlert({ message: loc.wallets.details_del_wb_err }); } } catch (_) {} - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, [navigateToOverviewAndDeleteWallet, wallet]); const navigateToWalletExport = () => { navigate('WalletExportRoot', { @@ -300,11 +307,11 @@ const WalletDetails: React.FC = () => { }, [wallet, walletName, saveToDisk]); useEffect(() => { - const unsubscribe = addListener('beforeRemove', () => { + const subscribe = addListener('beforeRemove', () => { walletNameTextInputOnBlur(); }); - return unsubscribe; + return subscribe; }, [addListener, walletName, walletNameTextInputOnBlur]); const exportHistoryContent = useCallback(() => { diff --git a/screen/wallets/WalletsList.tsx b/screen/wallets/WalletsList.tsx index 6fd776fec..1750516e2 100644 --- a/screen/wallets/WalletsList.tsx +++ b/screen/wallets/WalletsList.tsx @@ -173,7 +173,7 @@ const WalletsList: React.FC = () => { useEffect(() => { // new wallet added if (wallets.length > walletsCount.current) { - walletsCarousel.current?.scrollToItem({ item: wallets[walletsCount.current] }); + walletsCarousel.current?.scrollToItem({ item: wallets[walletsCount.current], viewPosition: 0.3 }); } walletsCount.current = wallets.length;