Skip to content
Snippets Groups Projects
Commit 38b454fb authored by emelko's avatar emelko
Browse files

Merge branch 'master' into 117-implement-pain

parents ab414937 54a3db40
No related branches found
No related tags found
No related merge requests found
import React, { Component } from 'react' import React, { Component } from 'react'
import { CalendarList } from 'react-native-calendars' import { CalendarList } from 'react-native-calendars'
import { getOrCreateCycleDay, bleedingDaysSortedByDate } from '../db' import { getOrCreateCycleDay, bleedingDaysSortedByDate } from '../db'
import cycleModule from '../lib/cycle'
export default class CalendarView extends Component { export default class CalendarView extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
const predictedMenses = cycleModule().getPredictedMenses()
this.state = { this.state = {
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate) bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses)
} }
this.setStateWithCalFormattedDays = (function (CalendarComponent) { this.setStateWithCalFormattedDays = (function (CalendarComponent) {
return function(_, changes) { return function() {
if (Object.values(changes).every(x => x && !x.length)) return const predictedMenses = cycleModule().getPredictedMenses()
CalendarComponent.setState({ CalendarComponent.setState({
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate) bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses)
}) })
} }
})(this) })(this)
...@@ -34,8 +38,14 @@ export default class CalendarView extends Component { ...@@ -34,8 +38,14 @@ export default class CalendarView extends Component {
render() { render() {
return ( return (
<CalendarList <CalendarList
onDayPress={this.passDateToDayView} onDayPress={this.passDateToDayView.bind(this)}
markedDates={this.state.bleedingDaysInCalFormat} markedDates={
Object.assign(
{},
this.state.bleedingDaysInCalFormat,
this.state.predictedBleedingDaysInCalFormat
)
}
markingType={'period'} markingType={'period'}
/> />
) )
...@@ -52,4 +62,21 @@ function toCalFormat(bleedingDaysSortedByDate) { ...@@ -52,4 +62,21 @@ function toCalFormat(bleedingDaysSortedByDate) {
} }
return acc return acc
}, {}) }, {})
}
function predictionToCalFormat(predictedDays) {
if (!predictedDays.length) return {}
const shadesOfGrey = ['#e5e5e5', '#cccccc'] // [lighter, darker]
const middleIndex = (predictedDays[0].length - 1) / 2
return predictedDays.reduce((acc, setOfDays) => {
setOfDays.reduce((accSet, day, i) => {
accSet[day] = {
startingDay: true,
endingDay: true,
color: (i === middleIndex) ? shadesOfGrey[1] : shadesOfGrey[0]
}
return accSet
}, acc)
return acc
}, {})
} }
\ No newline at end of file
...@@ -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
...@@ -76,4 +76,13 @@ export const stats = { ...@@ -76,4 +76,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
...@@ -4,10 +4,10 @@ import { ...@@ -4,10 +4,10 @@ import {
View, View,
ScrollView ScrollView
} from 'react-native' } from 'react-native'
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 getCycleInfo from '../lib/cycle-length' import {getCycleLengthStats as getCycleInfo} from '../lib/cycle-length'
import {stats as labels} from './labels' import {stats as labels} from './labels'
export default class Stats extends Component { export default class Stats extends Component {
...@@ -18,7 +18,7 @@ export default class Stats extends Component { ...@@ -18,7 +18,7 @@ export default class Stats extends Component {
let numberOfCycles let numberOfCycles
let cycleInfo let cycleInfo
if (atLeastOneCycle) { if (atLeastOneCycle) {
cycleLengths = getCycleLength(allMensesStarts) cycleLengths = cycleModule().getCycleLength(allMensesStarts)
numberOfCycles = cycleLengths.length numberOfCycles = cycleLengths.length
if (numberOfCycles > 1) { if (numberOfCycles > 1) {
cycleInfo = getCycleInfo(cycleLengths) cycleInfo = getCycleInfo(cycleLengths)
...@@ -31,10 +31,14 @@ export default class Stats extends Component { ...@@ -31,10 +31,14 @@ export default class Stats extends Component {
<Text style={styles.statsIntro}>{labels.emptyStats}</Text> <Text style={styles.statsIntro}>{labels.emptyStats}</Text>
} }
{atLeastOneCycle && numberOfCycles === 1 && {atLeastOneCycle && numberOfCycles === 1 &&
<Text style={styles.statsIntro}>{labels.oneCycleStats(cycleLengths[0])}</Text> <Text style={styles.statsIntro}>
{labels.oneCycleStats(cycleLengths[0])}
</Text>
} }
{atLeastOneCycle && numberOfCycles > 1 && <View> {atLeastOneCycle && numberOfCycles > 1 && <View>
<Text style={styles.statsIntro}>{labels.getBasisOfStats(numberOfCycles)}</Text> <Text style={styles.statsIntro}>
{labels.getBasisOfStats(numberOfCycles)}
</Text>
<View style={styles.statsRow}> <View style={styles.statsRow}>
<Text style={styles.statsLabelLeft}>{labels.averageLabel}</Text> <Text style={styles.statsLabelLeft}>{labels.averageLabel}</Text>
<Text style={styles.statsLabelRight}>{cycleInfo.mean + ' ' + labels.daysLabel}</Text> <Text style={styles.statsLabelRight}>{cycleInfo.mean + ' ' + labels.daysLabel}</Text>
...@@ -56,14 +60,4 @@ export default class Stats extends Component { ...@@ -56,14 +60,4 @@ export default class Stats extends Component {
</ScrollView> </ScrollView>
) )
} }
}
function getCycleLength(cycleStartDates) {
const cycleLengths = []
for (let i = 0; i < cycleStartDates.length - 1; i++) {
const nextCycleStart = LocalDate.parse(cycleStartDates[i])
const cycleStart = LocalDate.parse(cycleStartDates[i + 1])
cycleLengths.push(cycleStart.until(nextCycleStart, ChronoUnit.DAYS))
}
return cycleLengths
} }
\ No newline at end of file
import assert from 'assert' import assert from 'assert'
export default function getCycleLengthStats(cycleLengths) { export function getCycleLengthStats(cycleLengths) {
throwIfArgsAreNotInRequiredFormat(cycleLengths) throwIfArgsAreNotInRequiredFormat(cycleLengths)
const cycleLengthStats = {} const cycleLengthStats = {}
const sortedCycleLengths = cycleLengths.sort((a, b) => { const sortedCycleLengths = cycleLengths.sort((a, b) => {
......
import * as joda from 'js-joda' import * as joda from 'js-joda'
import {getCycleLengthStats} from './cycle-length'
const LocalDate = joda.LocalDate const LocalDate = joda.LocalDate
const DAYS = joda.ChronoUnit.DAYS const DAYS = joda.ChronoUnit.DAYS
...@@ -6,6 +7,8 @@ export default function config(opts) { ...@@ -6,6 +7,8 @@ export default function config(opts) {
let bleedingDaysSortedByDate let bleedingDaysSortedByDate
let cycleDaysSortedByDate let cycleDaysSortedByDate
let maxBreakInBleeding let maxBreakInBleeding
let maxCycleLength
let minCyclesForPrediction
if (!opts) { if (!opts) {
// we only want to require (and run) the db module // we only want to require (and run) the db module
...@@ -13,10 +16,14 @@ export default function config(opts) { ...@@ -13,10 +16,14 @@ export default function config(opts) {
bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate
cycleDaysSortedByDate = require('../db').cycleDaysSortedByDate cycleDaysSortedByDate = require('../db').cycleDaysSortedByDate
maxBreakInBleeding = 1 maxBreakInBleeding = 1
maxCycleLength = 99
minCyclesForPrediction = 3
} else { } else {
bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate || [] bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate || []
cycleDaysSortedByDate = opts.cycleDaysSortedByDate || [] cycleDaysSortedByDate = opts.cycleDaysSortedByDate || []
maxBreakInBleeding = opts.maxBreakInBleeding || 1 maxBreakInBleeding = opts.maxBreakInBleeding || 1
maxCycleLength = opts.maxCycleLength || 99
minCyclesForPrediction = opts.minCyclesForPrediction || 3
} }
function getLastMensesStart(targetDateString) { function getLastMensesStart(targetDateString) {
...@@ -143,11 +150,61 @@ export default function config(opts) { ...@@ -143,11 +150,61 @@ export default function config(opts) {
} }
} }
function getCycleLength(cycleStartDates) {
const cycleLengths = []
for (let i = 0; i < cycleStartDates.length - 1; i++) {
const nextCycleStart = LocalDate.parse(cycleStartDates[i])
const cycleStart = LocalDate.parse(cycleStartDates[i + 1])
const cycleLength = cycleStart.until(nextCycleStart, DAYS)
if (cycleLength <= maxCycleLength) { cycleLengths.push(cycleLength) }
}
return cycleLengths
}
function getPredictedMenses() {
const allMensesStarts = getAllMensesStarts()
const atLeastOneCycle = allMensesStarts.length > 1
if (!atLeastOneCycle ||
allMensesStarts.length < minCyclesForPrediction
) {
return []
}
const cycleLengths = getCycleLength(allMensesStarts)
const cycleInfo = getCycleLengthStats(cycleLengths)
const periodDistance = Math.round(cycleInfo.mean)
let periodStartVariation
if (cycleInfo.stdDeviation === null) {
periodStartVariation = 2
} else if (cycleInfo.stdDeviation < 1.5) { // threshold is choosen a little arbitrarily
periodStartVariation = 1
} else {
periodStartVariation = 2
}
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
return []
}
let lastStart = LocalDate.parse(allMensesStarts[0])
const predictedMenses = []
for (let i = 0; i < 3; i++) {
lastStart = lastStart.plusDays(periodDistance)
const nextPredictedDates = [lastStart.toString()]
for (let j = 0; j < periodStartVariation; j++) {
nextPredictedDates.push(lastStart.minusDays(j+1).toString())
nextPredictedDates.push(lastStart.plusDays(j+1).toString())
}
nextPredictedDates.sort()
predictedMenses.push(nextPredictedDates)
}
return predictedMenses
}
return { return {
getCycleDayNumber, getCycleDayNumber,
getCycleForDay, getCycleForDay,
getPreviousCycle, getPreviousCycle,
getCyclesBefore, getCyclesBefore,
getAllMensesStarts getAllMensesStarts,
getCycleLength,
getPredictedMenses
} }
} }
\ No newline at end of file
Source diff could not be displayed: it is too large. Options to address this: view the blob.
import chai from 'chai' import chai from 'chai'
import { AssertionError } from 'assert' import { AssertionError } from 'assert'
import cycleInfo from '../lib/cycle-length' import {getCycleLengthStats as cycleInfo} from '../lib/cycle-length'
const expect = chai.expect const expect = chai.expect
......
...@@ -345,6 +345,246 @@ describe('getCycleForDay', () => { ...@@ -345,6 +345,246 @@ describe('getCycleForDay', () => {
}) })
}) })
describe('getPredictedMenses', () => {
describe('cannot predict next menses', () => {
it('if no bleeding is documented', () => {
const cycleDaysSortedByDate = [ {} ]
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding),
maxCycleLength: 99,
minCyclesForPrediction: 1
})
const result = getPredictedMenses()
expect(result).to.eql([])
})
it('if one bleeding is documented (no completed cycle)', () => {
const cycleDaysSortedByDate = [
{
date: '2018-06-02',
bleeding: { value: 2 }
}
]
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding),
maxCycleLength: 99,
minCyclesForPrediction: 1
})
const result = getPredictedMenses()
expect(result).to.eql([])
})
it('if number of cycles is below minCyclesForPrediction', () => {
const cycleDaysSortedByDate = [
{
date: '2018-06-02',
bleeding: { value: 2 }
},
{
date: '2018-06-01',
bleeding: { value: 2 }
},
{
date: '2018-05-01',
bleeding: { value: 2 }
},
]
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const result = getPredictedMenses()
expect(result).to.eql([])
})
})
describe('works', () => {
it('for one completed cycle with minCyclesForPrediction = 1', () => {
const cycleDaysSortedByDate = [
{
date: '2018-07-15',
bleeding: { value: 2 }
},
{
date: '2018-07-01',
bleeding: { value: 2 }
}
]
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding),
minCyclesForPrediction: 1
})
const result = getPredictedMenses()
const expectedResult = [
[
'2018-07-27',
'2018-07-28',
'2018-07-29',
'2018-07-30',
'2018-07-31'
],
[
'2018-08-10',
'2018-08-11',
'2018-08-12',
'2018-08-13',
'2018-08-14',
],
[
'2018-08-24',
'2018-08-25',
'2018-08-26',
'2018-08-27',
'2018-08-28',
]
]
expect(result).to.eql(expectedResult)
})
it('if number of cycles is above minCyclesForPrediction', () => {
const cycleDaysSortedByDate = [
{
date: '2018-08-02',
bleeding: { value: 2 }
},
{
date: '2018-07-02',
bleeding: { value: 2 }
},
{
date: '2018-06-01',
bleeding: { value: 2 }
},
{
date: '2018-05-01',
bleeding: { value: 2 }
},
]
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const result = getPredictedMenses()
const expectedResult = [
[
'2018-09-01',
'2018-09-02',
'2018-09-03'
],
[
'2018-10-02',
'2018-10-03',
'2018-10-04'
],
[
'2018-11-02',
'2018-11-03',
'2018-11-04'
]
]
expect(result).to.eql(expectedResult)
})
it('3 cycles with little standard deviation', () => {
const cycleDaysSortedByDate = [
{
date: '2018-08-01',
bleeding: { value: 2 }
},
{
date: '2018-07-18',
bleeding: { value: 2 }
},
{
date: '2018-07-05',
bleeding: { value: 2 }
},
{
date: '2018-06-20',
bleeding: { value: 2 }
},
]
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const result = getPredictedMenses()
const expectedResult = [
[
'2018-08-14',
'2018-08-15',
'2018-08-16'
],
[
'2018-08-28',
'2018-08-29',
'2018-08-30'
],
[
'2018-09-11',
'2018-09-12',
'2018-09-13'
]
]
expect(result).to.eql(expectedResult)
})
it('3 cycles with bigger standard deviation', () => {
const cycleDaysSortedByDate = [
{
date: '2018-08-01',
bleeding: { value: 2 }
},
{
date: '2018-07-14',
bleeding: { value: 2 }
},
{
date: '2018-07-04',
bleeding: { value: 2 }
},
{
date: '2018-06-20',
bleeding: { value: 2 }
},
]
const { getPredictedMenses } = cycleModule({
cycleDaysSortedByDate,
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const result = getPredictedMenses()
const expectedResult = [
[
'2018-08-13',
'2018-08-14',
'2018-08-15',
'2018-08-16',
'2018-08-17',
],
[
'2018-08-27',
'2018-08-28',
'2018-08-29',
'2018-08-30',
'2018-08-31',
],
[
'2018-09-10',
'2018-09-11',
'2018-09-12',
'2018-09-13',
'2018-09-14',
]
]
expect(result).to.eql(expectedResult)
})
})
})
describe('getAllMensesStart', () => { describe('getAllMensesStart', () => {
it('works for one cycle start', () => { it('works for one cycle start', () => {
const cycleDaysSortedByDate = [ const cycleDaysSortedByDate = [
......
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