Skip to content
Snippets Groups Projects
Commit c99aa750 authored by tina's avatar tina
Browse files

improves functions and stats are displayed in a nicer format

parent 6135c922
No related branches found
No related tags found
No related merge requests found
import React, { Component } from 'react' import React, { Component } from 'react'
import { import {
View,
Text, Text,
ScrollView ScrollView
} from 'react-native' } from 'react-native'
...@@ -14,38 +13,50 @@ export default class Stats extends Component { ...@@ -14,38 +13,50 @@ export default class Stats extends Component {
super(props) super(props)
const allMensesStarts = cycleModule().getAllMensesStarts() const allMensesStarts = cycleModule().getAllMensesStarts()
this.test = allMensesStarts this.test = allMensesStarts
this.state = {
const cycleLengths = getCycleLength(allMensesStarts) text: determineStatsText(allMensesStarts)
this.bla = cycleLengths }
this.numberOfCycles = cycleLengths.length
this.periodInfo = getPeriodInfo(cycleLengths)
} }
render() { render() {
console.log('...............')
console.log(this.test)
console.log(this.bla)
return ( return (
<ScrollView> <ScrollView>
<Text style={styles.welcome}>based on {this.numberOfCycles} periods:</Text> <Text style={styles.welcome}>{this.state.text}</Text>
<Text style={styles.welcome}>min: {this.periodInfo.minimum}</Text>
<Text style={styles.welcome}>mean: {this.periodInfo.mean}</Text>
<Text style={styles.welcome}>max: {this.periodInfo.maximum}</Text>
<Text style={styles.welcome}>median: {this.periodInfo.median}</Text>
<Text style={styles.welcome}>standard deviation: {this.periodInfo.stdDeviation}</Text>
</ScrollView> </ScrollView>
) )
} }
} }
function getCycleLength(cycleStartDates) { function getCycleLength(cycleStartDates) {
const cycleStartDatesReverse = cycleStartDates.reverse()
const periodLengths = [] const periodLengths = []
for (let i = 0; i < cycleStartDates.length - 1; i++) { for (let i = 0; i < cycleStartDates.length - 1; i++) {
const periodStart = LocalDate.parse(cycleStartDatesReverse[i]) const nextPeriodStart = LocalDate.parse(cycleStartDates[i])
const periodEnd = LocalDate.parse(cycleStartDatesReverse[i + 1]) const periodStart = LocalDate.parse(cycleStartDates[i + 1])
periodLengths.unshift(periodStart.until(periodEnd, ChronoUnit.DAYS)) periodLengths.push(periodStart.until(nextPeriodStart, ChronoUnit.DAYS))
}
return periodLengths
}
function determineStatsText(allMensesStarts) {
const emptyStats = 'At least one completed period is needed to present you with stats here.'
if (allMensesStarts.length < 2) {
return emptyStats
} else {
const cycleLengths = getCycleLength(allMensesStarts)
const numberOfCycles = cycleLengths.length
const periodInfo = getPeriodInfo(cycleLengths)
if (numberOfCycles === 1) {
return `You have documented one period of ${cycleLengths[0]} days.`
} else {
const statsText = `Stats are based on ${numberOfCycles} completed
periods.\n\n
Average period length: ${periodInfo.mean} days\n\n
shortest period: ${periodInfo.minimum} days\n
longest period: ${periodInfo.maximum} days\n
median length (meaning 50% of periods are of this length or shorter):
${periodInfo.median} days\n
standard deviation: ${periodInfo.stdDeviation}`
return statsText
}
} }
return periodLengths.reverse()
} }
\ No newline at end of file
...@@ -133,17 +133,13 @@ export default function config(opts) { ...@@ -133,17 +133,13 @@ export default function config(opts) {
function getAllMensesStarts(day, collectedDates) { function getAllMensesStarts(day, collectedDates) {
day = day || LocalDate.now().toString() day = day || LocalDate.now().toString()
collectedDates = collectedDates || [] collectedDates = collectedDates || []
if (!day) { const lastStart = getLastMensesStart(day)
return null if (!lastStart) {
return collectedDates
} else { } else {
const lastStart = getLastMensesStart(day) const newDay = LocalDate.parse(lastStart.date).minusDays(1).toString()
if (!lastStart) { collectedDates.push(lastStart.date)
return collectedDates return getAllMensesStarts(newDay, collectedDates)
} else {
const newDay = LocalDate.parse(lastStart.date).minusDays(1).toString()
collectedDates.push(lastStart.date)
return getAllMensesStarts(newDay, collectedDates)
}
} }
} }
...@@ -151,7 +147,7 @@ export default function config(opts) { ...@@ -151,7 +147,7 @@ export default function config(opts) {
getCycleDayNumber, getCycleDayNumber,
getCycleForDay, getCycleForDay,
getPreviousCycle, getPreviousCycle,
getLastMensesStart, getCyclesBefore,
getAllMensesStarts getAllMensesStarts
} }
} }
\ No newline at end of file
This diff is collapsed.
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