From 95b7f8008c73a0ce6a0113d5b2d6864d74fd2652 Mon Sep 17 00:00:00 2001 From: Jonas Diemer Date: Mon, 19 Jan 2026 22:46:45 +0100 Subject: [PATCH] Refactor saving progress to dedicated function. --- src/activities/reader/EpubReaderActivity.cpp | 47 +++++++++----------- src/activities/reader/EpubReaderActivity.h | 1 + 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/activities/reader/EpubReaderActivity.cpp b/src/activities/reader/EpubReaderActivity.cpp index 2d7bbb4..d8a4c6d 100644 --- a/src/activities/reader/EpubReaderActivity.cpp +++ b/src/activities/reader/EpubReaderActivity.cpp @@ -182,31 +182,21 @@ void EpubReaderActivity::loop() { } case EpubReaderMenuActivity::MenuAction::DELETE_CACHE: { xSemaphoreTake(renderingMutex, portMAX_DELAY); - section.reset(); if (epub) { - // 2. BACKUP: Read current progress - // We use the current variables that track our position - uint16_t backupSpine = currentSpineIndex; - uint16_t backupPage = nextPageNumber; + // 2. BACKUP: Read current progress + // We use the current variables that track our position + uint16_t backupSpine = currentSpineIndex; + uint16_t backupPage = section->currentPage; - // 3. WIPE: Clear the cache directory - epub->clearCache(); + section.reset(); + // 3. WIPE: Clear the cache directory + epub->clearCache(); - // 4. RESTORE: Re-setup the directory and rewrite the progress file - epub->setupCacheDir(); + // 4. RESTORE: Re-setup the directory and rewrite the progress file + epub->setupCacheDir(); - FsFile f; - if (SdMan.openFileForWrite("ERS", epub->getCachePath() + "/progress.bin", f)) { - uint8_t data[4]; - data[0] = backupSpine & 0xFF; - data[1] = (backupSpine >> 8) & 0xFF; - data[2] = backupPage & 0xFF; - data[3] = (backupPage >> 8) & 0xFF; - f.write(data, 4); - f.close(); - Serial.println("[ERS] Progress restored after cache clear"); - } - } + saveProgress(backupSpine, backupPage); + } exitActivity(); updateRequired = true; xSemaphoreGive(renderingMutex); @@ -440,19 +430,24 @@ void EpubReaderActivity::renderScreen() { renderContents(std::move(p), orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft); Serial.printf("[%lu] [ERS] Rendered page in %dms\n", millis(), millis() - start); } + saveProgress(currentSpineIndex, section->currentPage); +} +void EpubReaderActivity::saveProgress(int spineIndex, int page) { FsFile f; if (SdMan.openFileForWrite("ERS", epub->getCachePath() + "/progress.bin", f)) { uint8_t data[4]; - data[0] = currentSpineIndex & 0xFF; - data[1] = (currentSpineIndex >> 8) & 0xFF; - data[2] = section->currentPage & 0xFF; - data[3] = (section->currentPage >> 8) & 0xFF; + data[0] = spineIndex & 0xFF; + data[1] = (spineIndex >> 8) & 0xFF; + data[2] = page & 0xFF; + data[3] = (page >> 8) & 0xFF; f.write(data, 4); f.close(); + Serial.printf("[ERS] Progress saved: Chapter %d, Page %d\n", spineIndex, page); + } else { + Serial.printf("[ERS] Could not save progress!\n"); } } - void EpubReaderActivity::renderContents(std::unique_ptr page, const int orientedMarginTop, const int orientedMarginRight, const int orientedMarginBottom, const int orientedMarginLeft) { diff --git a/src/activities/reader/EpubReaderActivity.h b/src/activities/reader/EpubReaderActivity.h index 63d4887..3f245e0 100644 --- a/src/activities/reader/EpubReaderActivity.h +++ b/src/activities/reader/EpubReaderActivity.h @@ -25,6 +25,7 @@ class EpubReaderActivity final : public ActivityWithSubactivity { void renderContents(std::unique_ptr page, int orientedMarginTop, int orientedMarginRight, int orientedMarginBottom, int orientedMarginLeft); void renderStatusBar(int orientedMarginRight, int orientedMarginBottom, int orientedMarginLeft) const; + void saveProgress(int spineIndex, int page); public: explicit EpubReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr epub,