links the period reminder to bleeding days, meaning it gets updated when they change
This commit is contained in:
+30
-22
@@ -3,7 +3,7 @@ import Notification from 'react-native-push-notification'
|
|||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
import Moment from 'moment'
|
import Moment from 'moment'
|
||||||
import { settings as labels } from '../components/labels'
|
import { settings as labels } from '../components/labels'
|
||||||
import { getOrCreateCycleDay } from '../db'
|
import { getOrCreateCycleDay, getBleedingDaysSortedByDate } from '../db'
|
||||||
import cycleModule from './cycle'
|
import cycleModule from './cycle'
|
||||||
|
|
||||||
export default function setupNotifications(navigate) {
|
export default function setupNotifications(navigate) {
|
||||||
@@ -44,26 +44,34 @@ export default function setupNotifications(navigate) {
|
|||||||
|
|
||||||
periodReminderObservable(reminder => {
|
periodReminderObservable(reminder => {
|
||||||
Notification.cancelLocalNotifications({id: '2'})
|
Notification.cancelLocalNotifications({id: '2'})
|
||||||
if (reminder.enabled) {
|
if (reminder.enabled) 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
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user