Move calculations functions to helpers file

This commit is contained in:
mashazyu
2019-11-18 17:41:50 +01:00
parent c24284a57d
commit 2ecb2e7647
2 changed files with 40 additions and 34 deletions
+3 -33
View File
@@ -1,6 +1,5 @@
import React, { Component } from 'react'
import { View, FlatList, ActivityIndicator } from 'react-native'
import { LocalDate } from 'js-joda'
import AppLoadingView from '../app-loading'
import YAxis from './y-axis'
@@ -8,9 +7,10 @@ import nfpLines from './nfp-lines'
import DayColumn from './day-column'
import HorizontalGrid from './horizontal-grid'
import { getCycleDaysSortedByDate, getAmountOfCycleDays } from '../../db'
import { getCycleDaysSortedByDate } from '../../db'
import nothingChanged from '../../db/db-unchanged'
import { scaleObservable } from '../../local-storage'
import { makeColumnInfo } from '../helpers/chart'
import config from '../../config'
@@ -71,7 +71,7 @@ export default class CycleChart extends Component {
this.chartSymptoms.push('temperature')
}
const columnData = this.makeColumnInfo()
const columnData = makeColumnInfo()
this.setState({
columns: columnData,
chartHeight: height
@@ -103,19 +103,6 @@ export default class CycleChart extends Component {
this.removeObvListener()
}
makeColumnInfo() {
let amountOfCycleDays = getAmountOfCycleDays()
// if there's not much data yet, we want to show at least 30 days on the chart
if (amountOfCycleDays < 30) {
amountOfCycleDays = 30
} else {
// we don't want the chart to end abruptly before the first data day
amountOfCycleDays += 5
}
const localDates = getTodayAndPreviousDays(amountOfCycleDays)
return localDates.map(localDate => localDate.toString())
}
render() {
const { chartHeight, chartLoaded } = this.state
return (
@@ -169,20 +156,3 @@ function LoadingMoreView(props) {
</View>
)
}
function getTodayAndPreviousDays(n) {
const today = LocalDate.now()
const targetDate = today.minusDays(n)
function getDaysInRange(currDate, range) {
if (currDate.isBefore(targetDate)) {
return range
} else {
range.push(currDate)
const next = currDate.minusDays(1)
return getDaysInRange(next, range)
}
}
return getDaysInRange(today, [])
}
+37 -1
View File
@@ -1,10 +1,12 @@
import { LocalDate } from 'js-joda'
import { scaleObservable, unitObservable } from '../../local-storage'
import { getCycleDay } from '../../db'
import { getCycleDay, getAmountOfCycleDays } from '../../db'
import config from '../../config'
//YAxis helpers
export function normalizeToScale(temp, columnHeight) {
const scale = scaleObservable.value
const valueRelativeToScale = (scale.max - temp) / (scale.max - scale.min)
@@ -70,6 +72,8 @@ export function getTickList(columnHeight) {
})
}
//DayColumn helpers
export function isSymptomDataComplete(symptom, dateString) {
const cycleDayData = getCycleDay(dateString)
const symptomData = cycleDayData[symptom]
@@ -162,3 +166,35 @@ export const symptomColorMethods = {
return colorIndex
}
}
// Chart helpers
export function makeColumnInfo() {
let amountOfCycleDays = getAmountOfCycleDays()
// if there's not much data yet, we want to show at least 30 days on the chart
if (amountOfCycleDays < 30) {
amountOfCycleDays = 30
} else {
// we don't want the chart to end abruptly before the first data day
amountOfCycleDays += 5
}
const localDates = getTodayAndPreviousDays(amountOfCycleDays)
return localDates.map(localDate => localDate.toString())
}
function getTodayAndPreviousDays(n) {
const today = LocalDate.now()
const targetDate = today.minusDays(n)
function getDaysInRange(currDate, range) {
if (currDate.isBefore(targetDate)) {
return range
} else {
range.push(currDate)
const next = currDate.minusDays(1)
return getDaysInRange(next, range)
}
}
return getDaysInRange(today, [])
}