diff --git a/app.js b/app.js index 8976a63..3a310a0 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,7 @@ import { createStackNavigator } from 'react-navigation' import Home from './home' -import TemperatureList from './list' + +import Temperature from './temperature' import Calendar from './calendar' import DayView from './day-view' import Bleeding from './bleeding' @@ -11,8 +12,9 @@ YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated']) export default createStackNavigator({ home: { screen: Home }, - temperatureList: { screen: TemperatureList }, + + temperature: { screen: Temperature }, calendar: { screen: Calendar }, dayView: { screen: DayView }, bleeding: { screen: Bleeding } -}) \ No newline at end of file +}) diff --git a/bleeding.js b/bleeding.js index 359a94f..01fc4ce 100644 --- a/bleeding.js +++ b/bleeding.js @@ -15,22 +15,21 @@ import getCycleDay from './get-cycle-day' export default class Bleeding extends Component { constructor(props) { super(props) - const cycleDay = props.navigation.state.params.cycleDay - let bleedingValue = cycleDay.bleeding && cycleDay.bleeding.value + this.cycleDay = props.navigation.state.params.cycleDay + let bleedingValue = this.cycleDay.bleeding && this.cycleDay.bleeding.value if (! (typeof bleedingValue === 'number') ){ bleedingValue = -1 } this.state = { - cycleDay, currentValue: bleedingValue, - exclude: cycleDay.bleeding ? cycleDay.bleeding.exclude : false + exclude: this.cycleDay.bleeding ? this.cycleDay.bleeding.exclude : false } } // TODO display cycle day render() { const navigate = this.props.navigation.navigate - const day = this.state.cycleDay + const day = this.cycleDay const bleedingRadioProps = [ {label: labels[0], value: 0 }, {label: labels[1], value: 1 }, @@ -48,13 +47,13 @@ export default class Bleeding extends Component { formHorizontal={true} labelHorizontal={false} onPress={(itemValue) => { - this.setState({ currentValue: itemValue }) + this.setState({currentValue: itemValue}) }} /> Exclude { - this.setState({ exclude: val }) + this.setState({exclude: val}) }} value={this.state.exclude} /> + ) } diff --git a/db.js b/db.js index dd2648e..8f34199 100644 --- a/db.js +++ b/db.js @@ -1,8 +1,10 @@ import realm from 'realm' +import { LocalDate } from 'js-joda' let db -let cycleDaysSortedbyTempValueView = [] +let cycleDaysSortedbyDate = [] let bleedingDaysSortedByDate = [] +let temperatureDaysSortedByDate const TemperatureSchema = { name: 'Temperature', @@ -46,19 +48,15 @@ async function openDatabase() { // we only want this in dev mode deleteRealmIfMigrationNeeded: true }) - // just for testing purposes, the highest temperature will be topmost - // because I was too layz to make a scroll view - cycleDaysSortedbyTempValueView = db.objects('CycleDay').filtered('temperature != null').sorted('temperature.value', true) + + cycleDaysSortedbyDate = db.objects('CycleDay').sorted('date', true) bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) + temperatureDaysSortedByDate = db.objects('CycleDay').filtered('temperature != null').sorted('date', true) } -function saveTemperature(date, temperature) { +function saveTemperature(cycleDay, temperature) { db.write(() => { - const doc = { - date, - temperature - } - db.create('CycleDay', doc) + cycleDay.temperature = temperature }) } @@ -80,11 +78,22 @@ function getOrCreateCycleDay(localDate) { return result } +function getPreviousTemperature(cycleDay) { + cycleDay.wrappedDate = LocalDate.parse(cycleDay.date) + const winner = temperatureDaysSortedByDate.find(day => { + const wrappedDate = LocalDate.parse(day.date) + return wrappedDate.isBefore(cycleDay.wrappedDate) + }) + if (!winner) return null + return winner.temperature.value +} + export { - cycleDaysSortedbyTempValueView, + cycleDaysSortedbyDate, openDatabase, saveTemperature, saveBleeding, getOrCreateCycleDay, - bleedingDaysSortedByDate -} \ No newline at end of file + bleedingDaysSortedByDate, + getPreviousTemperature +} diff --git a/home.js b/home.js index c32e26a..c02055e 100644 --- a/home.js +++ b/home.js @@ -37,4 +37,4 @@ export default class Home extends Component { ) } -} \ No newline at end of file +} diff --git a/list.js b/list.js deleted file mode 100644 index cd8d831..0000000 --- a/list.js +++ /dev/null @@ -1,60 +0,0 @@ -import React, { Component } from 'react' -import { - View, - Text, - Button, - TextInput, - FlatList, - Keyboard -} from 'react-native' - -import * as styles from './styles' -import { cycleDaysSortedbyTempValueView, saveTemperature } from './db' - -export default class Temp extends Component { - constructor(props) { - super(props) - this.state = { - currentValue: '', - rerenderToggle: false - } - } - - render() { - return ( - - { - this.setState({currentValue: val}) - }} - keyboardType='numeric' - value = {this.state.currentValue} - /> - + + + + ) + } +}