From 71249b45148613d3d89d9b0cb6064fc242766b2a Mon Sep 17 00:00:00 2001 From: Michal Date: Mon, 23 Sep 2024 19:50:00 +0100 Subject: [PATCH] WIP --- .../useCases/NotificationsDevScreen.tsx | 218 +++++----------- .../Notifications/useCases/common/hooks.ts | 242 ++++++++++++++++++ .../SelectWalletFromListScreen.tsx | 2 + .../yoroi-wallets/cardano/cardano-wallet.ts | 6 +- .../src/yoroi-wallets/cardano/types.ts | 3 + .../messages/src/AppNavigator.json | 32 +-- packages/types/src/index.ts | 5 + packages/types/src/notifications/manager.ts | 13 + 8 files changed, 352 insertions(+), 169 deletions(-) create mode 100644 apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts diff --git a/apps/wallet-mobile/src/features/Notifications/useCases/NotificationsDevScreen.tsx b/apps/wallet-mobile/src/features/Notifications/useCases/NotificationsDevScreen.tsx index 92ea021218..a8346d5ed4 100644 --- a/apps/wallet-mobile/src/features/Notifications/useCases/NotificationsDevScreen.tsx +++ b/apps/wallet-mobile/src/features/Notifications/useCases/NotificationsDevScreen.tsx @@ -1,153 +1,25 @@ -import {Notification, Notifications} from '@jamsinclair/react-native-notifications' -import {useAsyncStorage, useMutationWithInvalidations} from '@yoroi/common' -import {notificationManagerMaker} from '@yoroi/notifications' import {useTheme} from '@yoroi/theme' import {Notifications as NotificationTypes} from '@yoroi/types' import * as React from 'react' import {StyleSheet, Switch as RNSwitch, Text, View} from 'react-native' import {SafeAreaView} from 'react-native-safe-area-context' -import {UseMutationOptions, useQuery, UseQueryOptions} from 'react-query' -import {Subject} from 'rxjs' -import uuid from 'uuid' import {Button} from '../../../components/Button/Button' - -const useRequestPermissions = () => { - React.useEffect(() => { - Notifications.registerRemoteNotifications() - }, []) -} - -const useHandleNotification = () => { - React.useEffect(() => { - const s = Notifications.events().registerNotificationReceivedForeground( - (notification: Notification, completion) => { - console.log(`Notification received in foreground: ${notification.title} : ${notification.body}`) - completion({alert: true, sound: true, badge: true}) - }, - ) - return () => { - s.remove() - } - }, []) -} - -const useNotificationsConfig = () => { - const manager = useNotificationsManager() - return useQuery(['notificationsConfig'], () => manager.config.read()) -} - -const useUpdateNotificationsConfig = () => { - const manager = useNotificationsManager() - const mutationFn = async (newConfig: NotificationTypes.Config) => { - await manager.config.save(newConfig) - } - - return useMutationWithInvalidations({ - mutationFn, - invalidateQueries: [['notificationsConfig']], - }) -} - -const useResetNotificationsConfig = (options: UseMutationOptions = {}) => { - const manager = useNotificationsManager() - const mutationFn = async () => { - await manager.config.reset() - return manager.config.read() - } - - return useMutationWithInvalidations({ - mutationFn, - invalidateQueries: [['notificationsConfig']], - ...options, - }) -} - -const useReceivedNotificationEvents = ( - options: UseQueryOptions, Error> = {}, -) => { - const manager = useNotificationsManager() - const queryFn = () => manager.events.read() - return useQuery({ - queryKey: ['receivedNotificationEvents'], - queryFn, - ...options, - }) -} - -const useSendNotification = () => { - const sendNotification = React.useCallback((title: string, body: string) => { - const notification = new Notification({ - title, - body, - sound: 'default', - }) - Notifications.postLocalNotification(notification.payload) - }, []) - - return {send: sendNotification} -} - -const useNotificationsManager = (options?: {subscriptions?: NotificationTypes.ManagerMakerProps['subscriptions']}) => { - const storage = useAsyncStorage() - const subscriptions = options?.subscriptions - - const manager = React.useMemo(() => { - const eventsStorage = storage.join('events/') - const configStorage = storage.join('settings/') - - return notificationManagerMaker({ - eventsStorage, - configStorage, - subscriptions, - }) - }, [storage, subscriptions]) - - React.useEffect(() => { - manager.hydrate() - return () => { - manager.destroy() - } - }, [manager]) - - return manager -} - -const useNotifications = () => { - const {send} = useSendNotification() - const [transactionReceivedSubject] = React.useState(new Subject()) - const manager = useNotificationsManager({ - subscriptions: {[NotificationTypes.Trigger.TransactionReceived]: transactionReceivedSubject}, - }) - React.useEffect(() => { - const subscription = manager.notification$.subscribe((notificationEvent) => { - if (notificationEvent.trigger === NotificationTypes.Trigger.TransactionReceived) { - send('Transaction received', 'You have received a new transaction') - } - }) - return () => { - subscription.unsubscribe() - } - }, [manager, send]) - - const triggerTransactionReceived = (metadata: NotificationTypes.TransactionReceivedEvent['metadata']) => { - transactionReceivedSubject.next({ - id: uuid.v4(), - date: new Date().toISOString(), - isRead: false, - trigger: NotificationTypes.Trigger.TransactionReceived, - metadata, - }) - } - - return {triggerTransactionReceived} -} +import {ScrollableView} from '../../../components/ScrollableView' +import { + useHandleNotification, + useMockedNotifications, + useNotificationsConfig, + useReceivedNotificationEvents, + useRequestPermissions, + useResetNotificationsConfig, + useUpdateNotificationsConfig, +} from './common/hooks' export const NotificationsDevScreen = () => { useRequestPermissions() useHandleNotification() - const {triggerTransactionReceived} = useNotifications() - const {data: receivedNotifications} = useReceivedNotificationEvents() + const {triggerTransactionReceived} = useMockedNotifications() const handleOnTriggerTransactionReceived = () => { triggerTransactionReceived({ @@ -160,26 +32,68 @@ export const NotificationsDevScreen = () => { return ( - - Notifications Playground + + + Notifications Playground -