diff --git a/lib/Epub/Epub.cpp b/lib/Epub/Epub.cpp index fcd1615..e4b549d 100644 --- a/lib/Epub/Epub.cpp +++ b/lib/Epub/Epub.cpp @@ -318,3 +318,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 6b4855e..3825436 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());