From 1c791df5ad84bb88674ca99d156fdb93ca189563 Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Mon, 2 Jul 2018 16:55:20 +0200
Subject: [PATCH] Hook up fertility temp status to app

---
 components/cycle-day-overview.js |  2 --
 components/cycle-day.js          |  3 +++
 lib/cycle.js                     | 26 ++++++++++++++++++--------
 lib/sensiplan-adapter.js         | 29 +++++++++++++++++++++++++++++
 lib/sensiplan.js                 | 14 ++++----------
 styles/index.js                  |  8 +++-----
 6 files changed, 57 insertions(+), 25 deletions(-)
 create mode 100644 lib/sensiplan-adapter.js

diff --git a/components/cycle-day-overview.js b/components/cycle-day-overview.js
index f8c3acae..49068fea 100644
--- a/components/cycle-day-overview.js
+++ b/components/cycle-day-overview.js
@@ -14,8 +14,6 @@ const getCycleDayNumber = cycleModule().getCycleDayNumber
 export default class DayView extends Component {
   constructor(props) {
     super(props)
-    console.log('new')
-    console.log(props.cycleDay)
     this.cycleDay = props.cycleDay
     this.showView = props.showView
     this.state = {
diff --git a/components/cycle-day.js b/components/cycle-day.js
index 2d782202..5b54b2f9 100644
--- a/components/cycle-day.js
+++ b/components/cycle-day.js
@@ -4,6 +4,7 @@ import {
   Text
 } from 'react-native'
 import cycleModule from '../lib/cycle'
+import { getTemperatureFertilityStatus } from '../lib/sensiplan-adapter'
 import DayView from './cycle-day-overview'
 import BleedingEditView from './bleeding'
 import TemperatureEditView from './temperature'
@@ -28,6 +29,7 @@ export default class Day extends Component {
 
   render() {
     const cycleDayNumber = getCycleDayNumber(this.cycleDay.date)
+    const temperatureFertilityStatus = getTemperatureFertilityStatus(this.cycleDay.date)
     return (
       <View style={ styles.cycleDayOuterView }>
         <View style={ styles.cycleDayDateView }>
@@ -37,6 +39,7 @@ export default class Day extends Component {
         </View >
         <View style={ styles.cycleDayNumberView }>
           { cycleDayNumber && <Text style={styles.cycleDayNumber} >Cycle day {cycleDayNumber}</Text> }
+          { cycleDayNumber && <Text style={styles.cycleDayNumber} >Temperature status: {temperatureFertilityStatus}</Text> }
         </View >
         <View style={ styles.cycleDaySymptomsView }>
           {
diff --git a/lib/cycle.js b/lib/cycle.js
index 8dbe838c..ec334276 100644
--- a/lib/cycle.js
+++ b/lib/cycle.js
@@ -1,16 +1,21 @@
 import * as joda from 'js-joda'
-
 const LocalDate = joda.LocalDate
 
-export default function config(opts = {}) {
+export default function config(opts) {
   let bleedingDaysSortedByDate
-  if (!opts.bleedingDaysSortedByDate) {
+  let temperatureDaysSortedByDate
+  let maxBreakInBleeding
+
+  if (!opts) {
     // we only want to require (and run) the db module when not running the tests
     bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate
+    temperatureDaysSortedByDate = require('../db').temperatureDaysSortedByDate
+    maxBreakInBleeding = 1
   } else {
-    bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate
+    bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate || []
+    temperatureDaysSortedByDate = opts.temperatureDaysSortedByDate || []
+    maxBreakInBleeding = opts.maxBreakInBleeding || 1
   }
-  const maxBreakInBleeding = opts.maxBreakInBleeding || 1
 
   function getLastMensesStart(targetDateString) {
     const targetDate = LocalDate.parse(targetDateString)
@@ -53,13 +58,18 @@ export default function config(opts = {}) {
     return diffInDays + 1
   }
 
-  function getPreviousDaysInCycle() {
-    return []
+  function getPreviousTemperaturesInCycle(targetDateString, lastMensesStart) {
+    const startIndex = temperatureDaysSortedByDate.findIndex(day => day.date <= targetDateString)
+    const previousTemperaturesInCycle = temperatureDaysSortedByDate.slice(startIndex)
+    const endIndex = previousTemperaturesInCycle.findIndex(day => day.date < lastMensesStart.date)
+    return previousTemperaturesInCycle
+      .slice(0, endIndex)
+      .map(day => day.temperature.value)
   }
 
   return {
     getCycleDayNumber,
     getLastMensesStart,
-    getPreviousDaysInCycle
+    getPreviousTemperaturesInCycle
   }
 }
diff --git a/lib/sensiplan-adapter.js b/lib/sensiplan-adapter.js
new file mode 100644
index 00000000..7ae60b31
--- /dev/null
+++ b/lib/sensiplan-adapter.js
@@ -0,0 +1,29 @@
+import { detectTemperatureShift } from './sensiplan'
+import cycleModule from './cycle'
+
+const getLastMensesStart = cycleModule().getLastMensesStart
+const getPreviousTemperaturesInCycle = cycleModule().getPreviousTemperaturesInCycle
+
+function getTemperatureFertilityStatus(targetDateString) {
+  const lastMensesStart = getLastMensesStart(targetDateString)
+  if (!lastMensesStart) return formatStatusForApp({ detected: false })
+  const previousTemperaturesInCycle = getPreviousTemperaturesInCycle(targetDateString, lastMensesStart)
+  // we get temps with latest first, but sensiplan module expects latest last
+  previousTemperaturesInCycle.reverse()
+  const status = detectTemperatureShift(previousTemperaturesInCycle)
+  return formatStatusForApp(status)
+}
+
+function formatStatusForApp(status) {
+  if (!status.detected) return 'fertile'
+  const dict = [
+    "regular temperature",
+    "first exception",
+    "second exception"
+  ]
+  return `infertile according to the ${dict[status.rule]} rule`
+}
+
+export {
+  getTemperatureFertilityStatus
+}
\ No newline at end of file
diff --git a/lib/sensiplan.js b/lib/sensiplan.js
index ed369532..3f66c47d 100644
--- a/lib/sensiplan.js
+++ b/lib/sensiplan.js
@@ -31,7 +31,7 @@ function detectTemperatureShift(temperaturesOfCycle) {
     // if we do, remember the details and start collecting the high level temps
     acc.detected = true
     acc.high = [temp]
-    acc.rules = checkResult.rules
+    acc.rule = checkResult.rule
     acc.ltl = ltl
     acc.low = getSixTempsBefore(i)
 
@@ -57,9 +57,7 @@ function checkIfFirstHighMeasurement(temp, i, temps, ltl) {
   if (regularRuleApplies(temp, nextTemps, ltl)) {
     return {
       isFirstHighMeasurement: true,
-      rules: {
-        regular: true,
-      },
+      rule: 0,
       ltl
     }
   }
@@ -67,9 +65,7 @@ function checkIfFirstHighMeasurement(temp, i, temps, ltl) {
   if (firstExceptionRuleApplies(temp, nextTemps, ltl)) {
     return {
       isFirstHighMeasurement: true,
-      rules: {
-        firstException: true,
-      },
+      rule: 1,
       ltl
     }
   }
@@ -77,9 +73,7 @@ function checkIfFirstHighMeasurement(temp, i, temps, ltl) {
   if (secondExceptionRuleApplies(temp, nextTemps, ltl)) {
     return {
       isFirstHighMeasurement: true,
-      rules: {
-        secondException: true,
-      },
+      rule: 2,
       ltl
     }
   }
diff --git a/styles/index.js b/styles/index.js
index 5eb9cc18..204e1325 100644
--- a/styles/index.js
+++ b/styles/index.js
@@ -14,14 +14,13 @@ export default StyleSheet.create({
   dateHeader: {
     fontSize: 20,
     fontWeight: 'bold',
-    margin: 30,
+    margin: 20,
     color: 'white',
     textAlign: 'center',
     textAlignVertical: 'center'
   },
   cycleDayNumber: {
     fontSize: 18,
-    margin: 20,
     textAlign: 'center',
     textAlignVertical: 'center'
   },
@@ -77,14 +76,13 @@ export default StyleSheet.create({
     justifyContent: 'space-around'
   },
   cycleDayDateView: {
-    flex: 2,
     justifyContent: 'center',
     backgroundColor: 'steelblue'
   },
   cycleDayNumberView: {
-    flex: 1,
     justifyContent: 'center',
-    backgroundColor: 'skyblue'
+    backgroundColor: 'skyblue',
+    padding: 10
   },
   cycleDaySymptomsView: {
     flex: 8,
-- 
GitLab