changs output of getpredictedmenses, adds days to calendar
This commit is contained in:
+32
-5
@@ -1,19 +1,23 @@
|
|||||||
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)
|
||||||
@@ -35,7 +39,13 @@ export default class CalendarView extends Component {
|
|||||||
return (
|
return (
|
||||||
<CalendarList
|
<CalendarList
|
||||||
onDayPress={this.passDateToDayView.bind(this)}
|
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) {
|
|||||||
}
|
}
|
||||||
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
|
||||||
|
}, {})
|
||||||
}
|
}
|
||||||
+12
-8
@@ -163,12 +163,11 @@ export default function config(opts) {
|
|||||||
|
|
||||||
function getPredictedMenses() {
|
function getPredictedMenses() {
|
||||||
const allMensesStarts = getAllMensesStarts()
|
const allMensesStarts = getAllMensesStarts()
|
||||||
|
|
||||||
const atLeastOneCycle = allMensesStarts.length > 1
|
const atLeastOneCycle = allMensesStarts.length > 1
|
||||||
if (!atLeastOneCycle ||
|
if (!atLeastOneCycle ||
|
||||||
allMensesStarts.length < minCyclesForPrediction
|
allMensesStarts.length < minCyclesForPrediction
|
||||||
) {
|
) {
|
||||||
return {}
|
return []
|
||||||
}
|
}
|
||||||
const cycleLengths = getCycleLength(allMensesStarts)
|
const cycleLengths = getCycleLength(allMensesStarts)
|
||||||
const cycleInfo = getCycleLengthStats(cycleLengths)
|
const cycleInfo = getCycleLengthStats(cycleLengths)
|
||||||
@@ -181,15 +180,20 @@ export default function config(opts) {
|
|||||||
} else {
|
} else {
|
||||||
periodStartVariation = 2
|
periodStartVariation = 2
|
||||||
}
|
}
|
||||||
var lastStart = allMensesStarts[0]
|
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
let lastStart = LocalDate.parse(allMensesStarts[0])
|
||||||
const predictedMenses = []
|
const predictedMenses = []
|
||||||
for (let i = 0; i < 3; i++) {
|
for (let i = 0; i < 3; i++) {
|
||||||
lastStart = LocalDate.parse(lastStart).plusDays(periodDistance).toString()
|
lastStart = lastStart.plusDays(periodDistance)
|
||||||
const nextPredictedRange = {
|
const nextPredictedDates = [lastStart.toString()]
|
||||||
'startDate': LocalDate.parse(lastStart).minusDays(periodStartVariation).toString(),
|
for (let j = 0; j < periodStartVariation; j++) {
|
||||||
'endDate': LocalDate.parse(lastStart).plusDays(periodStartVariation).toString()
|
nextPredictedDates.push(lastStart.minusDays(j+1).toString())
|
||||||
|
nextPredictedDates.push(lastStart.plusDays(j+1).toString())
|
||||||
}
|
}
|
||||||
predictedMenses.push(nextPredictedRange)
|
nextPredictedDates.sort()
|
||||||
|
predictedMenses.push(nextPredictedDates)
|
||||||
}
|
}
|
||||||
return predictedMenses
|
return predictedMenses
|
||||||
}
|
}
|
||||||
|
|||||||
+75
-51
@@ -357,7 +357,7 @@ describe('getPredictedMenses', () => {
|
|||||||
minCyclesForPrediction: 1
|
minCyclesForPrediction: 1
|
||||||
})
|
})
|
||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
expect(result).to.eql({})
|
expect(result).to.eql([])
|
||||||
})
|
})
|
||||||
it('if one bleeding is documented (no completed cycle)', () => {
|
it('if one bleeding is documented (no completed cycle)', () => {
|
||||||
const cycleDaysSortedByDate = [
|
const cycleDaysSortedByDate = [
|
||||||
@@ -374,7 +374,7 @@ describe('getPredictedMenses', () => {
|
|||||||
minCyclesForPrediction: 1
|
minCyclesForPrediction: 1
|
||||||
})
|
})
|
||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
expect(result).to.eql({})
|
expect(result).to.eql([])
|
||||||
})
|
})
|
||||||
it('if number of cycles is below minCyclesForPrediction', () => {
|
it('if number of cycles is below minCyclesForPrediction', () => {
|
||||||
const cycleDaysSortedByDate = [
|
const cycleDaysSortedByDate = [
|
||||||
@@ -397,7 +397,7 @@ describe('getPredictedMenses', () => {
|
|||||||
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
|
bleedingDaysSortedByDate: cycleDaysSortedByDate.filter(d => d.bleeding)
|
||||||
})
|
})
|
||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
expect(result).to.eql({})
|
expect(result).to.eql([])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('works', () => {
|
describe('works', () => {
|
||||||
@@ -420,18 +420,27 @@ describe('getPredictedMenses', () => {
|
|||||||
})
|
})
|
||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
const expectedResult = [
|
const expectedResult = [
|
||||||
{
|
[
|
||||||
'startDate': '2018-07-27',
|
'2018-07-27',
|
||||||
'endDate': '2018-07-31'
|
'2018-07-28',
|
||||||
},
|
'2018-07-29',
|
||||||
{
|
'2018-07-30',
|
||||||
'startDate': '2018-08-10',
|
'2018-07-31'
|
||||||
'endDate': '2018-08-14'
|
],
|
||||||
},
|
[
|
||||||
{
|
'2018-08-10',
|
||||||
'startDate': '2018-08-24',
|
'2018-08-11',
|
||||||
'endDate': '2018-08-28'
|
'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)
|
expect(result).to.eql(expectedResult)
|
||||||
})
|
})
|
||||||
@@ -461,18 +470,21 @@ describe('getPredictedMenses', () => {
|
|||||||
})
|
})
|
||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
const expectedResult = [
|
const expectedResult = [
|
||||||
{
|
[
|
||||||
'startDate': '2018-09-01',
|
'2018-09-01',
|
||||||
'endDate': '2018-09-03'
|
'2018-09-02',
|
||||||
},
|
'2018-09-03'
|
||||||
{
|
],
|
||||||
'startDate': '2018-10-02',
|
[
|
||||||
'endDate': '2018-10-04'
|
'2018-10-02',
|
||||||
},
|
'2018-10-03',
|
||||||
{
|
'2018-10-04'
|
||||||
'startDate': '2018-11-02',
|
],
|
||||||
'endDate': '2018-11-04'
|
[
|
||||||
}
|
'2018-11-02',
|
||||||
|
'2018-11-03',
|
||||||
|
'2018-11-04'
|
||||||
|
]
|
||||||
]
|
]
|
||||||
expect(result).to.eql(expectedResult)
|
expect(result).to.eql(expectedResult)
|
||||||
})
|
})
|
||||||
@@ -502,18 +514,21 @@ describe('getPredictedMenses', () => {
|
|||||||
})
|
})
|
||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
const expectedResult = [
|
const expectedResult = [
|
||||||
{
|
[
|
||||||
'startDate': '2018-08-14',
|
'2018-08-14',
|
||||||
'endDate': '2018-08-16'
|
'2018-08-15',
|
||||||
},
|
'2018-08-16'
|
||||||
{
|
],
|
||||||
'startDate': '2018-08-28',
|
[
|
||||||
'endDate': '2018-08-30'
|
'2018-08-28',
|
||||||
},
|
'2018-08-29',
|
||||||
{
|
'2018-08-30'
|
||||||
'startDate': '2018-09-11',
|
],
|
||||||
'endDate': '2018-09-13'
|
[
|
||||||
}
|
'2018-09-11',
|
||||||
|
'2018-09-12',
|
||||||
|
'2018-09-13'
|
||||||
|
]
|
||||||
]
|
]
|
||||||
expect(result).to.eql(expectedResult)
|
expect(result).to.eql(expectedResult)
|
||||||
})
|
})
|
||||||
@@ -543,18 +558,27 @@ describe('getPredictedMenses', () => {
|
|||||||
})
|
})
|
||||||
const result = getPredictedMenses()
|
const result = getPredictedMenses()
|
||||||
const expectedResult = [
|
const expectedResult = [
|
||||||
{
|
[
|
||||||
'startDate': '2018-08-13',
|
'2018-08-13',
|
||||||
'endDate': '2018-08-17'
|
'2018-08-14',
|
||||||
},
|
'2018-08-15',
|
||||||
{
|
'2018-08-16',
|
||||||
'startDate': '2018-08-27',
|
'2018-08-17',
|
||||||
'endDate': '2018-08-31'
|
],
|
||||||
},
|
[
|
||||||
{
|
'2018-08-27',
|
||||||
'startDate': '2018-09-10',
|
'2018-08-28',
|
||||||
'endDate': '2018-09-14'
|
'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)
|
expect(result).to.eql(expectedResult)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user