Cache reader progress

This commit is contained in:
jpirnay
2026-04-26 23:21:19 +02:00
parent 7e9c953127
commit 5b0a7743d1
6 changed files with 72 additions and 122 deletions
+25 -98
View File
@@ -8,7 +8,6 @@
#include <HalPowerManager.h>
#include <HalStorage.h>
#include <I18n.h>
#include <Serialization.h>
#include <Txt.h>
#include <Xtc.h>
@@ -58,30 +57,6 @@ int clampProgressPercent(const int progressPercent) {
return progressPercent;
}
int readTxtTotalPages(const std::string& cachePath) {
FsFile indexFile;
if (!Storage.openFileForRead("LYR", cachePath + "/index.bin", indexFile)) {
return 0;
}
uint32_t magic = 0;
uint8_t version = 0;
serialization::readPod(indexFile, magic);
serialization::readPod(indexFile, version);
static constexpr uint32_t INDEX_CACHE_MAGIC = 0x54585449; // "TXTI"
static constexpr uint8_t INDEX_CACHE_VERSION = 2;
if (magic != INDEX_CACHE_MAGIC || version != INDEX_CACHE_VERSION) {
indexFile.close();
return 0;
}
indexFile.seek(32);
uint32_t totalPages = 0;
serialization::readPod(indexFile, totalPages);
indexFile.close();
return static_cast<int>(totalPages);
}
void drawLyraBatteryIcon(const GfxRenderer& renderer, int x, int y, int battWidth, int rectHeight,
uint16_t percentage) {
BaseTheme::drawBatteryOutline(renderer, x, y, battWidth, rectHeight);
@@ -152,92 +127,44 @@ const uint8_t* iconForName(UIIcon icon, int size) {
}
} // namespace
// Reads the overall progress percent stored as the last byte of progress.bin.
// The cache path is derived from the book path alone (no epub/xtc/txt loading needed).
// Returns -1 if the file is absent or the percent byte is not yet written.
int LyraTheme::getRecentBookProgressPercent(const RecentBook& book) {
if (book.path.empty()) {
return -1;
}
std::string cachePath;
int percentByteOffset; // byte index of the percent field in progress.bin
if (FsHelpers::hasEpubExtension(book.path)) {
Epub epub(book.path, "/.crosspoint");
if (!epub.load(true, true)) {
return -1;
}
FsFile progressFile;
if (!Storage.openFileForRead("LYR", epub.getCachePath() + "/progress.bin", progressFile)) {
return -1;
}
uint8_t data[6];
const int dataSize = progressFile.read(data, 6);
progressFile.close();
if (dataSize != 4 && dataSize != 6) {
return -1;
}
const int currentSpineIndex = data[0] + (data[1] << 8);
const int currentPage = data[2] + (data[3] << 8);
const int pageCount = (dataSize == 6) ? (data[4] + (data[5] << 8)) : 0;
if (pageCount <= 0) {
return -1;
}
const float chapterProgress = static_cast<float>(currentPage) / static_cast<float>(pageCount);
return clampProgressPercent(
static_cast<int>(std::lround(epub.calculateProgress(currentSpineIndex, chapterProgress) * 100.0f)));
cachePath = Epub(book.path, "/.crosspoint").getCachePath();
percentByteOffset = 6; // epub: [spineIdx(2), page(2), chapterPageCount(2), percent(1)]
} else if (FsHelpers::hasXtcExtension(book.path)) {
cachePath = Xtc(book.path, "/.crosspoint").getCachePath();
percentByteOffset = 4; // xtc: [page(4), percent(1)]
} else if (FsHelpers::hasTxtExtension(book.path) || FsHelpers::hasMarkdownExtension(book.path)) {
cachePath = Txt(book.path, "/.crosspoint").getCachePath();
percentByteOffset = 6; // [page(2), offset(4), percent(1)]
} else {
return -1;
}
if (FsHelpers::hasXtcExtension(book.path)) {
Xtc xtc(book.path, "/.crosspoint");
if (!xtc.load()) {
return -1;
}
FsFile progressFile;
if (!Storage.openFileForRead("LYR", xtc.getCachePath() + "/progress.bin", progressFile)) {
return -1;
}
uint8_t data[4];
if (progressFile.read(data, 4) != 4) {
progressFile.close();
return -1;
}
progressFile.close();
const uint32_t currentPage = static_cast<uint32_t>(data[0]) | (static_cast<uint32_t>(data[1]) << 8) |
(static_cast<uint32_t>(data[2]) << 16) | (static_cast<uint32_t>(data[3]) << 24);
return clampProgressPercent(static_cast<int>(xtc.calculateProgress(currentPage)));
FsFile progressFile;
if (!Storage.openFileForRead("LYR", cachePath + "/progress.bin", progressFile)) {
return -1;
}
if (FsHelpers::hasTxtExtension(book.path) || FsHelpers::hasMarkdownExtension(book.path)) {
Txt txt(book.path, "/.crosspoint");
if (!txt.load()) {
return -1;
}
uint8_t data[7];
const int dataSize = progressFile.read(data, 7);
progressFile.close();
FsFile progressFile;
if (!Storage.openFileForRead("LYR", txt.getCachePath() + "/progress.bin", progressFile)) {
return -1;
}
uint8_t data[4];
if (progressFile.read(data, 4) != 4) {
progressFile.close();
return -1;
}
progressFile.close();
const int currentPage = data[0] + (data[1] << 8);
const int totalPages = readTxtTotalPages(txt.getCachePath());
if (totalPages <= 0) {
return -1;
}
return clampProgressPercent(static_cast<int>(std::lround((currentPage + 1) * 100.0f / totalPages)));
if (dataSize <= percentByteOffset) {
return -1; // old format without percent byte — not yet read by new firmware
}
return -1;
return clampProgressPercent(static_cast<int>(data[percentByteOffset]));
}
void LyraTheme::drawProgressBadge(const GfxRenderer& renderer, Rect anchorRect, int progressPercent) {