From 09a71a391ed0eca8f2f548b205a5c2ad25112bbb Mon Sep 17 00:00:00 2001 From: tina Date: Wed, 26 Sep 2018 12:15:09 +0200 Subject: [PATCH] links the period reminder to bleeding days, meaning it gets updated when they change --- lib/notifications.js | 52 +++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/notifications.js b/lib/notifications.js index a2ba749..0605642 100644 --- a/lib/notifications.js +++ b/lib/notifications.js @@ -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 - - Notification.localNotificationSchedule({ - id: '2', - message: labels.periodReminder.notification(daysToEndOfPrediction), - date: reminderDate.toDate(), - vibrate: false - }) - } - } + if (reminder.enabled) setupPeriodReminder() }) + + 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