core: add support for yearly reminders

This commit is contained in:
Abdullah Atta
2023-12-21 08:57:30 +05:00
parent 12b8a9e487
commit 9edf6d165d
5 changed files with 48 additions and 20 deletions

View File

@@ -38,6 +38,7 @@
"ws": "^8.13.0"
},
"scripts": {
"postinstall": "patch-package",
"prebuild": "node scripts/prebuild.mjs",
"pretest": "node scripts/prebuild.mjs",
"pretest:e2e": "node scripts/prebuild.mjs",

View File

@@ -181,16 +181,32 @@ export function isReminderToday(reminder: Reminder) {
export function getUpcomingReminderTime(reminder: Reminder) {
if (reminder.mode === "once") return reminder.date;
// this is only the time (hour & minutes); date is not included
const time = dayjs(reminder.date);
const now = dayjs();
const relativeTime = now.clone().hour(time.hour()).minute(time.minute());
const isPast = relativeTime.isSameOrBefore(now);
const isDay = reminder.recurringMode === "day";
const isWeek = reminder.recurringMode === "week";
const isMonth = reminder.recurringMode === "month";
const isYear = reminder.recurringMode === "year";
// this is only the time (hour & minutes) unless it is a
// yearly reminder
const time = dayjs(reminder.date);
const now = dayjs();
const relativeTime = isYear
? now
.clone()
.hour(time.hour())
.minute(time.minute())
.month(time.month())
.date(time.date())
: now.clone().hour(time.hour()).minute(time.minute());
const isPast = relativeTime.isSameOrBefore(now);
if (isYear) {
if (isPast) return relativeTime.add(1, "year").valueOf();
else return relativeTime.valueOf();
}
if (isDay) {
if (isPast) return relativeTime.add(1, "day").valueOf();
else return relativeTime.valueOf();

View File

@@ -124,23 +124,27 @@ export const EVENTS = {
};
const separators = ["-", "/"];
const DD = "DD";
const MM = "MM";
const YYYY = "YYYY";
export const DATE_FORMATS = [
...separators
.map((sep) => {
const DD = "DD";
const MM = "MM";
const YYYY = "YYYY";
return [
[DD, MM, YYYY].join(sep),
[MM, DD, YYYY].join(sep),
[YYYY, MM, DD].join(sep)
];
})
.map((sep) => [
[DD, MM, YYYY].join(sep),
[MM, DD, YYYY].join(sep),
[YYYY, MM, DD].join(sep)
])
.flat(),
"MMM D, YYYY"
];
export const DATE_FORMATS_WITHOUT_YEAR = [
...separators
.map((sep) => [[DD, MM].join(sep), [MM, DD].join(sep), [MM, DD].join(sep)])
.flat(),
"MMM D"
];
export const TIME_FORMATS = ["12-hour", "24-hour"];
export const CURRENT_DATABASE_VERSION = 6.0;

View File

@@ -351,7 +351,7 @@ export interface Reminder extends BaseItem<"reminder"> {
priority: "silent" | "vibrate" | "urgent";
date: number;
mode: "repeat" | "once" | "permanent";
recurringMode?: "week" | "month" | "day";
recurringMode?: "week" | "month" | "day" | "year";
selectedDays?: number[];
localOnly?: boolean;
disabled?: boolean;

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import dayjs from "dayjs";
import { DATE_FORMATS, DATE_FORMATS_WITHOUT_YEAR } from "../common";
export type TimeFormat = "12-hour" | "24-hour";
@@ -69,7 +70,7 @@ export type TimeOptions = {
timeFormat: TimeFormat;
};
export type DateOptions = {
type: "date";
type: "date" | "date-without-year";
dateFormat: string;
};
export type DateTimeOptions = {
@@ -95,7 +96,13 @@ export function formatDate(
case "time":
return dayjs(date).format(getTimeFormat(options.timeFormat));
case "date":
return dayjs(date).format(`${options.dateFormat}`);
return dayjs(date).format(options.dateFormat);
case "date-without-year": {
const format =
DATE_FORMATS_WITHOUT_YEAR[DATE_FORMATS.indexOf(options.dateFormat)];
if (!format) return dayjs(date).format("MM-DD");
return dayjs(date).format(format);
}
}
}