Use settings from observable in chart

This commit is contained in:
Julia Friesel
2018-08-22 08:43:41 +02:00
parent d5f9e6c639
commit c597a654d8
4 changed files with 53 additions and 54 deletions
+4 -4
View File
@@ -2,13 +2,12 @@ import React, { Component } from 'react'
import { View, FlatList } from 'react-native' import { View, FlatList } from 'react-native'
import range from 'date-range' import range from 'date-range'
import { LocalDate } from 'js-joda' import { LocalDate } from 'js-joda'
import { yAxis, normalizeToScale, horizontalGrid } from './y-axis' import { makeYAxisLabels, normalizeToScale, makeHorizontalGrid } from './y-axis'
import setUpFertilityStatusFunc from './nfp-lines' import setUpFertilityStatusFunc from './nfp-lines'
import DayColumn from './day-column' import DayColumn from './day-column'
import { getCycleDay, cycleDaysSortedByDate, getAmountOfCycleDays } from '../../db' import { getCycleDay, cycleDaysSortedByDate, getAmountOfCycleDays } from '../../db'
import styles from './styles' import styles from './styles'
const yAxisView = <View {...styles.yAxis}>{yAxis.labels}</View>
export default class CycleChart extends Component { export default class CycleChart extends Component {
constructor(props) { constructor(props) {
@@ -25,6 +24,7 @@ export default class CycleChart extends Component {
/> />
) )
} }
this.yAxisView = <View {...styles.yAxis}>{makeYAxisLabels()}</View>
this.reCalculateChartInfo = (function(Chart) { this.reCalculateChartInfo = (function(Chart) {
return function() { return function() {
@@ -42,8 +42,8 @@ export default class CycleChart extends Component {
render() { render() {
return ( return (
<View style={{ flexDirection: 'row', marginTop: 50 }}> <View style={{ flexDirection: 'row', marginTop: 50 }}>
{yAxisView} {this.yAxisView}
{horizontalGrid} {makeHorizontalGrid()}
{<FlatList {<FlatList
horizontal={true} horizontal={true}
inverted={true} inverted={true}
+34 -28
View File
@@ -2,48 +2,54 @@ import React from 'react'
import { Text, View } from 'react-native' import { Text, View } from 'react-native'
import config from '../../config' import config from '../../config'
import styles from './styles' import styles from './styles'
import { tempScaleObservable } from '../../local-storage'
function makeYAxis() { export function makeYAxisLabels() {
const scale = config.temperatureScale const units = config.temperatureScale.units
const scaleMin = scale.low const scaleMax = tempScaleObservable.value.max
const scaleMax = scale.high
const numberOfTicks = (scaleMax - scaleMin) * (1 / scale.units)
const tickDistance = config.chartHeight / numberOfTicks
const tickPositions = [] return getTickPositions().map((y, i) => {
const labels = []
// for style reasons, we don't want the first and last tick
for (let i = 1; i < numberOfTicks - 1; i++) {
const y = tickDistance * i
const style = styles.yAxisLabel const style = styles.yAxisLabel
// this eyeballing is sadly necessary because RN does not // this eyeballing is sadly necessary because RN does not
// support percentage values for transforms, which we'd need // support percentage values for transforms, which we'd need
// to reliably place the label vertically centered to the grid // to reliably place the label vertically centered to the grid
style.top = y - 8 style.top = y - 8
labels.push( return (
<Text <Text
style={{...style}} style={{ ...style }}
key={i}> key={i}>
{scaleMax - i * scale.units} {scaleMax - i * units}
</Text> </Text>
) )
tickPositions.push(y) })
}
return {labels, tickPositions}
} }
export const yAxis = makeYAxis() export function makeHorizontalGrid() {
return getTickPositions().map(tick => {
return (
<View
top={tick}
{...styles.horizontalGrid}
key={tick}
/>
)
})
}
export const horizontalGrid = yAxis.tickPositions.map(tick => { function getTickPositions() {
return ( const units = config.temperatureScale.units
<View const scaleMin = tempScaleObservable.value.min
top={tick} const scaleMax = tempScaleObservable.value.max
{...styles.horizontalGrid} const numberOfTicks = (scaleMax - scaleMin) * (1 / units)
key={tick} const tickDistance = config.chartHeight / numberOfTicks
/>
) const tickPositions = []
}) // for style reasons, we don't want the first and last tick
for (let i = 1; i < numberOfTicks - 1; i++) {
tickPositions.push(tickDistance * i)
}
return tickPositions
}
export function normalizeToScale(temp) { export function normalizeToScale(temp) {
const scale = config.temperatureScale const scale = config.temperatureScale
+2 -18
View File
@@ -15,7 +15,7 @@ import config from '../config'
import { settings as labels } from './labels' import { settings as labels } from './labels'
import getDataAsCsvDataUri from '../lib/import-export/export-to-csv' import getDataAsCsvDataUri from '../lib/import-export/export-to-csv'
import importCsv from '../lib/import-export/import-from-csv' import importCsv from '../lib/import-export/import-from-csv'
import { getTempScale, saveTempScale } from '../local-storage' import { tempScaleObservable, saveTempScale } from '../local-storage'
export default class Settings extends Component { export default class Settings extends Component {
render() { render() {
@@ -62,23 +62,7 @@ export default class Settings extends Component {
class TempSlider extends Component { class TempSlider extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = Object.assign({}, tempScaleObservable.value)
min: config.temperatureScale.defaultLow,
max: config.temperatureScale.defaultHigh
}
this.getStoredScale()
}
async getStoredScale() {
let storedScale
try {
storedScale = await getTempScale()
} catch(err) {
alertError(labels.tempScale.loadError)
return
}
if (!storedScale) return
this.setState(storedScale)
} }
onValuesChange = (values) => { onValuesChange = (values) => {
+13 -4
View File
@@ -1,13 +1,22 @@
import { AsyncStorage } from 'react-native' import { AsyncStorage } from 'react-native'
import Observable from 'obv' import Observable from 'obv'
import config from '../config'
export const tempScaleObservable = Observable() export const tempScaleObservable = Observable()
getTempScale() setTempScaleInitially()
async function getTempScale() { async function setTempScaleInitially() {
const result = await AsyncStorage.getItem('tempScale') const result = await AsyncStorage.getItem('tempScale')
if (!result) return result let value
tempScaleObservable.set(JSON.parse(result)) if (result) {
value = JSON.parse(result)
} else {
value = {
min: config.temperatureScale.defaultLow,
max: config.temperatureScale.defaultHigh
}
}
tempScaleObservable.set(value)
} }
export async function saveTempScale(scale) { export async function saveTempScale(scale) {