diff --git a/components/labels.js b/components/labels.js
index 5ebae60..5a04427 100644
--- a/components/labels.js
+++ b/components/labels.js
@@ -57,6 +57,11 @@ export const settings = {
timeSet: time => `Daily reminder set for ${time}`,
notification: 'Record your morning temperature'
},
+ periodReminder: {
+ title: 'Next period reminder',
+ reminderText: 'Get a notification 3 days before your next period is likely to start.',
+ notification: daysToEndOfPrediction => `Your next period is likely to start in 3 to ${daysToEndOfPrediction} days.`
+ },
passwordSettings: {
title: 'App password',
explainerDisabled: "Encrypt the app's database with a password. You need to enter the password every time the app is started.",
diff --git a/components/settings/index.js b/components/settings/index.js
index e0828d8..1180f24 100644
--- a/components/settings/index.js
+++ b/components/settings/index.js
@@ -8,6 +8,7 @@ import styles from '../../styles/index'
import { settings as labels } from '../labels'
import { AppText } from '../app-text'
import TempReminderPicker from './temp-reminder-picker'
+import PeriodReminderPicker from './period-reminder'
import TempSlider from './temp-slider'
import openImportDialogAndImport from './import-dialog'
import openShareDialogAndExport from './export-dialog'
@@ -30,6 +31,7 @@ export default class Settings extends Component {
{labels.tempScale.segmentExplainer}
+
diff --git a/components/settings/period-reminder.js b/components/settings/period-reminder.js
new file mode 100644
index 0000000..70ec0fc
--- /dev/null
+++ b/components/settings/period-reminder.js
@@ -0,0 +1,41 @@
+import React, { Component } from 'react'
+import {
+ View,
+ Switch
+} from 'react-native'
+import { AppText } from '../app-text'
+import {
+ periodReminderObservable,
+ savePeriodReminder
+} from '../../local-storage'
+import styles from '../../styles/index'
+import { settings as labels } from '../labels'
+
+export default class PeriodReminderPicker extends Component {
+ constructor(props) {
+ super(props)
+ this.state = periodReminderObservable.value
+ }
+
+ render() {
+ return (
+
+
+ {labels.periodReminder.title}
+
+
+
+ {labels.periodReminder.reminderText}
+
+ {
+ this.setState({ enabled: switchOn })
+ savePeriodReminder({enabled: switchOn})
+ }}
+ />
+
+
+ )
+ }
+}
\ No newline at end of file
diff --git a/lib/notifications.js b/lib/notifications.js
index dc1a7a5..0605642 100644
--- a/lib/notifications.js
+++ b/lib/notifications.js
@@ -1,21 +1,26 @@
-import {tempReminderObservable} from '../local-storage'
+import {tempReminderObservable, periodReminderObservable} from '../local-storage'
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) {
Notification.configure({
- onNotification: () => {
- const todayDateString = LocalDate.now().toString()
- const cycleDay = getOrCreateCycleDay(todayDateString)
- navigate('TemperatureEditView', { cycleDay })
+ onNotification: (notification) => {
+ if (notification.id === '1') {
+ const todayDateString = LocalDate.now().toString()
+ const cycleDay = getOrCreateCycleDay(todayDateString)
+ navigate('TemperatureEditView', { cycleDay })
+ } else {
+ navigate('Home')
+ }
}
})
tempReminderObservable(reminder => {
- Notification.cancelAllLocalNotifications()
+ Notification.cancelLocalNotifications({id: '1'})
if (reminder.enabled) {
const [hours, minutes] = reminder.time.split(':')
let target = new Moment()
@@ -28,6 +33,7 @@ export default function setupNotifications(navigate) {
}
Notification.localNotificationSchedule({
+ id: '1',
message: labels.tempReminder.notification,
date: target.toDate(),
vibrate: false,
@@ -35,4 +41,37 @@ export default function setupNotifications(navigate) {
})
}
})
+
+ periodReminderObservable(reminder => {
+ Notification.cancelLocalNotifications({id: '2'})
+ 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
diff --git a/local-storage/index.js b/local-storage/index.js
index d41d4d7..5f4af70 100644
--- a/local-storage/index.js
+++ b/local-storage/index.js
@@ -34,6 +34,16 @@ export async function saveTempReminder(reminder) {
tempReminderObservable.set(reminder)
}
+export const periodReminderObservable = Observable()
+setObvWithInitValue('periodReminder', periodReminderObservable, {
+ enabled: false
+})
+
+export async function savePeriodReminder(reminder) {
+ await AsyncStorage.setItem('periodReminder', JSON.stringify(reminder))
+ periodReminderObservable.set(reminder)
+}
+
export const hasEncryptionObservable = Observable()
setObvWithInitValue('hasEncryption', hasEncryptionObservable, false)