diff --git a/src/commands/LogReadingFinishedCommand.ts b/src/commands/LogReadingFinishedCommand.ts index b590a09..1607078 100644 --- a/src/commands/LogReadingFinishedCommand.ts +++ b/src/commands/LogReadingFinishedCommand.ts @@ -56,7 +56,14 @@ export class LogReadingFinishedCommand extends EditorCheckCommand { this.settings.spiceProperty !== "" ); - await this.readingLog.addEntry(fileName, pageCount, pageCount); + 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"); diff --git a/src/commands/LogReadingProgressCommand.ts b/src/commands/LogReadingProgressCommand.ts index 6b8f833..ef4af3f 100644 --- a/src/commands/LogReadingProgressCommand.ts +++ b/src/commands/LogReadingProgressCommand.ts @@ -58,7 +58,15 @@ export class LogReadingProgressCommand extends EditorCheckCommand { return; } - await this.readingLog.addEntry(fileName, pageNumber, pageCount); + 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}.` ); diff --git a/src/utils/ReadingLog.ts b/src/utils/ReadingLog.ts index 6ad094a..4176b90 100644 --- a/src/utils/ReadingLog.ts +++ b/src/utils/ReadingLog.ts @@ -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 { - 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,