Skip to content

Commit

Permalink
fix: reminder not working as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
meypod committed Oct 10, 2022
1 parent 5075e2b commit 4974fd2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/screens/settings_reminders/edit_reminder_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export function EditReminderModal({
duration: reminderState?.duration || defaultDuration,
durationModifier: reminderState?.durationModifier || -1,
prayer: reminderState?.prayer || Prayer.Fajr,
modified: Date.now(),
whenIsFired: undefined,
});
} else {
setDraftReminderState(null);
Expand Down
6 changes: 6 additions & 0 deletions src/store/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export type Reminder = {
duration: number;
/** has a value of `-1` or `+1` */
durationModifier: number;
/** when is this reminder scheduled to be fired. can be undefined if not scheduled yet. or an outdated timestamp. */
whenIsFired?: number;
/** timestamp of when it was scheduled. */
whenScheduled?: number;
/** timestamp of when it was modified. */
modified?: number;
};

export type SettingsStore = {
Expand Down
38 changes: 35 additions & 3 deletions src/tasks/set_reminder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
REMINDER_CHANNEL_NAME,
} from '@/constants/notification';
import {hasAtLeastOneNotificationSetting} from '@/store/calculation_settings';
import {Reminder} from '@/store/settings';
import {Reminder, settings} from '@/store/settings';
import {getNextDayBeginning, getTime} from '@/utils/date';

type SetReminderOptions = {
Expand All @@ -22,6 +22,25 @@ type SetReminderOptions = {
reminders: Array<Reminder>;
};

function needSchedulePredicate(reminder: Reminder) {
if (reminder.whenIsFired) {
if (Date.now() <= reminder.whenIsFired) {
return false;
}
}
return true;
}

function needToCancelPredicate(reminder: Reminder) {
if (!reminder.enabled) return true;
if (!reminder.whenScheduled) return false;
if (!reminder.modified) return false;
if (reminder.whenScheduled <= reminder.modified) {
return true;
}
return false;
}

export async function setReminders(options: SetReminderOptions) {
const notificationSettingsIsValid = hasAtLeastOneNotificationSetting();

Expand All @@ -43,17 +62,23 @@ export async function setReminders(options: SetReminderOptions) {
});

await notifee
.cancelTriggerNotifications(options.reminders.map(r => r.id))
.cancelTriggerNotifications(
options.reminders.filter(needToCancelPredicate).map(r => r.id),
)
.catch(console.error);

for (const reminder of options.reminders.filter(r => r.enabled)) {
for (const reminder of options.reminders
.filter(r => r.enabled)
.filter(needSchedulePredicate)) {
let pTime = prayerTimes[reminder.prayer].getTime();
if (pTime < Date.now()) {
pTime = tomorrowPrayerTimes[reminder.prayer].getTime();
}

const timestamp = pTime + reminder.duration * reminder.durationModifier;

if (timestamp < Date.now()) continue;

const trigger: TimestampTrigger = {
type: TriggerType.TIMESTAMP,
timestamp,
Expand All @@ -78,6 +103,13 @@ export async function setReminders(options: SetReminderOptions) {
},
trigger,
)
.then(() => {
settings.getState().saveReminder({
...reminder,
whenScheduled: Date.now(),
whenIsFired: timestamp,
});
})
.catch(console.error);

if (!options?.noToast) {
Expand Down

0 comments on commit 4974fd2

Please sign in to comment.