Refactor cycle day overview screen
This commit is contained in:
+24
-13
@@ -1,25 +1,34 @@
|
||||
import {tempReminderObservable, periodReminderObservable} from '../local-storage'
|
||||
import {
|
||||
tempReminderObservable,
|
||||
periodReminderObservable,
|
||||
} from '../local-storage'
|
||||
import Notification from 'react-native-push-notification'
|
||||
import Moment from 'moment'
|
||||
import { LocalDate } from 'js-joda'
|
||||
|
||||
import labels from '../i18n/en/settings'
|
||||
import { getBleedingDaysSortedByDate } from '../db'
|
||||
import cycleModule from './cycle'
|
||||
import nothingChanged from '../db/db-unchanged'
|
||||
import store from '../store'
|
||||
import { setDate } from '../slices/date'
|
||||
|
||||
export default function setupNotifications(navigate) {
|
||||
Notification.configure({
|
||||
onNotification: (notification) => {
|
||||
// https://github.com/zo0r/react-native-push-notification/issues/966#issuecomment-479069106
|
||||
if (notification.data?.id === '1' || notification.id === '1') {
|
||||
const todayDate = LocalDate.now().toString()
|
||||
store.dispatch(setDate(todayDate))
|
||||
navigate('TemperatureEditView')
|
||||
} else {
|
||||
navigate('Home')
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
tempReminderObservable(reminder => {
|
||||
Notification.cancelLocalNotifications({id: '1'})
|
||||
tempReminderObservable((reminder) => {
|
||||
Notification.cancelLocalNotifications({ id: '1' })
|
||||
if (reminder.enabled) {
|
||||
const [hours, minutes] = reminder.time.split(':')
|
||||
let target = new Moment()
|
||||
@@ -27,7 +36,7 @@ export default function setupNotifications(navigate) {
|
||||
.minutes(parseInt(minutes))
|
||||
.seconds(0)
|
||||
|
||||
if(target.isBefore(new Moment())) {
|
||||
if (target.isBefore(new Moment())) {
|
||||
target = target.add(1, 'd')
|
||||
}
|
||||
|
||||
@@ -37,29 +46,31 @@ export default function setupNotifications(navigate) {
|
||||
message: labels.tempReminder.notification,
|
||||
date: target.toDate(),
|
||||
vibrate: false,
|
||||
repeatType: 'day'
|
||||
repeatType: 'day',
|
||||
})
|
||||
}
|
||||
}, false)
|
||||
|
||||
periodReminderObservable(reminder => {
|
||||
Notification.cancelLocalNotifications({id: '2'})
|
||||
periodReminderObservable((reminder) => {
|
||||
Notification.cancelLocalNotifications({ id: '2' })
|
||||
if (reminder.enabled) setupPeriodReminder()
|
||||
}, false)
|
||||
|
||||
getBleedingDaysSortedByDate().addListener((_, changes) => {
|
||||
// the listener fires on setup, so we check if there were actually any changes
|
||||
if (nothingChanged(changes)) return
|
||||
Notification.cancelLocalNotifications({id: '2'})
|
||||
Notification.cancelLocalNotifications({ id: '2' })
|
||||
if (periodReminderObservable.value.enabled) setupPeriodReminder()
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
function setupPeriodReminder() {
|
||||
const bleedingPrediction = cycleModule().getPredictedMenses()
|
||||
if (bleedingPrediction.length > 0) {
|
||||
const predictedBleedingStart = Moment(bleedingPrediction[0][0], "YYYY-MM-DD")
|
||||
const predictedBleedingStart = Moment(
|
||||
bleedingPrediction[0][0],
|
||||
'YYYY-MM-DD'
|
||||
)
|
||||
// 3 days before and at 6 am
|
||||
const reminderDate = predictedBleedingStart
|
||||
.subtract(3, 'days')
|
||||
@@ -68,7 +79,7 @@ function setupPeriodReminder() {
|
||||
.seconds(0)
|
||||
|
||||
if (reminderDate.isAfter()) {
|
||||
// period is likely to start in 3 to 3 + (length of prediction - 1) days
|
||||
// period is likely to start in 3 to 3 + (length of prediction - 1) days
|
||||
const daysToEndOfPrediction = bleedingPrediction[0].length + 2
|
||||
|
||||
Notification.localNotificationSchedule({
|
||||
@@ -76,7 +87,7 @@ function setupPeriodReminder() {
|
||||
userInfo: { id: '2' },
|
||||
message: labels.periodReminder.notification(daysToEndOfPrediction),
|
||||
date: reminderDate.toDate(),
|
||||
vibrate: false
|
||||
vibrate: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user