mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-03-13 11:09:20 +01:00
Merge pull request #7672 from BlueWallet/cc
REF: CompanionDelegate to hook
This commit is contained in:
commit
d375bd9780
3 changed files with 54 additions and 44 deletions
|
@ -19,25 +19,42 @@ import loc from '../loc';
|
|||
import { Chain } from '../models/bitcoinUnits';
|
||||
import { navigationRef } from '../NavigationService';
|
||||
import ActionSheet from '../screen/ActionSheet';
|
||||
import { useStorage } from '../hooks/context/useStorage';
|
||||
import { useStorage } from './context/useStorage';
|
||||
import RNQRGenerator from 'rn-qr-generator';
|
||||
import presentAlert from './Alert';
|
||||
import useMenuElements from '../hooks/useMenuElements';
|
||||
import useWidgetCommunication from '../hooks/useWidgetCommunication';
|
||||
import useWatchConnectivity from '../hooks/useWatchConnectivity';
|
||||
import useDeviceQuickActions from '../hooks/useDeviceQuickActions';
|
||||
import useHandoffListener from '../hooks/useHandoffListener';
|
||||
import presentAlert from '../components/Alert';
|
||||
import useMenuElements from './useMenuElements';
|
||||
import useWidgetCommunication from './useWidgetCommunication';
|
||||
import useWatchConnectivity from './useWatchConnectivity';
|
||||
import useDeviceQuickActions from './useDeviceQuickActions';
|
||||
import useHandoffListener from './useHandoffListener';
|
||||
|
||||
const ClipboardContentType = Object.freeze({
|
||||
BITCOIN: 'BITCOIN',
|
||||
LIGHTNING: 'LIGHTNING',
|
||||
});
|
||||
|
||||
const CompanionDelegates = () => {
|
||||
const { wallets, addWallet, saveToDisk, fetchAndSaveWalletTransactions, refreshAllWalletTransactions, setSharedCosigner } = useStorage();
|
||||
/**
|
||||
* Hook that initializes all companion listeners and functionality without rendering a component
|
||||
*/
|
||||
const useCompanionListeners = (skipIfNotInitialized = true) => {
|
||||
const {
|
||||
wallets,
|
||||
addWallet,
|
||||
saveToDisk,
|
||||
fetchAndSaveWalletTransactions,
|
||||
refreshAllWalletTransactions,
|
||||
setSharedCosigner,
|
||||
walletsInitialized,
|
||||
} = useStorage();
|
||||
const appState = useRef<AppStateStatus>(AppState.currentState);
|
||||
const clipboardContent = useRef<undefined | string>();
|
||||
|
||||
// We need to call hooks unconditionally before any conditional logic
|
||||
// We'll use this check inside the effects to conditionally run logic
|
||||
const shouldActivateListeners = !skipIfNotInitialized || walletsInitialized;
|
||||
|
||||
// Initialize other hooks regardless of activation status
|
||||
// They'll handle their own conditional logic internally
|
||||
useWatchConnectivity();
|
||||
useWidgetCommunication();
|
||||
useMenuElements();
|
||||
|
@ -45,6 +62,8 @@ const CompanionDelegates = () => {
|
|||
useHandoffListener();
|
||||
|
||||
const processPushNotifications = useCallback(async () => {
|
||||
if (!shouldActivateListeners) return false;
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
try {
|
||||
const notifications2process = await getStoredNotifications();
|
||||
|
@ -164,15 +183,19 @@ const CompanionDelegates = () => {
|
|||
console.error('Failed to process push notifications:', error);
|
||||
}
|
||||
return false;
|
||||
}, [fetchAndSaveWalletTransactions, refreshAllWalletTransactions, wallets]);
|
||||
}, [fetchAndSaveWalletTransactions, refreshAllWalletTransactions, wallets, shouldActivateListeners]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!shouldActivateListeners) return;
|
||||
|
||||
initializeNotifications(processPushNotifications);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
}, [shouldActivateListeners]);
|
||||
|
||||
const handleOpenURL = useCallback(
|
||||
async (event: { url: string }): Promise<void> => {
|
||||
if (!shouldActivateListeners) return;
|
||||
|
||||
try {
|
||||
if (!event.url) return;
|
||||
let decodedUrl: string;
|
||||
|
@ -227,11 +250,13 @@ const CompanionDelegates = () => {
|
|||
presentAlert({ message: err.message || loc.send.qr_error_no_qrcode });
|
||||
}
|
||||
},
|
||||
[wallets, addWallet, saveToDisk, setSharedCosigner],
|
||||
[wallets, addWallet, saveToDisk, setSharedCosigner, shouldActivateListeners],
|
||||
);
|
||||
|
||||
const showClipboardAlert = useCallback(
|
||||
({ contentType }: { contentType: undefined | string }) => {
|
||||
if (!shouldActivateListeners) return;
|
||||
|
||||
triggerHapticFeedback(HapticFeedbackTypes.ImpactLight);
|
||||
getClipboardContent().then(clipboard => {
|
||||
if (!clipboard) return;
|
||||
|
@ -254,12 +279,13 @@ const CompanionDelegates = () => {
|
|||
);
|
||||
});
|
||||
},
|
||||
[handleOpenURL],
|
||||
[handleOpenURL, shouldActivateListeners],
|
||||
);
|
||||
|
||||
const handleAppStateChange = useCallback(
|
||||
async (nextAppState: AppStateStatus | undefined) => {
|
||||
if (wallets.length === 0) return;
|
||||
if (!shouldActivateListeners || wallets.length === 0) return;
|
||||
|
||||
if ((appState.current.match(/background/) && nextAppState === 'active') || nextAppState === undefined) {
|
||||
setTimeout(() => A(A.ENUM.APP_UNSUSPENDED), 2000);
|
||||
updateExchangeRate();
|
||||
|
@ -299,10 +325,12 @@ const CompanionDelegates = () => {
|
|||
appState.current = nextAppState;
|
||||
}
|
||||
},
|
||||
[processPushNotifications, showClipboardAlert, wallets],
|
||||
[processPushNotifications, showClipboardAlert, wallets, shouldActivateListeners],
|
||||
);
|
||||
|
||||
const addListeners = useCallback(() => {
|
||||
if (!shouldActivateListeners) return { urlSubscription: null, appStateSubscription: null };
|
||||
|
||||
const urlSubscription = Linking.addEventListener('url', handleOpenURL);
|
||||
const appStateSubscription = AppState.addEventListener('change', handleAppStateChange);
|
||||
|
||||
|
@ -310,18 +338,16 @@ const CompanionDelegates = () => {
|
|||
urlSubscription,
|
||||
appStateSubscription,
|
||||
};
|
||||
}, [handleOpenURL, handleAppStateChange]);
|
||||
}, [handleOpenURL, handleAppStateChange, shouldActivateListeners]);
|
||||
|
||||
useEffect(() => {
|
||||
const subscriptions = addListeners();
|
||||
|
||||
return () => {
|
||||
subscriptions.urlSubscription?.remove();
|
||||
subscriptions.appStateSubscription?.remove();
|
||||
subscriptions.urlSubscription?.remove?.();
|
||||
subscriptions.appStateSubscription?.remove?.();
|
||||
};
|
||||
}, [addListeners]);
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export default CompanionDelegates;
|
||||
export default useCompanionListeners;
|
|
@ -1,20 +1,16 @@
|
|||
import React, { lazy, Suspense } from 'react';
|
||||
import { useStorage } from '../hooks/context/useStorage';
|
||||
import React from 'react';
|
||||
import DevMenu from '../components/DevMenu';
|
||||
import MainRoot from './index';
|
||||
const CompanionDelegates = lazy(() => import('../components/CompanionDelegates'));
|
||||
import useCompanionListeners from '../hooks/useCompanionListeners';
|
||||
|
||||
const MasterView = () => {
|
||||
const { walletsInitialized } = useStorage();
|
||||
// Initialize companion listeners only when wallets are initialized
|
||||
// The hook checks walletsInitialized internally, so it won't run until ready
|
||||
useCompanionListeners();
|
||||
|
||||
return (
|
||||
<>
|
||||
<MainRoot />
|
||||
{walletsInitialized && (
|
||||
<Suspense>
|
||||
<CompanionDelegates />
|
||||
</Suspense>
|
||||
)}
|
||||
{__DEV__ && <DevMenu />}
|
||||
</>
|
||||
);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { RouteProp, StackActions, useFocusEffect, useIsFocused, useRoute } from '@react-navigation/native';
|
||||
import { RouteProp, StackActions, useIsFocused, useRoute } from '@react-navigation/native';
|
||||
import * as bitcoin from 'bitcoinjs-lib';
|
||||
import createHash from 'create-hash';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Platform, StyleSheet, TextInput, TouchableOpacity, View } from 'react-native';
|
||||
import Base43 from '../../blue_modules/base43';
|
||||
import * as fs from '../../blue_modules/fs';
|
||||
|
@ -12,7 +12,6 @@ import Button from '../../components/Button';
|
|||
import { useTheme } from '../../components/themes';
|
||||
import { isCameraAuthorizationStatusGranted } from '../../helpers/scan-qr';
|
||||
import loc from '../../loc';
|
||||
import { useSettings } from '../../hooks/context/useSettings';
|
||||
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
|
||||
import CameraScreen from '../../components/CameraScreen';
|
||||
import SafeArea from '../../components/SafeArea';
|
||||
|
@ -57,7 +56,6 @@ const styles = StyleSheet.create({
|
|||
|
||||
const ScanQRCode = () => {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const { setIsDrawerShouldHide } = useSettings();
|
||||
const navigation = useExtendedNavigation();
|
||||
const route = useRoute<RouteProps>();
|
||||
const navigationState = navigation.getState();
|
||||
|
@ -96,16 +94,6 @@ const ScanQRCode = () => {
|
|||
return createHash('sha256').update(s).digest().toString('hex');
|
||||
};
|
||||
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
setIsDrawerShouldHide(true);
|
||||
|
||||
return () => {
|
||||
setIsDrawerShouldHide(false);
|
||||
};
|
||||
}, [setIsDrawerShouldHide]),
|
||||
);
|
||||
|
||||
const _onReadUniformResourceV2 = (part: string) => {
|
||||
if (!decoder) decoder = new BlueURDecoder();
|
||||
try {
|
||||
|
|
Loading…
Add table
Reference in a new issue