Skip to content
Snippets Groups Projects
Commit 09a71a39 authored by tina's avatar tina
Browse files

links the period reminder to bleeding days, meaning it gets updated when they change

parent 71016193
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ import Notification from 'react-native-push-notification'
import { LocalDate } from 'js-joda'
import Moment from 'moment'
import { settings as labels } from '../components/labels'
import { getOrCreateCycleDay } from '../db'
import { getOrCreateCycleDay, getBleedingDaysSortedByDate } from '../db'
import cycleModule from './cycle'
export default function setupNotifications(navigate) {
......@@ -44,26 +44,34 @@ export default function setupNotifications(navigate) {
periodReminderObservable(reminder => {
Notification.cancelLocalNotifications({id: '2'})
if (reminder.enabled) {
const bleedingPrediction = cycleModule().getPredictedMenses()
if (bleedingPrediction.length > 0) {
const bleedingStart = Moment(bleedingPrediction[0][0], "YYYY-MM-DD")
// 3 days before and at 6 am
const reminderDate = bleedingStart
.subtract(3, 'days')
.hours(6)
.minutes(0)
.seconds(0)
// period is likely to start in 3 to 3 + (length of prediction - 1) days
const daysToEndOfPrediction = bleedingPrediction[0].length + 2
if (reminder.enabled) setupPeriodReminder()
})
Notification.localNotificationSchedule({
id: '2',
message: labels.periodReminder.notification(daysToEndOfPrediction),
date: reminderDate.toDate(),
vibrate: false
})
}
}
getBleedingDaysSortedByDate().addListener(() => {
Notification.cancelLocalNotifications({id: '2'})
if (periodReminderObservable.value.enabled) setupPeriodReminder()
})
}
function setupPeriodReminder() {
const bleedingPrediction = cycleModule().getPredictedMenses()
if (bleedingPrediction.length > 0) {
const bleedingStart = Moment(bleedingPrediction[0][0], "YYYY-MM-DD")
// 3 days before and at 6 am
const reminderDate = bleedingStart
.subtract(3, 'days')
.hours(6)
.minutes(0)
.seconds(0)
// period is likely to start in 3 to 3 + (length of prediction - 1) days
const daysToEndOfPrediction = bleedingPrediction[0].length + 2
Notification.localNotificationSchedule({
id: '2',
message: labels.periodReminder.notification(daysToEndOfPrediction),
date: reminderDate.toDate(),
vibrate: false
})
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment