fix: notification ID generation and clean up scheduleChoreNotification function
This commit is contained in:
@@ -22,8 +22,11 @@ const canScheduleNotification = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getIdFromTemplate = (choreId, template) => {
|
const getIdFromTemplate = (choreId, template) => {
|
||||||
// convert to base 32 int for notification id using murmurhash :
|
// Generate a hash and convert to valid Java int range
|
||||||
return murmurhash.v3(`${choreId}-${template.value}-${template.unit}`)
|
const hash = murmurhash.v3(`${choreId}-${template.value}-${template.unit}`)
|
||||||
|
// Use Math.abs() with modulo to ensure positive ID within Java int range
|
||||||
|
// This guarantees the ID is always positive and within 1 to 2^31-1
|
||||||
|
return Math.abs(hash) % 2147483647
|
||||||
}
|
}
|
||||||
|
|
||||||
const getTimeFromTemplate = (template, relativeTime) => {
|
const getTimeFromTemplate = (template, relativeTime) => {
|
||||||
@@ -53,12 +56,6 @@ const scheduleNotificationFromTemplate = (
|
|||||||
) => {
|
) => {
|
||||||
for (const template of chore.notificationMetadata?.templates || []) {
|
for (const template of chore.notificationMetadata?.templates || []) {
|
||||||
// convert the template to time:
|
// convert the template to time:
|
||||||
console.log(
|
|
||||||
'Scheduling notification for chore:',
|
|
||||||
chore.id,
|
|
||||||
'with template:',
|
|
||||||
template,
|
|
||||||
)
|
|
||||||
const dueDate = new Date(chore.nextDueDate)
|
const dueDate = new Date(chore.nextDueDate)
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
const time = getTimeFromTemplate(template, dueDate)
|
const time = getTimeFromTemplate(template, dueDate)
|
||||||
@@ -89,11 +86,11 @@ const getNotificationText = (choreName, template = {}) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (template.value < 0) {
|
if (template.value < 0) {
|
||||||
return 'reminder' // Before due date
|
return 'reminder'
|
||||||
} else if (template.value === 0) {
|
} else if (template.value === 0) {
|
||||||
return 'due' // Due now
|
return 'due'
|
||||||
} else {
|
} else {
|
||||||
return 'overdue' // After due date
|
return 'overdue'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,8 +186,6 @@ const scheduleChoreNotification = async (
|
|||||||
await cancelPendingNotifications()
|
await cancelPendingNotifications()
|
||||||
const notifications = []
|
const notifications = []
|
||||||
|
|
||||||
const devicePreferences = await getNotificationPreferences()
|
|
||||||
|
|
||||||
for (let i = 0; i < chores.length; i++) {
|
for (let i = 0; i < chores.length; i++) {
|
||||||
const chore = chores[i]
|
const chore = chores[i]
|
||||||
try {
|
try {
|
||||||
@@ -216,7 +211,7 @@ const scheduleChoreNotification = async (
|
|||||||
LocalNotifications.schedule({
|
LocalNotifications.schedule({
|
||||||
notifications,
|
notifications,
|
||||||
})
|
})
|
||||||
console.log('Scheduled notifications:', notifications)
|
return notifications
|
||||||
}
|
}
|
||||||
|
|
||||||
export { canScheduleNotification, scheduleChoreNotification }
|
export { canScheduleNotification, scheduleChoreNotification }
|
||||||
|
|||||||
Reference in New Issue
Block a user