Finalize implementation

This commit is contained in:
jpirnay
2026-04-13 20:24:28 +02:00
parent 1c3ba11563
commit a03d232dd3
7 changed files with 96 additions and 16 deletions
+16 -4
View File
@@ -39,7 +39,8 @@ void logReaderMemSnapshot(const char* stage) {
LOG_DBG("ERS", "Reader mem[%s]: free=%lu contig=%lu", stage, freeHeap, contigHeap);
}
bool writeReaderProgressCache(const std::string& cachePath, const int spineIndex, const int currentPage, const int pageCount) {
bool writeReaderProgressCache(const std::string& cachePath, const int spineIndex, const int currentPage,
const int pageCount) {
FsFile f;
if (!Storage.openFileForWrite("ERS", cachePath + "/progress.bin", f)) {
LOG_ERR("ERS", "Failed to open progress cache for sync restore: %s", cachePath.c_str());
@@ -566,7 +567,18 @@ void EpubReaderActivity::applyPendingSyncSession() {
return;
}
LOG_DBG("ERS", "Applying pending sync session outcome=%d path=%s", static_cast<int>(sync.outcome), sync.epubPath.c_str());
LOG_DBG("ERS", "Applying pending sync session outcome=%d path=%s", static_cast<int>(sync.outcome),
sync.epubPath.c_str());
// Upload-complete returns to the same local position the reader already persisted
// before sync launched, so there is no need to rewrite progress.bin here.
if (sync.outcome == KOReaderSyncOutcomeState::UPLOAD_COMPLETE) {
LOG_DBG("ERS", "Upload-complete resume keeps existing local progress.bin unchanged");
sync.clear();
APP_STATE.saveToFile();
logReaderMemSnapshot("after_apply_pending_sync_session");
return;
}
int restoreSpineIndex = sync.spineIndex;
int restorePage = sync.page;
@@ -578,8 +590,8 @@ void EpubReaderActivity::applyPendingSyncSession() {
restorePage = sync.resultPage;
pendingParagraphLookup = sync.resultHasParagraphIndex;
pendingParagraphIndex = sync.resultParagraphIndex;
LOG_DBG("ERS", "Applied synced remote position: spine=%d page=%d paragraph=%u hasParagraph=%s",
restoreSpineIndex, restorePage, pendingParagraphIndex, pendingParagraphLookup ? "yes" : "no");
LOG_DBG("ERS", "Applied synced remote position: spine=%d page=%d paragraph=%u hasParagraph=%s", restoreSpineIndex,
restorePage, pendingParagraphIndex, pendingParagraphLookup ? "yes" : "no");
} else {
LOG_DBG("ERS", "Restored local pre-sync position: spine=%d page=%d paragraph=%u hasParagraph=%s", restoreSpineIndex,
restorePage, pendingParagraphIndex, pendingParagraphLookup ? "yes" : "no");
@@ -71,6 +71,10 @@ class EpubReaderActivity final : public Activity {
void jumpToPercent(int percent);
void onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action);
void launchKOReaderSync(SyncLaunchMode mode = SyncLaunchMode::COMPARE);
// Consume a persisted standalone KOReader sync session for this EPUB. Remote
// apply writes the mapped reopen position into progress.bin before the normal
// reader startup path reads it. Upload-complete leaves the existing local
// progress.bin untouched and simply clears the pending session marker.
void applyPendingSyncSession();
void applyOrientation(uint8_t orientation);
void applyTextDarkness(uint8_t textDarkness);
@@ -29,9 +29,10 @@ void logSyncMemSnapshot(const char* stage) {
integrityOk ? "ok" : "fail");
}
// Frees renderer-owned caches right before network work.
// Why: TLS handshake needs a large contiguous block, and font cache memory can
// increase fragmentation even when total free heap looks acceptable.
// Frees renderer-owned caches inside the standalone sync activity right before
// network work. The reader activity is already gone by this point, but sync UI
// rendering (status popups, compare screen, result screen) can repopulate font
// caches and chip away at the largest free block needed for TLS.
void trimMemoryBeforeTls(const GfxRenderer& renderer) {
if (auto* cacheManager = renderer.getFontCacheManager()) {
cacheManager->clearCache();
@@ -330,8 +331,8 @@ void KOReaderSyncActivity::performUpload() {
return;
}
// Result screen rendering repopulates glyph caches; trim again right before
// the upload handshake to maximize contiguous heap for TLS.
// Sync UI rendering can repopulate glyph caches after the initial GET / compare
// phase, so trim again right before the upload request.
trimMemoryBeforeTls(renderer);
logSyncMemSnapshot("after_trim_before_updateProgress");
+7 -2
View File
@@ -4,14 +4,19 @@
#include <memory>
#include "ChapterXPathIndexer.h"
#include "CrossPointState.h"
#include "KOReaderSyncClient.h"
#include "ProgressMapper.h"
#include "CrossPointState.h"
#include "activities/Activity.h"
/**
* Activity for syncing reading progress with KOReader sync server.
*
* This activity is launched as a standalone replacement screen, not as a
* child activity of the reader. The reader persists a compact handoff record,
* is destroyed to reclaim memory before WiFi/TLS work begins, and a fresh
* reader instance is reopened after sync completes or is cancelled.
*
* Shared pipeline:
* 1. Connect to WiFi (if not connected)
* 2. Optionally sync NTP (if stale)
@@ -21,7 +26,7 @@
* - COMPARE: fetch remote progress, show full comparison screen, let user
* choose Apply or Upload.
* - PULL_REMOTE: fetch and map remote progress, show success feedback, then
* return applied SyncResult to reader.
* persist an applied SyncResult for the reopened reader.
* - PUSH_LOCAL: compute local mapping, warm session with GET, then upload via
* reused connection to avoid a second full TLS handshake.
*/