Move calculations functions to helpers file
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import React, { Component } from 'react'
|
import React, { Component } from 'react'
|
||||||
import { View, FlatList, ActivityIndicator } from 'react-native'
|
import { View, FlatList, ActivityIndicator } from 'react-native'
|
||||||
import { LocalDate } from 'js-joda'
|
|
||||||
|
|
||||||
import AppLoadingView from '../app-loading'
|
import AppLoadingView from '../app-loading'
|
||||||
import YAxis from './y-axis'
|
import YAxis from './y-axis'
|
||||||
@@ -8,9 +7,10 @@ import nfpLines from './nfp-lines'
|
|||||||
import DayColumn from './day-column'
|
import DayColumn from './day-column'
|
||||||
import HorizontalGrid from './horizontal-grid'
|
import HorizontalGrid from './horizontal-grid'
|
||||||
|
|
||||||
import { getCycleDaysSortedByDate, getAmountOfCycleDays } from '../../db'
|
import { getCycleDaysSortedByDate } from '../../db'
|
||||||
import nothingChanged from '../../db/db-unchanged'
|
import nothingChanged from '../../db/db-unchanged'
|
||||||
import { scaleObservable } from '../../local-storage'
|
import { scaleObservable } from '../../local-storage'
|
||||||
|
import { makeColumnInfo } from '../helpers/chart'
|
||||||
|
|
||||||
import config from '../../config'
|
import config from '../../config'
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ export default class CycleChart extends Component {
|
|||||||
this.chartSymptoms.push('temperature')
|
this.chartSymptoms.push('temperature')
|
||||||
}
|
}
|
||||||
|
|
||||||
const columnData = this.makeColumnInfo()
|
const columnData = makeColumnInfo()
|
||||||
this.setState({
|
this.setState({
|
||||||
columns: columnData,
|
columns: columnData,
|
||||||
chartHeight: height
|
chartHeight: height
|
||||||
@@ -103,19 +103,6 @@ export default class CycleChart extends Component {
|
|||||||
this.removeObvListener()
|
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() {
|
render() {
|
||||||
const { chartHeight, chartLoaded } = this.state
|
const { chartHeight, chartLoaded } = this.state
|
||||||
return (
|
return (
|
||||||
@@ -169,20 +156,3 @@ function LoadingMoreView(props) {
|
|||||||
</View>
|
</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, [])
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
|
|
||||||
import { scaleObservable, unitObservable } from '../../local-storage'
|
import { scaleObservable, unitObservable } from '../../local-storage'
|
||||||
import { getCycleDay } from '../../db'
|
import { getCycleDay, getAmountOfCycleDays } from '../../db'
|
||||||
|
|
||||||
import config from '../../config'
|
import config from '../../config'
|
||||||
|
|
||||||
|
//YAxis helpers
|
||||||
|
|
||||||
export function normalizeToScale(temp, columnHeight) {
|
export function normalizeToScale(temp, columnHeight) {
|
||||||
const scale = scaleObservable.value
|
const scale = scaleObservable.value
|
||||||
const valueRelativeToScale = (scale.max - temp) / (scale.max - scale.min)
|
const valueRelativeToScale = (scale.max - temp) / (scale.max - scale.min)
|
||||||
@@ -70,6 +72,8 @@ export function getTickList(columnHeight) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//DayColumn helpers
|
||||||
|
|
||||||
export function isSymptomDataComplete(symptom, dateString) {
|
export function isSymptomDataComplete(symptom, dateString) {
|
||||||
const cycleDayData = getCycleDay(dateString)
|
const cycleDayData = getCycleDay(dateString)
|
||||||
const symptomData = cycleDayData[symptom]
|
const symptomData = cycleDayData[symptom]
|
||||||
@@ -162,3 +166,35 @@ export const symptomColorMethods = {
|
|||||||
return colorIndex
|
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, [])
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user