diff --git a/src/utils/ReadingLog.ts b/src/utils/ReadingLog.ts index 4176b90..9ddc384 100644 --- a/src/utils/ReadingLog.ts +++ b/src/utils/ReadingLog.ts @@ -8,6 +8,14 @@ export interface ReadingLogEntry { createdAt: Date; } +function isSameDay(a: Date, b: Date): boolean { + return ( + a.getFullYear() === b.getFullYear() && + a.getMonth() === b.getMonth() && + a.getDate() === b.getDate() + ); +} + export class ReadingLog { private entries: ReadingLogEntry[] = []; @@ -77,7 +85,12 @@ export class ReadingLog { createdAt: new Date(), }; - await this.addRawEntry(newEntry); + if (lastEntry && isSameDay(lastEntry.createdAt, newEntry.createdAt)) { + newEntry.pagesRead += lastEntry.pagesRead; + await this.updateEntry(this.entries.indexOf(lastEntry), lastEntry); + } else { + await this.addRawEntry(newEntry); + } } public async addRawEntry(entry: ReadingLogEntry) {