diff --git a/components/stats.js b/components/stats.js index 6d8a0855058979170ca093a5b4fbe8249f019da3..1f771a6a7a48f262e11e18f1e51276882721a4db 100644 --- a/components/stats.js +++ b/components/stats.js @@ -4,7 +4,7 @@ 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 getPeriodInfo from '../lib/period-length' @@ -12,21 +12,20 @@ import getPeriodInfo from '../lib/period-length' export default class Stats extends Component { constructor(props) { super(props) - const lastMensStart = cycleModule().getLastMensesStart( - LocalDate.now().toString() - ) - const completedCycles = cycleModule().getCyclesBefore(lastMensStart) - this.numberOfCycles = completedCycles.length - // TODO get first days, compare with joda - const periodLengths = completedCycles.map(cycle => { - return cycle.length - }) - // until this point - this.periodInfo = getPeriodInfo(periodLengths) + const allMensesStarts = cycleModule().getAllMensesStarts() + this.test = allMensesStarts + + const cycleLengths = getCycleLength(allMensesStarts) + this.bla = cycleLengths + this.numberOfCycles = cycleLengths.length + this.periodInfo = getPeriodInfo(cycleLengths) } render() { + console.log('...............') + console.log(this.test) + console.log(this.bla) return ( <ScrollView> <Text style={styles.welcome}>based on {this.numberOfCycles} periods:</Text> @@ -38,4 +37,15 @@ export default class Stats extends Component { </ScrollView> ) } +} + +function getCycleLength(cycleStartDates) { + const cycleStartDatesReverse = cycleStartDates.reverse() + const periodLengths = [] + for (let i = 0; i < cycleStartDates.length - 1; i++) { + const periodStart = LocalDate.parse(cycleStartDatesReverse[i]) + const periodEnd = LocalDate.parse(cycleStartDatesReverse[i + 1]) + periodLengths.unshift(periodStart.until(periodEnd, ChronoUnit.DAYS)) + } + return periodLengths.reverse() } \ No newline at end of file diff --git a/lib/cycle.js b/lib/cycle.js index 00a1985518f5773dea1af688910b4261c28a7e6c..2aba3bdce64718bae6ad9abd125a8f7111dc94c0 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -130,11 +130,28 @@ export default function config(opts) { } } + function getAllMensesStarts(day, collectedDates) { + day = day || LocalDate.now().toString() + collectedDates = collectedDates || [] + if (!day) { + return null + } else { + const lastStart = getLastMensesStart(day) + if (!lastStart) { + return collectedDates + } else { + const newDay = LocalDate.parse(lastStart.date).minusDays(1).toString() + collectedDates.push(lastStart.date) + return getAllMensesStarts(newDay, collectedDates) + } + } + } + return { getCycleDayNumber, getCycleForDay, getPreviousCycle, - getCyclesBefore, - getLastMensesStart + getLastMensesStart, + getAllMensesStarts } } \ No newline at end of file