diff --git a/components/calendar.js b/components/calendar.js index 95e8bf866b17600dc2cb379ed8048a691af18e9a..74b754bd7fb5283ae6dc01c9fd0de8ee7c032b3b 100644 --- a/components/calendar.js +++ b/components/calendar.js @@ -1,13 +1,15 @@ import React, { Component } from 'react' import { CalendarList } from 'react-native-calendars' import {LocalDate} from 'js-joda' -import { getOrCreateCycleDay, bleedingDaysSortedByDate } from '../db' +import { getOrCreateCycleDay, getBleedingDaysSortedByDate } from '../db' import cycleModule from '../lib/cycle' import {shadesOfRed} from '../styles/index' import styles from '../styles/index' + export default class CalendarView extends Component { constructor(props) { + const bleedingDaysSortedByDate = getBleedingDaysSortedByDate() super(props) const predictedMenses = cycleModule().getPredictedMenses() this.state = { @@ -31,7 +33,7 @@ export default class CalendarView extends Component { } componentWillUnmount() { - bleedingDaysSortedByDate.removeListener(this.setStateWithCalFormattedDays) + getBleedingDaysSortedByDate().removeListener(this.setStateWithCalFormattedDays) } passDateToDayView = (result) => { diff --git a/components/chart/chart.js b/components/chart/chart.js index a4cc7d19014e77a7bc46c98494c4988f68402904..f6da5399c39d6233e20e3b6f01b7cb56496793ba 100644 --- a/components/chart/chart.js +++ b/components/chart/chart.js @@ -5,7 +5,7 @@ import { LocalDate } from 'js-joda' import { makeYAxisLabels, normalizeToScale, makeHorizontalGrid } from './y-axis' import nfpLines from './nfp-lines' import DayColumn from './day-column' -import { getCycleDay, cycleDaysSortedByDate, getAmountOfCycleDays } from '../../db' +import { getCycleDay, getCycleDaysSortedByDate, getAmountOfCycleDays } from '../../db' import styles from './styles' import { scaleObservable } from '../../local-storage' import config from '../../config' @@ -25,6 +25,7 @@ export default class CycleChart extends Component { /> ) } + this.cycleDaysSortedByDate = getCycleDaysSortedByDate() } onLayout = ({ nativeEvent }) => { @@ -35,12 +36,12 @@ export default class CycleChart extends Component { this.setState({ columns: this.makeColumnInfo(nfpLines(height)) }) } - cycleDaysSortedByDate.addListener(this.reCalculateChartInfo) + this.cycleDaysSortedByDate.addListener(this.reCalculateChartInfo) this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false) } componentWillUnmount() { - cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo) + this.cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo) this.removeObvListener() } diff --git a/components/chart/day-column.js b/components/chart/day-column.js index 9b010fda01c53c7a5040e72addfceb590d7b354d..e541ab004426a230843ad1a920a40002cb039227 100644 --- a/components/chart/day-column.js +++ b/components/chart/day-column.js @@ -9,10 +9,13 @@ import { getOrCreateCycleDay } from '../../db' import cycleModule from '../../lib/cycle' import DotAndLine from './dot-and-line' -const getCycleDayNumber = cycleModule().getCycleDayNumber const label = styles.column.label export default class DayColumn extends Component { + constructor() { + super() + this.getCycleDayNumber = cycleModule().getCycleDayNumber + } passDateToDayView(dateString) { const cycleDay = getOrCreateCycleDay(dateString) 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 cycleDayLabel = ( <Text style = {label.number}> diff --git a/components/home.js b/components/home.js index b48327438ae82c064e3dd40a7f04c8d1db4b54fa..b7156d9493ba596624439b2bffd39608a79e2f1d 100644 --- a/components/home.js +++ b/components/home.js @@ -8,14 +8,13 @@ import { import { LocalDate, ChronoUnit } from 'js-joda' import styles from '../styles/index' 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' -const getCycleDayNumber = cycleModule().getCycleDayNumber - export default class Home extends Component { constructor(props) { super(props) + const getCycleDayNumber = cycleModule().getCycleDayNumber this.todayDateString = LocalDate.now().toString() const cycleDayNumber = getCycleDayNumber(this.todayDateString) @@ -34,11 +33,11 @@ export default class Home extends Component { } })(this) - bleedingDaysSortedByDate.addListener(this.setStateWithCurrentText) + getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText) } componentWillUnmount() { - bleedingDaysSortedByDate.removeListener(this.setStateWithCurrentText) + getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText) } passTodayToDayView() { diff --git a/lib/cycle.js b/lib/cycle.js index da91e2dcbe0eb8d51d03a57c798be87d4372ac4b..7deb02e0cedf79996deb5a90cd29626977055c7b 100644 --- a/lib/cycle.js +++ b/lib/cycle.js @@ -13,8 +13,8 @@ export default function config(opts) { if (!opts) { // we only want to require (and run) the db module // when not running the tests - bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate - cycleDaysSortedByDate = require('../db').cycleDaysSortedByDate + bleedingDaysSortedByDate = require('../db').getBleedingDaysSortedByDate() + cycleDaysSortedByDate = require('../db').getCycleDaysSortedByDate() maxBreakInBleeding = 1 maxCycleLength = 99 minCyclesForPrediction = 3 diff --git a/lib/sympto-adapter.js b/lib/sympto-adapter.js index b6ab2171789b4e8a2cb8ea5123ff623192c071cc..363538ef2554cbff05c0e5a4f806e2140592c572 100644 --- a/lib/sympto-adapter.js +++ b/lib/sympto-adapter.js @@ -2,12 +2,6 @@ import getFertilityStatus from './sympto' import cycleModule from './cycle' import { fertilityStatus } from '../components/cycle-day/labels/labels' -const { - getCycleForDay, - getCyclesBefore, - getPreviousCycle -} = cycleModule() - export function getFertilityStatusStringForDay(dateString) { const status = getCycleStatusForDay(dateString) if (!status) return fertilityStatus.unknown @@ -28,6 +22,12 @@ export function getFertilityStatusStringForDay(dateString) { } export function getCycleStatusForDay(dateString) { + const { + getCycleForDay, + getCyclesBefore, + getPreviousCycle + } = cycleModule() + const cycle = getCycleForDay(dateString) if (!cycle) return null