Merge branch '109-display-stats-about-period-length' into 'master'

Resolve "add stats button on home screen and show stats about period length"

Closes #109

See merge request bloodyhealth/drip!41
This commit is contained in:
tina
2018-08-14 12:36:57 +00:00
8 changed files with 1599 additions and 1520 deletions
+6
View File
@@ -87,6 +87,12 @@ export default class Home extends Component {
title="delete everything">
</Button>
</View>
<View style={styles.homeButton}>
<Button
onPress={() => navigate('stats')}
title="Go to stats">
</Button>
</View>
</View>
</ScrollView>
)
+51
View File
@@ -0,0 +1,51 @@
import React, { Component } from 'react'
import {
Text,
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'
export default class Stats extends Component {
render() {
const allMensesStarts = cycleModule().getAllMensesStarts()
const statsText = determineStatsText(allMensesStarts)
return (
<ScrollView>
<View>
<Text style={styles.stats}>{statsText}</Text>
</View>
</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
}
function determineStatsText(allMensesStarts) {
const emptyStats = 'At least one completed cycle is needed to present you with stats here.'
if (allMensesStarts.length < 2) {
return emptyStats
} else {
const cycleLengths = getCycleLength(allMensesStarts)
const numberOfCycles = cycleLengths.length
if (numberOfCycles === 1) {
return `You have documented one cycle of ${cycleLengths[0]} days.`
}
const cycleInfo = getCycleInfo(cycleLengths)
const statsText = `Stats are based on ${numberOfCycles} completed cycles.\n\n\
Average cycle length: ${cycleInfo.mean} days\n\nShortest cycle: ${cycleInfo.minimum} days\nLongest cycle: ${cycleInfo.maximum} days\nMedian length (meaning 50% of cycles are of this length or shorter): ${cycleInfo.median} days\nStandard deviation: ${cycleInfo.stdDeviation}`
return statsText
}
}