Simplify cycle start detection funtions
This commit is contained in:
+21
-24
@@ -8,7 +8,7 @@ import cycleModule from '../lib/cycle'
|
|||||||
|
|
||||||
let db
|
let db
|
||||||
let isMensesStart
|
let isMensesStart
|
||||||
let getMensesDaysAfter
|
let getMensesDaysRightAfter
|
||||||
|
|
||||||
export async function openDb ({ hash, persistConnection }) {
|
export async function openDb ({ hash, persistConnection }) {
|
||||||
const realmConfig = {}
|
const realmConfig = {}
|
||||||
@@ -37,7 +37,7 @@ export async function openDb ({ hash, persistConnection }) {
|
|||||||
if (persistConnection) db = connection
|
if (persistConnection) db = connection
|
||||||
const cycle = cycleModule()
|
const cycle = cycleModule()
|
||||||
isMensesStart = cycle.isMensesStart
|
isMensesStart = cycle.isMensesStart
|
||||||
getMensesDaysAfter = cycle.getMensesDaysAfter
|
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -57,51 +57,48 @@ export function getCycleStartsSortedByDate() {
|
|||||||
|
|
||||||
export function saveSymptom(symptom, cycleDay, val) {
|
export function saveSymptom(symptom, cycleDay, val) {
|
||||||
db.write(() => {
|
db.write(() => {
|
||||||
if (symptom === 'bleeding') {
|
if (bleedingValueDeleted(symptom, val)) {
|
||||||
saveBleeding(cycleDay, val)
|
cycleDay.bleeding = val
|
||||||
|
cycleDay.isCycleStart = false
|
||||||
|
maybeSetNewCycleStart(cycleDay, val)
|
||||||
|
} else if (bleedingValueAddedOrChanged(symptom, val)) {
|
||||||
|
cycleDay.bleeding = val
|
||||||
|
cycleDay.isCycleStart = isMensesStart(cycleDay)
|
||||||
|
maybeClearOldCycleStarts(cycleDay)
|
||||||
} else {
|
} else {
|
||||||
cycleDay[symptom] = val
|
cycleDay[symptom] = val
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function bleedingValueDeleted(symptom, val) {
|
||||||
|
return symptom === 'bleeding' && !val
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO this also needs a test
|
function bleedingValueAddedOrChanged(symptom, val) {
|
||||||
export function saveBleeding(cycleDay, bleeding) {
|
return symptom === 'bleeding' && val
|
||||||
if (!bleeding) {
|
|
||||||
updateCycleDayAndMaybeSetNewCycleStart(cycleDay, bleeding)
|
|
||||||
} else {
|
|
||||||
cycleDay.bleeding = bleeding
|
|
||||||
cycleDay.isCycleStart = isMensesStart(cycleDay)
|
|
||||||
maybeClearOldCycleStartsInThisMenses(cycleDay)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCycleDayAndMaybeSetNewCycleStart(oldCycleDay, newValue) {
|
function maybeSetNewCycleStart(dayWithDeletedBleeding) {
|
||||||
// if a bleeding value is deleted, we need to check if
|
// if a bleeding value is deleted, we need to check if
|
||||||
// there are any following bleeding days and if the
|
// there are any following bleeding days and if the
|
||||||
// next one of them is now a cycle start
|
// next one of them is now a cycle start
|
||||||
|
const mensesDaysAfter = getMensesDaysRightAfter(dayWithDeletedBleeding)
|
||||||
// in order to get the menses days, the cycle day in question still
|
|
||||||
// has to have a bleeding value, so we get those days first and only
|
|
||||||
// then update the cycle day
|
|
||||||
const mensesDaysAfter = getMensesDaysAfter(oldCycleDay)
|
|
||||||
oldCycleDay.bleeding = newValue
|
|
||||||
oldCycleDay.isCycleStart = false
|
|
||||||
|
|
||||||
if (!mensesDaysAfter.length) return
|
if (!mensesDaysAfter.length) return
|
||||||
|
|
||||||
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
|
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
|
||||||
if (isMensesStart(nextOne)) {
|
if (isMensesStart(nextOne)) {
|
||||||
nextOne.isCycleStart = true
|
nextOne.isCycleStart = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function maybeClearOldCycleStartsInThisMenses(cycleDay) {
|
function maybeClearOldCycleStarts(cycleDay) {
|
||||||
// if we have a new bleeding day, we need to clear the
|
// if we have a new bleeding day, we need to clear the
|
||||||
// menses start marker from all following days of this
|
// menses start marker from all following days of this
|
||||||
// menses that may have been marked as start before
|
// menses that may have been marked as start before
|
||||||
const mensesDaysAfter = getMensesDaysAfter(cycleDay)
|
const mensesDaysAfter = getMensesDaysRightAfter(cycleDay)
|
||||||
mensesDaysAfter.forEach(day => day.isCycleStart = false)
|
mensesDaysAfter.forEach(day => day.isCycleStart = false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getOrCreateCycleDay(localDate) {
|
export function getOrCreateCycleDay(localDate) {
|
||||||
|
|||||||
+1
-33
@@ -177,36 +177,6 @@ export default function config(opts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO this also needs a test
|
|
||||||
function maybeSetNewCycleStart(oldCycleDay, newValue) {
|
|
||||||
// if a bleeding value is deleted, we need to check if
|
|
||||||
// there are any following bleeding days and if the
|
|
||||||
// next one of them is now a cycle start
|
|
||||||
|
|
||||||
// in order to get the menses days, the cycle day in question still
|
|
||||||
// has to have a bleeding value, so we get those days first and only
|
|
||||||
// then update the cycle day
|
|
||||||
const mensesDaysAfter = getMensesDaysRightAfter(oldCycleDay)
|
|
||||||
oldCycleDay.bleeding = newValue
|
|
||||||
oldCycleDay.isCycleStart = false
|
|
||||||
|
|
||||||
if (!mensesDaysAfter.length) return
|
|
||||||
|
|
||||||
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
|
|
||||||
if (isMensesStart(nextOne)) {
|
|
||||||
nextOne.isCycleStart = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function maybeClearOldCycleStartsInThisMenses(cycleDay) {
|
|
||||||
// if we have a new bleeding day, we need to clear the
|
|
||||||
// menses start marker from all following days of this
|
|
||||||
// menses that may have been marked as start before
|
|
||||||
const mensesDaysAfter = getMensesDaysRightAfter(cycleDay)
|
|
||||||
mensesDaysAfter.forEach(day => day.isCycleStart = false)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getCycleDayNumber,
|
getCycleDayNumber,
|
||||||
getCycleForDay,
|
getCycleForDay,
|
||||||
@@ -215,8 +185,6 @@ export default function config(opts) {
|
|||||||
getAllCycleLengths,
|
getAllCycleLengths,
|
||||||
getPredictedMenses,
|
getPredictedMenses,
|
||||||
isMensesStart,
|
isMensesStart,
|
||||||
getMensesDaysRightAfter,
|
getMensesDaysRightAfter
|
||||||
maybeSetNewCycleStart,
|
|
||||||
maybeClearOldCycleStartsInThisMenses
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user