Merge branch '139-next-period-reminder' into 'master'
Resolve "next period reminder" Closes #139 See merge request bloodyhealth/drip!87
This commit is contained in:
@@ -57,6 +57,11 @@ export const settings = {
|
|||||||
timeSet: time => `Daily reminder set for ${time}`,
|
timeSet: time => `Daily reminder set for ${time}`,
|
||||||
notification: 'Record your morning temperature'
|
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: {
|
passwordSettings: {
|
||||||
title: 'App password',
|
title: 'App password',
|
||||||
explainerDisabled: "Encrypt the app's database with a password. You need to enter the password every time the app is started.",
|
explainerDisabled: "Encrypt the app's database with a password. You need to enter the password every time the app is started.",
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import styles from '../../styles/index'
|
|||||||
import { settings as labels } from '../labels'
|
import { settings as labels } from '../labels'
|
||||||
import { AppText } from '../app-text'
|
import { AppText } from '../app-text'
|
||||||
import TempReminderPicker from './temp-reminder-picker'
|
import TempReminderPicker from './temp-reminder-picker'
|
||||||
|
import PeriodReminderPicker from './period-reminder'
|
||||||
import TempSlider from './temp-slider'
|
import TempSlider from './temp-slider'
|
||||||
import openImportDialogAndImport from './import-dialog'
|
import openImportDialogAndImport from './import-dialog'
|
||||||
import openShareDialogAndExport from './export-dialog'
|
import openShareDialogAndExport from './export-dialog'
|
||||||
@@ -30,6 +31,7 @@ export default class Settings extends Component {
|
|||||||
<AppText>{labels.tempScale.segmentExplainer}</AppText>
|
<AppText>{labels.tempScale.segmentExplainer}</AppText>
|
||||||
<TempSlider/>
|
<TempSlider/>
|
||||||
</View>
|
</View>
|
||||||
|
<PeriodReminderPicker/>
|
||||||
<PasswordSetting />
|
<PasswordSetting />
|
||||||
<View style={styles.settingsSegment}>
|
<View style={styles.settingsSegment}>
|
||||||
<AppText style={styles.settingsSegmentTitle}>
|
<AppText style={styles.settingsSegmentTitle}>
|
||||||
|
|||||||
@@ -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 (
|
||||||
|
<View style={styles.settingsSegment}>
|
||||||
|
<AppText style={styles.settingsSegmentTitle}>
|
||||||
|
{labels.periodReminder.title}
|
||||||
|
</AppText>
|
||||||
|
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
|
||||||
|
<View style={{ flex: 1 }}>
|
||||||
|
<AppText>{labels.periodReminder.reminderText}</AppText>
|
||||||
|
</View>
|
||||||
|
<Switch
|
||||||
|
value={this.state.enabled}
|
||||||
|
onValueChange={switchOn => {
|
||||||
|
this.setState({ enabled: switchOn })
|
||||||
|
savePeriodReminder({enabled: switchOn})
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
+46
-7
@@ -1,21 +1,26 @@
|
|||||||
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, getBleedingDaysSortedByDate } from '../db'
|
||||||
|
import cycleModule from './cycle'
|
||||||
|
|
||||||
export default function setupNotifications(navigate) {
|
export default function setupNotifications(navigate) {
|
||||||
Notification.configure({
|
Notification.configure({
|
||||||
onNotification: () => {
|
onNotification: (notification) => {
|
||||||
const todayDateString = LocalDate.now().toString()
|
if (notification.id === '1') {
|
||||||
const cycleDay = getOrCreateCycleDay(todayDateString)
|
const todayDateString = LocalDate.now().toString()
|
||||||
navigate('TemperatureEditView', { cycleDay })
|
const cycleDay = getOrCreateCycleDay(todayDateString)
|
||||||
|
navigate('TemperatureEditView', { cycleDay })
|
||||||
|
} else {
|
||||||
|
navigate('Home')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
tempReminderObservable(reminder => {
|
tempReminderObservable(reminder => {
|
||||||
Notification.cancelAllLocalNotifications()
|
Notification.cancelLocalNotifications({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 +33,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 +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
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,16 @@ export async function saveTempReminder(reminder) {
|
|||||||
tempReminderObservable.set(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()
|
export const hasEncryptionObservable = Observable()
|
||||||
setObvWithInitValue('hasEncryption', hasEncryptionObservable, false)
|
setObvWithInitValue('hasEncryption', hasEncryptionObservable, false)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user