Skip to content

Commit

Permalink
fix: notifications asyncStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
regalstreak committed May 4, 2021
1 parent 46b26cb commit 55ae558
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
24 changes: 13 additions & 11 deletions src/utils/asyncStorageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 4 additions & 5 deletions src/utils/backgroundUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 });
}
Expand Down

0 comments on commit 55ae558

Please sign in to comment.