generated from tpl/obsidian-sample-plugin
Check to make sure new page read is after previous page read total
This commit is contained in:
parent
868b7d8cff
commit
7706544710
|
@ -56,7 +56,14 @@ export class LogReadingFinishedCommand extends EditorCheckCommand {
|
||||||
this.settings.spiceProperty !== ""
|
this.settings.spiceProperty !== ""
|
||||||
);
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
await this.readingLog.addEntry(fileName, pageCount, pageCount);
|
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
|
// @ts-expect-error Moment is provided by Obsidian
|
||||||
const endDate = moment().format("YYYY-MM-DD");
|
const endDate = moment().format("YYYY-MM-DD");
|
||||||
|
|
|
@ -58,7 +58,15 @@ export class LogReadingProgressCommand extends EditorCheckCommand {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
await this.readingLog.addEntry(fileName, pageNumber, pageCount);
|
await this.readingLog.addEntry(fileName, pageNumber, pageCount);
|
||||||
|
} catch (error) {
|
||||||
|
new Notice(
|
||||||
|
`Failed to log reading progress for ${fileName}: ${error}`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
new Notice(
|
new Notice(
|
||||||
`Logged reading progress for ${fileName}: Page ${pageNumber} of ${pageCount}.`
|
`Logged reading progress for ${fileName}: Page ${pageNumber} of ${pageCount}.`
|
||||||
);
|
);
|
||||||
|
|
|
@ -44,7 +44,7 @@ export class ReadingLog {
|
||||||
return this.entries;
|
return this.entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getLatestEntry(book: string): ReadingLogEntry | null {
|
public getLastEntry(book: string): ReadingLogEntry | null {
|
||||||
const entriesForBook = this.entries.filter(
|
const entriesForBook = this.entries.filter(
|
||||||
(entry) => entry.book === book
|
(entry) => entry.book === book
|
||||||
);
|
);
|
||||||
|
@ -59,12 +59,18 @@ export class ReadingLog {
|
||||||
pageEnded: number,
|
pageEnded: number,
|
||||||
pageCount: number
|
pageCount: number
|
||||||
): Promise<void> {
|
): 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 = {
|
const newEntry: ReadingLogEntry = {
|
||||||
book,
|
book,
|
||||||
pagesRead: latestEntry
|
pagesRead: lastEntry
|
||||||
? pageEnded - latestEntry.pagesReadTotal
|
? pageEnded - lastEntry.pagesReadTotal
|
||||||
: pageEnded,
|
: pageEnded,
|
||||||
pagesReadTotal: pageEnded,
|
pagesReadTotal: pageEnded,
|
||||||
pagesRemaining: pageCount - pageEnded,
|
pagesRemaining: pageCount - pageEnded,
|
||||||
|
|
Loading…
Reference in New Issue