tries to enable notifications for next period

This commit is contained in:
tina
2018-09-25 15:28:04 +02:00
parent ce5a8ce5a1
commit 80bec57f69
3 changed files with 34 additions and 6 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ export const settings = {
periodReminder: { periodReminder: {
title: 'Next period reminder', title: 'Next period reminder',
reminderText: 'Get a notification 3 days before your next period is likely to start.', reminderText: 'Get a notification 3 days before your next period is likely to start.',
notification: 'your next period is likely to start in x days.' notification: daysToEndOfPrediction => `Your next period is likely to start in 3 to ${daysToEndOfPrediction} days.`
}, },
passwordSettings: { passwordSettings: {
title: 'App password', title: 'App password',
+2 -2
View File
@@ -14,7 +14,7 @@ import { settings as labels } from '../labels'
export default class PeriodReminderPicker extends Component { export default class PeriodReminderPicker extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = Object.assign({}, periodReminderObservable.value) this.state = periodReminderObservable.value
} }
render() { render() {
@@ -31,7 +31,7 @@ export default class PeriodReminderPicker extends Component {
value={this.state.enabled} value={this.state.enabled}
onValueChange={switchOn => { onValueChange={switchOn => {
this.setState({ enabled: switchOn }) this.setState({ enabled: switchOn })
savePeriodReminder({enabled: true}) savePeriodReminder({enabled: switchOn})
}} }}
/> />
</View> </View>
+30 -2
View File
@@ -1,21 +1,28 @@
import {tempReminderObservable} from '../local-storage' import {tempReminderObservable, periodReminderObservable} from '../local-storage'
import Notification from 'react-native-push-notification' 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 } from '../db'
import cycleModule from './cycle'
export default function setupNotifications(navigate) { export default function setupNotifications(navigate) {
Notification.configure({ Notification.configure({
onNotification: () => { onNotification: () => {
const todayDateString = LocalDate.now().toString() const todayDateString = LocalDate.now().toString()
const cycleDay = getOrCreateCycleDay(todayDateString) const cycleDay = getOrCreateCycleDay(todayDateString)
if (this.id === '1') {
navigate('TemperatureEditView', { cycleDay }) navigate('TemperatureEditView', { cycleDay })
} else if (this.id === '2') {
navigate('Home')
} else {
navigate('Home')
}
} }
}) })
tempReminderObservable(reminder => { tempReminderObservable(reminder => {
Notification.cancelAllLocalNotifications() Notification.cancelAllLocalNotifications({id: '1'})
if (reminder.enabled) { if (reminder.enabled) {
const [hours, minutes] = reminder.time.split(':') const [hours, minutes] = reminder.time.split(':')
let target = new Moment() let target = new Moment()
@@ -28,6 +35,7 @@ export default function setupNotifications(navigate) {
} }
Notification.localNotificationSchedule({ Notification.localNotificationSchedule({
id: '1',
message: labels.tempReminder.notification, message: labels.tempReminder.notification,
date: target.toDate(), date: target.toDate(),
vibrate: false, vibrate: false,
@@ -35,4 +43,24 @@ export default function setupNotifications(navigate) {
}) })
} }
}) })
periodReminderObservable(reminder => {
if (reminder.enabled) {
const bleedingPrediction = cycleModule().getPredictedMenses()
if (bleedingPrediction.length > 0) {
const bleedingStart = LocalDate.parse(bleedingPrediction[0][0])
const reminderDate = bleedingStart.minusDays(3)
// 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,
vibrate: false,
onNotification: navigate('Home')
})
}
}
})
} }