2025-12-17 23:32:18 +11:00
|
|
|
#include "ReaderActivity.h"
|
|
|
|
|
|
|
|
|
|
#include <SD.h>
|
|
|
|
|
|
|
|
|
|
#include "Epub.h"
|
|
|
|
|
#include "EpubReaderActivity.h"
|
|
|
|
|
#include "FileSelectionActivity.h"
|
|
|
|
|
#include "activities/util/FullScreenMessageActivity.h"
|
|
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
std::string ReaderActivity::extractFolderPath(const std::string& filePath) {
|
|
|
|
|
const auto lastSlash = filePath.find_last_of('/');
|
|
|
|
|
if (lastSlash == std::string::npos || lastSlash == 0) {
|
|
|
|
|
return "/";
|
|
|
|
|
}
|
|
|
|
|
return filePath.substr(0, lastSlash);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
std::unique_ptr<Epub> ReaderActivity::loadEpub(const std::string& path) {
|
|
|
|
|
if (!SD.exists(path.c_str())) {
|
|
|
|
|
Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto epub = std::unique_ptr<Epub>(new Epub(path, "/.crosspoint"));
|
|
|
|
|
if (epub->load()) {
|
|
|
|
|
return epub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.printf("[%lu] [ ] Failed to load epub\n", millis());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReaderActivity::onSelectEpubFile(const std::string& path) {
|
2025-12-26 09:55:23 +09:00
|
|
|
currentEpubPath = path; // Track current book path
|
2025-12-17 23:32:18 +11:00
|
|
|
exitActivity();
|
|
|
|
|
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "Loading..."));
|
|
|
|
|
|
|
|
|
|
auto epub = loadEpub(path);
|
|
|
|
|
if (epub) {
|
|
|
|
|
onGoToEpubReader(std::move(epub));
|
|
|
|
|
} else {
|
|
|
|
|
exitActivity();
|
|
|
|
|
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "Failed to load epub", REGULAR,
|
|
|
|
|
EInkDisplay::HALF_REFRESH));
|
|
|
|
|
delay(2000);
|
|
|
|
|
onGoToFileSelection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
void ReaderActivity::onGoToFileSelection(const std::string& fromEpubPath) {
|
2025-12-17 23:32:18 +11:00
|
|
|
exitActivity();
|
2025-12-26 09:55:23 +09:00
|
|
|
// If coming from a book, start in that book's folder; otherwise start from root
|
|
|
|
|
const auto initialPath = fromEpubPath.empty() ? "/" : extractFolderPath(fromEpubPath);
|
2025-12-17 23:32:18 +11:00
|
|
|
enterNewActivity(new FileSelectionActivity(
|
2025-12-26 09:55:23 +09:00
|
|
|
renderer, inputManager, [this](const std::string& path) { onSelectEpubFile(path); }, onGoBack, initialPath));
|
2025-12-17 23:32:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReaderActivity::onGoToEpubReader(std::unique_ptr<Epub> epub) {
|
2025-12-26 09:55:23 +09:00
|
|
|
const auto epubPath = epub->getPath();
|
|
|
|
|
currentEpubPath = epubPath;
|
2025-12-17 23:32:18 +11:00
|
|
|
exitActivity();
|
2025-12-26 09:55:23 +09:00
|
|
|
enterNewActivity(new EpubReaderActivity(
|
|
|
|
|
renderer, inputManager, std::move(epub), [this, epubPath] { onGoToFileSelection(epubPath); },
|
|
|
|
|
[this] { onGoBack(); }));
|
2025-12-17 23:32:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReaderActivity::onEnter() {
|
2025-12-21 21:17:00 +11:00
|
|
|
ActivityWithSubactivity::onEnter();
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
if (initialEpubPath.empty()) {
|
2025-12-26 09:55:23 +09:00
|
|
|
onGoToFileSelection(); // Start from root when entering via Browse
|
2025-12-17 23:32:18 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
currentEpubPath = initialEpubPath;
|
2025-12-17 23:32:18 +11:00
|
|
|
auto epub = loadEpub(initialEpubPath);
|
|
|
|
|
if (!epub) {
|
|
|
|
|
onGoBack();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onGoToEpubReader(std::move(epub));
|
|
|
|
|
}
|