540 Rename method to get previous temperature

This commit is contained in:
Lisa Hillebrand
2022-04-15 17:28:35 +02:00
parent 534f554986
commit fe9d9b4fdc
3 changed files with 192 additions and 153 deletions
+24 -26
View File
@@ -11,22 +11,23 @@ import Segment from '../common/segment'
import { connect } from 'react-redux'
import { getDate } from '../../slices/date'
import { isTemperatureOutOfRange, isPreviousTemperature } from '../helpers/cycle-day'
import {
isTemperatureOutOfRange,
getPreviousTemperature,
} from '../helpers/cycle-day'
import { temperature as labels } from '../../i18n/en/cycle-day'
import { Colors, Containers, Sizes, Spacing } from '../../styles'
const formatTemperature = value => value === null
? value
: Number.parseFloat(value).toFixed(2)
const formatTemperature = (value) =>
value === null ? value : Number.parseFloat(value).toFixed(2)
class Temperature extends Component {
static propTypes = {
data: PropTypes.object,
date: PropTypes.string.isRequired,
save: PropTypes.func
save: PropTypes.func,
}
constructor(props) {
@@ -35,13 +36,13 @@ class Temperature extends Component {
const { data, date } = this.props
const { value } = data
const { shouldShowSuggestion, suggestedTemperature } =
isPreviousTemperature(date)
getPreviousTemperature(date)
this.state = {
isTimePickerVisible: false,
shouldShowSuggestion,
suggestedTemperature: formatTemperature(suggestedTemperature),
value: formatTemperature(value)
value: formatTemperature(value),
}
}
@@ -54,7 +55,7 @@ class Temperature extends Component {
this.setState({
value: value.trim(),
shouldShowSuggestion: false
shouldShowSuggestion: false,
})
}
@@ -80,7 +81,8 @@ class Temperature extends Component {
const { shouldShowSuggestion, suggestedTemperature, value } = this.state
const { time } = this.props.data
const inputStyle = (shouldShowSuggestion && value === null)
const inputStyle =
shouldShowSuggestion && value === null
? { color: Colors.grey }
: { color: Colors.greyDark }
const outOfRangeWarning = isTemperatureOutOfRange(value)
@@ -109,17 +111,17 @@ class Temperature extends Component {
/>
<AppText>°C</AppText>
</View>
{ outOfRangeWarning !== null &&
{outOfRangeWarning !== null && (
<View style={styles.hintContainer}>
<AppText style={styles.hint}>{outOfRangeWarning}</AppText>
</View>
}
)}
</Segment>
<Segment>
<AppText style={styles.title}>{labels.time}</AppText>
<AppTextInput
onFocus={this.onShowTimePicker}
testID='timeInput'
testID="timeInput"
value={time}
/>
<DateTimePicker
@@ -127,7 +129,7 @@ class Temperature extends Component {
mode="time"
onConfirm={this.setTime}
onCancel={this.onCancelTimePicker}
display={Platform.OS === "ios" ? "spinner" : "default"}
display={Platform.OS === 'ios' ? 'spinner' : 'default'}
/>
</Segment>
</React.Fragment>
@@ -137,28 +139,24 @@ class Temperature extends Component {
const styles = StyleSheet.create({
container: {
...Containers.rowContainer
...Containers.rowContainer,
},
hint: {
fontStyle: 'italic',
fontSize: Sizes.small
fontSize: Sizes.small,
},
hintContainer: {
marginVertical: Spacing.tiny
marginVertical: Spacing.tiny,
},
title: {
fontSize: Sizes.subtitle
}
fontSize: Sizes.subtitle,
},
})
const mapStateToProps = (state) => {
return({
return {
date: getDate(state),
})
}
}
export default connect(
mapStateToProps,
null,
)(Temperature)
export default connect(mapStateToProps, null)(Temperature)
+112 -82
View File
@@ -1,6 +1,6 @@
import { ChronoUnit, LocalDate, LocalTime } from 'js-joda'
import { getPreviousTemperature, saveSymptom } from '../../db'
import { getPreviousTemperatureForDate, saveSymptom } from '../../db'
import { scaleObservable } from '../../local-storage'
import * as labels from '../../i18n/en/cycle-day'
@@ -23,15 +23,16 @@ const temperatureLabels = labels.temperature
const minutes = ChronoUnit.MINUTES
const isNumber = (value) => typeof value === 'number'
export const shouldShow = (value) => value !== null ? true : false
export const shouldShow = (value) => (value !== null ? true : false)
export const isPreviousTemperature = (temperature) => {
const previousTemperature = getPreviousTemperature(temperature)
const shouldShowSuggestion = previousTemperature ? true : false
const suggestedTemperature = previousTemperature ?
previousTemperature.toString() : null
export const formatTemperature = (temperature) =>
!temperature
? temperature
: Number.parseFloat(temperature.toString()).toFixed(2)
return { shouldShowSuggestion, suggestedTemperature }
export const getPreviousTemperature = (date) => {
const previousTemperature = getPreviousTemperatureForDate(date)
return formatTemperature(previousTemperature)
}
export const isTemperatureOutOfRange = (temperature) => {
@@ -55,7 +56,7 @@ export const isTemperatureOutOfRange = (temperature) => {
export const blank = {
bleeding: {
exclude: false,
value: null
value: null,
},
cervix: {
exclude: false,
@@ -64,7 +65,7 @@ export const blank = {
position: null,
},
desire: {
value: null
value: null,
},
mood: {
happy: null,
@@ -77,16 +78,16 @@ export const blank = {
fatigue: null,
angry: null,
other: null,
note: null
note: null,
},
mucus: {
exclude: false,
feeling: null,
texture: null,
value: null
value: null,
},
note: {
value: null
value: null,
},
pain: {
cramps: null,
@@ -97,7 +98,7 @@ export const blank = {
tenderBreasts: null,
migraine: null,
other: null,
note: null
note: null,
},
sex: {
solo: null,
@@ -111,14 +112,14 @@ export const blank = {
diaphragm: null,
none: null,
other: null,
note: null
note: null,
},
temperature: {
exclude: false,
note: null,
time: LocalTime.now().truncatedTo(minutes).toString(),
value: null
}
value: null,
},
}
export const symtomPage = {
@@ -126,11 +127,13 @@ export const symtomPage = {
excludeText: labels.bleeding.exclude.explainer,
note: null,
selectBoxGroups: null,
selectTabGroups: [{
selectTabGroups: [
{
key: 'value',
options: getLabelsList(bleedingLabels),
title: labels.bleeding.heaviness.explainer,
}]
},
],
},
cervix: {
excludeText: cervixLabels.excludeExplainer,
@@ -151,18 +154,20 @@ export const symtomPage = {
key: 'position',
options: getLabelsList(cervixLabels.position.categories),
title: cervixLabels.position.explainer,
}
]
},
],
},
desire: {
excludeText: null,
note: null,
selectBoxGroups: null,
selectTabGroups: [{
selectTabGroups: [
{
key: 'value',
options: getLabelsList(intensityLabels),
title: labels.desire.explainer
}]
title: labels.desire.explainer,
},
],
},
mucus: {
excludeText: mucusLabels.excludeExplainer,
@@ -178,34 +183,38 @@ export const symtomPage = {
key: 'texture',
options: getLabelsList(mucusLabels.texture.categories),
title: mucusLabels.texture.explainer,
}
]
},
],
},
mood: {
excludeText: null,
note: null,
selectBoxGroups: [{
selectBoxGroups: [
{
key: 'mood',
options: moodLabels,
title: labels.mood.explainer
}],
selectTabGroups: null
title: labels.mood.explainer,
},
],
selectTabGroups: null,
},
note: {
excludeText: null,
note: noteDescription,
selectBoxGroups: null,
selectTabGroups: null
selectTabGroups: null,
},
pain: {
excludeText: null,
note: null,
selectBoxGroups: [{
selectBoxGroups: [
{
key: 'pain',
options: painLabels,
title: labels.pain.explainer
}],
selectTabGroups: null
title: labels.pain.explainer,
},
],
selectTabGroups: null,
},
sex: {
excludeText: null,
@@ -220,40 +229,42 @@ export const symtomPage = {
key: 'contraceptives',
options: contraceptiveLabels,
title: labels.contraceptives.explainer,
}
},
],
selectTabGroups: null
selectTabGroups: null,
},
temperature: {
excludeText: temperatureLabels.exclude.explainer,
note: temperatureLabels.note.explainer,
selectBoxGroups: null,
selectTabGroups: null
}
selectTabGroups: null,
},
}
export const save = {
bleeding: (data, date, shouldDeleteData) => {
const { exclude, value } = data
const isDataEntered = isNumber(value)
const valuesToSave = shouldDeleteData || !isDataEntered
? null : { value, exclude }
const valuesToSave =
shouldDeleteData || !isDataEntered ? null : { value, exclude }
saveSymptom('bleeding', date, valuesToSave)
},
cervix: (data, date, shouldDeleteData) => {
const { opening, firmness, position, exclude } = data
const isDataEntered = ['opening', 'firmness', 'position'].some(
value => isNumber(data[value]))
const valuesToSave = shouldDeleteData || !isDataEntered
? null : { opening, firmness, position, exclude }
const isDataEntered = ['opening', 'firmness', 'position'].some((value) =>
isNumber(data[value])
)
const valuesToSave =
shouldDeleteData || !isDataEntered
? null
: { opening, firmness, position, exclude }
saveSymptom('cervix', date, valuesToSave)
},
desire: (data, date, shouldDeleteData) => {
const { value } = data
const valuesToSave = shouldDeleteData || !isNumber(value)
? null : { value }
const valuesToSave = shouldDeleteData || !isNumber(value) ? null : { value }
saveSymptom('desire', date, valuesToSave)
},
@@ -262,11 +273,18 @@ export const save = {
},
mucus: (data, date, shouldDeleteData) => {
const { feeling, texture, exclude } = data
const isDataEntered = ['feeling', 'texture'].some(
value => isNumber(data[value]))
const valuesToSave = shouldDeleteData || !isDataEntered
const isDataEntered = ['feeling', 'texture'].some((value) =>
isNumber(data[value])
)
const valuesToSave =
shouldDeleteData || !isDataEntered
? null
: { feeling, texture, value: computeNfpValue(feeling, texture), exclude }
: {
feeling,
texture,
value: computeNfpValue(feeling, texture),
exclude,
}
saveSymptom('mucus', date, valuesToSave)
},
@@ -289,21 +307,20 @@ export const save = {
exclude,
note,
time,
value: Number(value)
value: Number(value),
}
saveSymptom(
'temperature',
date,
(shouldDeleteData || value === null) ? null : valuesToSave
shouldDeleteData || value === null ? null : valuesToSave
)
}
},
}
const saveBoxSymptom = (data, date, shouldDeleteData, symptom) => {
const isDataEntered = Object.keys(data).some(key => data[key] !== null)
const valuesToSave = shouldDeleteData || !isDataEntered
? null : data
const isDataEntered = Object.keys(data).some((key) => data[key] !== null)
const valuesToSave = shouldDeleteData || !isDataEntered ? null : data
saveSymptom(symptom, date, valuesToSave)
}
@@ -327,42 +344,55 @@ const label = {
return temperatureLabel
}
},
mucus: mucus => {
const filledCategories = ['feeling', 'texture'].filter(c => isNumber(mucus[c]))
let label = filledCategories.map(category => {
return labels.mucus.subcategories[category] + ': ' + labels.mucus[category].categories[mucus[category]]
}).join(', ')
mucus: (mucus) => {
const filledCategories = ['feeling', 'texture'].filter((c) =>
isNumber(mucus[c])
)
let label = filledCategories
.map((category) => {
return (
labels.mucus.subcategories[category] +
': ' +
labels.mucus[category].categories[mucus[category]]
)
})
.join(', ')
if (isNumber(mucus.value)) label += `\n => ${labels.mucusNFP[mucus.value]}`
if (mucus.exclude) label = `(${label})`
return label
},
cervix: cervix => {
const filledCategories = ['opening', 'firmness', 'position'].filter(c => isNumber(cervix[c]))
let label = filledCategories.map(category => {
return labels.cervix.subcategories[category] + ': ' + labels.cervix[category].categories[cervix[category]]
}).join(', ')
cervix: (cervix) => {
const filledCategories = ['opening', 'firmness', 'position'].filter((c) =>
isNumber(cervix[c])
)
let label = filledCategories
.map((category) => {
return (
labels.cervix.subcategories[category] +
': ' +
labels.cervix[category].categories[cervix[category]]
)
})
.join(', ')
if (cervix.exclude) label = `(${label})`
return label
},
note: note => note.value,
note: (note) => note.value,
desire: ({ value }) => {
if (isNumber(value)) {
return intensityLabels[value]
}
},
sex: sex => {
sex: (sex) => {
const sexLabel = []
if (sex && Object.values({...sex}).some(val => val)){
Object.keys(sex).forEach(key => {
if (sex && Object.values({ ...sex }).some((val) => val)) {
Object.keys(sex).forEach((key) => {
if (sex[key] && key !== 'other' && key !== 'note') {
sexLabel.push(
sexLabels[key] ||
contraceptiveLabels[key]
)
sexLabel.push(sexLabels[key] || contraceptiveLabels[key])
}
if (key === 'other' && sex.other) {
let label = contraceptiveLabels[key]
@@ -375,10 +405,10 @@ const label = {
return sexLabel.join(', ')
}
},
pain: pain => {
pain: (pain) => {
const painLabel = []
if (pain && Object.values({...pain}).some(val => val)){
Object.keys(pain).forEach(key => {
if (pain && Object.values({ ...pain }).some((val) => val)) {
Object.keys(pain).forEach((key) => {
if (pain[key] && key !== 'other' && key !== 'note') {
painLabel.push(painLabels[key])
}
@@ -393,10 +423,10 @@ const label = {
return painLabel.join(', ')
}
},
mood: mood => {
mood: (mood) => {
const moodLabel = []
if (mood && Object.values({...mood}).some(val => val)){
Object.keys(mood).forEach(key => {
if (mood && Object.values({ ...mood }).some((val) => val)) {
Object.keys(mood).forEach((key) => {
if (mood[key] && key !== 'other' && key !== 'note') {
moodLabel.push(moodLabels[key])
}
@@ -410,7 +440,7 @@ const label = {
})
return moodLabel.join(', ')
}
}
},
}
export const getData = (symptom, symptomData) => {
+30 -19
View File
@@ -36,20 +36,16 @@ export async function openDb (hash) {
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath)
tempConnection.close()
while (nextSchemaIndex < schemas.length - 1) {
const tempConfig = Object.assign(
realmConfig,
schemas[nextSchemaIndex++]
)
const tempConfig = Object.assign(realmConfig, schemas[nextSchemaIndex++])
const migratedRealm = new Realm(tempConfig)
migratedRealm.close()
}
// open the Realm with the latest schema
realmConfig.schema = schemas[schemas.length - 1]
const connection = await Realm.open(Object.assign(
realmConfig,
schemas[schemas.length - 1]
))
const connection = await Realm.open(
Object.assign(realmConfig, schemas[schemas.length - 1])
)
db = connection
const cycle = cycleModule()
@@ -63,17 +59,26 @@ export function closeDb() {
}
export function getBleedingDaysSortedByDate() {
return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
return db
.objects('CycleDay')
.filtered('bleeding != null')
.sorted('date', true)
}
export function getTemperatureDaysSortedByDate() {
return db.objects('CycleDay').filtered('temperature != null').sorted('date', true)
return db
.objects('CycleDay')
.filtered('temperature != null')
.sorted('date', true)
}
export function getCycleDaysSortedByDate() {
return db.objects('CycleDay').sorted('date', true)
}
export function getCycleStartsSortedByDate() {
return db.objects('CycleDay').filtered('isCycleStart = true').sorted('date', true)
return db
.objects('CycleDay')
.filtered('isCycleStart = true')
.sorted('date', true)
}
export function saveSymptom(symptom, date, val) {
let cycleDay = getCycleDay(date)
@@ -83,7 +88,10 @@ export function saveSymptom(symptom, date, val) {
if (symptom === 'bleeding') {
const mensesDaysAfter = getMensesDaysRightAfter(cycleDay)
maybeSetNewCycleStart({
val, cycleDay, mensesDaysAfter, checkIsMensesStart
val,
cycleDay,
mensesDaysAfter,
checkIsMensesStart,
})
} else {
cycleDay[symptom] = val
@@ -93,7 +101,7 @@ export function saveSymptom(symptom, date, val) {
export function updateCycleStartsForAllCycleDays() {
db.write(() => {
getBleedingDaysSortedByDate().forEach(day => {
getBleedingDaysSortedByDate().forEach((day) => {
if (checkIsMensesStart(day)) {
day.isCycleStart = true
}
@@ -106,7 +114,7 @@ export function createCycleDay(dateString) {
db.write(() => {
result = db.create('CycleDay', {
date: dateString,
isCycleStart: false
isCycleStart: false,
})
})
return result
@@ -116,9 +124,9 @@ export function getCycleDay(dateString) {
return db.objectForPrimaryKey('CycleDay', dateString)
}
export function getPreviousTemperature(date) {
export function getPreviousTemperatureForDate(date) {
const targetDate = LocalDate.parse(date)
const winner = getTemperatureDaysSortedByDate().find(candidate => {
const winner = getTemperatureDaysSortedByDate().find((candidate) => {
return LocalDate.parse(candidate.date).isBefore(targetDate)
})
if (!winner) return null
@@ -171,10 +179,13 @@ export function tryToImportWithoutDelete(cycleDays) {
}
export function requestHash(type, pw) {
nodejs.channel.post('request-SHA512', JSON.stringify({
nodejs.channel.post(
'request-SHA512',
JSON.stringify({
type: type,
message: pw
}))
message: pw,
})
)
}
export async function changeEncryptionAndRestartApp(hash) {