Files
Crosspoint/src/activities/reader/KOReaderSyncActivity.h
T
2026-05-27 15:52:01 -04:00

94 lines
2.9 KiB
C++

#pragma once
#include <Epub.h>
#include <functional>
#include <memory>
#include <optional>
#include "KOReaderSyncClient.h"
#include "ProgressMapper.h"
#include "activities/Activity.h"
/**
* Activity for syncing reading progress with KOReader sync server.
*
* Flow:
* 1. Connect to WiFi (if not connected)
* 2. Calculate document hash
* 3. Fetch remote progress
* 4. Show comparison and options (Apply/Upload)
* 5. Apply or upload progress
*/
class KOReaderSyncActivity final : public Activity {
public:
explicit KOReaderSyncActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& epubPath,
int currentSpineIndex, int currentPage, int totalPagesInSpine,
SavedProgressPosition localKoPos, std::string localChapterName,
std::optional<uint16_t> currentParagraphIndex = std::nullopt)
: Activity("KOReaderSync", renderer, mappedInput),
epubPath(epubPath),
currentSpineIndex(currentSpineIndex),
currentPage(currentPage),
totalPagesInSpine(totalPagesInSpine),
currentParagraphIndex(currentParagraphIndex),
localChapterName(std::move(localChapterName)),
remoteProgress{},
remotePosition{},
localProgress(std::move(localKoPos)) {}
void onEnter() override;
void onExit() override;
void loop() override;
void render(RenderLock&&) override;
bool preventAutoSleep() override { return state == CONNECTING || state == SYNCING; }
private:
enum State {
WIFI_SELECTION,
CONNECTING,
SYNCING,
SHOWING_RESULT,
UPLOADING,
UPLOAD_COMPLETE,
NO_REMOTE_PROGRESS,
SYNC_FAILED,
NO_CREDENTIALS
};
std::shared_ptr<Epub> epub; // null until lazy-loaded after TLS in performSync()
std::string epubPath;
std::string localChapterName;
int currentSpineIndex;
int currentPage;
int totalPagesInSpine;
std::optional<uint16_t> currentParagraphIndex;
State state = WIFI_SELECTION;
std::string statusMessage;
std::string documentHash;
// Remote progress data
bool hasRemoteProgress = false;
KOReaderProgress remoteProgress;
CrossPointPosition remotePosition;
// Local progress as KOReader format (pre-computed before Epub was released)
SavedProgressPosition localProgress;
// Selection in result screen (0=Apply, 1=Upload)
int selectedOption = 0;
// Tracks whether this session activated WiFi. Set in onEnter past the credentials
// check; checked in onExit to decide whether to silent-reboot. Can't rely on
// WiFi.getMode() because performUpload() calls esp_wifi_stop() on the way out,
// which makes WiFi.getMode() return WIFI_MODE_NULL.
bool wifiActivated = false;
void onWifiSelectionComplete(bool success);
void performSync();
void performUpload();
void ensureEpubLoaded();
void saveProgressAndReturn(int spineIndex, int page);
void returnToReader();
};