Check to make sure new page read is after previous page read total

This commit is contained in:
Evan Fiordeliso 2025-06-30 21:12:36 -04:00
parent 868b7d8cff
commit 7706544710
3 changed files with 27 additions and 6 deletions

View File

@ -56,7 +56,14 @@ export class LogReadingFinishedCommand extends EditorCheckCommand {
this.settings.spiceProperty !== ""
);
try {
await this.readingLog.addEntry(fileName, pageCount, pageCount);
} catch (error) {
new Notice(
`Failed to log reading progress for ${fileName}: ${error}`
);
return;
}
// @ts-expect-error Moment is provided by Obsidian
const endDate = moment().format("YYYY-MM-DD");

View File

@ -58,7 +58,15 @@ export class LogReadingProgressCommand extends EditorCheckCommand {
return;
}
try {
await this.readingLog.addEntry(fileName, pageNumber, pageCount);
} catch (error) {
new Notice(
`Failed to log reading progress for ${fileName}: ${error}`
);
return;
}
new Notice(
`Logged reading progress for ${fileName}: Page ${pageNumber} of ${pageCount}.`
);

View File

@ -44,7 +44,7 @@ export class ReadingLog {
return this.entries;
}
public getLatestEntry(book: string): ReadingLogEntry | null {
public getLastEntry(book: string): ReadingLogEntry | null {
const entriesForBook = this.entries.filter(
(entry) => entry.book === book
);
@ -59,12 +59,18 @@ export class ReadingLog {
pageEnded: number,
pageCount: number
): Promise<void> {
const latestEntry = this.getLatestEntry(book);
const lastEntry = this.getLastEntry(book);
if (lastEntry && lastEntry.pagesReadTotal >= pageEnded) {
throw new Error(
"Given page ended is less than the previous entry's pages read total."
);
}
const newEntry: ReadingLogEntry = {
book,
pagesRead: latestEntry
? pageEnded - latestEntry.pagesReadTotal
pagesRead: lastEntry
? pageEnded - lastEntry.pagesReadTotal
: pageEnded,
pagesReadTotal: pageEnded,
pagesRemaining: pageCount - pageEnded,