Skip to content
Snippets Groups Projects
Commit 432c56c0 authored by tina's avatar tina
Browse files

information about bleeding data is shown on home screen

parent e0474400
No related branches found
No related tags found
No related merge requests found
...@@ -5,10 +5,11 @@ import { ...@@ -5,10 +5,11 @@ import {
Text, Text,
ScrollView ScrollView
} from 'react-native' } from 'react-native'
import { LocalDate } from 'js-joda' import { LocalDate, ChronoUnit } from 'js-joda'
import styles from '../styles/index' import styles from '../styles/index'
import cycleModule from '../lib/cycle' import cycleModule from '../lib/cycle'
import { getOrCreateCycleDay, bleedingDaysSortedByDate, fillWithDummyData, deleteAll } from '../db' import { getOrCreateCycleDay, bleedingDaysSortedByDate, fillWithDummyData, deleteAll } from '../db'
import {bleedingPrediction as labels} from './labels'
const getCycleDayNumber = cycleModule().getCycleDayNumber const getCycleDayNumber = cycleModule().getCycleDayNumber
...@@ -19,23 +20,25 @@ export default class Home extends Component { ...@@ -19,23 +20,25 @@ export default class Home extends Component {
const cycleDayNumber = getCycleDayNumber(this.todayDateString) const cycleDayNumber = getCycleDayNumber(this.todayDateString)
this.state = { this.state = {
welcomeText: determineWelcomeText(cycleDayNumber) welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText()
} }
this.setStateWithCurrentWelcomeText = (function (HomeComponent) { this.setStateWithCurrentText = (function (HomeComponent) {
return function () { return function () {
const cycleDayNumber = getCycleDayNumber(HomeComponent.todayDateString) const cycleDayNumber = getCycleDayNumber(HomeComponent.todayDateString)
HomeComponent.setState({ HomeComponent.setState({
welcomeText: determineWelcomeText(cycleDayNumber) welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText()
}) })
} }
})(this) })(this)
bleedingDaysSortedByDate.addListener(this.setStateWithCurrentWelcomeText) bleedingDaysSortedByDate.addListener(this.setStateWithCurrentText)
} }
componentWillUnmount() { componentWillUnmount() {
bleedingDaysSortedByDate.removeListener(this.setStateWithCurrentWelcomeText) bleedingDaysSortedByDate.removeListener(this.setStateWithCurrentText)
} }
passTodayToDayView() { passTodayToDayView() {
...@@ -49,6 +52,7 @@ export default class Home extends Component { ...@@ -49,6 +52,7 @@ export default class Home extends Component {
return ( return (
<ScrollView> <ScrollView>
<Text style={styles.welcome}>{this.state.welcomeText}</Text> <Text style={styles.welcome}>{this.state.welcomeText}</Text>
<Text style={styles.welcome}>{this.state.predictionText}</Text>
<View style={styles.homeButtons}> <View style={styles.homeButtons}>
<View style={styles.homeButton}> <View style={styles.homeButton}>
<Button <Button
...@@ -80,3 +84,27 @@ function determineWelcomeText(cycleDayNumber) { ...@@ -80,3 +84,27 @@ function determineWelcomeText(cycleDayNumber) {
return cycleDayNumber ? welcomeTextWithCycleDay : welcomeText return cycleDayNumber ? welcomeTextWithCycleDay : welcomeText
} }
function determinePredictionText() {
const bleedingPrediction = cycleModule().getPredictedMenses()
if (!bleedingPrediction.length) return labels.noPrediction
const todayDate = LocalDate.now()
const bleedingStart = LocalDate.parse(bleedingPrediction[0][0])
const bleedingEnd = LocalDate.parse(bleedingPrediction[0][ bleedingPrediction[0].length - 1 ])
if (todayDate.isBefore(bleedingStart)) {
return labels.predictionInFuture(
todayDate.until(bleedingStart, ChronoUnit.DAYS),
todayDate.until(bleedingEnd, ChronoUnit.DAYS)
)
}
if (todayDate.isAfter(bleedingEnd)) {
return labels.predictionInPast(bleedingStart.toString(), bleedingEnd.toString())
}
const daysToEnd = todayDate.until(bleedingEnd, ChronoUnit.DAYS)
if (daysToEnd === 0) {
return labels.predictionStartedNoDaysLeft
} else if (daysToEnd === 1) {
return labels.predictionStarted1DayLeft
} else {
return labels.predictionStartedXDaysLeft(daysToEnd)
}
}
\ No newline at end of file
...@@ -56,4 +56,13 @@ export const stats = { ...@@ -56,4 +56,13 @@ export const stats = {
minLabel: 'Shortest cycle', minLabel: 'Shortest cycle',
maxLabel: 'Longest cycle', maxLabel: 'Longest cycle',
stdLabel: 'Standard deviation' stdLabel: 'Standard deviation'
}
export const bleedingPrediction = {
noPrediction: 'There is not enough period data to predict the next one.',
predictionInFuture: (startDays, endDays) => `Your next period is likely to start in ${startDays} to ${endDays} days.`,
predictionStartedXDaysLeft: (numberOfDays) => `Your period is likely to start today or during the next ${numberOfDays} days.`,
predictionStarted1DayLeft: 'Your period is likely to start today or tomorrow.',
predictionStartedNoDaysLeft: 'Your period is likely to start today.',
predictionInPast: (startDate, endDate) => `Based on your documented data, your period was likely to start between ${startDate} and ${endDate}.`
} }
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment