Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect day shift after cycle edit #262

Merged
merged 9 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import "./theme/variables.css";
import { storage } from "./data/Storage";

import type { Cycle } from "./data/ClassCycle";
import { maxOfCycles } from "./state/CalculationLogics";
import { CyclesContext, ThemeContext } from "./state/Context";
import { Menu } from "./modals/Menu";
import { isNewVersionAvailable } from "./data/AppVersion";
Expand Down Expand Up @@ -86,9 +87,9 @@ const App = (props: AppProps) => {
);

function updateCycles(newCycles: Cycle[]) {
const maxOfCycles = 7;
setCycles(newCycles.slice(0, maxOfCycles));
storage.set.cycles(newCycles).catch((err) => console.error(err));
const slicedCycles = newCycles.slice(0, maxOfCycles);
setCycles(slicedCycles);
storage.set.cycles(slicedCycles).catch((err) => console.error(err));
}

function updateTheme(newTheme: string) {
Expand Down
22 changes: 11 additions & 11 deletions src/data/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ export const storage = {
// for use just uncomment one of the following lines:

// _emptyArrayOfCycles().catch((err) => console.error(err));
// _todayPeriod(7).catch((err) => console.error(err));
// _todayOvulation(7).catch((err) => console.error(err));
// _tomorrowOvulation(7).catch((err) => console.error(err));
// _menstrualPhase(7).catch((err) => console.error(err));
// _follicularPhase(7).catch((err) => console.error(err));
// _lutealPhase(7).catch((err) => console.error(err));
// _delayOfCycle(7).catch((err) => console.error(err));
// _randomMenstrualPhase(7).catch((err) => console.error(err));
// _randomFollicularPhase(7).catch((err) => console.error(err));
// _randomLutealPhase(7).catch((err) => console.error(err));
// _randomDelayOfCycle(7).catch((err) => console.error(err));
// _todayPeriod(8).catch((err) => console.error(err));
// _todayOvulation(8).catch((err) => console.error(err));
// _tomorrowOvulation(8).catch((err) => console.error(err));
// _menstrualPhase(8).catch((err) => console.error(err));
// _follicularPhase(8).catch((err) => console.error(err));
// _lutealPhase(8).catch((err) => console.error(err));
// _delayOfCycle(8).catch((err) => console.error(err));
// _randomMenstrualPhase(8).catch((err) => console.error(err));
// _randomFollicularPhase(8).catch((err) => console.error(err));
// _randomLutealPhase(8).catch((err) => console.error(err));
// _randomDelayOfCycle(8).catch((err) => console.error(err));

function _emptyArrayOfCycles(): Promise<void> {
return storageImpl.remove("cycles") as Promise<void>;
Expand Down
50 changes: 25 additions & 25 deletions src/state/CalculationLogics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
import { Cycle } from "../data/ClassCycle";
import { format } from "../utils/datetime";

export const maxOfCycles = 8;
const maxDisplayedCycles = 6;

export function getLastStartDate(cycles: Cycle[]) {
if (cycles.length === 0) {
return "";
Expand Down Expand Up @@ -227,41 +230,37 @@ export function getPhase(cycles: Cycle[]) {
}

export function getAverageLengthOfCycle(cycles: Cycle[]) {
if (cycles.length === 0) {
return 0;
}
const displayedCycles = cycles.slice(0, maxDisplayedCycles);
const length = displayedCycles.length;

if (cycles.length === 1) {
return cycles[0].cycleLength;
if (length <= 1) {
// NOTE: If there is only one cycle in history (the current one), then its length is at least 28 or more
return length === 0 ? 0 : displayedCycles[0].cycleLength;
}

const sum = cycles.reduce((prev, current, idx) => {
if (idx > 0) {
return prev + current.cycleLength;
}
return 0;
}, 0);
const sum = displayedCycles.reduce(
(prev, current) => prev + current.cycleLength,
0,
);

return Math.round(sum / (cycles.length - 1));
//NOTE: We subtract 1 because the length of the current cycle is 0 (i.e. cycles[0].cycleLength = 0) and we don't need to take it into account in the calculation.
return Math.round(sum / (length - 1));
}

export function getAverageLengthOfPeriod(cycles: Cycle[]) {
if (cycles.length === 0) {
return 0;
}
const displayedCycles = cycles.slice(0, maxDisplayedCycles);
const length = displayedCycles.length;

if (cycles.length === 1) {
return cycles[0].periodLength;
if (length <= 1) {
return length === 0 ? 0 : displayedCycles[0].periodLength;
}

const sum = cycles.reduce((prev, current, idx) => {
if (idx > 0) {
return prev + current.periodLength;
}
return 0;
}, 0);
const sum = displayedCycles.reduce(
(prev, current) => prev + current.periodLength,
0,
);

return Math.round(sum / (cycles.length - 1));
return Math.round(sum / length);
}

export function getNewCyclesHistory(periodDays: string[]) {
Expand Down Expand Up @@ -309,9 +308,10 @@ export function getNewCyclesHistory(periodDays: string[]) {
}

export function getPeriodDays(cycles: Cycle[]) {
const maxCountCycles = cycles.slice(0, maxDisplayedCycles);
const periodDays: string[] = [];

for (const cycle of cycles) {
for (const cycle of maxCountCycles) {
const startOfCycle = startOfDay(new Date(cycle.startDate));

for (let i = 0; i < cycle.periodLength; i++) {
Expand Down
36 changes: 36 additions & 0 deletions src/tests/CalculationLogic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ describe("getOvulationStatus", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getOvulationStatus(cycles)).toEqual(
`${i18n.t("in")} 9 ${i18n.t("Days", {
postProcess: "interval",
Expand All @@ -72,6 +74,8 @@ describe("getOvulationStatus", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getOvulationStatus(cycles)).toEqual("tomorrow");
});

Expand All @@ -90,6 +94,8 @@ describe("getOvulationStatus", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getOvulationStatus(cycles)).toEqual("today");
});

Expand All @@ -108,6 +114,8 @@ describe("getOvulationStatus", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getOvulationStatus(cycles)).toEqual("possible");
});

Expand All @@ -126,6 +134,8 @@ describe("getOvulationStatus", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getOvulationStatus(cycles)).toEqual("finished");
});
});
Expand All @@ -150,6 +160,8 @@ describe("getPregnancyChance", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getPregnancyChance(cycles)).toEqual("High");
});

Expand All @@ -167,6 +179,8 @@ describe("getPregnancyChance", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getPregnancyChance(cycles)).toEqual("Low");
});
});
Expand All @@ -192,6 +206,7 @@ describe("getDayOfCycle", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getDayOfCycle(cycles)).toEqual(14);
});
Expand Down Expand Up @@ -311,6 +326,7 @@ describe("getDaysBeforePeriod", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getDaysBeforePeriod(cycles)).toEqual({
title: i18n.t("Period in"),
Expand All @@ -336,6 +352,7 @@ describe("getDaysBeforePeriod", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getDaysBeforePeriod(cycles)).toEqual({
title: i18n.t("Period in"),
Expand All @@ -362,6 +379,7 @@ describe("getDaysBeforePeriod", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getDaysBeforePeriod(cycles)).toEqual({
title: i18n.t("Period"),
Expand Down Expand Up @@ -404,6 +422,7 @@ describe("getDaysBeforePeriod", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getDaysBeforePeriod(cycles)).toEqual({
title: i18n.t("Delay"),
Expand All @@ -429,6 +448,7 @@ describe("getDaysBeforePeriod", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getDaysBeforePeriod(cycles)).toEqual({
title: i18n.t("Delay"),
Expand Down Expand Up @@ -473,6 +493,7 @@ describe("getDaysBeforePeriod", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getDaysBeforePeriod(cycles)).toEqual({
title: i18n.t("Period"),
Expand Down Expand Up @@ -558,6 +579,7 @@ describe("getPhase", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getPhase(cycles)).toEqual(phases.menstrual);
});
Expand All @@ -577,6 +599,7 @@ describe("getPhase", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getPhase(cycles)).toEqual(phases.follicular);
});
Expand All @@ -596,6 +619,7 @@ describe("getPhase", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getPhase(cycles)).toEqual(phases.ovulation);
});
Expand All @@ -615,6 +639,7 @@ describe("getPhase", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getPhase(cycles)).toEqual(phases.luteal);
});
Expand Down Expand Up @@ -801,6 +826,7 @@ describe("getActiveDates", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const dateCheck = addDays(startOfDay(new Date(cycles[0].startDate)), 1);
expect(getActiveDates(dateCheck, cycles)).toEqual(true);
Expand All @@ -821,6 +847,7 @@ describe("getActiveDates", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const dateCheck = addDays(startOfDay(new Date(cycles[0].startDate)), 7);
expect(getActiveDates(dateCheck, cycles)).toEqual(false);
Expand All @@ -841,6 +868,7 @@ describe("getActiveDates", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const dateCheck = addDays(startOfDay(new Date(cycles[0].startDate)), 7);
expect(getActiveDates(dateCheck, cycles)).toEqual(true);
Expand All @@ -861,6 +889,7 @@ describe("getActiveDates", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const dateCheck = addDays(startOfDay(new Date(cycles[0].startDate)), 15);
expect(getActiveDates(dateCheck, cycles)).toEqual(false);
Expand All @@ -881,6 +910,7 @@ describe("getActiveDates", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const dateCheck = addDays(startOfDay(new Date(cycles[0].startDate)), 10);
expect(getActiveDates(dateCheck, cycles)).toEqual(true);
Expand All @@ -901,6 +931,7 @@ describe("getActiveDates", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const dateCheck = addDays(startOfDay(new Date(cycles[0].startDate)), 40);
expect(getActiveDates(dateCheck, cycles)).toEqual(false);
Expand Down Expand Up @@ -936,6 +967,7 @@ describe("getPastFuturePeriodDays", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const periodDates = getPeriodDays(cycles).map((isoDateString) => {
return parseISO(isoDateString).toString();
Expand Down Expand Up @@ -964,6 +996,7 @@ describe("getPastFuturePeriodDays", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const periodDates = getPeriodDays(cycles).map((isoDateString) => {
return parseISO(isoDateString).toString();
Expand Down Expand Up @@ -999,6 +1032,7 @@ describe("getLastStartDate", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getLastStartDate(cycles)).toEqual(cycles[0].startDate);
});
Expand All @@ -1024,6 +1058,7 @@ describe("getLengthOfLastPeriod", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

expect(getLengthOfLastPeriod(cycles)).toEqual(cycles[0].periodLength);
});
Expand Down Expand Up @@ -1051,6 +1086,7 @@ describe("getForecastPeriodDays", () => {
startDate: date.toString(),
});
}
cycles[0].cycleLength = 0;

const forecastDays = [];
let nextCycleStart = addDays(startOfDay(new Date(cycles[0].startDate)), 28);
Expand Down
Loading