This commit is contained in:
Marcos Rodriguez Velez 2024-11-08 19:12:14 -04:00
parent 7926ea6218
commit 1c8b0908b2
5 changed files with 25 additions and 75 deletions

View File

@ -2,7 +2,7 @@ import 'react-native-gesture-handler'; // should be on top
import { CommonActions } from '@react-navigation/native';
import React, { lazy, Suspense, useCallback, useEffect, useRef } from 'react';
import { AppState, AppStateStatus, Linking, NativeEventEmitter, NativeModules, Platform } from 'react-native';
import { AppState, AppStateStatus, Linking } from 'react-native';
import A from '../blue_modules/analytics';
import BlueClipboard from '../blue_modules/clipboard';
import { updateExchangeRate } from '../blue_modules/currency';
@ -24,9 +24,6 @@ const HandOffComponentListener = lazy(() => import('../components/HandOffCompone
const WidgetCommunication = lazy(() => import('../components/WidgetCommunication'));
const WatchConnectivity = lazy(() => import('./WatchConnectivity'));
// @ts-ignore: NativeModules.EventEmitter is not typed
const eventEmitter = Platform.OS === 'ios' ? new NativeEventEmitter(NativeModules.EventEmitter) : undefined;
const ClipboardContentType = Object.freeze({
BITCOIN: 'BITCOIN',
LIGHTNING: 'LIGHTNING',
@ -224,32 +221,15 @@ const CompanionDelegates = () => {
[processPushNotifications, showClipboardAlert, wallets],
);
const onNotificationReceived = useCallback(
async (notification: { data: { data: any } }) => {
const payload = Object.assign({}, notification, notification.data);
if (notification.data && notification.data.data) Object.assign(payload, notification.data.data);
// @ts-ignore: Notifications type is not defined
payload.foreground = true;
// @ts-ignore: Notifications type is not defined
await Notifications.addNotification(payload);
// @ts-ignore: Notifications type is not defined
if (payload.foreground) await processPushNotifications();
},
[processPushNotifications],
);
const addListeners = useCallback(() => {
const urlSubscription = Linking.addEventListener('url', handleOpenURL);
const appStateSubscription = AppState.addEventListener('change', handleAppStateChange);
const notificationSubscription = eventEmitter?.addListener('onNotificationReceived', onNotificationReceived);
return {
urlSubscription,
appStateSubscription,
notificationSubscription,
};
}, [handleOpenURL, handleAppStateChange, onNotificationReceived]);
}, [handleOpenURL, handleAppStateChange]);
useEffect(() => {
const subscriptions = addListeners();
@ -257,7 +237,6 @@ const CompanionDelegates = () => {
return () => {
subscriptions.urlSubscription?.remove();
subscriptions.appStateSubscription?.remove();
subscriptions.notificationSubscription?.remove();
};
}, [addListeners]);

View File

@ -10,24 +10,20 @@ Uses MenuElementsEmitter for event handling.
*/
const { MenuElementsEmitter } = NativeModules;
const eventEmitter = Platform.OS === 'ios' || Platform.OS === 'macos' ? new NativeEventEmitter(MenuElementsEmitter) : undefined;
const eventEmitter =
(Platform.OS === 'ios' || Platform.OS === 'macos') && MenuElementsEmitter ? new NativeEventEmitter(MenuElementsEmitter) : null;
const useMenuElements = () => {
const { walletsInitialized } = useStorage();
const reloadTransactionsMenuActionRef = useRef<() => void>(() => {});
const setReloadTransactionsMenuActionFunction = (newFunction: () => void) => {
const setReloadTransactionsMenuActionFunction = useCallback((newFunction: () => void) => {
console.debug('Setting reloadTransactionsMenuActionFunction.');
reloadTransactionsMenuActionRef.current = newFunction;
};
}, []);
const dispatchNavigate = useCallback((routeName: string, screen?: string) => {
const action = CommonActions.navigate({
name: routeName,
params: screen ? { screen } : undefined,
});
NavigationService.dispatch(action);
NavigationService.dispatch(CommonActions.navigate({ name: routeName, params: screen ? { screen } : undefined }));
}, []);
const eventActions = useMemo(
@ -36,47 +32,29 @@ const useMenuElements = () => {
addWallet: () => dispatchNavigate('AddWalletRoot'),
importWallet: () => dispatchNavigate('AddWalletRoot', 'ImportWallet'),
reloadTransactions: () => {
if (__DEV__) {
console.debug('Calling reloadTransactionsMenuActionFunction');
}
if (reloadTransactionsMenuActionRef.current) {
reloadTransactionsMenuActionRef.current();
} else {
console.warn('No reload function set for reloadTransactions menu action');
}
console.debug('Calling reloadTransactionsMenuActionFunction');
reloadTransactionsMenuActionRef.current?.();
},
}),
[dispatchNavigate],
);
useEffect(() => {
if (__DEV__) {
console.debug('useEffect: walletsInitialized =', walletsInitialized);
}
if (walletsInitialized && eventEmitter) {
if (__DEV__) {
console.debug('Adding event listeners for menu actions');
}
try {
const listeners = [
eventEmitter.addListener('openSettings', eventActions.openSettings),
eventEmitter.addListener('addWalletMenuAction', eventActions.addWallet),
eventEmitter.addListener('importWalletMenuAction', eventActions.importWallet),
eventEmitter.addListener('reloadTransactionsMenuAction', eventActions.reloadTransactions),
];
if (!walletsInitialized || !eventEmitter) return;
return () => {
if (__DEV__) {
console.debug('Removing event listeners for menu actions');
}
listeners.forEach(listener => listener.remove());
};
} catch (error) {
console.error('Failed to set up menu event listeners:', error);
}
}
console.debug('Setting up menu event listeners');
const listeners = [
eventEmitter.addListener('openSettings', eventActions.openSettings),
eventEmitter.addListener('addWalletMenuAction', eventActions.addWallet),
eventEmitter.addListener('importWalletMenuAction', eventActions.importWallet),
eventEmitter.addListener('reloadTransactionsMenuAction', eventActions.reloadTransactions),
];
return () => {
console.debug('Removing menu event listeners');
listeners.forEach(listener => listener.remove());
};
}, [walletsInitialized, eventActions]);
return {
@ -84,4 +62,4 @@ const useMenuElements = () => {
};
};
export default useMenuElements;
export default useMenuElements;

View File

@ -185,7 +185,6 @@
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
NSDictionary *userInfo = notification.request.content.userInfo;
[EventEmitter.sharedInstance sendNotification:userInfo];
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}

View File

@ -12,7 +12,6 @@
@interface EventEmitter : RCTEventEmitter <RCTBridgeModule>
+ (EventEmitter *)sharedInstance;
- (void)sendNotification:(NSDictionary *)userInfo;
- (void)sendUserActivity:(NSDictionary *)userInfo;
@end

View File

@ -36,12 +36,7 @@ RCT_EXPORT_MODULE();
}
- (NSArray<NSString *> *)supportedEvents {
return @[@"onNotificationReceived",@"onUserActivityOpen"];
}
- (void)sendNotification:(NSDictionary *)userInfo
{
[sharedInstance sendEventWithName:@"onNotificationReceived" body:userInfo];
return @[@"onUserActivityOpen"];
}
- (void)sendUserActivity:(NSDictionary *)userInfo