From 714f3865589d54163f406467f28a52b4f36c5cfd Mon Sep 17 00:00:00 2001 From: Jonas Diemer Date: Wed, 17 Dec 2025 11:38:07 +0100 Subject: [PATCH] Moved progress functions to epub, showing integer percentages. --- lib/Epub/Epub.cpp | 13 +++++++++++++ lib/Epub/Epub.h | 3 +++ src/screens/EpubReaderScreen.cpp | 16 +++------------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index f41a8b3..2a47bd3 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -316,3 +316,16 @@ int Epub::getTocIndexForSpineIndex(const int spineIndex) const { Serial.printf("[%lu] [EBP] TOC item not found\n", millis()); return -1; } + +size_t Epub::getBookSize() const { + return getCumulativeSpineItemSize(getSpineItemsCount() - 1); +} + +// Calculate progress in book +uint8_t Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead){ + size_t prevChapterSize = getCumulativeSpineItemSize(currentSpineIndex - 1); + size_t curChapterSize = getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize; + size_t bookSize = getBookSize(); + size_t sectionProgSize = currentSpineRead * curChapterSize; + return round(static_cast(prevChapterSize + sectionProgSize) / bookSize * 100.0); +} diff --git a/lib/Epub/Epub.h b/lib/Epub/Epub.h index d06fa31..1f2dfa9 100644 --- a/lib/Epub/Epub.h +++ b/lib/Epub/Epub.h @@ -58,4 +58,7 @@ class Epub { int getTocItemsCount() const; int getSpineIndexForTocIndex(int tocIndex) const; int getTocIndexForSpineIndex(int spineIndex) const; + + size_t getBookSize() const; + uint8_t calculateProgress(const int currentSpineIndex, const float currentSpineRead); }; diff --git a/src/screens/EpubReaderScreen.cpp b/src/screens/EpubReaderScreen.cpp index df9d712..e4c0969 100644 --- a/src/screens/EpubReaderScreen.cpp +++ b/src/screens/EpubReaderScreen.cpp @@ -326,22 +326,12 @@ void EpubReaderScreen::renderStatusBar() const { constexpr auto textY = 776; // Calculate progress in book - size_t prevChapterSize = epub->getCumulativeSpineItemSize(currentSpineIndex - 1); - size_t curChapterSize = epub->getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize; - size_t bookSize = epub->getCumulativeSpineItemSize(epub->getSpineItemsCount() - 1); - size_t sectionProgSize = - static_cast(static_cast(section->currentPage) / section->pageCount * curChapterSize); - float bookProgress = static_cast(prevChapterSize + sectionProgSize) / bookSize * 100.0; - char bookProgressStr[6] = "--.-"; -std: - snprintf(bookProgressStr, 6, "%.1f", bookProgress); - // Serial.printf("[%lu] [EBP] prevChapterSize: %u bookSize: %u sectionProgSize: %u bookSize:%u Book progress: %s - // %%\n", millis(), - // prevChapterSize, bookSize, sectionProgSize, bookSize, bookProgressStr); + float sectionChapterProg = static_cast(section->currentPage) / section->pageCount; + uint8_t bookProgress = epub->calculateProgress(currentSpineIndex, sectionChapterProg); // Right aligned text for progress counter const std::string progress = std::to_string(section->currentPage + 1) + "/" + std::to_string(section->pageCount) + - " " + bookProgressStr + "%"; + " " + std::to_string(bookProgress) + "%"; const auto progressTextWidth = renderer.getTextWidth(SMALL_FONT_ID, progress.c_str()); renderer.drawText(SMALL_FONT_ID, GfxRenderer::getScreenWidth() - marginRight - progressTextWidth, textY, progress.c_str());