Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Oct 29, 2024
1 parent c14e545 commit a1d905f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
9 changes: 0 additions & 9 deletions apps/wallet-mobile/.storybook/storybook.requires.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {notificationManager} from './notification-manager'
import {parseNotificationId} from './notifications'
import {usePrimaryTokenPriceChangedNotification} from './primary-token-price-changed-notification'
import {useTransactionReceivedNotifications} from './transaction-received-notification'
import {useRewardsUpdatedNotifications} from './rewards-updated-notification'

let initialized = false

Expand Down Expand Up @@ -41,4 +42,5 @@ export const useInitNotifications = ({enabled}: {enabled: boolean}) => {
React.useEffect(() => (enabled ? init() : undefined), [enabled])
useTransactionReceivedNotifications({enabled})
usePrimaryTokenPriceChangedNotification({enabled})
useRewardsUpdatedNotifications({enabled})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as BackgroundFetch from 'expo-background-fetch'

Check failure on line 1 in apps/wallet-mobile/src/features/Notifications/useCases/common/rewards-updated-notification.ts

View workflow job for this annotation

GitHub Actions / check

Run autofix to sort these imports!
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 = () => {

Check failure on line 50 in apps/wallet-mobile/src/features/Notifications/useCases/common/rewards-updated-notification.ts

View workflow job for this annotation

GitHub Actions / check

'createRewardsUpdatedNotification' is assigned a value but never used. Allowed unused vars must match /^_/u
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])
}

0 comments on commit a1d905f

Please sign in to comment.