Rerender chart on scale observable change

This commit is contained in:
Julia Friesel
2018-08-22 10:17:32 +02:00
parent c597a654d8
commit 7538dbdccc
5 changed files with 16 additions and 15 deletions
+4 -3
View File
@@ -7,7 +7,7 @@ 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'
import { scaleObservable } from '../../local-storage'
export default class CycleChart extends Component { export default class CycleChart extends Component {
constructor(props) { constructor(props) {
@@ -24,7 +24,6 @@ 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() {
@@ -33,16 +32,18 @@ export default class CycleChart extends Component {
})(this) })(this)
cycleDaysSortedByDate.addListener(this.reCalculateChartInfo) cycleDaysSortedByDate.addListener(this.reCalculateChartInfo)
this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false)
} }
componentWillUnmount() { componentWillUnmount() {
cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo) cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo)
this.removeObvListener()
} }
render() { render() {
return ( return (
<View style={{ flexDirection: 'row', marginTop: 50 }}> <View style={{ flexDirection: 'row', marginTop: 50 }}>
{this.yAxisView} <View {...styles.yAxis}>{makeYAxisLabels()}</View>
{makeHorizontalGrid()} {makeHorizontalGrid()}
{<FlatList {<FlatList
horizontal={true} horizontal={true}
+1 -1
View File
@@ -15,7 +15,7 @@ const label = styles.column.label
export default class DayColumn extends Component { export default class DayColumn extends Component {
passDateToDayView(dateString) { passDateToDayView(dateString) {
const cycleDay = getOrCreateCycleDay(dateString) const cycleDay = getOrCreateCycleDay(dateString)
this.props.navigate('cycleDay', { cycleDay }) this.props.navigate('CycleDay', { cycleDay })
} }
shouldComponentUpdate(newProps) { shouldComponentUpdate(newProps) {
+6 -6
View File
@@ -2,11 +2,11 @@ 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' import { scaleObservable } from '../../local-storage'
export function makeYAxisLabels() { export function makeYAxisLabels() {
const units = config.temperatureScale.units const units = config.temperatureScale.units
const scaleMax = tempScaleObservable.value.max const scaleMax = scaleObservable.value.max
return getTickPositions().map((y, i) => { return getTickPositions().map((y, i) => {
const style = styles.yAxisLabel const style = styles.yAxisLabel
@@ -38,8 +38,8 @@ export function makeHorizontalGrid() {
function getTickPositions() { function getTickPositions() {
const units = config.temperatureScale.units const units = config.temperatureScale.units
const scaleMin = tempScaleObservable.value.min const scaleMin = scaleObservable.value.min
const scaleMax = tempScaleObservable.value.max const scaleMax = scaleObservable.value.max
const numberOfTicks = (scaleMax - scaleMin) * (1 / units) const numberOfTicks = (scaleMax - scaleMin) * (1 / units)
const tickDistance = config.chartHeight / numberOfTicks const tickDistance = config.chartHeight / numberOfTicks
@@ -52,8 +52,8 @@ function getTickPositions() {
} }
export function normalizeToScale(temp) { export function normalizeToScale(temp) {
const scale = config.temperatureScale const scale = scaleObservable.value
const valueRelativeToScale = (scale.high - temp) / (scale.high - scale.low) const valueRelativeToScale = (scale.max - temp) / (scale.max - scale.min)
const scaleHeight = config.chartHeight const scaleHeight = config.chartHeight
return scaleHeight * valueRelativeToScale return scaleHeight * valueRelativeToScale
} }
+2 -2
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 { tempScaleObservable, saveTempScale } from '../local-storage' import { scaleObservable, saveTempScale } from '../local-storage'
export default class Settings extends Component { export default class Settings extends Component {
render() { render() {
@@ -62,7 +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 = Object.assign({}, tempScaleObservable.value) this.state = Object.assign({}, scaleObservable.value)
} }
onValuesChange = (values) => { onValuesChange = (values) => {
+3 -3
View File
@@ -2,7 +2,7 @@ import { AsyncStorage } from 'react-native'
import Observable from 'obv' import Observable from 'obv'
import config from '../config' import config from '../config'
export const tempScaleObservable = Observable() export const scaleObservable = Observable()
setTempScaleInitially() setTempScaleInitially()
async function setTempScaleInitially() { async function setTempScaleInitially() {
@@ -16,11 +16,11 @@ async function setTempScaleInitially() {
max: config.temperatureScale.defaultHigh max: config.temperatureScale.defaultHigh
} }
} }
tempScaleObservable.set(value) scaleObservable.set(value)
} }
export async function saveTempScale(scale) { export async function saveTempScale(scale) {
await AsyncStorage.setItem('tempScale', JSON.stringify(scale)) await AsyncStorage.setItem('tempScale', JSON.stringify(scale))
tempScaleObservable.set(scale) scaleObservable.set(scale)
} }