506: Enable custom timing for period reminders
Users can now specify how many days prior to their next predicted period they want to be notified
This commit is contained in:
+49
-27
@@ -2,9 +2,9 @@ import { Platform } from 'react-native'
|
||||
import {
|
||||
tempReminderObservable,
|
||||
periodReminderObservable,
|
||||
advanceNoticeDaysObservable,
|
||||
} from '../local-storage'
|
||||
import * as PN from 'react-native-push-notification'
|
||||
import { requestNotifications } from 'react-native-permissions'
|
||||
import Moment from 'moment'
|
||||
import { LocalDate } from '@js-joda/core'
|
||||
|
||||
@@ -13,12 +13,14 @@ import { getBleedingDaysSortedByDate } from '../db'
|
||||
import cycleModule from './cycle'
|
||||
import nothingChanged from '../db/db-unchanged'
|
||||
|
||||
export default function setupNotifications(navigate, setDate) {
|
||||
Platform.OS === 'android' ? requestNotifications() : null
|
||||
const PushNotification = Platform.OS === 'ios' ? PN : PN.default
|
||||
const DRIP_CHANNEL_ID = 'drip-channel-id'
|
||||
const TEMPERATURE_REMINDER_ID = '1'
|
||||
const PERIOD_REMINDER_ID = '2'
|
||||
const PushNotification = Platform.OS === 'ios' ? PN : PN.default
|
||||
|
||||
export default function setupNotifications(navigate, setDate) {
|
||||
PushNotification.createChannel({
|
||||
channelId: 'drip-channel-id', // (required)
|
||||
channelId: DRIP_CHANNEL_ID, // (required)
|
||||
channelName: 'drip reminder', // (required)
|
||||
playSound: false, // (optional) default: true
|
||||
})
|
||||
@@ -26,7 +28,10 @@ export default function setupNotifications(navigate, setDate) {
|
||||
PushNotification.configure({
|
||||
onNotification: (notification) => {
|
||||
// https://github.com/zo0r/react-native-push-notification/issues/966#issuecomment-479069106
|
||||
if (notification.data?.id === '1' || notification.id === '1') {
|
||||
if (
|
||||
notification.data?.id === TEMPERATURE_REMINDER_ID ||
|
||||
notification.id === TEMPERATURE_REMINDER_ID
|
||||
) {
|
||||
const todayDate = LocalDate.now().toString()
|
||||
setDate(todayDate)
|
||||
navigate('TemperatureEditView')
|
||||
@@ -37,7 +42,7 @@ export default function setupNotifications(navigate, setDate) {
|
||||
})
|
||||
|
||||
tempReminderObservable((reminder) => {
|
||||
PushNotification.cancelLocalNotification({ id: '1' })
|
||||
PushNotification.cancelLocalNotification({ id: TEMPERATURE_REMINDER_ID })
|
||||
if (reminder.enabled) {
|
||||
const [hours, minutes] = reminder.time.split(':')
|
||||
let target = new Moment()
|
||||
@@ -50,57 +55,74 @@ export default function setupNotifications(navigate, setDate) {
|
||||
}
|
||||
|
||||
PushNotification.localNotificationSchedule({
|
||||
id: '1',
|
||||
userInfo: { id: '1' },
|
||||
id: TEMPERATURE_REMINDER_ID,
|
||||
userInfo: { id: TEMPERATURE_REMINDER_ID },
|
||||
message: labels.tempReminder.notification,
|
||||
date: target.toDate(),
|
||||
vibrate: false,
|
||||
repeatType: 'day',
|
||||
channelId: 'drip-channel-id',
|
||||
channelId: DRIP_CHANNEL_ID,
|
||||
allowWhileIdle: true,
|
||||
})
|
||||
}
|
||||
}, false)
|
||||
|
||||
periodReminderObservable((reminder) => {
|
||||
PushNotification.cancelLocalNotification({ id: '2' })
|
||||
if (reminder.enabled) setupPeriodReminder()
|
||||
}, false)
|
||||
periodReminderObservable(() => updatePeriodNotification(), false)
|
||||
advanceNoticeDaysObservable(() => updatePeriodNotification(), false)
|
||||
|
||||
getBleedingDaysSortedByDate().addListener((_, changes) => {
|
||||
// the listener fires on setup, so we check if there were actually any changes
|
||||
if (nothingChanged(changes)) return
|
||||
PushNotification.cancelLocalNotification({ id: '2' })
|
||||
if (periodReminderObservable.value.enabled) setupPeriodReminder()
|
||||
if (nothingChanged(changes)) {
|
||||
return
|
||||
}
|
||||
|
||||
updatePeriodNotification()
|
||||
})
|
||||
}
|
||||
|
||||
function setupPeriodReminder() {
|
||||
const PushNotification = Platform.OS === 'ios' ? PN : PN.default
|
||||
const updatePeriodNotification = () => {
|
||||
// Cancel any existing period reminder
|
||||
PushNotification.cancelLocalNotification({ id: PERIOD_REMINDER_ID })
|
||||
|
||||
// Set up a new period reminder if enabled
|
||||
if (periodReminderObservable.value.enabled) {
|
||||
schedulePeriodNotification()
|
||||
}
|
||||
}
|
||||
|
||||
function schedulePeriodNotification() {
|
||||
const bleedingPrediction = cycleModule().getPredictedMenses()
|
||||
|
||||
if (bleedingPrediction.length > 0) {
|
||||
const predictedBleedingStart = Moment(
|
||||
bleedingPrediction[0][0],
|
||||
'YYYY-MM-DD'
|
||||
)
|
||||
// 3 days before and at 6 am
|
||||
|
||||
const advanceNoticeDays = parseInt(advanceNoticeDaysObservable.value, 10)
|
||||
|
||||
// ${advanceNoticeDays} days before and at 6 am
|
||||
const reminderDate = predictedBleedingStart
|
||||
.subtract(3, 'days')
|
||||
.subtract(advanceNoticeDays, 'days')
|
||||
.hours(6)
|
||||
.minutes(0)
|
||||
.seconds(0)
|
||||
|
||||
if (reminderDate.isAfter()) {
|
||||
// period is likely to start in 3 to 3 + (length of prediction - 1) days
|
||||
const daysToEndOfPrediction = bleedingPrediction[0].length + 2
|
||||
// period is likely to start in advanceNoticeDays to advanceNoticeDays + (length of prediction - 1) days
|
||||
const daysToEndOfPrediction =
|
||||
advanceNoticeDays + bleedingPrediction[0].length - 1
|
||||
|
||||
PushNotification.localNotificationSchedule({
|
||||
id: '2',
|
||||
userInfo: { id: '2' },
|
||||
message: labels.periodReminder.notification(daysToEndOfPrediction),
|
||||
id: PERIOD_REMINDER_ID,
|
||||
userInfo: { id: PERIOD_REMINDER_ID },
|
||||
message: labels.periodReminder.notification(
|
||||
advanceNoticeDays,
|
||||
daysToEndOfPrediction
|
||||
),
|
||||
date: reminderDate.toDate(),
|
||||
vibrate: false,
|
||||
channelId: 'drip-channel-id',
|
||||
channelId: DRIP_CHANNEL_ID,
|
||||
allowWhileIdle: true,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user