From 55ae55836d59e961b8f94233d767c8e617d383b4 Mon Sep 17 00:00:00 2001 From: Neil Agarwal Date: Tue, 4 May 2021 21:09:55 +0530 Subject: [PATCH] fix: notifications asyncStorage --- src/utils/asyncStorageUtils.ts | 24 +++++++++++++----------- src/utils/backgroundUpdater.ts | 9 ++++----- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/utils/asyncStorageUtils.ts b/src/utils/asyncStorageUtils.ts index a0e09e3..c934f08 100644 --- a/src/utils/asyncStorageUtils.ts +++ b/src/utils/asyncStorageUtils.ts @@ -63,17 +63,19 @@ export interface INotificationStore { export const setNotificationStore = async ( notificationStore: INotificationStore, ) => { - try { - if (!notificationStore.lastChecked) { - notificationStore.lastChecked = await ( - await getAsyncStorageItem(AsyncStorageKeys.NOTIFICATION) - ).lastChecked; - } - await setAsyncStorageItem( - AsyncStorageKeys.NOTIFICATION, - notificationStore, - ); - } catch (error) {} + const currentNotificationStore: INotificationStore = await getAsyncStorageItem( + AsyncStorageKeys.NOTIFICATION, + ); + + const notificationStoreToSet: INotificationStore = { + notified: notificationStore.notified, + lastChecked: currentNotificationStore?.lastChecked, + }; + + await setAsyncStorageItem( + AsyncStorageKeys.NOTIFICATION, + notificationStoreToSet, + ); }; export interface ISettingsStore { diff --git a/src/utils/backgroundUpdater.ts b/src/utils/backgroundUpdater.ts index cd65e51..1c8c0ef 100644 --- a/src/utils/backgroundUpdater.ts +++ b/src/utils/backgroundUpdater.ts @@ -24,10 +24,7 @@ const backgroundUpdaterTask = async ({ taskId, timeout }: HeadlessEvent) => { AsyncStorageKeys.SETTINGS, ); - if ( - notificationStore?.notified === true || - !settingsStore?.notificationsEnabled - ) { + if (notificationStore?.notified || !settingsStore?.notificationsEnabled) { BackgroundFetch.finish(taskId); return; } @@ -51,7 +48,9 @@ const backgroundUpdaterTask = async ({ taskId, timeout }: HeadlessEvent) => { }, 0, ); - const message = `Found ${numberOfSlots} slots for ${settings.district?.value}`; + const message = `Found ${numberOfSlots} slot${ + numberOfSlots > 1 ? 's' : '' + } for ${settings.district?.value}`; sendSlotFoundNotification(message, lastChecked); setNotificationStore({ notified: true, lastChecked }); }