changs output of getpredictedmenses, adds days to calendar

This commit is contained in:
tina
2018-08-23 17:28:53 +02:00
parent 497a3a3ff5
commit e047440068
3 changed files with 119 additions and 64 deletions
+32 -5
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)
@@ -35,7 +39,13 @@ export default class CalendarView extends Component {
return (
<CalendarList
onDayPress={this.passDateToDayView.bind(this)}
markedDates={this.state.bleedingDaysInCalFormat}
markedDates={
Object.assign(
{},
this.state.bleedingDaysInCalFormat,
this.state.predictedBleedingDaysInCalFormat
)
}
markingType={'period'}
/>
)
@@ -53,3 +63,20 @@ 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
}, {})
}
+12 -8
View File
@@ -163,12 +163,11 @@ export default function config(opts) {
function getPredictedMenses() {
const allMensesStarts = getAllMensesStarts()
const atLeastOneCycle = allMensesStarts.length > 1
if (!atLeastOneCycle ||
allMensesStarts.length < minCyclesForPrediction
) {
return {}
return []
}
const cycleLengths = getCycleLength(allMensesStarts)
const cycleInfo = getCycleLengthStats(cycleLengths)
@@ -181,15 +180,20 @@ export default function config(opts) {
} else {
periodStartVariation = 2
}
var lastStart = allMensesStarts[0]
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
return []
}
let lastStart = LocalDate.parse(allMensesStarts[0])
const predictedMenses = []
for (let i = 0; i < 3; i++) {
lastStart = LocalDate.parse(lastStart).plusDays(periodDistance).toString()
const nextPredictedRange = {
'startDate': LocalDate.parse(lastStart).minusDays(periodStartVariation).toString(),
'endDate': LocalDate.parse(lastStart).plusDays(periodStartVariation).toString()
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())
}
predictedMenses.push(nextPredictedRange)
nextPredictedDates.sort()
predictedMenses.push(nextPredictedDates)
}
return predictedMenses
}
+75 -51
View File
@@ -357,7 +357,7 @@ describe('getPredictedMenses', () => {
minCyclesForPrediction: 1
})
const result = getPredictedMenses()
expect(result).to.eql({})
expect(result).to.eql([])
})
it('if one bleeding is documented (no completed cycle)', () => {
const cycleDaysSortedByDate = [
@@ -374,7 +374,7 @@ describe('getPredictedMenses', () => {
minCyclesForPrediction: 1
})
const result = getPredictedMenses()
expect(result).to.eql({})
expect(result).to.eql([])
})
it('if number of cycles is below minCyclesForPrediction', () => {
const cycleDaysSortedByDate = [
@@ -397,7 +397,7 @@ describe('getPredictedMenses', () => {
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
})
const result = getPredictedMenses()
expect(result).to.eql({})
expect(result).to.eql([])
})
})
describe('works', () => {
@@ -420,18 +420,27 @@ describe('getPredictedMenses', () => {
})
const result = getPredictedMenses()
const expectedResult = [
{
'startDate': '2018-07-27',
'endDate': '2018-07-31'
},
{
'startDate': '2018-08-10',
'endDate': '2018-08-14'
},
{
'startDate': '2018-08-24',
'endDate': '2018-08-28'
}
[
'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)
})
@@ -461,18 +470,21 @@ describe('getPredictedMenses', () => {
})
const result = getPredictedMenses()
const expectedResult = [
{
'startDate': '2018-09-01',
'endDate': '2018-09-03'
},
{
'startDate': '2018-10-02',
'endDate': '2018-10-04'
},
{
'startDate': '2018-11-02',
'endDate': '2018-11-04'
}
[
'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)
})
@@ -502,18 +514,21 @@ describe('getPredictedMenses', () => {
})
const result = getPredictedMenses()
const expectedResult = [
{
'startDate': '2018-08-14',
'endDate': '2018-08-16'
},
{
'startDate': '2018-08-28',
'endDate': '2018-08-30'
},
{
'startDate': '2018-09-11',
'endDate': '2018-09-13'
}
[
'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)
})
@@ -543,18 +558,27 @@ describe('getPredictedMenses', () => {
})
const result = getPredictedMenses()
const expectedResult = [
{
'startDate': '2018-08-13',
'endDate': '2018-08-17'
},
{
'startDate': '2018-08-27',
'endDate': '2018-08-31'
},
{
'startDate': '2018-09-10',
'endDate': '2018-09-14'
}
[
'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)
})