Only use db functions when components are actually mounted

This commit is contained in:
Julia Friesel
2018-09-11 18:06:19 +02:00
parent 04e43b823d
commit 3e3cef8769
6 changed files with 25 additions and 20 deletions
+4 -2
View File
@@ -1,13 +1,15 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { CalendarList } from 'react-native-calendars' import { CalendarList } from 'react-native-calendars'
import {LocalDate} from 'js-joda' import {LocalDate} from 'js-joda'
import { getOrCreateCycleDay, bleedingDaysSortedByDate } from '../db' import { getOrCreateCycleDay, getBleedingDaysSortedByDate } from '../db'
import cycleModule from '../lib/cycle' import cycleModule from '../lib/cycle'
import {shadesOfRed} from '../styles/index' import {shadesOfRed} from '../styles/index'
import styles from '../styles/index' import styles from '../styles/index'
export default class CalendarView extends Component { export default class CalendarView extends Component {
constructor(props) { constructor(props) {
const bleedingDaysSortedByDate = getBleedingDaysSortedByDate()
super(props) super(props)
const predictedMenses = cycleModule().getPredictedMenses() const predictedMenses = cycleModule().getPredictedMenses()
this.state = { this.state = {
@@ -31,7 +33,7 @@ export default class CalendarView extends Component {
} }
componentWillUnmount() { componentWillUnmount() {
bleedingDaysSortedByDate.removeListener(this.setStateWithCalFormattedDays) getBleedingDaysSortedByDate().removeListener(this.setStateWithCalFormattedDays)
} }
passDateToDayView = (result) => { passDateToDayView = (result) => {
+4 -3
View File
@@ -5,7 +5,7 @@ import { LocalDate } from 'js-joda'
import { makeYAxisLabels, normalizeToScale, makeHorizontalGrid } from './y-axis' import { makeYAxisLabels, normalizeToScale, makeHorizontalGrid } from './y-axis'
import nfpLines from './nfp-lines' import nfpLines from './nfp-lines'
import DayColumn from './day-column' import DayColumn from './day-column'
import { getCycleDay, cycleDaysSortedByDate, getAmountOfCycleDays } from '../../db' import { getCycleDay, getCycleDaysSortedByDate, getAmountOfCycleDays } from '../../db'
import styles from './styles' import styles from './styles'
import { scaleObservable } from '../../local-storage' import { scaleObservable } from '../../local-storage'
import config from '../../config' import config from '../../config'
@@ -25,6 +25,7 @@ export default class CycleChart extends Component {
/> />
) )
} }
this.cycleDaysSortedByDate = getCycleDaysSortedByDate()
} }
onLayout = ({ nativeEvent }) => { onLayout = ({ nativeEvent }) => {
@@ -35,12 +36,12 @@ export default class CycleChart extends Component {
this.setState({ columns: this.makeColumnInfo(nfpLines(height)) }) this.setState({ columns: this.makeColumnInfo(nfpLines(height)) })
} }
cycleDaysSortedByDate.addListener(this.reCalculateChartInfo) this.cycleDaysSortedByDate.addListener(this.reCalculateChartInfo)
this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false) this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false)
} }
componentWillUnmount() { componentWillUnmount() {
cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo) this.cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo)
this.removeObvListener() this.removeObvListener()
} }
+5 -2
View File
@@ -9,10 +9,13 @@ import { getOrCreateCycleDay } from '../../db'
import cycleModule from '../../lib/cycle' import cycleModule from '../../lib/cycle'
import DotAndLine from './dot-and-line' import DotAndLine from './dot-and-line'
const getCycleDayNumber = cycleModule().getCycleDayNumber
const label = styles.column.label const label = styles.column.label
export default class DayColumn extends Component { export default class DayColumn extends Component {
constructor() {
super()
this.getCycleDayNumber = cycleModule().getCycleDayNumber
}
passDateToDayView(dateString) { passDateToDayView(dateString) {
const cycleDay = getOrCreateCycleDay(dateString) const cycleDay = getOrCreateCycleDay(dateString)
this.props.navigate('CycleDay', { cycleDay }) this.props.navigate('CycleDay', { cycleDay })
@@ -68,7 +71,7 @@ export default class DayColumn extends Component {
) )
} }
const cycleDayNumber = getCycleDayNumber(dateString) const cycleDayNumber = this.getCycleDayNumber(dateString)
const shortDate = dateString.split('-').slice(1).join('-') const shortDate = dateString.split('-').slice(1).join('-')
const cycleDayLabel = ( const cycleDayLabel = (
<Text style = {label.number}> <Text style = {label.number}>
+4 -5
View File
@@ -8,14 +8,13 @@ import {
import { LocalDate, ChronoUnit } from 'js-joda' import { LocalDate, ChronoUnit } from 'js-joda'
import styles from '../styles/index' import styles from '../styles/index'
import cycleModule from '../lib/cycle' import cycleModule from '../lib/cycle'
import { requestHash, getOrCreateCycleDay, bleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll } from '../db' import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll } from '../db'
import {bleedingPrediction as labels} from './labels' import {bleedingPrediction as labels} from './labels'
const getCycleDayNumber = cycleModule().getCycleDayNumber
export default class Home extends Component { export default class Home extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
const getCycleDayNumber = cycleModule().getCycleDayNumber
this.todayDateString = LocalDate.now().toString() this.todayDateString = LocalDate.now().toString()
const cycleDayNumber = getCycleDayNumber(this.todayDateString) const cycleDayNumber = getCycleDayNumber(this.todayDateString)
@@ -34,11 +33,11 @@ export default class Home extends Component {
} }
})(this) })(this)
bleedingDaysSortedByDate.addListener(this.setStateWithCurrentText) getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText)
} }
componentWillUnmount() { componentWillUnmount() {
bleedingDaysSortedByDate.removeListener(this.setStateWithCurrentText) getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText)
} }
passTodayToDayView() { passTodayToDayView() {
+2 -2
View File
@@ -13,8 +13,8 @@ export default function config(opts) {
if (!opts) { if (!opts) {
// we only want to require (and run) the db module // we only want to require (and run) the db module
// when not running the tests // when not running the tests
bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate bleedingDaysSortedByDate = require('../db').getBleedingDaysSortedByDate()
cycleDaysSortedByDate = require('../db').cycleDaysSortedByDate cycleDaysSortedByDate = require('../db').getCycleDaysSortedByDate()
maxBreakInBleeding = 1 maxBreakInBleeding = 1
maxCycleLength = 99 maxCycleLength = 99
minCyclesForPrediction = 3 minCyclesForPrediction = 3
+6 -6
View File
@@ -2,12 +2,6 @@ import getFertilityStatus from './sympto'
import cycleModule from './cycle' import cycleModule from './cycle'
import { fertilityStatus } from '../components/cycle-day/labels/labels' import { fertilityStatus } from '../components/cycle-day/labels/labels'
const {
getCycleForDay,
getCyclesBefore,
getPreviousCycle
} = cycleModule()
export function getFertilityStatusStringForDay(dateString) { export function getFertilityStatusStringForDay(dateString) {
const status = getCycleStatusForDay(dateString) const status = getCycleStatusForDay(dateString)
if (!status) return fertilityStatus.unknown if (!status) return fertilityStatus.unknown
@@ -28,6 +22,12 @@ export function getFertilityStatusStringForDay(dateString) {
} }
export function getCycleStatusForDay(dateString) { export function getCycleStatusForDay(dateString) {
const {
getCycleForDay,
getCyclesBefore,
getPreviousCycle
} = cycleModule()
const cycle = getCycleForDay(dateString) const cycle = getCycleForDay(dateString)
if (!cycle) return null if (!cycle) return null