From bc358bd9ed9b1b34cc85b08a5c34e512599dc9c4 Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Sun, 26 May 2019 07:39:15 +0200
Subject: [PATCH] Auto save whenever symptom view updates

---
 components/cycle-day/symptoms/bleeding.js     |  2 +-
 components/cycle-day/symptoms/cervix.js       |  2 +-
 components/cycle-day/symptoms/desire.js       |  2 +-
 components/cycle-day/symptoms/mood.js         |  2 +-
 components/cycle-day/symptoms/mucus.js        |  2 +-
 components/cycle-day/symptoms/note.js         |  2 +-
 components/cycle-day/symptoms/pain.js         |  2 +-
 components/cycle-day/symptoms/sex.js          |  2 +-
 components/cycle-day/symptoms/symptom-view.js | 17 +++++++----------
 9 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/components/cycle-day/symptoms/bleeding.js b/components/cycle-day/symptoms/bleeding.js
index 9d94a03c..6893bfab 100644
--- a/components/cycle-day/symptoms/bleeding.js
+++ b/components/cycle-day/symptoms/bleeding.js
@@ -22,7 +22,7 @@ export default class Bleeding extends SymptomView {
 
   symptomName = 'bleeding'
 
-  onBackButtonPress() {
+  autoSave = () => {
     if (typeof this.state.currentValue != 'number') {
       this.deleteSymptomEntry()
       return
diff --git a/components/cycle-day/symptoms/cervix.js b/components/cycle-day/symptoms/cervix.js
index f58ebf27..4515f5dd 100644
--- a/components/cycle-day/symptoms/cervix.js
+++ b/components/cycle-day/symptoms/cervix.js
@@ -19,7 +19,7 @@ export default class Cervix extends SymptomView {
 
   symptomName = 'cervix'
 
-  onBackButtonPress() {
+  autoSave = () => {
     const nothingEntered = ['opening', 'firmness', 'position'].every(val => typeof this.state[val] != 'number')
     if (nothingEntered) {
       this.deleteSymptomEntry()
diff --git a/components/cycle-day/symptoms/desire.js b/components/cycle-day/symptoms/desire.js
index 5ff9fadc..f28ff85e 100644
--- a/components/cycle-day/symptoms/desire.js
+++ b/components/cycle-day/symptoms/desire.js
@@ -19,7 +19,7 @@ export default class Desire extends SymptomView {
 
   symptomName = 'desire'
 
-  onBackButtonPress() {
+  autoSave = () => {
     if (typeof this.state.currentValue != 'number') {
       this.deleteSymptomEntry()
       return
diff --git a/components/cycle-day/symptoms/mood.js b/components/cycle-day/symptoms/mood.js
index 09f8fbcf..6b78891d 100644
--- a/components/cycle-day/symptoms/mood.js
+++ b/components/cycle-day/symptoms/mood.js
@@ -24,7 +24,7 @@ export default class Mood extends SymptomView {
 
   symptomName = "mood"
 
-  onBackButtonPress() {
+  autoSave = () => {
     const nothingEntered = Object.values(this.state).every(val => !val)
     if (nothingEntered) {
       this.deleteSymptomEntry()
diff --git a/components/cycle-day/symptoms/mucus.js b/components/cycle-day/symptoms/mucus.js
index 2d7b9d19..cc73591f 100644
--- a/components/cycle-day/symptoms/mucus.js
+++ b/components/cycle-day/symptoms/mucus.js
@@ -20,7 +20,7 @@ export default class Mucus extends SymptomView {
 
   symptomName = 'mucus'
 
-  onBackButtonPress() {
+  autoSave = () => {
     const nothingEntered = ['feeling', 'texture'].every(val => typeof this.state[val] != 'number')
     if (nothingEntered) {
       this.deleteSymptomEntry()
diff --git a/components/cycle-day/symptoms/note.js b/components/cycle-day/symptoms/note.js
index c2bed812..e9a2df6e 100644
--- a/components/cycle-day/symptoms/note.js
+++ b/components/cycle-day/symptoms/note.js
@@ -23,7 +23,7 @@ export default class Note extends SymptomView {
 
   symptomName = 'note'
 
-  onBackButtonPress() {
+  autoSave = () => {
     if (!this.state.currentValue) {
       this.deleteSymptomEntry()
       return
diff --git a/components/cycle-day/symptoms/pain.js b/components/cycle-day/symptoms/pain.js
index 30ac3552..fdf3fec0 100644
--- a/components/cycle-day/symptoms/pain.js
+++ b/components/cycle-day/symptoms/pain.js
@@ -26,7 +26,7 @@ export default class Pain extends SymptomView {
 
   symptomName = 'pain'
 
-  onBackButtonPress() {
+  autoSave = () => {
     const nothingEntered = Object.values(this.state).every(val => !val)
     if (nothingEntered) {
       this.deleteSymptomEntry()
diff --git a/components/cycle-day/symptoms/sex.js b/components/cycle-day/symptoms/sex.js
index 9b195da7..140afa31 100644
--- a/components/cycle-day/symptoms/sex.js
+++ b/components/cycle-day/symptoms/sex.js
@@ -26,7 +26,7 @@ export default class Sex extends SymptomView {
 
   symptomName = "sex"
 
-  onBackButtonPress() {
+  autoSave = () => {
     const nothingEntered = Object.values(this.state).every(val => !val)
     if (nothingEntered) {
       this.deleteSymptomEntry()
diff --git a/components/cycle-day/symptoms/symptom-view.js b/components/cycle-day/symptoms/symptom-view.js
index 158b2e6d..793b8ef4 100644
--- a/components/cycle-day/symptoms/symptom-view.js
+++ b/components/cycle-day/symptoms/symptom-view.js
@@ -1,6 +1,6 @@
 import React, { Component } from 'react'
 import {
-  BackHandler, View, Alert, TouchableOpacity
+  View, Alert, TouchableOpacity
 } from 'react-native'
 import { saveSymptom } from '../../../db'
 import InfoPopUp from './info-symptom'
@@ -13,11 +13,6 @@ import styles, { iconStyles } from '../../../styles'
 export default class SymptomView extends Component {
   constructor(props) {
     super()
-    this.backHandler = BackHandler.addEventListener(
-      'hardwareBackPress',
-      this.handleBackButtonPressOnSymptomView.bind(this)
-    )
-    this.globalBackhandler = props.handleBackButtonPress
     this.date = props.date
     this.navigate = props.navigate
     this.state = {
@@ -25,6 +20,11 @@ export default class SymptomView extends Component {
     }
   }
 
+  componentDidUpdate() {
+    this.autoSave()
+  }
+
+  // TODO where is this needed now?
   async handleBackButtonPressOnSymptomView() {
     // every specific symptom view provides their own onBackButtonPress method
     const stopHere = await this.onBackButtonPress()
@@ -39,10 +39,6 @@ export default class SymptomView extends Component {
     saveSymptom(this.symptomName, this.date)
   }
 
-  componentWillUnmount() {
-    this.backHandler.remove()
-  }
-
   isDeleteIconActive() {
     const symptomValueHasBeenFilledOut = key => {
       // the state tracks whether the symptom info should be shown,
@@ -63,6 +59,7 @@ export default class SymptomView extends Component {
         <Header
           title={headerTitles[this.symptomName].toLowerCase()}
           date={this.date}
+          // TODO what to put here instead?
           goBack={this.handleBackButtonPressOnSymptomView.bind(this)}
           deleteIconActive={this.isDeleteIconActive()}
           deleteEntry={() => {
-- 
GitLab