Merge branch 'master' into 117-implement-pain

This commit is contained in:
emelko
2018-08-31 11:58:57 +02:00
9 changed files with 1852 additions and 1497 deletions
+33 -6
View File
@@ -1,19 +1,23 @@
import React, { Component } from 'react'
import { CalendarList } from 'react-native-calendars'
import { getOrCreateCycleDay, bleedingDaysSortedByDate } from '../db'
import cycleModule from '../lib/cycle'
export default class CalendarView extends Component {
constructor(props) {
super(props)
const predictedMenses = cycleModule().getPredictedMenses()
this.state = {
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate)
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses)
}
this.setStateWithCalFormattedDays = (function (CalendarComponent) {
return function(_, changes) {
if (Object.values(changes).every(x => x && !x.length)) return
return function() {
const predictedMenses = cycleModule().getPredictedMenses()
CalendarComponent.setState({
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate)
bleedingDaysInCalFormat: toCalFormat(bleedingDaysSortedByDate),
predictedBleedingDaysInCalFormat: predictionToCalFormat(predictedMenses)
})
}
})(this)
@@ -34,8 +38,14 @@ export default class CalendarView extends Component {
render() {
return (
<CalendarList
onDayPress={this.passDateToDayView}
markedDates={this.state.bleedingDaysInCalFormat}
onDayPress={this.passDateToDayView.bind(this)}
markedDates={
Object.assign(
{},
this.state.bleedingDaysInCalFormat,
this.state.predictedBleedingDaysInCalFormat
)
}
markingType={'period'}
/>
)
@@ -52,4 +62,21 @@ function toCalFormat(bleedingDaysSortedByDate) {
}
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
}, {})
}
+34 -6
View File
@@ -5,10 +5,11 @@ import {
Text,
ScrollView
} from 'react-native'
import { LocalDate } from 'js-joda'
import { LocalDate, ChronoUnit } from 'js-joda'
import styles from '../styles/index'
import cycleModule from '../lib/cycle'
import { getOrCreateCycleDay, bleedingDaysSortedByDate, fillWithDummyData, deleteAll } from '../db'
import {bleedingPrediction as labels} from './labels'
const getCycleDayNumber = cycleModule().getCycleDayNumber
@@ -19,23 +20,25 @@ export default class Home extends Component {
const cycleDayNumber = getCycleDayNumber(this.todayDateString)
this.state = {
welcomeText: determineWelcomeText(cycleDayNumber)
welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText()
}
this.setStateWithCurrentWelcomeText = (function (HomeComponent) {
this.setStateWithCurrentText = (function (HomeComponent) {
return function () {
const cycleDayNumber = getCycleDayNumber(HomeComponent.todayDateString)
HomeComponent.setState({
welcomeText: determineWelcomeText(cycleDayNumber)
welcomeText: determineWelcomeText(cycleDayNumber),
predictionText: determinePredictionText()
})
}
})(this)
bleedingDaysSortedByDate.addListener(this.setStateWithCurrentWelcomeText)
bleedingDaysSortedByDate.addListener(this.setStateWithCurrentText)
}
componentWillUnmount() {
bleedingDaysSortedByDate.removeListener(this.setStateWithCurrentWelcomeText)
bleedingDaysSortedByDate.removeListener(this.setStateWithCurrentText)
}
passTodayToDayView() {
@@ -49,6 +52,7 @@ export default class Home extends Component {
return (
<ScrollView>
<Text style={styles.welcome}>{this.state.welcomeText}</Text>
<Text style={styles.welcome}>{this.state.predictionText}</Text>
<View style={styles.homeButtons}>
<View style={styles.homeButton}>
<Button
@@ -80,3 +84,27 @@ function determineWelcomeText(cycleDayNumber) {
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)
}
}
+9
View File
@@ -76,4 +76,13 @@ export const stats = {
minLabel: 'Shortest cycle',
maxLabel: 'Longest cycle',
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}.`
}
+9 -15
View File
@@ -4,10 +4,10 @@ import {
View,
ScrollView
} from 'react-native'
import { LocalDate, ChronoUnit } from 'js-joda'
import styles from '../styles/index'
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'
export default class Stats extends Component {
@@ -18,7 +18,7 @@ export default class Stats extends Component {
let numberOfCycles
let cycleInfo
if (atLeastOneCycle) {
cycleLengths = getCycleLength(allMensesStarts)
cycleLengths = cycleModule().getCycleLength(allMensesStarts)
numberOfCycles = cycleLengths.length
if (numberOfCycles > 1) {
cycleInfo = getCycleInfo(cycleLengths)
@@ -31,10 +31,14 @@ export default class Stats extends Component {
<Text style={styles.statsIntro}>{labels.emptyStats}</Text>
}
{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>
<Text style={styles.statsIntro}>{labels.getBasisOfStats(numberOfCycles)}</Text>
<Text style={styles.statsIntro}>
{labels.getBasisOfStats(numberOfCycles)}
</Text>
<View style={styles.statsRow}>
<Text style={styles.statsLabelLeft}>{labels.averageLabel}</Text>
<Text style={styles.statsLabelRight}>{cycleInfo.mean + ' ' + labels.daysLabel}</Text>
@@ -56,14 +60,4 @@ export default class Stats extends Component {
</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
}
+1 -1
View File
@@ -1,6 +1,6 @@
import assert from 'assert'
export default function getCycleLengthStats(cycleLengths) {
export function getCycleLengthStats(cycleLengths) {
throwIfArgsAreNotInRequiredFormat(cycleLengths)
const cycleLengthStats = {}
const sortedCycleLengths = cycleLengths.sort((a, b) => {
+58 -1
View File
@@ -1,4 +1,5 @@
import * as joda from 'js-joda'
import {getCycleLengthStats} from './cycle-length'
const LocalDate = joda.LocalDate
const DAYS = joda.ChronoUnit.DAYS
@@ -6,6 +7,8 @@ export default function config(opts) {
let bleedingDaysSortedByDate
let cycleDaysSortedByDate
let maxBreakInBleeding
let maxCycleLength
let minCyclesForPrediction
if (!opts) {
// we only want to require (and run) the db module
@@ -13,10 +16,14 @@ export default function config(opts) {
bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate
cycleDaysSortedByDate = require('../db').cycleDaysSortedByDate
maxBreakInBleeding = 1
maxCycleLength = 99
minCyclesForPrediction = 3
} else {
bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate || []
cycleDaysSortedByDate = opts.cycleDaysSortedByDate || []
maxBreakInBleeding = opts.maxBreakInBleeding || 1
maxCycleLength = opts.maxCycleLength || 99
minCyclesForPrediction = opts.minCyclesForPrediction || 3
}
function getLastMensesStart(targetDateString) {
@@ -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 {
getCycleDayNumber,
getCycleForDay,
getPreviousCycle,
getCyclesBefore,
getAllMensesStarts
getAllMensesStarts,
getCycleLength,
getPredictedMenses
}
}
+1467 -1467
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,7 +1,7 @@
import chai from 'chai'
import { AssertionError } from 'assert'
import cycleInfo from '../lib/cycle-length'
import {getCycleLengthStats as cycleInfo} from '../lib/cycle-length'
const expect = chai.expect
+240
View File
@@ -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', () => {
it('works for one cycle start', () => {
const cycleDaysSortedByDate = [