-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c14e545
commit a1d905f
Showing
3 changed files
with
89 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
.../wallet-mobile/src/features/Notifications/useCases/common/rewards-updated-notification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import * as BackgroundFetch from 'expo-background-fetch' | ||
import * as TaskManager from 'expo-task-manager' | ||
import * as React from 'react' | ||
import {mountAsyncStorage, useAsyncStorage} from '@yoroi/common' | ||
import {walletManager} from '../../../WalletManager/wallet-manager' | ||
import {notificationManager} from './notification-manager' | ||
import {Notifications as NotificationTypes} from '@yoroi/types' | ||
import {generateNotificationId} from './notifications' | ||
import {Subject} from 'rxjs' | ||
import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider' | ||
|
||
const backgroundTaskId = 'yoroi-rewards-updated-notifications-background-fetch' | ||
const backgroundSyncInMinutes = 60 * 10 | ||
|
||
// Check is needed for hot reloading, as task can not be defined twice | ||
if (!TaskManager.isTaskDefined(backgroundTaskId)) { | ||
const appStorage = mountAsyncStorage({path: '/'}) | ||
TaskManager.defineTask(backgroundTaskId, async () => { | ||
await syncAllWallets() | ||
const notifications = await buildNotifications(appStorage) | ||
notifications.forEach((notification) => notificationManager.events.push(notification)) | ||
const hasNewData = notifications.length > 0 | ||
return hasNewData ? BackgroundFetch.BackgroundFetchResult.NewData : BackgroundFetch.BackgroundFetchResult.NoData | ||
}) | ||
} | ||
|
||
export const rewardsUpdatedSubject = new Subject<NotificationTypes.RewardsUpdatedEvent>() | ||
|
||
const registerBackgroundFetchAsync = () => { | ||
return BackgroundFetch.registerTaskAsync(backgroundTaskId, { | ||
minimumInterval: backgroundSyncInMinutes, | ||
stopOnTerminate: false, | ||
startOnBoot: true, | ||
}) | ||
} | ||
|
||
const unregisterBackgroundFetchAsync = () => { | ||
return BackgroundFetch.unregisterTaskAsync(backgroundTaskId) | ||
} | ||
|
||
const syncAllWallets = async () => { | ||
const ids = [...walletManager.walletMetas.keys()] | ||
for (const id of ids) { | ||
const wallet = walletManager.getWalletById(id) | ||
if (!wallet) continue | ||
await wallet.sync({}) | ||
} | ||
} | ||
|
||
const createRewardsUpdatedNotification = () => { | ||
return { | ||
id: generateNotificationId(), | ||
date: new Date().toISOString(), | ||
isRead: false, | ||
trigger: NotificationTypes.Trigger.RewardsUpdated, | ||
} as const | ||
} | ||
|
||
export const useRewardsUpdatedNotifications = ({enabled}: {enabled: boolean}) => { | ||
const {walletManager} = useWalletManager() | ||
const asyncStorage = useAsyncStorage() | ||
|
||
React.useEffect(() => { | ||
if (!enabled) return | ||
registerBackgroundFetchAsync() | ||
return () => { | ||
unregisterBackgroundFetchAsync() | ||
} | ||
}, [enabled]) | ||
|
||
React.useEffect(() => { | ||
if (!enabled) return | ||
const subscription = walletManager.syncWalletInfos$.subscribe(async (status) => { | ||
const walletInfos = Array.from(status.values()) | ||
const walletsDoneSyncing = walletInfos.filter((info) => info.status === 'done') | ||
const areAllDone = walletsDoneSyncing.length === walletInfos.length | ||
if (!areAllDone) return | ||
|
||
const notifications = await buildNotifications(asyncStorage) | ||
notifications.forEach((notification) => rewardsUpdatedSubject.next(notification)) | ||
}) | ||
|
||
return () => { | ||
subscription.unsubscribe() | ||
} | ||
}, [walletManager, asyncStorage, enabled]) | ||
} |