First attempt

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
jpirnay
2026-04-27 15:30:43 +02:00
co-authored by Copilot
parent 291898bb7f
commit aed297b2b1
11 changed files with 213 additions and 17 deletions
@@ -294,6 +294,7 @@ void EpubReaderActivity::loop() {
// Long press BACK (1s+) goes to home screen
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= ReaderUtils::GO_HOME_MS) {
ReaderUtils::enforceExitFullRefresh(renderer);
if (tryAutoPushOnClose()) return;
onGoHome();
return;
}
@@ -306,6 +307,7 @@ void EpubReaderActivity::loop() {
return;
}
ReaderUtils::enforceExitFullRefresh(renderer);
if (tryAutoPushOnClose()) return;
finish();
return;
}
@@ -318,6 +320,7 @@ void EpubReaderActivity::loop() {
// At end of the book, forward button returns to caller and back button returns to last page
if (currentSpineIndex > 0 && currentSpineIndex >= epub->getSpineItemsCount()) {
if (nextTriggered) {
if (tryAutoPushOnClose()) return;
finish();
} else {
currentSpineIndex = epub->getSpineItemsCount() - 1;
@@ -568,6 +571,7 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
break;
}
case EpubReaderMenuActivity::MenuAction::GO_HOME: {
if (tryAutoPushOnClose()) return;
onGoHome();
return;
}
@@ -831,6 +835,8 @@ void EpubReaderActivity::launchKOReaderSync(const SyncLaunchMode mode) {
syncIntent = KOReaderSyncIntentState::PULL_REMOTE;
} else if (mode == SyncLaunchMode::PUSH_LOCAL) {
syncIntent = KOReaderSyncIntentState::PUSH_LOCAL;
} else if (mode == SyncLaunchMode::AUTO_PUSH) {
syncIntent = KOReaderSyncIntentState::AUTO_PUSH;
}
auto& sync = APP_STATE.koReaderSyncSession;
@@ -865,6 +871,10 @@ void EpubReaderActivity::launchKOReaderSync(const SyncLaunchMode mode) {
sync.resultPage = 0;
sync.resultParagraphIndex = 0;
sync.resultHasParagraphIndex = false;
// Only auto-push-on-close should bypass the reader on resume; explicit syncs from the
// reader menu always come back to the reader. Reset here so a stale flag from a prior
// run cannot steal the user back to home.
sync.exitToHomeAfterSync = (mode == SyncLaunchMode::AUTO_PUSH);
APP_STATE.saveToFile();
LOG_DBG("ERS", "Standalone sync handoff: spine=%d page=%d/%d", currentSpineIndex, currentPage, totalPages);
@@ -872,6 +882,27 @@ void EpubReaderActivity::launchKOReaderSync(const SyncLaunchMode mode) {
activityManager.goToKOReaderSync();
}
bool EpubReaderActivity::tryAutoPushOnClose() {
// Three-page minimum filters out brief inspections — opening to check the cover or
// skim the TOC shouldn't burn a network round-trip. Counter is per-activity-instance.
constexpr int MIN_SESSION_PAGES = 3;
if (!SETTINGS.koSyncOnBookClose) {
return false;
}
if (!KOREADER_STORE.hasCredentials()) {
return false;
}
if (sessionPagesAdvanced < MIN_SESSION_PAGES) {
return false;
}
if (!epub) {
return false;
}
// exitToHomeAfterSync flag is set inside launchKOReaderSync for AUTO_PUSH mode.
launchKOReaderSync(SyncLaunchMode::AUTO_PUSH);
return true;
}
void EpubReaderActivity::applyPendingSyncSession() {
auto& sync = APP_STATE.koReaderSyncSession;
if (!sync.active || !epub || sync.epubPath != epub->getPath()) {
@@ -891,6 +922,17 @@ void EpubReaderActivity::applyPendingSyncSession() {
return;
}
// AUTO_PULL handed off zeroed local state (the reader was not yet running when sync started),
// so on cancel/fail we must NOT restore those zeros to progress.bin — they would clobber the
// user's real local progress. Just clear the session and let the normal startup load progress.bin.
if (sync.intent == KOReaderSyncIntentState::AUTO_PULL && sync.outcome != KOReaderSyncOutcomeState::APPLIED_REMOTE) {
LOG_DBG("ERS", "AUTO_PULL non-success outcome=%d: leaving progress.bin untouched", static_cast<int>(sync.outcome));
sync.clear();
APP_STATE.saveToFile();
logReaderMemSnapshot("after_apply_pending_sync_session");
return;
}
int restoreSpineIndex = sync.spineIndex;
int restorePage = sync.page;
pendingParagraphLookup = sync.hasParagraphIndex;
@@ -1142,6 +1184,9 @@ void EpubReaderActivity::pageTurn(bool isForwardTurn) {
if (!stepPageState(isForwardTurn)) {
return;
}
// Track real progress within this session so auto-push-on-close can ignore brief
// book inspections. Counts both directions — the user is engaging with the book either way.
sessionPagesAdvanced++;
requestUpdate();
}
@@ -1888,6 +1933,7 @@ void EpubReaderActivity::onButtonAction(const CrossPointSettings::BUTTON_ACTION
}
case BA::BTN_EXIT_READER:
ReaderUtils::enforceExitFullRefresh(renderer);
if (tryAutoPushOnClose()) break;
finish();
break;
case BA::BTN_READER_MENU: