refactor: implement ActivityManager (#1016)
## Summary Ref comment: https://github.com/crosspoint-reader/crosspoint-reader/pull/1010#pullrequestreview-3828854640 This PR introduces `ActivityManager`, which mirrors the same concept of Activity in Android, where an activity represents a single screen of the UI. The manager is responsible for launching activities, and ensuring that only one activity is active at a time. Main differences from Android's ActivityManager: - No concept of Bundle or Intent extras - No onPause/onResume, since we don't have a concept of background activities - onActivityResult is implemented via a callback instead of a separate method, for simplicity ## Key changes - Single `renderTask` shared across all activities - No more sub-activity, we manage them using a stack; Results can be passed via `startActivityForResult` and `setResult` - Activity can call `finish()` to destroy themself, but the actual deletion will be handled by `ActivityManager` to avoid `delete this` pattern As a bonus: the manager will automatically call `requestUpdate()` when returning from another activity ## Example usage **BEFORE**: ```cpp // caller enterNewActivity(new WifiSelectionActivity(renderer, mappedInput, [this](const bool connected) { onWifiSelectionComplete(connected); })); // subactivity onComplete(true); // will eventually call exitActivity(), which deletes the caller instance (dangerous behavior) ``` **AFTER**: (mirrors the `startActivityForResult` and `setResult` from android) ```cpp // caller startActivityForResult(new NetworkModeSelectionActivity(renderer, mappedInput), [this](const ActivityResult& result) { onNetworkModeSelected(result.selectedNetworkMode); }); // subactivity ActivityResult result; result.isCancelled = false; result.selectedNetworkMode = mode; setResult(result); finish(); // signals to ActivityManager to go back to last activity AFTER this function returns ``` TODO: - [x] Reconsider if the `Intent` is really necessary or it should be removed (note: it's inspired by [Intent](https://developer.android.com/guide/components/intents-common) from Android API) ==> I decided to keep this pattern fr clarity - [x] Verify if behavior is still correct (i.e. back from sub-activity) - [x] Refactor the `ActivityWithSubactivity` to just simple `Activity` --> We are using a stack for keeping track of sub-activity now - [x] Use single task for rendering --> avoid allocating 8KB stack per activity - [x] Implement the idea of [Activity result](https://developer.android.com/training/basics/intents/result) --> Allow sub-activity like Wifi to report back the status (connected, failed, etc) --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? **PARTIALLY**, some repetitive migrations are done by Claude, but I'm the one how ultimately approve it --------- Co-authored-by: Zach Nelson <zach@zdnelson.com>
This commit is contained in:
@@ -1,61 +1,28 @@
|
|||||||
#include "Activity.h"
|
#include "Activity.h"
|
||||||
|
|
||||||
#include <HalPowerManager.h>
|
#include "ActivityManager.h"
|
||||||
|
|
||||||
void Activity::renderTaskTrampoline(void* param) {
|
void Activity::onEnter() { LOG_DBG("ACT", "Entering activity: %s", name.c_str()); }
|
||||||
auto* self = static_cast<Activity*>(param);
|
|
||||||
self->renderTaskLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Activity::renderTaskLoop() {
|
void Activity::onExit() { LOG_DBG("ACT", "Exiting activity: %s", name.c_str()); }
|
||||||
while (true) {
|
|
||||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
||||||
{
|
|
||||||
HalPowerManager::Lock powerLock; // Ensure we don't go into low-power mode while rendering
|
|
||||||
RenderLock lock(*this);
|
|
||||||
render(std::move(lock));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Activity::onEnter() {
|
void Activity::requestUpdate(bool immediate) { activityManager.requestUpdate(immediate); }
|
||||||
xTaskCreate(&renderTaskTrampoline, name.c_str(),
|
|
||||||
8192, // Stack size
|
|
||||||
this, // Parameters
|
|
||||||
1, // Priority
|
|
||||||
&renderTaskHandle // Task handle
|
|
||||||
);
|
|
||||||
assert(renderTaskHandle != nullptr && "Failed to create render task");
|
|
||||||
LOG_DBG("ACT", "Entering activity: %s", name.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Activity::onExit() {
|
|
||||||
RenderLock lock(*this); // Ensure we don't delete the task while it's rendering
|
|
||||||
if (renderTaskHandle) {
|
|
||||||
vTaskDelete(renderTaskHandle);
|
|
||||||
renderTaskHandle = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_DBG("ACT", "Exiting activity: %s", name.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Activity::requestUpdate() {
|
|
||||||
// Using direct notification to signal the render task to update
|
|
||||||
// Increment counter so multiple rapid calls won't be lost
|
|
||||||
if (renderTaskHandle) {
|
|
||||||
xTaskNotify(renderTaskHandle, 1, eIncrement);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Activity::requestUpdateAndWait() {
|
void Activity::requestUpdateAndWait() {
|
||||||
// FIXME @ngxson : properly implement this using freeRTOS notification
|
// FIXME @ngxson : properly implement this using freeRTOS notification
|
||||||
|
activityManager.requestUpdate(true);
|
||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderLock
|
void Activity::onGoHome() { activityManager.goHome(); }
|
||||||
|
|
||||||
Activity::RenderLock::RenderLock(Activity& activity) : activity(activity) {
|
void Activity::onSelectBook(const std::string& path) { activityManager.goToReader(path); }
|
||||||
xSemaphoreTake(activity.renderingMutex, portMAX_DELAY);
|
|
||||||
|
void Activity::startActivityForResult(std::unique_ptr<Activity>&& activity, ActivityResultHandler resultHandler) {
|
||||||
|
this->resultHandler = std::move(resultHandler);
|
||||||
|
activityManager.pushActivity(std::move(activity));
|
||||||
}
|
}
|
||||||
|
|
||||||
Activity::RenderLock::~RenderLock() { xSemaphoreGive(activity.renderingMutex); }
|
void Activity::setResult(ActivityResult&& result) { this->result = std::move(result); }
|
||||||
|
|
||||||
|
void Activity::finish() { activityManager.popActivity(); }
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <HardwareSerial.h>
|
|
||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <freertos/FreeRTOS.h>
|
|
||||||
#include <freertos/semphr.h>
|
|
||||||
#include <freertos/task.h>
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "ActivityManager.h" // for using the ActivityManager singleton
|
||||||
|
#include "ActivityResult.h"
|
||||||
#include "GfxRenderer.h"
|
#include "GfxRenderer.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
|
#include "RenderLock.h"
|
||||||
|
|
||||||
class Activity {
|
class Activity {
|
||||||
protected:
|
protected:
|
||||||
@@ -18,44 +18,42 @@ class Activity {
|
|||||||
GfxRenderer& renderer;
|
GfxRenderer& renderer;
|
||||||
MappedInputManager& mappedInput;
|
MappedInputManager& mappedInput;
|
||||||
|
|
||||||
// Task to render and display the activity
|
|
||||||
TaskHandle_t renderTaskHandle = nullptr;
|
|
||||||
[[noreturn]] static void renderTaskTrampoline(void* param);
|
|
||||||
[[noreturn]] virtual void renderTaskLoop();
|
|
||||||
|
|
||||||
// Mutex to protect rendering operations from being deleted mid-render
|
|
||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
ActivityResultHandler resultHandler;
|
||||||
|
ActivityResult result;
|
||||||
|
|
||||||
explicit Activity(std::string name, GfxRenderer& renderer, MappedInputManager& mappedInput)
|
explicit Activity(std::string name, GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
: name(std::move(name)), renderer(renderer), mappedInput(mappedInput), renderingMutex(xSemaphoreCreateMutex()) {
|
: name(std::move(name)), renderer(renderer), mappedInput(mappedInput) {}
|
||||||
assert(renderingMutex != nullptr && "Failed to create rendering mutex");
|
virtual ~Activity() = default;
|
||||||
}
|
|
||||||
virtual ~Activity() {
|
|
||||||
vSemaphoreDelete(renderingMutex);
|
|
||||||
renderingMutex = nullptr;
|
|
||||||
};
|
|
||||||
class RenderLock;
|
|
||||||
virtual void onEnter();
|
virtual void onEnter();
|
||||||
virtual void onExit();
|
virtual void onExit();
|
||||||
virtual void loop() {}
|
virtual void loop() {}
|
||||||
|
|
||||||
virtual void render(RenderLock&&) {}
|
virtual void render(RenderLock&&) {}
|
||||||
virtual void requestUpdate();
|
|
||||||
|
// If immediate is true, the update will be triggered immediately.
|
||||||
|
// Otherwise, it will be deferred until the end of the current loop iteration.
|
||||||
|
virtual void requestUpdate(bool immediate = false);
|
||||||
|
|
||||||
|
// Request an immediate render and block until it completes.
|
||||||
virtual void requestUpdateAndWait();
|
virtual void requestUpdateAndWait();
|
||||||
|
|
||||||
virtual bool skipLoopDelay() { return false; }
|
virtual bool skipLoopDelay() { return false; }
|
||||||
virtual bool preventAutoSleep() { return false; }
|
virtual bool preventAutoSleep() { return false; }
|
||||||
virtual bool isReaderActivity() const { return false; }
|
virtual bool isReaderActivity() const { return false; }
|
||||||
|
|
||||||
// RAII helper to lock rendering mutex for the duration of a scope.
|
// Start a new activity without destroying the current one
|
||||||
class RenderLock {
|
// Note: requestUpdate() will be invoked automatically once resultHandler finishes
|
||||||
Activity& activity;
|
void startActivityForResult(std::unique_ptr<Activity>&& activity, ActivityResultHandler resultHandler);
|
||||||
|
|
||||||
public:
|
// Set the result to be passed back to the previous activity when this activity finishes
|
||||||
explicit RenderLock(Activity& activity);
|
void setResult(ActivityResult&& result);
|
||||||
RenderLock(const RenderLock&) = delete;
|
|
||||||
RenderLock& operator=(const RenderLock&) = delete;
|
// Finish this activity and return to the previous one on the stack (if any)
|
||||||
~RenderLock();
|
void finish();
|
||||||
};
|
|
||||||
|
// Convenience method to facilitate API transition to ActivityManager
|
||||||
|
// TODO: remove this in near future
|
||||||
|
void onGoHome();
|
||||||
|
void onSelectBook(const std::string& path);
|
||||||
};
|
};
|
||||||
|
|||||||
252
src/activities/ActivityManager.cpp
Normal file
252
src/activities/ActivityManager.cpp
Normal file
@@ -0,0 +1,252 @@
|
|||||||
|
#include "ActivityManager.h"
|
||||||
|
|
||||||
|
#include <HalPowerManager.h>
|
||||||
|
|
||||||
|
#include "boot_sleep/BootActivity.h"
|
||||||
|
#include "boot_sleep/SleepActivity.h"
|
||||||
|
#include "browser/OpdsBookBrowserActivity.h"
|
||||||
|
#include "home/HomeActivity.h"
|
||||||
|
#include "home/MyLibraryActivity.h"
|
||||||
|
#include "home/RecentBooksActivity.h"
|
||||||
|
#include "network/CrossPointWebServerActivity.h"
|
||||||
|
#include "reader/ReaderActivity.h"
|
||||||
|
#include "settings/SettingsActivity.h"
|
||||||
|
#include "util/FullScreenMessageActivity.h"
|
||||||
|
|
||||||
|
void ActivityManager::begin() {
|
||||||
|
xTaskCreate(&renderTaskTrampoline, "ActivityManagerRender",
|
||||||
|
8192, // Stack size
|
||||||
|
this, // Parameters
|
||||||
|
1, // Priority
|
||||||
|
&renderTaskHandle // Task handle
|
||||||
|
);
|
||||||
|
assert(renderTaskHandle != nullptr && "Failed to create render task");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::renderTaskTrampoline(void* param) {
|
||||||
|
auto* self = static_cast<ActivityManager*>(param);
|
||||||
|
self->renderTaskLoop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::renderTaskLoop() {
|
||||||
|
while (true) {
|
||||||
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||||
|
// Acquire the lock before reading currentActivity to avoid a TOCTOU race
|
||||||
|
// where the main task deletes the activity between the null-check and render().
|
||||||
|
RenderLock lock;
|
||||||
|
if (currentActivity) {
|
||||||
|
HalPowerManager::Lock powerLock; // Ensure we don't go into low-power mode while rendering
|
||||||
|
currentActivity->render(std::move(lock));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::loop() {
|
||||||
|
if (currentActivity) {
|
||||||
|
// Note: do not hold a lock here, the loop() method must be responsible for acquire one if needed
|
||||||
|
currentActivity->loop();
|
||||||
|
}
|
||||||
|
|
||||||
|
while (pendingAction != PendingAction::None) {
|
||||||
|
if (pendingAction == PendingAction::Pop) {
|
||||||
|
RenderLock lock;
|
||||||
|
|
||||||
|
if (!currentActivity) {
|
||||||
|
// Should never happen in practice
|
||||||
|
LOG_ERR("ACT", "Pop set but currentActivity is null; ignoring pop request");
|
||||||
|
pendingAction = PendingAction::None;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ActivityResult pendingResult = std::move(currentActivity->result);
|
||||||
|
|
||||||
|
// Destroy the current activity
|
||||||
|
exitActivity(lock);
|
||||||
|
pendingAction = PendingAction::None;
|
||||||
|
|
||||||
|
if (stackActivities.empty()) {
|
||||||
|
LOG_DBG("ACT", "No more activities on stack, going home");
|
||||||
|
lock.unlock(); // goHome may acquire its own lock
|
||||||
|
goHome();
|
||||||
|
continue; // Will launch goHome immediately
|
||||||
|
|
||||||
|
} else {
|
||||||
|
currentActivity = std::move(stackActivities.back());
|
||||||
|
stackActivities.pop_back();
|
||||||
|
LOG_DBG("ACT", "Popped from activity stack, new size = %zu", stackActivities.size());
|
||||||
|
// Handle result if necessary
|
||||||
|
if (currentActivity->resultHandler) {
|
||||||
|
LOG_DBG("ACT", "Handling result for popped activity");
|
||||||
|
|
||||||
|
// Move it here to avoid the case where handler calling another startActivityForResult()
|
||||||
|
auto handler = std::move(currentActivity->resultHandler);
|
||||||
|
currentActivity->resultHandler = nullptr;
|
||||||
|
lock.unlock(); // Handler may acquire its own lock
|
||||||
|
handler(pendingResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request an update to ensure the popped activity gets re-rendered
|
||||||
|
if (pendingAction == PendingAction::None) {
|
||||||
|
requestUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler may request another pending action, we will handle it in the next loop iteration
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (pendingActivity) {
|
||||||
|
// Current activity has requested a new activity to be launched
|
||||||
|
RenderLock lock;
|
||||||
|
|
||||||
|
if (pendingAction == PendingAction::Replace) {
|
||||||
|
// Destroy the current activity
|
||||||
|
exitActivity(lock);
|
||||||
|
// Clear the stack
|
||||||
|
while (!stackActivities.empty()) {
|
||||||
|
stackActivities.back()->onExit();
|
||||||
|
stackActivities.pop_back();
|
||||||
|
}
|
||||||
|
} else if (pendingAction == PendingAction::Push) {
|
||||||
|
// Move current activity to stack
|
||||||
|
stackActivities.push_back(std::move(currentActivity));
|
||||||
|
LOG_DBG("ACT", "Pushed to activity stack, new size = %zu", stackActivities.size());
|
||||||
|
}
|
||||||
|
pendingAction = PendingAction::None;
|
||||||
|
currentActivity = std::move(pendingActivity);
|
||||||
|
|
||||||
|
lock.unlock(); // onEnter may acquire its own lock
|
||||||
|
currentActivity->onEnter();
|
||||||
|
|
||||||
|
// onEnter may request another pending action, we will handle it in the next loop iteration
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requestedUpdate) {
|
||||||
|
requestedUpdate = false;
|
||||||
|
// Using direct notification to signal the render task to update
|
||||||
|
// Increment counter so multiple rapid calls won't be lost
|
||||||
|
if (renderTaskHandle) {
|
||||||
|
xTaskNotify(renderTaskHandle, 1, eIncrement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::exitActivity(const RenderLock& lock) {
|
||||||
|
// Note: lock must be held by the caller
|
||||||
|
if (currentActivity) {
|
||||||
|
currentActivity->onExit();
|
||||||
|
currentActivity.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::replaceActivity(std::unique_ptr<Activity>&& newActivity) {
|
||||||
|
// Note: no lock here, this is usually called by loop() and we may run into deadlock
|
||||||
|
if (currentActivity) {
|
||||||
|
// Defer launch if we're currently in an activity, to avoid deleting the current activity
|
||||||
|
// leading to the "delete this" problem
|
||||||
|
pendingActivity = std::move(newActivity);
|
||||||
|
pendingAction = PendingAction::Replace;
|
||||||
|
} else {
|
||||||
|
// No current activity, safe to launch immediately
|
||||||
|
currentActivity = std::move(newActivity);
|
||||||
|
currentActivity->onEnter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goToFileTransfer() {
|
||||||
|
replaceActivity(std::make_unique<CrossPointWebServerActivity>(renderer, mappedInput));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goToSettings() { replaceActivity(std::make_unique<SettingsActivity>(renderer, mappedInput)); }
|
||||||
|
|
||||||
|
void ActivityManager::goToMyLibrary(std::string path) {
|
||||||
|
replaceActivity(std::make_unique<MyLibraryActivity>(renderer, mappedInput, std::move(path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goToRecentBooks() {
|
||||||
|
replaceActivity(std::make_unique<RecentBooksActivity>(renderer, mappedInput));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goToBrowser() {
|
||||||
|
replaceActivity(std::make_unique<OpdsBookBrowserActivity>(renderer, mappedInput));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goToReader(std::string path) {
|
||||||
|
replaceActivity(std::make_unique<ReaderActivity>(renderer, mappedInput, std::move(path)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goToSleep() {
|
||||||
|
replaceActivity(std::make_unique<SleepActivity>(renderer, mappedInput));
|
||||||
|
loop(); // Important: sleep screen must be rendered immediately, the caller will go to sleep right after this returns
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goToBoot() { replaceActivity(std::make_unique<BootActivity>(renderer, mappedInput)); }
|
||||||
|
|
||||||
|
void ActivityManager::goToFullScreenMessage(std::string message, EpdFontFamily::Style style) {
|
||||||
|
replaceActivity(std::make_unique<FullScreenMessageActivity>(renderer, mappedInput, std::move(message), style));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::goHome() { replaceActivity(std::make_unique<HomeActivity>(renderer, mappedInput)); }
|
||||||
|
|
||||||
|
void ActivityManager::pushActivity(std::unique_ptr<Activity>&& activity) {
|
||||||
|
if (pendingActivity) {
|
||||||
|
// Should never happen in practice
|
||||||
|
LOG_ERR("ACT", "pendingActivity while pushActivity is not expected");
|
||||||
|
pendingActivity.reset();
|
||||||
|
}
|
||||||
|
pendingActivity = std::move(activity);
|
||||||
|
pendingAction = PendingAction::Push;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActivityManager::popActivity() {
|
||||||
|
if (pendingActivity) {
|
||||||
|
// Should never happen in practice
|
||||||
|
LOG_ERR("ACT", "pendingActivity while popActivity is not expected");
|
||||||
|
pendingActivity.reset();
|
||||||
|
}
|
||||||
|
pendingAction = PendingAction::Pop;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ActivityManager::preventAutoSleep() const { return currentActivity && currentActivity->preventAutoSleep(); }
|
||||||
|
|
||||||
|
bool ActivityManager::isReaderActivity() const { return currentActivity && currentActivity->isReaderActivity(); }
|
||||||
|
|
||||||
|
bool ActivityManager::skipLoopDelay() const { return currentActivity && currentActivity->skipLoopDelay(); }
|
||||||
|
|
||||||
|
void ActivityManager::requestUpdate(bool immediate) {
|
||||||
|
if (immediate) {
|
||||||
|
if (renderTaskHandle) {
|
||||||
|
xTaskNotify(renderTaskHandle, 1, eIncrement);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Deferring the update until current loop is finished
|
||||||
|
// This is to avoid multiple updates being requested in the same loop
|
||||||
|
requestedUpdate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// RenderLock
|
||||||
|
|
||||||
|
RenderLock::RenderLock() {
|
||||||
|
xSemaphoreTake(activityManager.renderingMutex, portMAX_DELAY);
|
||||||
|
isLocked = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderLock::RenderLock(Activity& /* unused */) {
|
||||||
|
xSemaphoreTake(activityManager.renderingMutex, portMAX_DELAY);
|
||||||
|
isLocked = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderLock::~RenderLock() {
|
||||||
|
if (isLocked) {
|
||||||
|
xSemaphoreGive(activityManager.renderingMutex);
|
||||||
|
isLocked = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderLock::unlock() {
|
||||||
|
if (isLocked) {
|
||||||
|
xSemaphoreGive(activityManager.renderingMutex);
|
||||||
|
isLocked = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
102
src/activities/ActivityManager.h
Normal file
102
src/activities/ActivityManager.h
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/semphr.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "GfxRenderer.h"
|
||||||
|
#include "MappedInputManager.h"
|
||||||
|
#include "RenderLock.h"
|
||||||
|
|
||||||
|
class Activity; // forward declaration
|
||||||
|
class RenderLock; // forward declaration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ActivityManager
|
||||||
|
*
|
||||||
|
* This mirrors the same concept of Activity in Android, where an activity represents a single screen of the UI. The
|
||||||
|
* manager is responsible for launching activities, and ensuring that only one activity is active at a time.
|
||||||
|
*
|
||||||
|
* It also provides a stack mechanism to allow activities to launch sub-activities and get back the results when the
|
||||||
|
* sub-activity is done. For example, the WebServer activity can launch a WifiSelect activity to let the user choose a
|
||||||
|
* wifi network, and get back the selected network when the user is done.
|
||||||
|
*
|
||||||
|
* Main differences from Android's ActivityManager:
|
||||||
|
* - No onPause/onResume, since we don't have a concept of background activities
|
||||||
|
* - onActivityResult is implemented via a callback instead of a separate method, for simplicity
|
||||||
|
*/
|
||||||
|
class ActivityManager {
|
||||||
|
protected:
|
||||||
|
GfxRenderer& renderer;
|
||||||
|
MappedInputManager& mappedInput;
|
||||||
|
std::vector<std::unique_ptr<Activity>> stackActivities;
|
||||||
|
std::unique_ptr<Activity> currentActivity;
|
||||||
|
|
||||||
|
void exitActivity(const RenderLock& lock);
|
||||||
|
|
||||||
|
// Pending activity to be launched on next loop iteration
|
||||||
|
std::unique_ptr<Activity> pendingActivity;
|
||||||
|
enum class PendingAction { None, Push, Pop, Replace };
|
||||||
|
PendingAction pendingAction = PendingAction::None;
|
||||||
|
|
||||||
|
// Task to render and display the activity
|
||||||
|
TaskHandle_t renderTaskHandle = nullptr;
|
||||||
|
static void renderTaskTrampoline(void* param);
|
||||||
|
[[noreturn]] virtual void renderTaskLoop();
|
||||||
|
|
||||||
|
// Whether to trigger a render after the current loop()
|
||||||
|
// This variable must only be set by the main loop, to avoid race conditions
|
||||||
|
bool requestedUpdate = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ActivityManager(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
|
: renderer(renderer), mappedInput(mappedInput), renderingMutex(xSemaphoreCreateMutex()) {
|
||||||
|
assert(renderingMutex != nullptr && "Failed to create rendering mutex");
|
||||||
|
stackActivities.reserve(10);
|
||||||
|
}
|
||||||
|
~ActivityManager() { assert(false); /* should never be called */ };
|
||||||
|
|
||||||
|
// Mutex to protect rendering operations from race conditions
|
||||||
|
// Must only be used via RenderLock
|
||||||
|
SemaphoreHandle_t renderingMutex = nullptr;
|
||||||
|
|
||||||
|
void begin();
|
||||||
|
void loop();
|
||||||
|
|
||||||
|
// Will replace currentActivity and drop all activities on stack
|
||||||
|
void replaceActivity(std::unique_ptr<Activity>&& newActivity);
|
||||||
|
|
||||||
|
// goTo... functions are convenient wrapper for replaceActivity()
|
||||||
|
void goToFileTransfer();
|
||||||
|
void goToSettings();
|
||||||
|
void goToMyLibrary(std::string path = {});
|
||||||
|
void goToRecentBooks();
|
||||||
|
void goToBrowser();
|
||||||
|
void goToReader(std::string path);
|
||||||
|
void goToSleep();
|
||||||
|
void goToBoot();
|
||||||
|
void goToFullScreenMessage(std::string message, EpdFontFamily::Style style = EpdFontFamily::REGULAR);
|
||||||
|
void goHome();
|
||||||
|
|
||||||
|
// This will move current activity to stack instead of deleting it
|
||||||
|
void pushActivity(std::unique_ptr<Activity>&& activity);
|
||||||
|
|
||||||
|
// Remove the currentActivity, returning the last one on stack
|
||||||
|
// Note: if popActivity() on last activity on the stack, we will goHome()
|
||||||
|
void popActivity();
|
||||||
|
|
||||||
|
bool preventAutoSleep() const;
|
||||||
|
bool isReaderActivity() const;
|
||||||
|
bool skipLoopDelay() const;
|
||||||
|
|
||||||
|
// If immediate is true, the update will be triggered immediately.
|
||||||
|
// Otherwise, it will be deferred until the end of the current loop iteration.
|
||||||
|
void requestUpdate(bool immediate = false);
|
||||||
|
};
|
||||||
|
|
||||||
|
extern ActivityManager activityManager; // singleton, to be defined in main.cpp
|
||||||
66
src/activities/ActivityResult.h
Normal file
66
src/activities/ActivityResult.h
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
|
struct WifiResult {
|
||||||
|
bool connected = false;
|
||||||
|
std::string ssid;
|
||||||
|
std::string ip;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct KeyboardResult {
|
||||||
|
std::string text;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MenuResult {
|
||||||
|
int action = -1;
|
||||||
|
uint8_t orientation = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ChapterResult {
|
||||||
|
int spineIndex = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PercentResult {
|
||||||
|
int percent = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PageResult {
|
||||||
|
uint32_t page = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SyncResult {
|
||||||
|
int spineIndex = 0;
|
||||||
|
int page = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class NetworkMode;
|
||||||
|
|
||||||
|
struct NetworkModeResult {
|
||||||
|
NetworkMode mode;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FootnoteResult {
|
||||||
|
std::string href;
|
||||||
|
};
|
||||||
|
|
||||||
|
using ResultVariant = std::variant<std::monostate, WifiResult, KeyboardResult, MenuResult, ChapterResult, PercentResult,
|
||||||
|
PageResult, SyncResult, NetworkModeResult, FootnoteResult>;
|
||||||
|
|
||||||
|
struct ActivityResult {
|
||||||
|
bool isCancelled = false;
|
||||||
|
ResultVariant data;
|
||||||
|
|
||||||
|
explicit ActivityResult() = default;
|
||||||
|
|
||||||
|
template <typename ResultType, typename = std::enable_if_t<std::is_constructible_v<ResultVariant, ResultType&&>>>
|
||||||
|
// cppcheck-suppress noExplicitConstructor
|
||||||
|
ActivityResult(ResultType&& result) : data{std::forward<ResultType>(result)} {}
|
||||||
|
};
|
||||||
|
|
||||||
|
using ActivityResultHandler = std::function<void(const ActivityResult&)>;
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
#include "ActivityWithSubactivity.h"
|
|
||||||
|
|
||||||
#include <HalPowerManager.h>
|
|
||||||
|
|
||||||
void ActivityWithSubactivity::renderTaskLoop() {
|
|
||||||
while (true) {
|
|
||||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
||||||
{
|
|
||||||
HalPowerManager::Lock powerLock; // Ensure we don't go into low-power mode while rendering
|
|
||||||
RenderLock lock(*this);
|
|
||||||
if (!subActivity) {
|
|
||||||
render(std::move(lock));
|
|
||||||
}
|
|
||||||
// If subActivity is set, consume the notification but skip parent render
|
|
||||||
// Note: the sub-activity will call its render() from its own display task
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ActivityWithSubactivity::exitActivity() {
|
|
||||||
// No need to lock, since onExit() already acquires its own lock
|
|
||||||
if (subActivity) {
|
|
||||||
LOG_DBG("ACT", "Exiting subactivity...");
|
|
||||||
subActivity->onExit();
|
|
||||||
subActivity.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ActivityWithSubactivity::enterNewActivity(Activity* activity) {
|
|
||||||
// Acquire lock to avoid 2 activities rendering at the same time during transition
|
|
||||||
RenderLock lock(*this);
|
|
||||||
subActivity.reset(activity);
|
|
||||||
subActivity->onEnter();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ActivityWithSubactivity::loop() {
|
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ActivityWithSubactivity::requestUpdate() {
|
|
||||||
if (!subActivity) {
|
|
||||||
Activity::requestUpdate();
|
|
||||||
}
|
|
||||||
// Sub-activity should call their own requestUpdate() from their loop() function
|
|
||||||
}
|
|
||||||
|
|
||||||
void ActivityWithSubactivity::onExit() {
|
|
||||||
// No need to lock, onExit() already acquires its own lock
|
|
||||||
exitActivity();
|
|
||||||
Activity::onExit();
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "Activity.h"
|
|
||||||
|
|
||||||
class ActivityWithSubactivity : public Activity {
|
|
||||||
protected:
|
|
||||||
std::unique_ptr<Activity> subActivity = nullptr;
|
|
||||||
void exitActivity();
|
|
||||||
void enterNewActivity(Activity* activity);
|
|
||||||
[[noreturn]] void renderTaskLoop() override;
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit ActivityWithSubactivity(std::string name, GfxRenderer& renderer, MappedInputManager& mappedInput)
|
|
||||||
: Activity(std::move(name), renderer, mappedInput) {}
|
|
||||||
void loop() override;
|
|
||||||
// Note: when a subactivity is active, parent requestUpdate() calls are ignored;
|
|
||||||
// the subactivity should request its own renders. This pauses parent rendering until exit.
|
|
||||||
void requestUpdate() override;
|
|
||||||
void onExit() override;
|
|
||||||
};
|
|
||||||
16
src/activities/RenderLock.h
Normal file
16
src/activities/RenderLock.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class Activity; // forward declaration
|
||||||
|
|
||||||
|
// RAII helper to lock rendering mutex for the duration of a scope.
|
||||||
|
class RenderLock {
|
||||||
|
bool isLocked = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit RenderLock();
|
||||||
|
explicit RenderLock(Activity&); // unused for now, but keep for compatibility
|
||||||
|
RenderLock(const RenderLock&) = delete;
|
||||||
|
RenderLock& operator=(const RenderLock&) = delete;
|
||||||
|
~RenderLock();
|
||||||
|
void unlock();
|
||||||
|
};
|
||||||
@@ -21,7 +21,7 @@ constexpr int PAGE_ITEMS = 23;
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::onEnter() {
|
void OpdsBookBrowserActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
state = BrowserState::CHECK_WIFI;
|
state = BrowserState::CHECK_WIFI;
|
||||||
entries.clear();
|
entries.clear();
|
||||||
@@ -37,7 +37,7 @@ void OpdsBookBrowserActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::onExit() {
|
void OpdsBookBrowserActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
// Turn off WiFi when exiting
|
// Turn off WiFi when exiting
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
@@ -49,7 +49,7 @@ void OpdsBookBrowserActivity::onExit() {
|
|||||||
void OpdsBookBrowserActivity::loop() {
|
void OpdsBookBrowserActivity::loop() {
|
||||||
// Handle WiFi selection subactivity
|
// Handle WiFi selection subactivity
|
||||||
if (state == BrowserState::WIFI_SELECTION) {
|
if (state == BrowserState::WIFI_SELECTION) {
|
||||||
ActivityWithSubactivity::loop();
|
// Should already handled by the WifiSelectionActivity
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +136,7 @@ void OpdsBookBrowserActivity::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::render(Activity::RenderLock&&) {
|
void OpdsBookBrowserActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
@@ -279,7 +279,7 @@ void OpdsBookBrowserActivity::navigateToEntry(const OpdsEntry& entry) {
|
|||||||
statusMessage = tr(STR_LOADING);
|
statusMessage = tr(STR_LOADING);
|
||||||
entries.clear();
|
entries.clear();
|
||||||
selectorIndex = 0;
|
selectorIndex = 0;
|
||||||
requestUpdate();
|
requestUpdate(true); // Force update to show loading state immediately before fetch
|
||||||
|
|
||||||
fetchFeed(currentPath);
|
fetchFeed(currentPath);
|
||||||
}
|
}
|
||||||
@@ -308,7 +308,7 @@ void OpdsBookBrowserActivity::downloadBook(const OpdsEntry& book) {
|
|||||||
statusMessage = book.title;
|
statusMessage = book.title;
|
||||||
downloadProgress = 0;
|
downloadProgress = 0;
|
||||||
downloadTotal = 0;
|
downloadTotal = 0;
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
|
|
||||||
// Build full download URL
|
// Build full download URL
|
||||||
std::string downloadUrl = UrlUtils::buildUrl(SETTINGS.opdsServerUrl, book.href);
|
std::string downloadUrl = UrlUtils::buildUrl(SETTINGS.opdsServerUrl, book.href);
|
||||||
@@ -326,7 +326,7 @@ void OpdsBookBrowserActivity::downloadBook(const OpdsEntry& book) {
|
|||||||
HttpDownloader::downloadToFile(downloadUrl, filename, [this](const size_t downloaded, const size_t total) {
|
HttpDownloader::downloadToFile(downloadUrl, filename, [this](const size_t downloaded, const size_t total) {
|
||||||
downloadProgress = downloaded;
|
downloadProgress = downloaded;
|
||||||
downloadTotal = total;
|
downloadTotal = total;
|
||||||
requestUpdate();
|
requestUpdate(true); // Force update to refresh progress bar
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result == HttpDownloader::OK) {
|
if (result == HttpDownloader::OK) {
|
||||||
@@ -364,18 +364,16 @@ void OpdsBookBrowserActivity::launchWifiSelection() {
|
|||||||
state = BrowserState::WIFI_SELECTION;
|
state = BrowserState::WIFI_SELECTION;
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
|
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::onWifiSelectionComplete(const bool connected) {
|
void OpdsBookBrowserActivity::onWifiSelectionComplete(const bool connected) {
|
||||||
exitActivity();
|
|
||||||
|
|
||||||
if (connected) {
|
if (connected) {
|
||||||
LOG_DBG("OPDS", "WiFi connected via selection, fetching feed");
|
LOG_DBG("OPDS", "WiFi connected via selection, fetching feed");
|
||||||
state = BrowserState::LOADING;
|
state = BrowserState::LOADING;
|
||||||
statusMessage = tr(STR_LOADING);
|
statusMessage = tr(STR_LOADING);
|
||||||
requestUpdate();
|
requestUpdate(true); // Force update to show loading state immediately before fetch
|
||||||
fetchFeed(currentPath);
|
fetchFeed(currentPath);
|
||||||
} else {
|
} else {
|
||||||
LOG_DBG("OPDS", "WiFi selection cancelled/failed");
|
LOG_DBG("OPDS", "WiFi selection cancelled/failed");
|
||||||
@@ -385,6 +383,5 @@ void OpdsBookBrowserActivity::onWifiSelectionComplete(const bool connected) {
|
|||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
state = BrowserState::ERROR;
|
state = BrowserState::ERROR;
|
||||||
errorMessage = tr(STR_WIFI_CONN_FAILED);
|
errorMessage = tr(STR_WIFI_CONN_FAILED);
|
||||||
requestUpdate();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../ActivityWithSubactivity.h"
|
#include "../Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
* Supports navigation through catalog hierarchy and downloading EPUBs.
|
* Supports navigation through catalog hierarchy and downloading EPUBs.
|
||||||
* When WiFi connection fails, launches WiFi selection to let user connect.
|
* When WiFi connection fails, launches WiFi selection to let user connect.
|
||||||
*/
|
*/
|
||||||
class OpdsBookBrowserActivity final : public ActivityWithSubactivity {
|
class OpdsBookBrowserActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
enum class BrowserState {
|
enum class BrowserState {
|
||||||
CHECK_WIFI, // Checking WiFi connection
|
CHECK_WIFI, // Checking WiFi connection
|
||||||
@@ -24,14 +24,13 @@ class OpdsBookBrowserActivity final : public ActivityWithSubactivity {
|
|||||||
ERROR // Error state with message
|
ERROR // Error state with message
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit OpdsBookBrowserActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit OpdsBookBrowserActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onGoHome)
|
: Activity("OpdsBookBrowser", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("OpdsBookBrowser", renderer, mappedInput), onGoHome(onGoHome) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
@@ -45,8 +44,6 @@ class OpdsBookBrowserActivity final : public ActivityWithSubactivity {
|
|||||||
size_t downloadProgress = 0;
|
size_t downloadProgress = 0;
|
||||||
size_t downloadTotal = 0;
|
size_t downloadTotal = 0;
|
||||||
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
void checkAndConnectWifi();
|
void checkAndConnectWifi();
|
||||||
void launchWifiSelection();
|
void launchWifiSelection();
|
||||||
void onWifiSelectionComplete(bool connected);
|
void onWifiSelectionComplete(bool connected);
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ void HomeActivity::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HomeActivity::render(Activity::RenderLock&&) {
|
void HomeActivity::render(RenderLock&&) {
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
const auto pageHeight = renderer.getScreenHeight();
|
||||||
@@ -258,3 +258,15 @@ void HomeActivity::render(Activity::RenderLock&&) {
|
|||||||
loadRecentCovers(metrics.homeCoverHeight);
|
loadRecentCovers(metrics.homeCoverHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HomeActivity::onSelectBook(const std::string& path) { activityManager.goToReader(path); }
|
||||||
|
|
||||||
|
void HomeActivity::onMyLibraryOpen() { activityManager.goToMyLibrary(); }
|
||||||
|
|
||||||
|
void HomeActivity::onRecentsOpen() { activityManager.goToRecentBooks(); }
|
||||||
|
|
||||||
|
void HomeActivity::onSettingsOpen() { activityManager.goToSettings(); }
|
||||||
|
|
||||||
|
void HomeActivity::onFileTransferOpen() { activityManager.goToFileTransfer(); }
|
||||||
|
|
||||||
|
void HomeActivity::onOpdsBrowserOpen() { activityManager.goToBrowser(); }
|
||||||
|
|||||||
@@ -20,12 +20,12 @@ class HomeActivity final : public Activity {
|
|||||||
bool coverBufferStored = false; // Track if cover buffer is stored
|
bool coverBufferStored = false; // Track if cover buffer is stored
|
||||||
uint8_t* coverBuffer = nullptr; // HomeActivity's own buffer for cover image
|
uint8_t* coverBuffer = nullptr; // HomeActivity's own buffer for cover image
|
||||||
std::vector<RecentBook> recentBooks;
|
std::vector<RecentBook> recentBooks;
|
||||||
const std::function<void(const std::string& path)> onSelectBook;
|
void onSelectBook(const std::string& path);
|
||||||
const std::function<void()> onMyLibraryOpen;
|
void onMyLibraryOpen();
|
||||||
const std::function<void()> onRecentsOpen;
|
void onRecentsOpen();
|
||||||
const std::function<void()> onSettingsOpen;
|
void onSettingsOpen();
|
||||||
const std::function<void()> onFileTransferOpen;
|
void onFileTransferOpen();
|
||||||
const std::function<void()> onOpdsBrowserOpen;
|
void onOpdsBrowserOpen();
|
||||||
|
|
||||||
int getMenuItemCount() const;
|
int getMenuItemCount() const;
|
||||||
bool storeCoverBuffer(); // Store frame buffer for cover image
|
bool storeCoverBuffer(); // Store frame buffer for cover image
|
||||||
@@ -35,20 +35,10 @@ class HomeActivity final : public Activity {
|
|||||||
void loadRecentCovers(int coverHeight);
|
void loadRecentCovers(int coverHeight);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void(const std::string& path)>& onSelectBook,
|
: Activity("Home", renderer, mappedInput) {}
|
||||||
const std::function<void()>& onMyLibraryOpen, const std::function<void()>& onRecentsOpen,
|
|
||||||
const std::function<void()>& onSettingsOpen, const std::function<void()>& onFileTransferOpen,
|
|
||||||
const std::function<void()>& onOpdsBrowserOpen)
|
|
||||||
: Activity("Home", renderer, mappedInput),
|
|
||||||
onSelectBook(onSelectBook),
|
|
||||||
onMyLibraryOpen(onMyLibraryOpen),
|
|
||||||
onRecentsOpen(onRecentsOpen),
|
|
||||||
onSettingsOpen(onSettingsOpen),
|
|
||||||
onFileTransferOpen(onFileTransferOpen),
|
|
||||||
onOpdsBrowserOpen(onOpdsBrowserOpen) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ std::string getFileName(std::string filename) {
|
|||||||
return filename.substr(0, pos);
|
return filename.substr(0, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyLibraryActivity::render(Activity::RenderLock&&) {
|
void MyLibraryActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|||||||
@@ -17,25 +17,15 @@ class MyLibraryActivity final : public Activity {
|
|||||||
std::string basepath = "/";
|
std::string basepath = "/";
|
||||||
std::vector<std::string> files;
|
std::vector<std::string> files;
|
||||||
|
|
||||||
// Callbacks
|
|
||||||
const std::function<void(const std::string& path)> onSelectBook;
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
// Data loading
|
// Data loading
|
||||||
void loadFiles();
|
void loadFiles();
|
||||||
size_t findEntry(const std::string& name) const;
|
size_t findEntry(const std::string& name) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit MyLibraryActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit MyLibraryActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string initialPath = "/")
|
||||||
const std::function<void()>& onGoHome,
|
: Activity("MyLibrary", renderer, mappedInput), basepath(initialPath.empty() ? "/" : std::move(initialPath)) {}
|
||||||
const std::function<void(const std::string& path)>& onSelectBook,
|
|
||||||
std::string initialPath = "/")
|
|
||||||
: Activity("MyLibrary", renderer, mappedInput),
|
|
||||||
basepath(initialPath.empty() ? "/" : std::move(initialPath)),
|
|
||||||
onSelectBook(onSelectBook),
|
|
||||||
onGoHome(onGoHome) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ void RecentBooksActivity::loop() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void RecentBooksActivity::render(Activity::RenderLock&&) {
|
void RecentBooksActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|||||||
@@ -18,20 +18,14 @@ class RecentBooksActivity final : public Activity {
|
|||||||
// Recent tab state
|
// Recent tab state
|
||||||
std::vector<RecentBook> recentBooks;
|
std::vector<RecentBook> recentBooks;
|
||||||
|
|
||||||
// Callbacks
|
|
||||||
const std::function<void(const std::string& path)> onSelectBook;
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
// Data loading
|
// Data loading
|
||||||
void loadRecentBooks();
|
void loadRecentBooks();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit RecentBooksActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit RecentBooksActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onGoHome,
|
: Activity("RecentBooks", renderer, mappedInput) {}
|
||||||
const std::function<void(const std::string& path)>& onSelectBook)
|
|
||||||
: Activity("RecentBooks", renderer, mappedInput), onSelectBook(onSelectBook), onGoHome(onGoHome) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ constexpr const char* HOSTNAME = "crosspoint";
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void CalibreConnectActivity::onEnter() {
|
void CalibreConnectActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
state = CalibreConnectState::WIFI_SELECTION;
|
state = CalibreConnectState::WIFI_SELECTION;
|
||||||
@@ -32,8 +32,15 @@ void CalibreConnectActivity::onEnter() {
|
|||||||
exitRequested = false;
|
exitRequested = false;
|
||||||
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
if (WiFi.status() != WL_CONNECTED) {
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
[this](const ActivityResult& result) {
|
||||||
|
if (!result.isCancelled) {
|
||||||
|
const auto& wifi = std::get<WifiResult>(result.data);
|
||||||
|
connectedIP = wifi.ip;
|
||||||
|
connectedSSID = wifi.ssid;
|
||||||
|
}
|
||||||
|
onWifiSelectionComplete(!result.isCancelled);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
connectedIP = WiFi.localIP().toString().c_str();
|
connectedIP = WiFi.localIP().toString().c_str();
|
||||||
connectedSSID = WiFi.SSID().c_str();
|
connectedSSID = WiFi.SSID().c_str();
|
||||||
@@ -42,7 +49,7 @@ void CalibreConnectActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CalibreConnectActivity::onExit() {
|
void CalibreConnectActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
stopWebServer();
|
stopWebServer();
|
||||||
MDNS.end();
|
MDNS.end();
|
||||||
@@ -56,18 +63,10 @@ void CalibreConnectActivity::onExit() {
|
|||||||
|
|
||||||
void CalibreConnectActivity::onWifiSelectionComplete(const bool connected) {
|
void CalibreConnectActivity::onWifiSelectionComplete(const bool connected) {
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
exitActivity();
|
activityManager.popActivity();
|
||||||
onComplete();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subActivity) {
|
|
||||||
connectedIP = static_cast<WifiSelectionActivity*>(subActivity.get())->getConnectedIP();
|
|
||||||
} else {
|
|
||||||
connectedIP = WiFi.localIP().toString().c_str();
|
|
||||||
}
|
|
||||||
connectedSSID = WiFi.SSID().c_str();
|
|
||||||
exitActivity();
|
|
||||||
startWebServer();
|
startWebServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,11 +99,6 @@ void CalibreConnectActivity::stopWebServer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CalibreConnectActivity::loop() {
|
void CalibreConnectActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
exitRequested = true;
|
exitRequested = true;
|
||||||
}
|
}
|
||||||
@@ -168,12 +162,12 @@ void CalibreConnectActivity::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (exitRequested) {
|
if (exitRequested) {
|
||||||
onComplete();
|
activityManager.popActivity();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CalibreConnectActivity::render(Activity::RenderLock&&) {
|
void CalibreConnectActivity::render(RenderLock&&) {
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
const auto pageHeight = renderer.getScreenHeight();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
#include "network/CrossPointWebServer.h"
|
#include "network/CrossPointWebServer.h"
|
||||||
|
|
||||||
enum class CalibreConnectState { WIFI_SELECTION, SERVER_STARTING, SERVER_RUNNING, ERROR };
|
enum class CalibreConnectState { WIFI_SELECTION, SERVER_STARTING, SERVER_RUNNING, ERROR };
|
||||||
@@ -13,9 +13,8 @@ enum class CalibreConnectState { WIFI_SELECTION, SERVER_STARTING, SERVER_RUNNING
|
|||||||
* CalibreConnectActivity starts the file transfer server in STA mode,
|
* CalibreConnectActivity starts the file transfer server in STA mode,
|
||||||
* but renders Calibre-specific instructions instead of the web transfer UI.
|
* but renders Calibre-specific instructions instead of the web transfer UI.
|
||||||
*/
|
*/
|
||||||
class CalibreConnectActivity final : public ActivityWithSubactivity {
|
class CalibreConnectActivity final : public Activity {
|
||||||
CalibreConnectState state = CalibreConnectState::WIFI_SELECTION;
|
CalibreConnectState state = CalibreConnectState::WIFI_SELECTION;
|
||||||
const std::function<void()> onComplete;
|
|
||||||
|
|
||||||
std::unique_ptr<CrossPointWebServer> webServer;
|
std::unique_ptr<CrossPointWebServer> webServer;
|
||||||
std::string connectedIP;
|
std::string connectedIP;
|
||||||
@@ -36,13 +35,12 @@ class CalibreConnectActivity final : public ActivityWithSubactivity {
|
|||||||
void stopWebServer();
|
void stopWebServer();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit CalibreConnectActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit CalibreConnectActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onComplete)
|
: Activity("CalibreConnect", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("CalibreConnect", renderer, mappedInput), onComplete(onComplete) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
||||||
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ constexpr uint16_t DNS_PORT = 53;
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void CrossPointWebServerActivity::onEnter() {
|
void CrossPointWebServerActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
LOG_DBG("WEBACT", "Free heap at onEnter: %d bytes", ESP.getFreeHeap());
|
LOG_DBG("WEBACT", "Free heap at onEnter: %d bytes", ESP.getFreeHeap());
|
||||||
|
|
||||||
@@ -48,14 +48,18 @@ void CrossPointWebServerActivity::onEnter() {
|
|||||||
|
|
||||||
// Launch network mode selection subactivity
|
// Launch network mode selection subactivity
|
||||||
LOG_DBG("WEBACT", "Launching NetworkModeSelectionActivity...");
|
LOG_DBG("WEBACT", "Launching NetworkModeSelectionActivity...");
|
||||||
enterNewActivity(new NetworkModeSelectionActivity(
|
startActivityForResult(std::make_unique<NetworkModeSelectionActivity>(renderer, mappedInput),
|
||||||
renderer, mappedInput, [this](const NetworkMode mode) { onNetworkModeSelected(mode); },
|
[this](const ActivityResult& result) {
|
||||||
[this]() { onGoBack(); } // Cancel goes back to home
|
if (result.isCancelled) {
|
||||||
));
|
onGoHome();
|
||||||
|
} else {
|
||||||
|
onNetworkModeSelected(std::get<NetworkModeResult>(result.data).mode);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServerActivity::onExit() {
|
void CrossPointWebServerActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
LOG_DBG("WEBACT", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
LOG_DBG("WEBACT", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
||||||
|
|
||||||
@@ -107,18 +111,20 @@ void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode)
|
|||||||
networkMode = mode;
|
networkMode = mode;
|
||||||
isApMode = (mode == NetworkMode::CREATE_HOTSPOT);
|
isApMode = (mode == NetworkMode::CREATE_HOTSPOT);
|
||||||
|
|
||||||
// Exit mode selection subactivity
|
|
||||||
exitActivity();
|
|
||||||
|
|
||||||
if (mode == NetworkMode::CONNECT_CALIBRE) {
|
if (mode == NetworkMode::CONNECT_CALIBRE) {
|
||||||
exitActivity();
|
startActivityForResult(
|
||||||
enterNewActivity(new CalibreConnectActivity(renderer, mappedInput, [this] {
|
std::make_unique<CalibreConnectActivity>(renderer, mappedInput), [this](const ActivityResult& result) {
|
||||||
exitActivity();
|
state = WebServerActivityState::MODE_SELECTION;
|
||||||
state = WebServerActivityState::MODE_SELECTION;
|
|
||||||
enterNewActivity(new NetworkModeSelectionActivity(
|
startActivityForResult(std::make_unique<NetworkModeSelectionActivity>(renderer, mappedInput),
|
||||||
renderer, mappedInput, [this](const NetworkMode nextMode) { onNetworkModeSelected(nextMode); },
|
[this](const ActivityResult& result) {
|
||||||
[this]() { onGoBack(); }));
|
if (result.isCancelled) {
|
||||||
}));
|
onGoHome();
|
||||||
|
} else {
|
||||||
|
onNetworkModeSelected(std::get<NetworkModeResult>(result.data).mode);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,8 +135,15 @@ void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode)
|
|||||||
|
|
||||||
state = WebServerActivityState::WIFI_SELECTION;
|
state = WebServerActivityState::WIFI_SELECTION;
|
||||||
LOG_DBG("WEBACT", "Launching WifiSelectionActivity...");
|
LOG_DBG("WEBACT", "Launching WifiSelectionActivity...");
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
[this](const ActivityResult& result) {
|
||||||
|
if (!result.isCancelled) {
|
||||||
|
const auto& wifi = std::get<WifiResult>(result.data);
|
||||||
|
connectedIP = wifi.ip;
|
||||||
|
connectedSSID = wifi.ssid;
|
||||||
|
}
|
||||||
|
onWifiSelectionComplete(!result.isCancelled);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// AP mode - start access point
|
// AP mode - start access point
|
||||||
state = WebServerActivityState::AP_STARTING;
|
state = WebServerActivityState::AP_STARTING;
|
||||||
@@ -144,12 +157,8 @@ void CrossPointWebServerActivity::onWifiSelectionComplete(const bool connected)
|
|||||||
|
|
||||||
if (connected) {
|
if (connected) {
|
||||||
// Get connection info before exiting subactivity
|
// Get connection info before exiting subactivity
|
||||||
connectedIP = static_cast<WifiSelectionActivity*>(subActivity.get())->getConnectedIP();
|
|
||||||
connectedSSID = WiFi.SSID().c_str();
|
|
||||||
isApMode = false;
|
isApMode = false;
|
||||||
|
|
||||||
exitActivity();
|
|
||||||
|
|
||||||
// Start mDNS for hostname resolution
|
// Start mDNS for hostname resolution
|
||||||
if (MDNS.begin(AP_HOSTNAME)) {
|
if (MDNS.begin(AP_HOSTNAME)) {
|
||||||
LOG_DBG("WEBACT", "mDNS started: http://%s.local/", AP_HOSTNAME);
|
LOG_DBG("WEBACT", "mDNS started: http://%s.local/", AP_HOSTNAME);
|
||||||
@@ -159,11 +168,16 @@ void CrossPointWebServerActivity::onWifiSelectionComplete(const bool connected)
|
|||||||
startWebServer();
|
startWebServer();
|
||||||
} else {
|
} else {
|
||||||
// User cancelled - go back to mode selection
|
// User cancelled - go back to mode selection
|
||||||
exitActivity();
|
|
||||||
state = WebServerActivityState::MODE_SELECTION;
|
state = WebServerActivityState::MODE_SELECTION;
|
||||||
enterNewActivity(new NetworkModeSelectionActivity(
|
|
||||||
renderer, mappedInput, [this](const NetworkMode mode) { onNetworkModeSelected(mode); },
|
startActivityForResult(std::make_unique<NetworkModeSelectionActivity>(renderer, mappedInput),
|
||||||
[this]() { onGoBack(); }));
|
[this](const ActivityResult& result) {
|
||||||
|
if (result.isCancelled) {
|
||||||
|
onGoHome();
|
||||||
|
} else {
|
||||||
|
onNetworkModeSelected(std::get<NetworkModeResult>(result.data).mode);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +200,7 @@ void CrossPointWebServerActivity::startAccessPoint() {
|
|||||||
|
|
||||||
if (!apStarted) {
|
if (!apStarted) {
|
||||||
LOG_ERR("WEBACT", "ERROR: Failed to start Access Point!");
|
LOG_ERR("WEBACT", "ERROR: Failed to start Access Point!");
|
||||||
onGoBack();
|
onGoHome();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,16 +250,12 @@ void CrossPointWebServerActivity::startWebServer() {
|
|||||||
|
|
||||||
// Force an immediate render since we're transitioning from a subactivity
|
// Force an immediate render since we're transitioning from a subactivity
|
||||||
// that had its own rendering task. We need to make sure our display is shown.
|
// that had its own rendering task. We need to make sure our display is shown.
|
||||||
{
|
requestUpdate();
|
||||||
RenderLock lock(*this);
|
|
||||||
render(std::move(lock));
|
|
||||||
}
|
|
||||||
LOG_DBG("WEBACT", "Rendered File Transfer screen");
|
|
||||||
} else {
|
} else {
|
||||||
LOG_ERR("WEBACT", "ERROR: Failed to start web server!");
|
LOG_ERR("WEBACT", "ERROR: Failed to start web server!");
|
||||||
webServer.reset();
|
webServer.reset();
|
||||||
// Go back on error
|
// Go back on error
|
||||||
onGoBack();
|
onGoHome();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,12 +269,6 @@ void CrossPointWebServerActivity::stopWebServer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServerActivity::loop() {
|
void CrossPointWebServerActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
// Forward loop to subactivity
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle different states
|
// Handle different states
|
||||||
if (state == WebServerActivityState::SERVER_RUNNING) {
|
if (state == WebServerActivityState::SERVER_RUNNING) {
|
||||||
// Handle DNS requests for captive portal (AP mode only)
|
// Handle DNS requests for captive portal (AP mode only)
|
||||||
@@ -322,7 +326,7 @@ void CrossPointWebServerActivity::loop() {
|
|||||||
mappedInput.update();
|
mappedInput.update();
|
||||||
// Check for exit button inside loop for responsiveness
|
// Check for exit button inside loop for responsiveness
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
onGoBack();
|
onGoHome();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -332,13 +336,13 @@ void CrossPointWebServerActivity::loop() {
|
|||||||
|
|
||||||
// Handle exit on Back button (also check outside loop)
|
// Handle exit on Back button (also check outside loop)
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
onGoBack();
|
onGoHome();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServerActivity::render(Activity::RenderLock&&) {
|
void CrossPointWebServerActivity::render(RenderLock&&) {
|
||||||
// Only render our own UI when server is running
|
// Only render our own UI when server is running
|
||||||
// Subactivities handle their own rendering
|
// Subactivities handle their own rendering
|
||||||
if (state == WebServerActivityState::SERVER_RUNNING || state == WebServerActivityState::AP_STARTING) {
|
if (state == WebServerActivityState::SERVER_RUNNING || state == WebServerActivityState::AP_STARTING) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "NetworkModeSelectionActivity.h"
|
#include "NetworkModeSelectionActivity.h"
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
#include "network/CrossPointWebServer.h"
|
#include "network/CrossPointWebServer.h"
|
||||||
|
|
||||||
// Web server activity states
|
// Web server activity states
|
||||||
@@ -27,9 +27,8 @@ enum class WebServerActivityState {
|
|||||||
* - Handles client requests in its loop() function
|
* - Handles client requests in its loop() function
|
||||||
* - Cleans up the server and shuts down WiFi on exit
|
* - Cleans up the server and shuts down WiFi on exit
|
||||||
*/
|
*/
|
||||||
class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
class CrossPointWebServerActivity final : public Activity {
|
||||||
WebServerActivityState state = WebServerActivityState::MODE_SELECTION;
|
WebServerActivityState state = WebServerActivityState::MODE_SELECTION;
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
|
|
||||||
// Network mode
|
// Network mode
|
||||||
NetworkMode networkMode = NetworkMode::JOIN_NETWORK;
|
NetworkMode networkMode = NetworkMode::JOIN_NETWORK;
|
||||||
@@ -54,13 +53,12 @@ class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
|||||||
void stopWebServer();
|
void stopWebServer();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit CrossPointWebServerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit CrossPointWebServerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onGoBack)
|
: Activity("CrossPointWebServer", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("CrossPointWebServer", renderer, mappedInput), onGoBack(onGoBack) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
||||||
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ void NetworkModeSelectionActivity::loop() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkModeSelectionActivity::render(Activity::RenderLock&&) {
|
void NetworkModeSelectionActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
@@ -83,3 +83,15 @@ void NetworkModeSelectionActivity::render(Activity::RenderLock&&) {
|
|||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetworkModeSelectionActivity::onModeSelected(NetworkMode mode) {
|
||||||
|
setResult(NetworkModeResult{mode});
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkModeSelectionActivity::onCancel() {
|
||||||
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "../Activity.h"
|
#include "../Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
// Enum for network mode selection
|
|
||||||
enum class NetworkMode { JOIN_NETWORK, CONNECT_CALIBRE, CREATE_HOTSPOT };
|
enum class NetworkMode { JOIN_NETWORK, CONNECT_CALIBRE, CREATE_HOTSPOT };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -22,16 +21,14 @@ class NetworkModeSelectionActivity final : public Activity {
|
|||||||
|
|
||||||
int selectedIndex = 0;
|
int selectedIndex = 0;
|
||||||
|
|
||||||
const std::function<void(NetworkMode)> onModeSelected;
|
|
||||||
const std::function<void()> onCancel;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit NetworkModeSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit NetworkModeSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void(NetworkMode)>& onModeSelected,
|
: Activity("NetworkModeSelection", renderer, mappedInput) {}
|
||||||
const std::function<void()>& onCancel)
|
|
||||||
: Activity("NetworkModeSelection", renderer, mappedInput), onModeSelected(onModeSelected), onCancel(onCancel) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
|
void onModeSelected(NetworkMode mode);
|
||||||
|
void onCancel();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -190,20 +190,19 @@ void WifiSelectionActivity::selectNetwork(const int index) {
|
|||||||
// Show password entry
|
// Show password entry
|
||||||
state = WifiSelectionState::PASSWORD_ENTRY;
|
state = WifiSelectionState::PASSWORD_ENTRY;
|
||||||
// Don't allow screen updates while changing activity
|
// Don't allow screen updates while changing activity
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
startActivityForResult(
|
||||||
renderer, mappedInput, tr(STR_ENTER_WIFI_PASSWORD),
|
std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_ENTER_WIFI_PASSWORD),
|
||||||
"", // No initial text
|
"", // No initial text
|
||||||
64, // Max password length
|
64, // Max password length
|
||||||
false, // Show password by default (hard keyboard to use)
|
false // Show password by default (hard keyboard to use)
|
||||||
[this](const std::string& text) {
|
),
|
||||||
enteredPassword = text;
|
[this](const ActivityResult& result) {
|
||||||
exitActivity();
|
if (result.isCancelled) {
|
||||||
},
|
state = WifiSelectionState::NETWORK_LIST;
|
||||||
[this] {
|
} else {
|
||||||
state = WifiSelectionState::NETWORK_LIST;
|
enteredPassword = std::get<KeyboardResult>(result.data).text;
|
||||||
exitActivity();
|
}
|
||||||
requestUpdate();
|
});
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
// Connect directly for open networks
|
// Connect directly for open networks
|
||||||
attemptConnection();
|
attemptConnection();
|
||||||
@@ -291,11 +290,6 @@ void WifiSelectionActivity::checkConnectionStatus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WifiSelectionActivity::loop() {
|
void WifiSelectionActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check scan progress
|
// Check scan progress
|
||||||
if (state == WifiSelectionState::SCANNING) {
|
if (state == WifiSelectionState::SCANNING) {
|
||||||
processWifiScanResults();
|
processWifiScanResults();
|
||||||
@@ -467,7 +461,7 @@ std::string WifiSelectionActivity::getSignalStrengthIndicator(const int32_t rssi
|
|||||||
return " |"; // Very weak
|
return " |"; // Very weak
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiSelectionActivity::render(Activity::RenderLock&&) {
|
void WifiSelectionActivity::render(RenderLock&&) {
|
||||||
// Don't render if we're in PASSWORD_ENTRY state - we're just transitioning
|
// Don't render if we're in PASSWORD_ENTRY state - we're just transitioning
|
||||||
// from the keyboard subactivity back to the main activity
|
// from the keyboard subactivity back to the main activity
|
||||||
if (state == WifiSelectionState::PASSWORD_ENTRY) {
|
if (state == WifiSelectionState::PASSWORD_ENTRY) {
|
||||||
@@ -693,3 +687,13 @@ void WifiSelectionActivity::renderForgetPrompt() const {
|
|||||||
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SELECT), tr(STR_DIR_LEFT), tr(STR_DIR_RIGHT));
|
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SELECT), tr(STR_DIR_LEFT), tr(STR_DIR_RIGHT));
|
||||||
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WifiSelectionActivity::onComplete(const bool connected) {
|
||||||
|
ActivityResult result;
|
||||||
|
result.isCancelled = !connected;
|
||||||
|
if (connected) {
|
||||||
|
result.data = WifiResult{true, selectedSSID, connectedIP};
|
||||||
|
}
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
// Structure to hold WiFi network information
|
// Structure to hold WiFi network information
|
||||||
@@ -15,6 +15,7 @@ struct WifiNetworkInfo {
|
|||||||
int32_t rssi;
|
int32_t rssi;
|
||||||
bool isEncrypted;
|
bool isEncrypted;
|
||||||
bool hasSavedPassword; // Whether we have saved credentials for this network
|
bool hasSavedPassword; // Whether we have saved credentials for this network
|
||||||
|
std::string ipAddress; // Populated after connection for display
|
||||||
};
|
};
|
||||||
|
|
||||||
// WiFi selection states
|
// WiFi selection states
|
||||||
@@ -41,13 +42,12 @@ enum class WifiSelectionState {
|
|||||||
*
|
*
|
||||||
* The onComplete callback receives true if connected successfully, false if cancelled.
|
* The onComplete callback receives true if connected successfully, false if cancelled.
|
||||||
*/
|
*/
|
||||||
class WifiSelectionActivity final : public ActivityWithSubactivity {
|
class WifiSelectionActivity final : public Activity {
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
|
|
||||||
WifiSelectionState state = WifiSelectionState::SCANNING;
|
WifiSelectionState state = WifiSelectionState::SCANNING;
|
||||||
size_t selectedNetworkIndex = 0;
|
size_t selectedNetworkIndex = 0;
|
||||||
std::vector<WifiNetworkInfo> networks;
|
std::vector<WifiNetworkInfo> networks;
|
||||||
const std::function<void(bool connected)> onComplete;
|
|
||||||
|
|
||||||
// Selected network for connection
|
// Selected network for connection
|
||||||
std::string selectedSSID;
|
std::string selectedSSID;
|
||||||
@@ -95,17 +95,13 @@ class WifiSelectionActivity final : public ActivityWithSubactivity {
|
|||||||
void checkConnectionStatus();
|
void checkConnectionStatus();
|
||||||
std::string getSignalStrengthIndicator(int32_t rssi) const;
|
std::string getSignalStrengthIndicator(int32_t rssi) const;
|
||||||
|
|
||||||
|
void onComplete(bool connected);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit WifiSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit WifiSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, bool autoConnect = true)
|
||||||
const std::function<void(bool connected)>& onComplete, bool autoConnect = true)
|
: Activity("WifiSelection", renderer, mappedInput), allowAutoConnect(autoConnect) {}
|
||||||
: ActivityWithSubactivity("WifiSelection", renderer, mappedInput),
|
|
||||||
onComplete(onComplete),
|
|
||||||
allowAutoConnect(autoConnect) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
// Get the IP address after successful connection
|
|
||||||
const std::string& getConnectedIP() const { return connectedIP; }
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ void applyReaderOrientation(GfxRenderer& renderer, const uint8_t orientation) {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void EpubReaderActivity::onEnter() {
|
void EpubReaderActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
if (!epub) {
|
if (!epub) {
|
||||||
return;
|
return;
|
||||||
@@ -108,7 +108,7 @@ void EpubReaderActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderActivity::onExit() {
|
void EpubReaderActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
// Reset orientation back to portrait for the rest of the UI
|
// Reset orientation back to portrait for the rest of the UI
|
||||||
renderer.setOrientation(GfxRenderer::Orientation::Portrait);
|
renderer.setOrientation(GfxRenderer::Orientation::Portrait);
|
||||||
@@ -120,48 +120,9 @@ void EpubReaderActivity::onExit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderActivity::loop() {
|
void EpubReaderActivity::loop() {
|
||||||
// Pass input responsibility to sub activity if exists
|
if (!epub) {
|
||||||
if (subActivity) {
|
// Should never happen
|
||||||
subActivity->loop();
|
finish();
|
||||||
// Deferred exit: process after subActivity->loop() returns to avoid use-after-free
|
|
||||||
if (pendingSubactivityExit) {
|
|
||||||
pendingSubactivityExit = false;
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
skipNextButtonCheck = true; // Skip button processing to ignore stale events
|
|
||||||
}
|
|
||||||
// Deferred go home: process after subActivity->loop() returns to avoid race condition
|
|
||||||
if (pendingGoHome) {
|
|
||||||
pendingGoHome = false;
|
|
||||||
exitActivity();
|
|
||||||
if (onGoHome) {
|
|
||||||
onGoHome();
|
|
||||||
}
|
|
||||||
return; // Don't access 'this' after callback
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle pending go home when no subactivity (e.g., from long press back)
|
|
||||||
if (pendingGoHome) {
|
|
||||||
pendingGoHome = false;
|
|
||||||
if (onGoHome) {
|
|
||||||
onGoHome();
|
|
||||||
}
|
|
||||||
return; // Don't access 'this' after callback
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip button processing after returning from subactivity
|
|
||||||
// This prevents stale button release events from triggering actions
|
|
||||||
// We wait until: (1) all relevant buttons are released, AND (2) wasReleased events have been cleared
|
|
||||||
if (skipNextButtonCheck) {
|
|
||||||
const bool confirmCleared = !mappedInput.isPressed(MappedInputManager::Button::Confirm) &&
|
|
||||||
!mappedInput.wasReleased(MappedInputManager::Button::Confirm);
|
|
||||||
const bool backCleared = !mappedInput.isPressed(MappedInputManager::Button::Back) &&
|
|
||||||
!mappedInput.wasReleased(MappedInputManager::Button::Back);
|
|
||||||
if (confirmCleared && backCleared) {
|
|
||||||
skipNextButtonCheck = false;
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,22 +131,27 @@ void EpubReaderActivity::loop() {
|
|||||||
const int currentPage = section ? section->currentPage + 1 : 0;
|
const int currentPage = section ? section->currentPage + 1 : 0;
|
||||||
const int totalPages = section ? section->pageCount : 0;
|
const int totalPages = section ? section->pageCount : 0;
|
||||||
float bookProgress = 0.0f;
|
float bookProgress = 0.0f;
|
||||||
if (epub && epub->getBookSize() > 0 && section && section->pageCount > 0) {
|
if (epub->getBookSize() > 0 && section && section->pageCount > 0) {
|
||||||
const float chapterProgress = static_cast<float>(section->currentPage) / static_cast<float>(section->pageCount);
|
const float chapterProgress = static_cast<float>(section->currentPage) / static_cast<float>(section->pageCount);
|
||||||
bookProgress = epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
|
bookProgress = epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
|
||||||
}
|
}
|
||||||
const int bookProgressPercent = clampPercent(static_cast<int>(bookProgress + 0.5f));
|
const int bookProgressPercent = clampPercent(static_cast<int>(bookProgress + 0.5f));
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<EpubReaderMenuActivity>(
|
||||||
enterNewActivity(new EpubReaderMenuActivity(
|
renderer, mappedInput, epub->getTitle(), currentPage, totalPages, bookProgressPercent,
|
||||||
this->renderer, this->mappedInput, epub->getTitle(), currentPage, totalPages, bookProgressPercent,
|
SETTINGS.orientation, !currentPageFootnotes.empty()),
|
||||||
SETTINGS.orientation, !currentPageFootnotes.empty(),
|
[this](const ActivityResult& result) {
|
||||||
[this](const uint8_t orientation) { onReaderMenuBack(orientation); },
|
// Always apply orientation change even if the menu was cancelled
|
||||||
[this](EpubReaderMenuActivity::MenuAction action) { onReaderMenuConfirm(action); }));
|
const auto& menu = std::get<MenuResult>(result.data);
|
||||||
|
applyOrientation(menu.orientation);
|
||||||
|
if (!result.isCancelled) {
|
||||||
|
onReaderMenuConfirm(static_cast<EpubReaderMenuActivity::MenuAction>(menu.action));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Long press BACK (1s+) goes to file selection
|
// Long press BACK (1s+) goes to file selection
|
||||||
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
||||||
onGoBack();
|
activityManager.goToMyLibrary(epub ? epub->getPath() : "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,14 +240,6 @@ void EpubReaderActivity::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderActivity::onReaderMenuBack(const uint8_t orientation) {
|
|
||||||
exitActivity();
|
|
||||||
// Apply the user-selected orientation when the menu is dismissed.
|
|
||||||
// This ensures the menu can be navigated without immediately rotating the screen.
|
|
||||||
applyOrientation(orientation);
|
|
||||||
requestUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Translate an absolute percent into a spine index plus a normalized position
|
// Translate an absolute percent into a spine index plus a normalized position
|
||||||
// within that spine so we can jump after the section is loaded.
|
// within that spine so we can jump after the section is loaded.
|
||||||
void EpubReaderActivity::jumpToPercent(int percent) {
|
void EpubReaderActivity::jumpToPercent(int percent) {
|
||||||
@@ -348,82 +306,44 @@ void EpubReaderActivity::jumpToPercent(int percent) {
|
|||||||
void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action) {
|
void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action) {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case EpubReaderMenuActivity::MenuAction::SELECT_CHAPTER: {
|
case EpubReaderMenuActivity::MenuAction::SELECT_CHAPTER: {
|
||||||
// Calculate values BEFORE we start destroying things
|
|
||||||
const int currentP = section ? section->currentPage : 0;
|
|
||||||
const int totalP = section ? section->pageCount : 0;
|
|
||||||
const int spineIdx = currentSpineIndex;
|
const int spineIdx = currentSpineIndex;
|
||||||
const std::string path = epub->getPath();
|
const std::string path = epub->getPath();
|
||||||
|
startActivityForResult(
|
||||||
// 1. Close the menu
|
std::make_unique<EpubReaderChapterSelectionActivity>(renderer, mappedInput, epub, path, spineIdx),
|
||||||
exitActivity();
|
[this](const ActivityResult& result) {
|
||||||
|
if (!result.isCancelled && currentSpineIndex != std::get<ChapterResult>(result.data).spineIndex) {
|
||||||
// 2. Open the Chapter Selector
|
currentSpineIndex = std::get<ChapterResult>(result.data).spineIndex;
|
||||||
enterNewActivity(new EpubReaderChapterSelectionActivity(
|
|
||||||
this->renderer, this->mappedInput, epub, path, spineIdx, currentP, totalP,
|
|
||||||
[this] {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
},
|
|
||||||
[this](const int newSpineIndex) {
|
|
||||||
if (currentSpineIndex != newSpineIndex) {
|
|
||||||
currentSpineIndex = newSpineIndex;
|
|
||||||
nextPageNumber = 0;
|
nextPageNumber = 0;
|
||||||
section.reset();
|
section.reset();
|
||||||
}
|
}
|
||||||
exitActivity();
|
});
|
||||||
requestUpdate();
|
|
||||||
},
|
|
||||||
[this](const int newSpineIndex, const int newPage) {
|
|
||||||
if (currentSpineIndex != newSpineIndex || (section && section->currentPage != newPage)) {
|
|
||||||
currentSpineIndex = newSpineIndex;
|
|
||||||
nextPageNumber = newPage;
|
|
||||||
section.reset();
|
|
||||||
}
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case EpubReaderMenuActivity::MenuAction::FOOTNOTES: {
|
case EpubReaderMenuActivity::MenuAction::FOOTNOTES: {
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<EpubReaderFootnotesActivity>(renderer, mappedInput, currentPageFootnotes),
|
||||||
enterNewActivity(new EpubReaderFootnotesActivity(
|
[this](const ActivityResult& result) {
|
||||||
this->renderer, this->mappedInput, currentPageFootnotes,
|
if (!result.isCancelled) {
|
||||||
[this] {
|
const auto& footnoteResult = std::get<FootnoteResult>(result.data);
|
||||||
// Go back from footnotes list
|
navigateToHref(footnoteResult.href, true);
|
||||||
exitActivity();
|
}
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
},
|
});
|
||||||
[this](const char* href) {
|
|
||||||
// Navigate to selected footnote
|
|
||||||
navigateToHref(href, true);
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case EpubReaderMenuActivity::MenuAction::GO_TO_PERCENT: {
|
case EpubReaderMenuActivity::MenuAction::GO_TO_PERCENT: {
|
||||||
// Launch the slider-based percent selector and return here on confirm/cancel.
|
|
||||||
float bookProgress = 0.0f;
|
float bookProgress = 0.0f;
|
||||||
if (epub && epub->getBookSize() > 0 && section && section->pageCount > 0) {
|
if (epub && epub->getBookSize() > 0 && section && section->pageCount > 0) {
|
||||||
const float chapterProgress = static_cast<float>(section->currentPage) / static_cast<float>(section->pageCount);
|
const float chapterProgress = static_cast<float>(section->currentPage) / static_cast<float>(section->pageCount);
|
||||||
bookProgress = epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
|
bookProgress = epub->calculateProgress(currentSpineIndex, chapterProgress) * 100.0f;
|
||||||
}
|
}
|
||||||
const int initialPercent = clampPercent(static_cast<int>(bookProgress + 0.5f));
|
const int initialPercent = clampPercent(static_cast<int>(bookProgress + 0.5f));
|
||||||
exitActivity();
|
startActivityForResult(
|
||||||
enterNewActivity(new EpubReaderPercentSelectionActivity(
|
std::make_unique<EpubReaderPercentSelectionActivity>(renderer, mappedInput, initialPercent),
|
||||||
renderer, mappedInput, initialPercent,
|
[this](const ActivityResult& result) {
|
||||||
[this](const int percent) {
|
if (!result.isCancelled) {
|
||||||
// Apply the new position and exit back to the reader.
|
jumpToPercent(std::get<PercentResult>(result.data).percent);
|
||||||
jumpToPercent(percent);
|
}
|
||||||
exitActivity();
|
});
|
||||||
requestUpdate();
|
|
||||||
},
|
|
||||||
[this]() {
|
|
||||||
// Cancel selection and return to the reader.
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case EpubReaderMenuActivity::MenuAction::DISPLAY_QR: {
|
case EpubReaderMenuActivity::MenuAction::DISPLAY_QR: {
|
||||||
@@ -444,55 +364,41 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!fullText.empty()) {
|
if (!fullText.empty()) {
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<QrDisplayActivity>(renderer, mappedInput, fullText),
|
||||||
enterNewActivity(new QrDisplayActivity(renderer, mappedInput, fullText, [this]() {
|
[this](const ActivityResult& result) {});
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If no text or page loading failed, just close menu
|
// If no text or page loading failed, just close menu
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case EpubReaderMenuActivity::MenuAction::GO_HOME: {
|
case EpubReaderMenuActivity::MenuAction::GO_HOME: {
|
||||||
// Defer go home to avoid race condition with display task
|
onGoHome();
|
||||||
pendingGoHome = true;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case EpubReaderMenuActivity::MenuAction::DELETE_CACHE: {
|
case EpubReaderMenuActivity::MenuAction::DELETE_CACHE: {
|
||||||
{
|
{
|
||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
if (epub) {
|
if (epub && section) {
|
||||||
// 2. BACKUP: Read current progress
|
|
||||||
// We use the current variables that track our position
|
|
||||||
uint16_t backupSpine = currentSpineIndex;
|
uint16_t backupSpine = currentSpineIndex;
|
||||||
uint16_t backupPage = section->currentPage;
|
uint16_t backupPage = section->currentPage;
|
||||||
uint16_t backupPageCount = section->pageCount;
|
uint16_t backupPageCount = section->pageCount;
|
||||||
|
|
||||||
section.reset();
|
section.reset();
|
||||||
// 3. WIPE: Clear the cache directory
|
|
||||||
epub->clearCache();
|
epub->clearCache();
|
||||||
|
|
||||||
// 4. RESTORE: Re-setup the directory and rewrite the progress file
|
|
||||||
epub->setupCacheDir();
|
epub->setupCacheDir();
|
||||||
|
|
||||||
saveProgress(backupSpine, backupPage, backupPageCount);
|
saveProgress(backupSpine, backupPage, backupPageCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Defer go home to avoid race condition with display task
|
onGoHome();
|
||||||
pendingGoHome = true;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case EpubReaderMenuActivity::MenuAction::SCREENSHOT: {
|
case EpubReaderMenuActivity::MenuAction::SCREENSHOT: {
|
||||||
{
|
{
|
||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
pendingScreenshot = true;
|
pendingScreenshot = true;
|
||||||
}
|
}
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -500,22 +406,19 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
|
|||||||
if (KOREADER_STORE.hasCredentials()) {
|
if (KOREADER_STORE.hasCredentials()) {
|
||||||
const int currentPage = section ? section->currentPage : 0;
|
const int currentPage = section ? section->currentPage : 0;
|
||||||
const int totalPages = section ? section->pageCount : 0;
|
const int totalPages = section ? section->pageCount : 0;
|
||||||
exitActivity();
|
startActivityForResult(
|
||||||
enterNewActivity(new KOReaderSyncActivity(
|
std::make_unique<KOReaderSyncActivity>(renderer, mappedInput, epub, epub->getPath(), currentSpineIndex,
|
||||||
renderer, mappedInput, epub, epub->getPath(), currentSpineIndex, currentPage, totalPages,
|
currentPage, totalPages),
|
||||||
[this]() {
|
[this](const ActivityResult& result) {
|
||||||
// On cancel - defer exit to avoid use-after-free
|
if (!result.isCancelled) {
|
||||||
pendingSubactivityExit = true;
|
const auto& sync = std::get<SyncResult>(result.data);
|
||||||
},
|
if (currentSpineIndex != sync.spineIndex || (section && section->currentPage != sync.page)) {
|
||||||
[this](int newSpineIndex, int newPage) {
|
currentSpineIndex = sync.spineIndex;
|
||||||
// On sync complete - update position and defer exit
|
nextPageNumber = sync.page;
|
||||||
if (currentSpineIndex != newSpineIndex || (section && section->currentPage != newPage)) {
|
section.reset();
|
||||||
currentSpineIndex = newSpineIndex;
|
}
|
||||||
nextPageNumber = newPage;
|
|
||||||
section.reset();
|
|
||||||
}
|
}
|
||||||
pendingSubactivityExit = true;
|
});
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -550,7 +453,7 @@ void EpubReaderActivity::applyOrientation(const uint8_t orientation) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Failure handling
|
// TODO: Failure handling
|
||||||
void EpubReaderActivity::render(Activity::RenderLock&& lock) {
|
void EpubReaderActivity::render(RenderLock&& lock) {
|
||||||
if (!epub) {
|
if (!epub) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -785,8 +688,8 @@ void EpubReaderActivity::renderStatusBar() const {
|
|||||||
GUI.drawStatusBar(renderer, bookProgress, currentPage, pageCount, title);
|
GUI.drawStatusBar(renderer, bookProgress, currentPage, pageCount, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderActivity::navigateToHref(const char* href, const bool savePosition) {
|
void EpubReaderActivity::navigateToHref(const std::string& hrefStr, const bool savePosition) {
|
||||||
if (!epub || !href) return;
|
if (!epub) return;
|
||||||
|
|
||||||
// Push current position onto saved stack
|
// Push current position onto saved stack
|
||||||
if (savePosition && section && footnoteDepth < MAX_FOOTNOTE_DEPTH) {
|
if (savePosition && section && footnoteDepth < MAX_FOOTNOTE_DEPTH) {
|
||||||
@@ -795,8 +698,6 @@ void EpubReaderActivity::navigateToHref(const char* href, const bool savePositio
|
|||||||
LOG_DBG("ERS", "Saved position [%d]: spine %d, page %d", footnoteDepth, currentSpineIndex, section->currentPage);
|
LOG_DBG("ERS", "Saved position [%d]: spine %d, page %d", footnoteDepth, currentSpineIndex, section->currentPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string hrefStr(href);
|
|
||||||
|
|
||||||
// Check for same-file anchor reference (#anchor only)
|
// Check for same-file anchor reference (#anchor only)
|
||||||
bool sameFile = !hrefStr.empty() && hrefStr[0] == '#';
|
bool sameFile = !hrefStr.empty() && hrefStr[0] == '#';
|
||||||
|
|
||||||
@@ -809,7 +710,7 @@ void EpubReaderActivity::navigateToHref(const char* href, const bool savePositio
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (targetSpineIndex < 0) {
|
if (targetSpineIndex < 0) {
|
||||||
LOG_DBG("ERS", "Could not resolve href: %s", href);
|
LOG_DBG("ERS", "Could not resolve href: %s", hrefStr.c_str());
|
||||||
if (savePosition && footnoteDepth > 0) footnoteDepth--; // undo push
|
if (savePosition && footnoteDepth > 0) footnoteDepth--; // undo push
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -821,7 +722,7 @@ void EpubReaderActivity::navigateToHref(const char* href, const bool savePositio
|
|||||||
section.reset();
|
section.reset();
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
LOG_DBG("ERS", "Navigated to spine %d for href: %s", targetSpineIndex, href);
|
LOG_DBG("ERS", "Navigated to spine %d for href: %s", targetSpineIndex, hrefStr.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderActivity::restoreSavedPosition() {
|
void EpubReaderActivity::restoreSavedPosition() {
|
||||||
|
|||||||
@@ -4,9 +4,9 @@
|
|||||||
#include <Epub/Section.h>
|
#include <Epub/Section.h>
|
||||||
|
|
||||||
#include "EpubReaderMenuActivity.h"
|
#include "EpubReaderMenuActivity.h"
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
class EpubReaderActivity final : public ActivityWithSubactivity {
|
class EpubReaderActivity final : public Activity {
|
||||||
std::shared_ptr<Epub> epub;
|
std::shared_ptr<Epub> epub;
|
||||||
std::unique_ptr<Section> section = nullptr;
|
std::unique_ptr<Section> section = nullptr;
|
||||||
int currentSpineIndex = 0;
|
int currentSpineIndex = 0;
|
||||||
@@ -19,12 +19,8 @@ class EpubReaderActivity final : public ActivityWithSubactivity {
|
|||||||
bool pendingPercentJump = false;
|
bool pendingPercentJump = false;
|
||||||
// Normalized 0.0-1.0 progress within the target spine item, computed from book percentage.
|
// Normalized 0.0-1.0 progress within the target spine item, computed from book percentage.
|
||||||
float pendingSpineProgress = 0.0f;
|
float pendingSpineProgress = 0.0f;
|
||||||
bool pendingSubactivityExit = false; // Defer subactivity exit to avoid use-after-free
|
|
||||||
bool pendingGoHome = false; // Defer go home to avoid race condition with display task
|
|
||||||
bool pendingScreenshot = false;
|
bool pendingScreenshot = false;
|
||||||
bool skipNextButtonCheck = false; // Skip button processing for one frame after subactivity exit
|
bool skipNextButtonCheck = false; // Skip button processing for one frame after subactivity exit
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
// Footnote support
|
// Footnote support
|
||||||
std::vector<FootnoteEntry> currentPageFootnotes;
|
std::vector<FootnoteEntry> currentPageFootnotes;
|
||||||
@@ -42,23 +38,19 @@ class EpubReaderActivity final : public ActivityWithSubactivity {
|
|||||||
void saveProgress(int spineIndex, int currentPage, int pageCount);
|
void saveProgress(int spineIndex, int currentPage, int pageCount);
|
||||||
// Jump to a percentage of the book (0-100), mapping it to spine and page.
|
// Jump to a percentage of the book (0-100), mapping it to spine and page.
|
||||||
void jumpToPercent(int percent);
|
void jumpToPercent(int percent);
|
||||||
void onReaderMenuBack(uint8_t orientation);
|
|
||||||
void onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action);
|
void onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action);
|
||||||
void applyOrientation(uint8_t orientation);
|
void applyOrientation(uint8_t orientation);
|
||||||
|
|
||||||
// Footnote navigation
|
// Footnote navigation
|
||||||
void navigateToHref(const char* href, bool savePosition = false);
|
void navigateToHref(const std::string& href, bool savePosition = false);
|
||||||
void restoreSavedPosition();
|
void restoreSavedPosition();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit EpubReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Epub> epub,
|
explicit EpubReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Epub> epub)
|
||||||
const std::function<void()>& onGoBack, const std::function<void()>& onGoHome)
|
: Activity("EpubReader", renderer, mappedInput), epub(std::move(epub)) {}
|
||||||
: ActivityWithSubactivity("EpubReader", renderer, mappedInput),
|
|
||||||
epub(std::move(epub)),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onGoHome(onGoHome) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&& lock) override;
|
void render(RenderLock&& lock) override;
|
||||||
|
bool isReaderActivity() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ int EpubReaderChapterSelectionActivity::getPageItems() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderChapterSelectionActivity::onEnter() {
|
void EpubReaderChapterSelectionActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
if (!epub) {
|
if (!epub) {
|
||||||
return;
|
return;
|
||||||
@@ -41,26 +41,28 @@ void EpubReaderChapterSelectionActivity::onEnter() {
|
|||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderChapterSelectionActivity::onExit() { ActivityWithSubactivity::onExit(); }
|
void EpubReaderChapterSelectionActivity::onExit() { Activity::onExit(); }
|
||||||
|
|
||||||
void EpubReaderChapterSelectionActivity::loop() {
|
void EpubReaderChapterSelectionActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int pageItems = getPageItems();
|
const int pageItems = getPageItems();
|
||||||
const int totalItems = getTotalItems();
|
const int totalItems = getTotalItems();
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
const auto newSpineIndex = epub->getSpineIndexForTocIndex(selectorIndex);
|
const auto newSpineIndex = epub->getSpineIndexForTocIndex(selectorIndex);
|
||||||
if (newSpineIndex == -1) {
|
if (newSpineIndex == -1) {
|
||||||
onGoBack();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
} else {
|
} else {
|
||||||
onSelectSpineIndex(newSpineIndex);
|
setResult(ChapterResult{newSpineIndex});
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
onGoBack();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
buttonNavigator.onNextRelease([this, totalItems] {
|
buttonNavigator.onNextRelease([this, totalItems] {
|
||||||
@@ -84,7 +86,7 @@ void EpubReaderChapterSelectionActivity::loop() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderChapterSelectionActivity::render(Activity::RenderLock&&) {
|
void EpubReaderChapterSelectionActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|||||||
@@ -3,22 +3,16 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "../ActivityWithSubactivity.h"
|
#include "../Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
class EpubReaderChapterSelectionActivity final : public ActivityWithSubactivity {
|
class EpubReaderChapterSelectionActivity final : public Activity {
|
||||||
std::shared_ptr<Epub> epub;
|
std::shared_ptr<Epub> epub;
|
||||||
std::string epubPath;
|
std::string epubPath;
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
int currentSpineIndex = 0;
|
int currentSpineIndex = 0;
|
||||||
int currentPage = 0;
|
|
||||||
int totalPagesInSpine = 0;
|
|
||||||
int selectorIndex = 0;
|
int selectorIndex = 0;
|
||||||
|
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void(int newSpineIndex)> onSelectSpineIndex;
|
|
||||||
const std::function<void(int newSpineIndex, int newPage)> onSyncPosition;
|
|
||||||
|
|
||||||
// Number of items that fit on a page, derived from logical screen height.
|
// Number of items that fit on a page, derived from logical screen height.
|
||||||
// This adapts automatically when switching between portrait and landscape.
|
// This adapts automatically when switching between portrait and landscape.
|
||||||
int getPageItems() const;
|
int getPageItems() const;
|
||||||
@@ -29,21 +23,13 @@ class EpubReaderChapterSelectionActivity final : public ActivityWithSubactivity
|
|||||||
public:
|
public:
|
||||||
explicit EpubReaderChapterSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit EpubReaderChapterSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const std::shared_ptr<Epub>& epub, const std::string& epubPath,
|
const std::shared_ptr<Epub>& epub, const std::string& epubPath,
|
||||||
const int currentSpineIndex, const int currentPage,
|
const int currentSpineIndex)
|
||||||
const int totalPagesInSpine, const std::function<void()>& onGoBack,
|
: Activity("EpubReaderChapterSelection", renderer, mappedInput),
|
||||||
const std::function<void(int newSpineIndex)>& onSelectSpineIndex,
|
|
||||||
const std::function<void(int newSpineIndex, int newPage)>& onSyncPosition)
|
|
||||||
: ActivityWithSubactivity("EpubReaderChapterSelection", renderer, mappedInput),
|
|
||||||
epub(epub),
|
epub(epub),
|
||||||
epubPath(epubPath),
|
epubPath(epubPath),
|
||||||
currentSpineIndex(currentSpineIndex),
|
currentSpineIndex(currentSpineIndex) {}
|
||||||
currentPage(currentPage),
|
|
||||||
totalPagesInSpine(totalPagesInSpine),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onSelectSpineIndex(onSelectSpineIndex),
|
|
||||||
onSyncPosition(onSyncPosition) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,27 +10,26 @@
|
|||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
void EpubReaderFootnotesActivity::onEnter() {
|
void EpubReaderFootnotesActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
selectedIndex = 0;
|
selectedIndex = 0;
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderFootnotesActivity::onExit() { ActivityWithSubactivity::onExit(); }
|
void EpubReaderFootnotesActivity::onExit() { Activity::onExit(); }
|
||||||
|
|
||||||
void EpubReaderFootnotesActivity::loop() {
|
void EpubReaderFootnotesActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
onGoBack();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
if (selectedIndex >= 0 && selectedIndex < static_cast<int>(footnotes.size())) {
|
if (selectedIndex >= 0 && selectedIndex < static_cast<int>(footnotes.size())) {
|
||||||
onSelectFootnote(footnotes[selectedIndex].href);
|
setResult(FootnoteResult{footnotes[selectedIndex].href});
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -50,7 +49,7 @@ void EpubReaderFootnotesActivity::loop() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderFootnotesActivity::render(Activity::RenderLock&&) {
|
void EpubReaderFootnotesActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
renderer.drawCenteredText(UI_12_FONT_ID, 15, tr(STR_FOOTNOTES), true, EpdFontFamily::BOLD);
|
renderer.drawCenteredText(UI_12_FONT_ID, 15, tr(STR_FOOTNOTES), true, EpdFontFamily::BOLD);
|
||||||
|
|||||||
@@ -6,29 +6,22 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../ActivityWithSubactivity.h"
|
#include "../Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
class EpubReaderFootnotesActivity final : public ActivityWithSubactivity {
|
class EpubReaderFootnotesActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit EpubReaderFootnotesActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit EpubReaderFootnotesActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const std::vector<FootnoteEntry>& footnotes,
|
const std::vector<FootnoteEntry>& footnotes)
|
||||||
const std::function<void()>& onGoBack,
|
: Activity("EpubReaderFootnotes", renderer, mappedInput), footnotes(footnotes) {}
|
||||||
const std::function<void(const char*)>& onSelectFootnote)
|
|
||||||
: ActivityWithSubactivity("EpubReaderFootnotes", renderer, mappedInput),
|
|
||||||
footnotes(footnotes),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onSelectFootnote(onSelectFootnote) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::vector<FootnoteEntry>& footnotes;
|
const std::vector<FootnoteEntry>& footnotes;
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void(const char*)> onSelectFootnote;
|
|
||||||
int selectedIndex = 0;
|
int selectedIndex = 0;
|
||||||
int scrollOffset = 0;
|
int scrollOffset = 0;
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
|
|||||||
@@ -10,17 +10,14 @@
|
|||||||
EpubReaderMenuActivity::EpubReaderMenuActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
EpubReaderMenuActivity::EpubReaderMenuActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const std::string& title, const int currentPage, const int totalPages,
|
const std::string& title, const int currentPage, const int totalPages,
|
||||||
const int bookProgressPercent, const uint8_t currentOrientation,
|
const int bookProgressPercent, const uint8_t currentOrientation,
|
||||||
const bool hasFootnotes, const std::function<void(uint8_t)>& onBack,
|
const bool hasFootnotes)
|
||||||
const std::function<void(MenuAction)>& onAction)
|
: Activity("EpubReaderMenu", renderer, mappedInput),
|
||||||
: ActivityWithSubactivity("EpubReaderMenu", renderer, mappedInput),
|
|
||||||
menuItems(buildMenuItems(hasFootnotes)),
|
menuItems(buildMenuItems(hasFootnotes)),
|
||||||
title(title),
|
title(title),
|
||||||
pendingOrientation(currentOrientation),
|
pendingOrientation(currentOrientation),
|
||||||
currentPage(currentPage),
|
currentPage(currentPage),
|
||||||
totalPages(totalPages),
|
totalPages(totalPages),
|
||||||
bookProgressPercent(bookProgressPercent),
|
bookProgressPercent(bookProgressPercent) {}
|
||||||
onBack(onBack),
|
|
||||||
onAction(onAction) {}
|
|
||||||
|
|
||||||
std::vector<EpubReaderMenuActivity::MenuItem> EpubReaderMenuActivity::buildMenuItems(bool hasFootnotes) {
|
std::vector<EpubReaderMenuActivity::MenuItem> EpubReaderMenuActivity::buildMenuItems(bool hasFootnotes) {
|
||||||
std::vector<MenuItem> items;
|
std::vector<MenuItem> items;
|
||||||
@@ -40,18 +37,13 @@ std::vector<EpubReaderMenuActivity::MenuItem> EpubReaderMenuActivity::buildMenuI
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderMenuActivity::onEnter() {
|
void EpubReaderMenuActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderMenuActivity::onExit() { ActivityWithSubactivity::onExit(); }
|
void EpubReaderMenuActivity::onExit() { Activity::onExit(); }
|
||||||
|
|
||||||
void EpubReaderMenuActivity::loop() {
|
void EpubReaderMenuActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle navigation
|
// Handle navigation
|
||||||
buttonNavigator.onNext([this] {
|
buttonNavigator.onNext([this] {
|
||||||
selectedIndex = ButtonNavigator::nextIndex(selectedIndex, static_cast<int>(menuItems.size()));
|
selectedIndex = ButtonNavigator::nextIndex(selectedIndex, static_cast<int>(menuItems.size()));
|
||||||
@@ -63,7 +55,6 @@ void EpubReaderMenuActivity::loop() {
|
|||||||
requestUpdate();
|
requestUpdate();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Use local variables for items we need to check after potential deletion
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
const auto selectedAction = menuItems[selectedIndex].action;
|
const auto selectedAction = menuItems[selectedIndex].action;
|
||||||
if (selectedAction == MenuAction::ROTATE_SCREEN) {
|
if (selectedAction == MenuAction::ROTATE_SCREEN) {
|
||||||
@@ -73,22 +64,20 @@ void EpubReaderMenuActivity::loop() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Capture the callback and action locally
|
setResult(MenuResult{static_cast<int>(selectedAction), pendingOrientation});
|
||||||
auto actionCallback = onAction;
|
finish();
|
||||||
|
|
||||||
// 2. Execute the callback
|
|
||||||
actionCallback(selectedAction);
|
|
||||||
|
|
||||||
// 3. CRITICAL: Return immediately. 'this' is likely deleted now.
|
|
||||||
return;
|
return;
|
||||||
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
// Return the pending orientation to the parent so it can apply on exit.
|
ActivityResult result;
|
||||||
onBack(pendingOrientation);
|
result.isCancelled = true;
|
||||||
return; // Also return here just in case
|
result.data = MenuResult{-1, pendingOrientation};
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderMenuActivity::render(Activity::RenderLock&&) {
|
void EpubReaderMenuActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
const auto orientation = renderer.getOrientation();
|
const auto orientation = renderer.getOrientation();
|
||||||
|
|||||||
@@ -2,14 +2,13 @@
|
|||||||
#include <Epub.h>
|
#include <Epub.h>
|
||||||
#include <I18n.h>
|
#include <I18n.h>
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../ActivityWithSubactivity.h"
|
#include "../Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
class EpubReaderMenuActivity final : public ActivityWithSubactivity {
|
class EpubReaderMenuActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
// Menu actions available from the reader menu.
|
// Menu actions available from the reader menu.
|
||||||
enum class MenuAction {
|
enum class MenuAction {
|
||||||
@@ -26,14 +25,12 @@ class EpubReaderMenuActivity final : public ActivityWithSubactivity {
|
|||||||
|
|
||||||
explicit EpubReaderMenuActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& title,
|
explicit EpubReaderMenuActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& title,
|
||||||
const int currentPage, const int totalPages, const int bookProgressPercent,
|
const int currentPage, const int totalPages, const int bookProgressPercent,
|
||||||
const uint8_t currentOrientation, const bool hasFootnotes,
|
const uint8_t currentOrientation, const bool hasFootnotes);
|
||||||
const std::function<void(uint8_t)>& onBack,
|
|
||||||
const std::function<void(MenuAction)>& onAction);
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct MenuItem {
|
struct MenuItem {
|
||||||
@@ -56,7 +53,4 @@ class EpubReaderMenuActivity final : public ActivityWithSubactivity {
|
|||||||
int currentPage = 0;
|
int currentPage = 0;
|
||||||
int totalPages = 0;
|
int totalPages = 0;
|
||||||
int bookProgressPercent = 0;
|
int bookProgressPercent = 0;
|
||||||
|
|
||||||
const std::function<void(uint8_t)> onBack;
|
|
||||||
const std::function<void(MenuAction)> onAction;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,12 +14,12 @@ constexpr int kLargeStep = 10;
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void EpubReaderPercentSelectionActivity::onEnter() {
|
void EpubReaderPercentSelectionActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
// Set up rendering task and mark first frame dirty.
|
// Set up rendering task and mark first frame dirty.
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderPercentSelectionActivity::onExit() { ActivityWithSubactivity::onExit(); }
|
void EpubReaderPercentSelectionActivity::onExit() { Activity::onExit(); }
|
||||||
|
|
||||||
void EpubReaderPercentSelectionActivity::adjustPercent(const int delta) {
|
void EpubReaderPercentSelectionActivity::adjustPercent(const int delta) {
|
||||||
// Apply delta and clamp within 0-100.
|
// Apply delta and clamp within 0-100.
|
||||||
@@ -33,19 +33,18 @@ void EpubReaderPercentSelectionActivity::adjustPercent(const int delta) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderPercentSelectionActivity::loop() {
|
void EpubReaderPercentSelectionActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Back cancels, confirm selects, arrows adjust the percent.
|
// Back cancels, confirm selects, arrows adjust the percent.
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
onCancel();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
onSelect(percent);
|
setResult(PercentResult{percent});
|
||||||
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +55,7 @@ void EpubReaderPercentSelectionActivity::loop() {
|
|||||||
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Down}, [this] { adjustPercent(-kLargeStep); });
|
buttonNavigator.onPressAndContinuous({MappedInputManager::Button::Down}, [this] { adjustPercent(-kLargeStep); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderPercentSelectionActivity::render(Activity::RenderLock&&) {
|
void EpubReaderPercentSelectionActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
// Title and numeric percent value.
|
// Title and numeric percent value.
|
||||||
|
|||||||
@@ -1,26 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
class EpubReaderPercentSelectionActivity final : public ActivityWithSubactivity {
|
class EpubReaderPercentSelectionActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
// Slider-style percent selector for jumping within a book.
|
// Slider-style percent selector for jumping within a book.
|
||||||
explicit EpubReaderPercentSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit EpubReaderPercentSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const int initialPercent, const std::function<void(int)>& onSelect,
|
const int initialPercent)
|
||||||
const std::function<void()>& onCancel)
|
: Activity("EpubReaderPercentSelection", renderer, mappedInput), percent(initialPercent) {}
|
||||||
: ActivityWithSubactivity("EpubReaderPercentSelection", renderer, mappedInput),
|
|
||||||
percent(initialPercent),
|
|
||||||
onSelect(onSelect),
|
|
||||||
onCancel(onCancel) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Current percent value (0-100) shown on the slider.
|
// Current percent value (0-100) shown on the slider.
|
||||||
@@ -28,11 +22,6 @@ class EpubReaderPercentSelectionActivity final : public ActivityWithSubactivity
|
|||||||
|
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
|
|
||||||
// Callback invoked when the user confirms a percent.
|
|
||||||
const std::function<void(int)> onSelect;
|
|
||||||
// Callback invoked when the user cancels the slider.
|
|
||||||
const std::function<void()> onCancel;
|
|
||||||
|
|
||||||
// Change the current percent by a delta and clamp within bounds.
|
// Change the current percent by a delta and clamp within bounds.
|
||||||
void adjustPercent(int delta);
|
void adjustPercent(int delta);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -51,11 +51,12 @@ void wifiOff() {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
|
void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
|
||||||
exitActivity();
|
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
LOG_DBG("KOSync", "WiFi connection failed, exiting");
|
LOG_DBG("KOSync", "WiFi connection failed, exiting");
|
||||||
onCancel();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +67,7 @@ void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
|
|||||||
state = SYNCING;
|
state = SYNCING;
|
||||||
statusMessage = tr(STR_SYNCING_TIME);
|
statusMessage = tr(STR_SYNCING_TIME);
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
|
|
||||||
// Sync time with NTP before making API requests
|
// Sync time with NTP before making API requests
|
||||||
syncTimeWithNTP();
|
syncTimeWithNTP();
|
||||||
@@ -75,7 +76,7 @@ void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
|
|||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
statusMessage = tr(STR_CALC_HASH);
|
statusMessage = tr(STR_CALC_HASH);
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
|
|
||||||
performSync();
|
performSync();
|
||||||
}
|
}
|
||||||
@@ -93,7 +94,7 @@ void KOReaderSyncActivity::performSync() {
|
|||||||
state = SYNC_FAILED;
|
state = SYNC_FAILED;
|
||||||
statusMessage = tr(STR_HASH_FAILED);
|
statusMessage = tr(STR_HASH_FAILED);
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +116,7 @@ void KOReaderSyncActivity::performSync() {
|
|||||||
state = NO_REMOTE_PROGRESS;
|
state = NO_REMOTE_PROGRESS;
|
||||||
hasRemoteProgress = false;
|
hasRemoteProgress = false;
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +126,7 @@ void KOReaderSyncActivity::performSync() {
|
|||||||
state = SYNC_FAILED;
|
state = SYNC_FAILED;
|
||||||
statusMessage = KOReaderSyncClient::errorString(result);
|
statusMessage = KOReaderSyncClient::errorString(result);
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +150,7 @@ void KOReaderSyncActivity::performSync() {
|
|||||||
selectedOption = 0; // Apply remote progress
|
selectedOption = 0; // Apply remote progress
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderSyncActivity::performUpload() {
|
void KOReaderSyncActivity::performUpload() {
|
||||||
@@ -158,7 +159,6 @@ void KOReaderSyncActivity::performUpload() {
|
|||||||
state = UPLOADING;
|
state = UPLOADING;
|
||||||
statusMessage = tr(STR_UPLOAD_PROGRESS);
|
statusMessage = tr(STR_UPLOAD_PROGRESS);
|
||||||
}
|
}
|
||||||
requestUpdate();
|
|
||||||
requestUpdateAndWait();
|
requestUpdateAndWait();
|
||||||
|
|
||||||
// Convert current position to KOReader format
|
// Convert current position to KOReader format
|
||||||
@@ -188,11 +188,11 @@ void KOReaderSyncActivity::performUpload() {
|
|||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
state = UPLOAD_COMPLETE;
|
state = UPLOAD_COMPLETE;
|
||||||
}
|
}
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderSyncActivity::onEnter() {
|
void KOReaderSyncActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
// Check for credentials first
|
// Check for credentials first
|
||||||
if (!KOREADER_STORE.hasCredentials()) {
|
if (!KOREADER_STORE.hasCredentials()) {
|
||||||
@@ -206,7 +206,7 @@ void KOReaderSyncActivity::onEnter() {
|
|||||||
LOG_DBG("KOSync", "Already connected to WiFi");
|
LOG_DBG("KOSync", "Already connected to WiFi");
|
||||||
state = SYNCING;
|
state = SYNCING;
|
||||||
statusMessage = tr(STR_SYNCING_TIME);
|
statusMessage = tr(STR_SYNCING_TIME);
|
||||||
requestUpdate();
|
requestUpdate(true);
|
||||||
|
|
||||||
// Perform sync directly (will be handled in loop)
|
// Perform sync directly (will be handled in loop)
|
||||||
xTaskCreate(
|
xTaskCreate(
|
||||||
@@ -218,7 +218,7 @@ void KOReaderSyncActivity::onEnter() {
|
|||||||
RenderLock lock(*self);
|
RenderLock lock(*self);
|
||||||
self->statusMessage = tr(STR_CALC_HASH);
|
self->statusMessage = tr(STR_CALC_HASH);
|
||||||
}
|
}
|
||||||
self->requestUpdate();
|
self->requestUpdate(true);
|
||||||
self->performSync();
|
self->performSync();
|
||||||
vTaskDelete(nullptr);
|
vTaskDelete(nullptr);
|
||||||
},
|
},
|
||||||
@@ -228,21 +228,17 @@ void KOReaderSyncActivity::onEnter() {
|
|||||||
|
|
||||||
// Launch WiFi selection subactivity
|
// Launch WiFi selection subactivity
|
||||||
LOG_DBG("KOSync", "Launching WifiSelectionActivity...");
|
LOG_DBG("KOSync", "Launching WifiSelectionActivity...");
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderSyncActivity::onExit() {
|
void KOReaderSyncActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
wifiOff();
|
wifiOff();
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderSyncActivity::render(Activity::RenderLock&&) {
|
void KOReaderSyncActivity::render(RenderLock&&) {
|
||||||
if (subActivity) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
@@ -357,49 +353,50 @@ void KOReaderSyncActivity::render(Activity::RenderLock&&) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderSyncActivity::loop() {
|
void KOReaderSyncActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == NO_CREDENTIALS || state == SYNC_FAILED || state == UPLOAD_COMPLETE) {
|
if (state == NO_CREDENTIALS || state == SYNC_FAILED || state == UPLOAD_COMPLETE) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
onCancel();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state == SHOWING_RESULT) {
|
if (state == SHOWING_RESULT) {
|
||||||
// Navigate options
|
// Navigate options
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Up) ||
|
if (mappedInput.wasReleased(MappedInputManager::Button::Up) ||
|
||||||
mappedInput.wasPressed(MappedInputManager::Button::Left)) {
|
mappedInput.wasReleased(MappedInputManager::Button::Left)) {
|
||||||
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
|
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
} else if (mappedInput.wasReleased(MappedInputManager::Button::Down) ||
|
||||||
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
|
mappedInput.wasReleased(MappedInputManager::Button::Right)) {
|
||||||
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
|
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
if (selectedOption == 0) {
|
if (selectedOption == 0) {
|
||||||
// Apply remote progress — WiFi no longer needed
|
// Wifi will be turned off in onExit()
|
||||||
wifiOff();
|
setResult(SyncResult{remotePosition.spineIndex, remotePosition.pageNumber});
|
||||||
onSyncComplete(remotePosition.spineIndex, remotePosition.pageNumber);
|
finish();
|
||||||
} else if (selectedOption == 1) {
|
} else if (selectedOption == 1) {
|
||||||
// Upload local progress
|
// Upload local progress
|
||||||
performUpload();
|
performUpload();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
onCancel();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state == NO_REMOTE_PROGRESS) {
|
if (state == NO_REMOTE_PROGRESS) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
// Calculate hash if not done yet
|
// Calculate hash if not done yet
|
||||||
if (documentHash.empty()) {
|
if (documentHash.empty()) {
|
||||||
if (KOREADER_STORE.getMatchMethod() == DocumentMatchMethod::FILENAME) {
|
if (KOREADER_STORE.getMatchMethod() == DocumentMatchMethod::FILENAME) {
|
||||||
@@ -411,8 +408,11 @@ void KOReaderSyncActivity::loop() {
|
|||||||
performUpload();
|
performUpload();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
onCancel();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "KOReaderSyncClient.h"
|
#include "KOReaderSyncClient.h"
|
||||||
#include "ProgressMapper.h"
|
#include "ProgressMapper.h"
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activity for syncing reading progress with KOReader sync server.
|
* Activity for syncing reading progress with KOReader sync server.
|
||||||
@@ -18,16 +18,12 @@
|
|||||||
* 4. Show comparison and options (Apply/Upload)
|
* 4. Show comparison and options (Apply/Upload)
|
||||||
* 5. Apply or upload progress
|
* 5. Apply or upload progress
|
||||||
*/
|
*/
|
||||||
class KOReaderSyncActivity final : public ActivityWithSubactivity {
|
class KOReaderSyncActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
using OnCancelCallback = std::function<void()>;
|
|
||||||
using OnSyncCompleteCallback = std::function<void(int newSpineIndex, int newPageNumber)>;
|
|
||||||
|
|
||||||
explicit KOReaderSyncActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit KOReaderSyncActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const std::shared_ptr<Epub>& epub, const std::string& epubPath, int currentSpineIndex,
|
const std::shared_ptr<Epub>& epub, const std::string& epubPath, int currentSpineIndex,
|
||||||
int currentPage, int totalPagesInSpine, OnCancelCallback onCancel,
|
int currentPage, int totalPagesInSpine)
|
||||||
OnSyncCompleteCallback onSyncComplete)
|
: Activity("KOReaderSync", renderer, mappedInput),
|
||||||
: ActivityWithSubactivity("KOReaderSync", renderer, mappedInput),
|
|
||||||
epub(epub),
|
epub(epub),
|
||||||
epubPath(epubPath),
|
epubPath(epubPath),
|
||||||
currentSpineIndex(currentSpineIndex),
|
currentSpineIndex(currentSpineIndex),
|
||||||
@@ -35,14 +31,12 @@ class KOReaderSyncActivity final : public ActivityWithSubactivity {
|
|||||||
totalPagesInSpine(totalPagesInSpine),
|
totalPagesInSpine(totalPagesInSpine),
|
||||||
remoteProgress{},
|
remoteProgress{},
|
||||||
remotePosition{},
|
remotePosition{},
|
||||||
localProgress{},
|
localProgress{} {}
|
||||||
onCancel(std::move(onCancel)),
|
|
||||||
onSyncComplete(std::move(onSyncComplete)) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
bool preventAutoSleep() override { return state == CONNECTING || state == SYNCING; }
|
bool preventAutoSleep() override { return state == CONNECTING || state == SYNCING; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -79,9 +73,6 @@ class KOReaderSyncActivity final : public ActivityWithSubactivity {
|
|||||||
// Selection in result screen (0=Apply, 1=Upload)
|
// Selection in result screen (0=Apply, 1=Upload)
|
||||||
int selectedOption = 0;
|
int selectedOption = 0;
|
||||||
|
|
||||||
OnCancelCallback onCancel;
|
|
||||||
OnSyncCompleteCallback onSyncComplete;
|
|
||||||
|
|
||||||
void onWifiSelectionComplete(bool success);
|
void onWifiSelectionComplete(bool success);
|
||||||
void performSync();
|
void performSync();
|
||||||
void performUpload();
|
void performUpload();
|
||||||
|
|||||||
@@ -18,12 +18,12 @@ void QrDisplayActivity::onExit() { Activity::onExit(); }
|
|||||||
void QrDisplayActivity::loop() {
|
void QrDisplayActivity::loop() {
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back) ||
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back) ||
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
onGoBack();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void QrDisplayActivity::render(Activity::RenderLock&&) {
|
void QrDisplayActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
auto metrics = UITheme::getInstance().getMetrics();
|
auto metrics = UITheme::getInstance().getMetrics();
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|||||||
@@ -7,16 +7,14 @@
|
|||||||
|
|
||||||
class QrDisplayActivity final : public Activity {
|
class QrDisplayActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit QrDisplayActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& textPayload,
|
explicit QrDisplayActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& textPayload)
|
||||||
const std::function<void()>& onGoBack)
|
: Activity("QrDisplay", renderer, mappedInput), textPayload(textPayload) {}
|
||||||
: Activity("QrDisplay", renderer, mappedInput), textPayload(textPayload), onGoBack(onGoBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string textPayload;
|
std::string textPayload;
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -79,41 +79,34 @@ std::unique_ptr<Txt> ReaderActivity::loadTxt(const std::string& path) {
|
|||||||
|
|
||||||
void ReaderActivity::goToLibrary(const std::string& fromBookPath) {
|
void ReaderActivity::goToLibrary(const std::string& fromBookPath) {
|
||||||
// If coming from a book, start in that book's folder; otherwise start from root
|
// If coming from a book, start in that book's folder; otherwise start from root
|
||||||
const auto initialPath = fromBookPath.empty() ? "/" : extractFolderPath(fromBookPath);
|
auto initialPath = fromBookPath.empty() ? "/" : extractFolderPath(fromBookPath);
|
||||||
onGoToLibrary(initialPath);
|
activityManager.goToMyLibrary(std::move(initialPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReaderActivity::onGoToEpubReader(std::unique_ptr<Epub> epub) {
|
void ReaderActivity::onGoToEpubReader(std::unique_ptr<Epub> epub) {
|
||||||
const auto epubPath = epub->getPath();
|
const auto epubPath = epub->getPath();
|
||||||
currentBookPath = epubPath;
|
currentBookPath = epubPath;
|
||||||
exitActivity();
|
activityManager.replaceActivity(std::make_unique<EpubReaderActivity>(renderer, mappedInput, std::move(epub)));
|
||||||
enterNewActivity(new EpubReaderActivity(
|
|
||||||
renderer, mappedInput, std::move(epub), [this, epubPath] { goToLibrary(epubPath); }, [this] { onGoBack(); }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReaderActivity::onGoToBmpViewer(const std::string& path) {
|
void ReaderActivity::onGoToBmpViewer(const std::string& path) {
|
||||||
exitActivity();
|
activityManager.replaceActivity(std::make_unique<BmpViewerActivity>(renderer, mappedInput, path));
|
||||||
enterNewActivity(new BmpViewerActivity(renderer, mappedInput, path, [this, path] { goToLibrary(path); }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReaderActivity::onGoToXtcReader(std::unique_ptr<Xtc> xtc) {
|
void ReaderActivity::onGoToXtcReader(std::unique_ptr<Xtc> xtc) {
|
||||||
const auto xtcPath = xtc->getPath();
|
const auto xtcPath = xtc->getPath();
|
||||||
currentBookPath = xtcPath;
|
currentBookPath = xtcPath;
|
||||||
exitActivity();
|
activityManager.replaceActivity(std::make_unique<XtcReaderActivity>(renderer, mappedInput, std::move(xtc)));
|
||||||
enterNewActivity(new XtcReaderActivity(
|
|
||||||
renderer, mappedInput, std::move(xtc), [this, xtcPath] { goToLibrary(xtcPath); }, [this] { onGoBack(); }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReaderActivity::onGoToTxtReader(std::unique_ptr<Txt> txt) {
|
void ReaderActivity::onGoToTxtReader(std::unique_ptr<Txt> txt) {
|
||||||
const auto txtPath = txt->getPath();
|
const auto txtPath = txt->getPath();
|
||||||
currentBookPath = txtPath;
|
currentBookPath = txtPath;
|
||||||
exitActivity();
|
activityManager.replaceActivity(std::make_unique<TxtReaderActivity>(renderer, mappedInput, std::move(txt)));
|
||||||
enterNewActivity(new TxtReaderActivity(
|
|
||||||
renderer, mappedInput, std::move(txt), [this, txtPath] { goToLibrary(txtPath); }, [this] { onGoBack(); }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReaderActivity::onEnter() {
|
void ReaderActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
if (initialBookPath.empty()) {
|
if (initialBookPath.empty()) {
|
||||||
goToLibrary(); // Start from root when entering via Browse
|
goToLibrary(); // Start from root when entering via Browse
|
||||||
@@ -146,3 +139,5 @@ void ReaderActivity::onEnter() {
|
|||||||
onGoToEpubReader(std::move(epub));
|
onGoToEpubReader(std::move(epub));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ReaderActivity::onGoBack() { finish(); }
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "../ActivityWithSubactivity.h"
|
#include "../Activity.h"
|
||||||
#include "activities/home/MyLibraryActivity.h"
|
#include "activities/home/MyLibraryActivity.h"
|
||||||
|
|
||||||
class Epub;
|
class Epub;
|
||||||
class Xtc;
|
class Xtc;
|
||||||
class Txt;
|
class Txt;
|
||||||
|
|
||||||
class ReaderActivity final : public ActivityWithSubactivity {
|
class ReaderActivity final : public Activity {
|
||||||
std::string initialBookPath;
|
std::string initialBookPath;
|
||||||
std::string currentBookPath; // Track current book path for navigation
|
std::string currentBookPath; // Track current book path for navigation
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void(const std::string&)> onGoToLibrary;
|
|
||||||
static std::unique_ptr<Epub> loadEpub(const std::string& path);
|
static std::unique_ptr<Epub> loadEpub(const std::string& path);
|
||||||
static std::unique_ptr<Xtc> loadXtc(const std::string& path);
|
static std::unique_ptr<Xtc> loadXtc(const std::string& path);
|
||||||
static std::unique_ptr<Txt> loadTxt(const std::string& path);
|
static std::unique_ptr<Txt> loadTxt(const std::string& path);
|
||||||
@@ -27,14 +25,11 @@ class ReaderActivity final : public ActivityWithSubactivity {
|
|||||||
void onGoToTxtReader(std::unique_ptr<Txt> txt);
|
void onGoToTxtReader(std::unique_ptr<Txt> txt);
|
||||||
void onGoToBmpViewer(const std::string& path);
|
void onGoToBmpViewer(const std::string& path);
|
||||||
|
|
||||||
|
void onGoBack();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string initialBookPath,
|
explicit ReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string initialBookPath)
|
||||||
const std::function<void()>& onGoBack,
|
: Activity("Reader", renderer, mappedInput), initialBookPath(std::move(initialBookPath)) {}
|
||||||
const std::function<void(const std::string&)>& onGoToLibrary)
|
|
||||||
: ActivityWithSubactivity("Reader", renderer, mappedInput),
|
|
||||||
initialBookPath(std::move(initialBookPath)),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onGoToLibrary(onGoToLibrary) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
bool isReaderActivity() const override { return true; }
|
bool isReaderActivity() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ constexpr uint8_t CACHE_VERSION = 2; // Increment when cache format cha
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void TxtReaderActivity::onEnter() {
|
void TxtReaderActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
if (!txt) {
|
if (!txt) {
|
||||||
return;
|
return;
|
||||||
@@ -61,7 +61,7 @@ void TxtReaderActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TxtReaderActivity::onExit() {
|
void TxtReaderActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
// Reset orientation back to portrait for the rest of the UI
|
// Reset orientation back to portrait for the rest of the UI
|
||||||
renderer.setOrientation(GfxRenderer::Orientation::Portrait);
|
renderer.setOrientation(GfxRenderer::Orientation::Portrait);
|
||||||
@@ -74,14 +74,9 @@ void TxtReaderActivity::onExit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TxtReaderActivity::loop() {
|
void TxtReaderActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Long press BACK (1s+) goes to file selection
|
// Long press BACK (1s+) goes to file selection
|
||||||
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
||||||
onGoBack();
|
activityManager.goToMyLibrary(txt ? txt->getPath() : "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,7 +320,7 @@ bool TxtReaderActivity::loadPageAtOffset(size_t offset, std::vector<std::string>
|
|||||||
return !outLines.empty();
|
return !outLines.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TxtReaderActivity::render(Activity::RenderLock&&) {
|
void TxtReaderActivity::render(RenderLock&&) {
|
||||||
if (!txt) {
|
if (!txt) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,18 +5,15 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
#include "CrossPointSettings.h"
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
class TxtReaderActivity final : public ActivityWithSubactivity {
|
class TxtReaderActivity final : public Activity {
|
||||||
std::unique_ptr<Txt> txt;
|
std::unique_ptr<Txt> txt;
|
||||||
|
|
||||||
int currentPage = 0;
|
int currentPage = 0;
|
||||||
int totalPages = 1;
|
int totalPages = 1;
|
||||||
int pagesUntilFullRefresh = 0;
|
int pagesUntilFullRefresh = 0;
|
||||||
|
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
// Streaming text reader - stores file offsets for each page
|
// Streaming text reader - stores file offsets for each page
|
||||||
std::vector<size_t> pageOffsets; // File offset for start of each page
|
std::vector<size_t> pageOffsets; // File offset for start of each page
|
||||||
std::vector<std::string> currentPageLines;
|
std::vector<std::string> currentPageLines;
|
||||||
@@ -45,14 +42,11 @@ class TxtReaderActivity final : public ActivityWithSubactivity {
|
|||||||
void loadProgress();
|
void loadProgress();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit TxtReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Txt> txt,
|
explicit TxtReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Txt> txt)
|
||||||
const std::function<void()>& onGoBack, const std::function<void()>& onGoHome)
|
: Activity("TxtReader", renderer, mappedInput), txt(std::move(txt)) {}
|
||||||
: ActivityWithSubactivity("TxtReader", renderer, mappedInput),
|
|
||||||
txt(std::move(txt)),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onGoHome(onGoHome) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
bool isReaderActivity() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ constexpr unsigned long goHomeMs = 1000;
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void XtcReaderActivity::onEnter() {
|
void XtcReaderActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
if (!xtc) {
|
if (!xtc) {
|
||||||
return;
|
return;
|
||||||
@@ -47,7 +47,7 @@ void XtcReaderActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void XtcReaderActivity::onExit() {
|
void XtcReaderActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
APP_STATE.readerActivityLoadCount = 0;
|
APP_STATE.readerActivityLoadCount = 0;
|
||||||
APP_STATE.saveToFile();
|
APP_STATE.saveToFile();
|
||||||
@@ -55,33 +55,22 @@ void XtcReaderActivity::onExit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void XtcReaderActivity::loop() {
|
void XtcReaderActivity::loop() {
|
||||||
// Pass input responsibility to sub activity if exists
|
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enter chapter selection activity
|
// Enter chapter selection activity
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
if (xtc && xtc->hasChapters() && !xtc->getChapters().empty()) {
|
if (xtc && xtc->hasChapters() && !xtc->getChapters().empty()) {
|
||||||
exitActivity();
|
startActivityForResult(
|
||||||
enterNewActivity(new XtcReaderChapterSelectionActivity(
|
std::make_unique<XtcReaderChapterSelectionActivity>(renderer, mappedInput, xtc, currentPage),
|
||||||
this->renderer, this->mappedInput, xtc, currentPage,
|
[this](const ActivityResult& result) {
|
||||||
[this] {
|
if (!result.isCancelled) {
|
||||||
exitActivity();
|
currentPage = std::get<PageResult>(result.data).page;
|
||||||
requestUpdate();
|
}
|
||||||
},
|
});
|
||||||
[this](const uint32_t newPage) {
|
|
||||||
currentPage = newPage;
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Long press BACK (1s+) goes to file selection
|
// Long press BACK (1s+) goes to file selection
|
||||||
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
||||||
onGoBack();
|
activityManager.goToMyLibrary(xtc ? xtc->getPath() : "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +124,7 @@ void XtcReaderActivity::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void XtcReaderActivity::render(Activity::RenderLock&&) {
|
void XtcReaderActivity::render(RenderLock&&) {
|
||||||
if (!xtc) {
|
if (!xtc) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,30 +9,24 @@
|
|||||||
|
|
||||||
#include <Xtc.h>
|
#include <Xtc.h>
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
class XtcReaderActivity final : public ActivityWithSubactivity {
|
class XtcReaderActivity final : public Activity {
|
||||||
std::shared_ptr<Xtc> xtc;
|
std::shared_ptr<Xtc> xtc;
|
||||||
|
|
||||||
uint32_t currentPage = 0;
|
uint32_t currentPage = 0;
|
||||||
int pagesUntilFullRefresh = 0;
|
int pagesUntilFullRefresh = 0;
|
||||||
|
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
void renderPage();
|
void renderPage();
|
||||||
void saveProgress() const;
|
void saveProgress() const;
|
||||||
void loadProgress();
|
void loadProgress();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit XtcReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Xtc> xtc,
|
explicit XtcReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Xtc> xtc)
|
||||||
const std::function<void()>& onGoBack, const std::function<void()>& onGoHome)
|
: Activity("XtcReader", renderer, mappedInput), xtc(std::move(xtc)) {}
|
||||||
: ActivityWithSubactivity("XtcReader", renderer, mappedInput),
|
|
||||||
xtc(std::move(xtc)),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onGoHome(onGoHome) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
bool isReaderActivity() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -59,10 +59,14 @@ void XtcReaderChapterSelectionActivity::loop() {
|
|||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
const auto& chapters = xtc->getChapters();
|
const auto& chapters = xtc->getChapters();
|
||||||
if (!chapters.empty() && selectorIndex >= 0 && selectorIndex < static_cast<int>(chapters.size())) {
|
if (!chapters.empty() && selectorIndex >= 0 && selectorIndex < static_cast<int>(chapters.size())) {
|
||||||
onSelectPage(chapters[selectorIndex].startPage);
|
setResult(PageResult{chapters[selectorIndex].startPage});
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
onGoBack();
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
buttonNavigator.onNextRelease([this, totalItems] {
|
buttonNavigator.onNextRelease([this, totalItems] {
|
||||||
@@ -86,7 +90,7 @@ void XtcReaderChapterSelectionActivity::loop() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void XtcReaderChapterSelectionActivity::render(Activity::RenderLock&&) {
|
void XtcReaderChapterSelectionActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|||||||
@@ -12,24 +12,16 @@ class XtcReaderChapterSelectionActivity final : public Activity {
|
|||||||
uint32_t currentPage = 0;
|
uint32_t currentPage = 0;
|
||||||
int selectorIndex = 0;
|
int selectorIndex = 0;
|
||||||
|
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void(uint32_t newPage)> onSelectPage;
|
|
||||||
|
|
||||||
int getPageItems() const;
|
int getPageItems() const;
|
||||||
int findChapterIndexForPage(uint32_t page) const;
|
int findChapterIndexForPage(uint32_t page) const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit XtcReaderChapterSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit XtcReaderChapterSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const std::shared_ptr<Xtc>& xtc, uint32_t currentPage,
|
const std::shared_ptr<Xtc>& xtc, uint32_t currentPage)
|
||||||
const std::function<void()>& onGoBack,
|
: Activity("XtcReaderChapterSelection", renderer, mappedInput), xtc(xtc), currentPage(currentPage) {}
|
||||||
const std::function<void(uint32_t newPage)>& onSelectPage)
|
|
||||||
: Activity("XtcReaderChapterSelection", renderer, mappedInput),
|
|
||||||
xtc(xtc),
|
|
||||||
currentPage(currentPage),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onSelectPage(onSelectPage) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
bool isReaderActivity() const override { return true; }
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -52,20 +52,20 @@ void ButtonRemapActivity::loop() {
|
|||||||
SETTINGS.frontButtonLeft = CrossPointSettings::FRONT_HW_LEFT;
|
SETTINGS.frontButtonLeft = CrossPointSettings::FRONT_HW_LEFT;
|
||||||
SETTINGS.frontButtonRight = CrossPointSettings::FRONT_HW_RIGHT;
|
SETTINGS.frontButtonRight = CrossPointSettings::FRONT_HW_RIGHT;
|
||||||
SETTINGS.saveToFile();
|
SETTINGS.saveToFile();
|
||||||
onBack();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Down)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Down)) {
|
||||||
// Exit without changing settings.
|
// Exit without changing settings.
|
||||||
onBack();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
// Wait for the UI to refresh before accepting another assignment.
|
// Make sure UI done rendering before accepting another assignment.
|
||||||
// This avoids rapid double-presses that can advance the step without a visible redraw.
|
// This avoids rapid double-presses that can advance the step without a visible redraw.
|
||||||
requestUpdateAndWait();
|
RenderLock lock(*this);
|
||||||
|
|
||||||
// Wait for a front button press to assign to the current role.
|
// Wait for a front button press to assign to the current role.
|
||||||
const int pressedButton = mappedInput.getPressedFrontButton();
|
const int pressedButton = mappedInput.getPressedFrontButton();
|
||||||
@@ -86,7 +86,7 @@ void ButtonRemapActivity::loop() {
|
|||||||
// All roles assigned; save to settings and exit.
|
// All roles assigned; save to settings and exit.
|
||||||
applyTempMapping();
|
applyTempMapping();
|
||||||
SETTINGS.saveToFile();
|
SETTINGS.saveToFile();
|
||||||
onBack();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ void ButtonRemapActivity::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ButtonRemapActivity::render(Activity::RenderLock&&) {
|
void ButtonRemapActivity::render(RenderLock&&) {
|
||||||
const auto labelForHardware = [&](uint8_t hardwareIndex) -> const char* {
|
const auto labelForHardware = [&](uint8_t hardwareIndex) -> const char* {
|
||||||
for (uint8_t i = 0; i < kRoleCount; i++) {
|
for (uint8_t i = 0; i < kRoleCount; i++) {
|
||||||
if (tempMapping[i] == hardwareIndex) {
|
if (tempMapping[i] == hardwareIndex) {
|
||||||
|
|||||||
@@ -7,20 +7,17 @@
|
|||||||
|
|
||||||
class ButtonRemapActivity final : public Activity {
|
class ButtonRemapActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit ButtonRemapActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit ButtonRemapActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onBack)
|
: Activity("ButtonRemap", renderer, mappedInput) {}
|
||||||
: Activity("ButtonRemap", renderer, mappedInput), onBack(onBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Rendering task state.
|
// Rendering task state.
|
||||||
|
|
||||||
// Callback used to exit the remap flow back to the settings list.
|
|
||||||
const std::function<void()> onBack;
|
|
||||||
// Index of the logical role currently awaiting input.
|
// Index of the logical role currently awaiting input.
|
||||||
uint8_t currentStep = 0;
|
uint8_t currentStep = 0;
|
||||||
// Temporary mapping from logical role -> hardware button index.
|
// Temporary mapping from logical role -> hardware button index.
|
||||||
|
|||||||
@@ -17,22 +17,17 @@ const StrId menuNames[MENU_ITEMS] = {StrId::STR_CALIBRE_WEB_URL, StrId::STR_USER
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void CalibreSettingsActivity::onEnter() {
|
void CalibreSettingsActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
selectedIndex = 0;
|
selectedIndex = 0;
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CalibreSettingsActivity::onExit() { ActivityWithSubactivity::onExit(); }
|
void CalibreSettingsActivity::onExit() { Activity::onExit(); }
|
||||||
|
|
||||||
void CalibreSettingsActivity::loop() {
|
void CalibreSettingsActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
onBack();
|
activityManager.popActivity();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,62 +51,44 @@ void CalibreSettingsActivity::loop() {
|
|||||||
void CalibreSettingsActivity::handleSelection() {
|
void CalibreSettingsActivity::handleSelection() {
|
||||||
if (selectedIndex == 0) {
|
if (selectedIndex == 0) {
|
||||||
// OPDS Server URL
|
// OPDS Server URL
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_CALIBRE_WEB_URL),
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
SETTINGS.opdsServerUrl, 127, false),
|
||||||
renderer, mappedInput, tr(STR_CALIBRE_WEB_URL), SETTINGS.opdsServerUrl,
|
[this](const ActivityResult& result) {
|
||||||
127, // maxLength
|
if (!result.isCancelled) {
|
||||||
false, // not password
|
const auto& kb = std::get<KeyboardResult>(result.data);
|
||||||
[this](const std::string& url) {
|
strncpy(SETTINGS.opdsServerUrl, kb.text.c_str(), sizeof(SETTINGS.opdsServerUrl) - 1);
|
||||||
strncpy(SETTINGS.opdsServerUrl, url.c_str(), sizeof(SETTINGS.opdsServerUrl) - 1);
|
SETTINGS.opdsServerUrl[sizeof(SETTINGS.opdsServerUrl) - 1] = '\0';
|
||||||
SETTINGS.opdsServerUrl[sizeof(SETTINGS.opdsServerUrl) - 1] = '\0';
|
SETTINGS.saveToFile();
|
||||||
SETTINGS.saveToFile();
|
}
|
||||||
exitActivity();
|
});
|
||||||
requestUpdate();
|
|
||||||
},
|
|
||||||
[this]() {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
} else if (selectedIndex == 1) {
|
} else if (selectedIndex == 1) {
|
||||||
// Username
|
// Username
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_USERNAME),
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
SETTINGS.opdsUsername, 63, false),
|
||||||
renderer, mappedInput, tr(STR_USERNAME), SETTINGS.opdsUsername,
|
[this](const ActivityResult& result) {
|
||||||
63, // maxLength
|
if (!result.isCancelled) {
|
||||||
false, // not password
|
const auto& kb = std::get<KeyboardResult>(result.data);
|
||||||
[this](const std::string& username) {
|
strncpy(SETTINGS.opdsUsername, kb.text.c_str(), sizeof(SETTINGS.opdsUsername) - 1);
|
||||||
strncpy(SETTINGS.opdsUsername, username.c_str(), sizeof(SETTINGS.opdsUsername) - 1);
|
SETTINGS.opdsUsername[sizeof(SETTINGS.opdsUsername) - 1] = '\0';
|
||||||
SETTINGS.opdsUsername[sizeof(SETTINGS.opdsUsername) - 1] = '\0';
|
SETTINGS.saveToFile();
|
||||||
SETTINGS.saveToFile();
|
}
|
||||||
exitActivity();
|
});
|
||||||
requestUpdate();
|
|
||||||
},
|
|
||||||
[this]() {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
} else if (selectedIndex == 2) {
|
} else if (selectedIndex == 2) {
|
||||||
// Password
|
// Password
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_PASSWORD),
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
SETTINGS.opdsPassword, 63, false),
|
||||||
renderer, mappedInput, tr(STR_PASSWORD), SETTINGS.opdsPassword,
|
[this](const ActivityResult& result) {
|
||||||
63, // maxLength
|
if (!result.isCancelled) {
|
||||||
false, // not password mode
|
const auto& kb = std::get<KeyboardResult>(result.data);
|
||||||
[this](const std::string& password) {
|
strncpy(SETTINGS.opdsPassword, kb.text.c_str(), sizeof(SETTINGS.opdsPassword) - 1);
|
||||||
strncpy(SETTINGS.opdsPassword, password.c_str(), sizeof(SETTINGS.opdsPassword) - 1);
|
SETTINGS.opdsPassword[sizeof(SETTINGS.opdsPassword) - 1] = '\0';
|
||||||
SETTINGS.opdsPassword[sizeof(SETTINGS.opdsPassword) - 1] = '\0';
|
SETTINGS.saveToFile();
|
||||||
SETTINGS.saveToFile();
|
}
|
||||||
exitActivity();
|
});
|
||||||
requestUpdate();
|
|
||||||
},
|
|
||||||
[this]() {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CalibreSettingsActivity::render(Activity::RenderLock&&) {
|
void CalibreSettingsActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
|
|||||||
@@ -1,29 +1,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Submenu for OPDS Browser settings.
|
* Submenu for OPDS Browser settings.
|
||||||
* Shows OPDS Server URL and HTTP authentication options.
|
* Shows OPDS Server URL and HTTP authentication options.
|
||||||
*/
|
*/
|
||||||
class CalibreSettingsActivity final : public ActivityWithSubactivity {
|
class CalibreSettingsActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit CalibreSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit CalibreSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onBack)
|
: Activity("CalibreSettings", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("CalibreSettings", renderer, mappedInput), onBack(onBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
|
|
||||||
size_t selectedIndex = 0;
|
size_t selectedIndex = 0;
|
||||||
const std::function<void()> onBack;
|
|
||||||
void handleSelection();
|
void handleSelection();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,15 +10,15 @@
|
|||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
void ClearCacheActivity::onEnter() {
|
void ClearCacheActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
state = WARNING;
|
state = WARNING;
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClearCacheActivity::onExit() { ActivityWithSubactivity::onExit(); }
|
void ClearCacheActivity::onExit() { Activity::onExit(); }
|
||||||
|
|
||||||
void ClearCacheActivity::render(Activity::RenderLock&&) {
|
void ClearCacheActivity::render(RenderLock&&) {
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
const auto pageHeight = renderer.getScreenHeight();
|
||||||
|
|||||||
@@ -2,26 +2,25 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
class ClearCacheActivity final : public ActivityWithSubactivity {
|
class ClearCacheActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit ClearCacheActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit ClearCacheActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& goBack)
|
: Activity("ClearCache", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("ClearCache", renderer, mappedInput), goBack(goBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
bool skipLoopDelay() override { return true; } // Prevent power-saving mode
|
bool skipLoopDelay() override { return true; } // Prevent power-saving mode
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum State { WARNING, CLEARING, SUCCESS, FAILED };
|
enum State { WARNING, CLEARING, SUCCESS, FAILED };
|
||||||
|
|
||||||
State state = WARNING;
|
State state = WARNING;
|
||||||
|
|
||||||
const std::function<void()> goBack;
|
void goBack() { finish(); }
|
||||||
|
|
||||||
int clearedCount = 0;
|
int clearedCount = 0;
|
||||||
int failedCount = 0;
|
int failedCount = 0;
|
||||||
|
|||||||
@@ -12,8 +12,6 @@
|
|||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
void KOReaderAuthActivity::onWifiSelectionComplete(const bool success) {
|
void KOReaderAuthActivity::onWifiSelectionComplete(const bool success) {
|
||||||
exitActivity();
|
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
{
|
{
|
||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
@@ -51,7 +49,7 @@ void KOReaderAuthActivity::performAuthentication() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderAuthActivity::onEnter() {
|
void KOReaderAuthActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
// Turn on WiFi
|
// Turn on WiFi
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
@@ -74,12 +72,12 @@ void KOReaderAuthActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Launch WiFi selection
|
// Launch WiFi selection
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderAuthActivity::onExit() {
|
void KOReaderAuthActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
// Turn off wifi
|
// Turn off wifi
|
||||||
WiFi.disconnect(false);
|
WiFi.disconnect(false);
|
||||||
@@ -88,7 +86,7 @@ void KOReaderAuthActivity::onExit() {
|
|||||||
delay(100);
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderAuthActivity::render(Activity::RenderLock&&) {
|
void KOReaderAuthActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
@@ -115,15 +113,10 @@ void KOReaderAuthActivity::render(Activity::RenderLock&&) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderAuthActivity::loop() {
|
void KOReaderAuthActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == SUCCESS || state == FAILED) {
|
if (state == SUCCESS || state == FAILED) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back) ||
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back) ||
|
||||||
mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
||||||
onComplete();
|
finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,21 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activity for testing KOReader credentials.
|
* Activity for testing KOReader credentials.
|
||||||
* Connects to WiFi and authenticates with the KOReader sync server.
|
* Connects to WiFi and authenticates with the KOReader sync server.
|
||||||
*/
|
*/
|
||||||
class KOReaderAuthActivity final : public ActivityWithSubactivity {
|
class KOReaderAuthActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit KOReaderAuthActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit KOReaderAuthActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onComplete)
|
: Activity("KOReaderAuth", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("KOReaderAuth", renderer, mappedInput), onComplete(onComplete) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
bool preventAutoSleep() override { return state == CONNECTING || state == AUTHENTICATING; }
|
bool preventAutoSleep() override { return state == CONNECTING || state == AUTHENTICATING; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -27,8 +26,6 @@ class KOReaderAuthActivity final : public ActivityWithSubactivity {
|
|||||||
std::string statusMessage;
|
std::string statusMessage;
|
||||||
std::string errorMessage;
|
std::string errorMessage;
|
||||||
|
|
||||||
const std::function<void()> onComplete;
|
|
||||||
|
|
||||||
void onWifiSelectionComplete(bool success);
|
void onWifiSelectionComplete(bool success);
|
||||||
void performAuthentication();
|
void performAuthentication();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,22 +19,17 @@ const StrId menuNames[MENU_ITEMS] = {StrId::STR_USERNAME, StrId::STR_PASSWORD, S
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void KOReaderSettingsActivity::onEnter() {
|
void KOReaderSettingsActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
selectedIndex = 0;
|
selectedIndex = 0;
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderSettingsActivity::onExit() { ActivityWithSubactivity::onExit(); }
|
void KOReaderSettingsActivity::onExit() { Activity::onExit(); }
|
||||||
|
|
||||||
void KOReaderSettingsActivity::loop() {
|
void KOReaderSettingsActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
onBack();
|
activityManager.popActivity();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,59 +53,46 @@ void KOReaderSettingsActivity::loop() {
|
|||||||
void KOReaderSettingsActivity::handleSelection() {
|
void KOReaderSettingsActivity::handleSelection() {
|
||||||
if (selectedIndex == 0) {
|
if (selectedIndex == 0) {
|
||||||
// Username
|
// Username
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_KOREADER_USERNAME),
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
KOREADER_STORE.getUsername(),
|
||||||
renderer, mappedInput, tr(STR_KOREADER_USERNAME), KOREADER_STORE.getUsername(),
|
64, // maxLength
|
||||||
64, // maxLength
|
false), // not password
|
||||||
false, // not password
|
[this](const ActivityResult& result) {
|
||||||
[this](const std::string& username) {
|
if (!result.isCancelled) {
|
||||||
KOREADER_STORE.setCredentials(username, KOREADER_STORE.getPassword());
|
const auto& kb = std::get<KeyboardResult>(result.data);
|
||||||
KOREADER_STORE.saveToFile();
|
KOREADER_STORE.setCredentials(kb.text, KOREADER_STORE.getPassword());
|
||||||
exitActivity();
|
KOREADER_STORE.saveToFile();
|
||||||
requestUpdate();
|
}
|
||||||
},
|
});
|
||||||
[this]() {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
} else if (selectedIndex == 1) {
|
} else if (selectedIndex == 1) {
|
||||||
// Password
|
// Password
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_KOREADER_PASSWORD),
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
KOREADER_STORE.getPassword(),
|
||||||
renderer, mappedInput, tr(STR_KOREADER_PASSWORD), KOREADER_STORE.getPassword(),
|
64, // maxLength
|
||||||
64, // maxLength
|
false), // show characters
|
||||||
false, // show characters
|
[this](const ActivityResult& result) {
|
||||||
[this](const std::string& password) {
|
if (!result.isCancelled) {
|
||||||
KOREADER_STORE.setCredentials(KOREADER_STORE.getUsername(), password);
|
const auto& kb = std::get<KeyboardResult>(result.data);
|
||||||
KOREADER_STORE.saveToFile();
|
KOREADER_STORE.setCredentials(KOREADER_STORE.getUsername(), kb.text);
|
||||||
exitActivity();
|
KOREADER_STORE.saveToFile();
|
||||||
requestUpdate();
|
}
|
||||||
},
|
});
|
||||||
[this]() {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
} else if (selectedIndex == 2) {
|
} else if (selectedIndex == 2) {
|
||||||
// Sync Server URL - prefill with https:// if empty to save typing
|
// Sync Server URL - prefill with https:// if empty to save typing
|
||||||
const std::string currentUrl = KOREADER_STORE.getServerUrl();
|
const std::string currentUrl = KOREADER_STORE.getServerUrl();
|
||||||
const std::string prefillUrl = currentUrl.empty() ? "https://" : currentUrl;
|
const std::string prefillUrl = currentUrl.empty() ? "https://" : currentUrl;
|
||||||
exitActivity();
|
startActivityForResult(
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_SYNC_SERVER_URL), prefillUrl,
|
||||||
renderer, mappedInput, tr(STR_SYNC_SERVER_URL), prefillUrl,
|
128, // maxLength - URLs can be long
|
||||||
128, // maxLength - URLs can be long
|
false), // not password
|
||||||
false, // not password
|
[this](const ActivityResult& result) {
|
||||||
[this](const std::string& url) {
|
if (!result.isCancelled) {
|
||||||
// Clear if user just left the prefilled https://
|
const auto& kb = std::get<KeyboardResult>(result.data);
|
||||||
const std::string urlToSave = (url == "https://" || url == "http://") ? "" : url;
|
const std::string urlToSave = (kb.text == "https://" || kb.text == "http://") ? "" : kb.text;
|
||||||
KOREADER_STORE.setServerUrl(urlToSave);
|
KOREADER_STORE.setServerUrl(urlToSave);
|
||||||
KOREADER_STORE.saveToFile();
|
KOREADER_STORE.saveToFile();
|
||||||
exitActivity();
|
}
|
||||||
requestUpdate();
|
});
|
||||||
},
|
|
||||||
[this]() {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
} else if (selectedIndex == 3) {
|
} else if (selectedIndex == 3) {
|
||||||
// Document Matching - toggle between Filename and Binary
|
// Document Matching - toggle between Filename and Binary
|
||||||
const auto current = KOREADER_STORE.getMatchMethod();
|
const auto current = KOREADER_STORE.getMatchMethod();
|
||||||
@@ -125,15 +107,11 @@ void KOReaderSettingsActivity::handleSelection() {
|
|||||||
// Can't authenticate without credentials - just show message briefly
|
// Can't authenticate without credentials - just show message briefly
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
exitActivity();
|
startActivityForResult(std::make_unique<KOReaderAuthActivity>(renderer, mappedInput), [](const ActivityResult&) {});
|
||||||
enterNewActivity(new KOReaderAuthActivity(renderer, mappedInput, [this] {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KOReaderSettingsActivity::render(Activity::RenderLock&&) {
|
void KOReaderSettingsActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
|
|||||||
@@ -1,30 +1,26 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include "activities/Activity.h"
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Submenu for KOReader Sync settings.
|
* Submenu for KOReader Sync settings.
|
||||||
* Shows username, password, and authenticate options.
|
* Shows username, password, and authenticate options.
|
||||||
*/
|
*/
|
||||||
class KOReaderSettingsActivity final : public ActivityWithSubactivity {
|
class KOReaderSettingsActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit KOReaderSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit KOReaderSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onBack)
|
: Activity("KOReaderSettings", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("KOReaderSettings", renderer, mappedInput), onBack(onBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
|
|
||||||
size_t selectedIndex = 0;
|
size_t selectedIndex = 0;
|
||||||
const std::function<void()> onBack;
|
|
||||||
|
|
||||||
void handleSelection();
|
void handleSelection();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ void LanguageSelectActivity::handleSelection() {
|
|||||||
onBack();
|
onBack();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LanguageSelectActivity::render(Activity::RenderLock&&) {
|
void LanguageSelectActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include "../ActivityWithSubactivity.h"
|
#include "../Activity.h"
|
||||||
#include "components/UITheme.h"
|
#include "components/UITheme.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
@@ -16,19 +16,18 @@ class MappedInputManager;
|
|||||||
*/
|
*/
|
||||||
class LanguageSelectActivity final : public Activity {
|
class LanguageSelectActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit LanguageSelectActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit LanguageSelectActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onBack)
|
: Activity("LanguageSelect", renderer, mappedInput) {}
|
||||||
: Activity("LanguageSelect", renderer, mappedInput), onBack(onBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handleSelection();
|
void handleSelection();
|
||||||
|
|
||||||
std::function<void()> onBack;
|
void onBack() { finish(); }
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
int selectedIndex = 0;
|
int selectedIndex = 0;
|
||||||
constexpr static uint8_t totalItems = getLanguageCount();
|
constexpr static uint8_t totalItems = getLanguageCount();
|
||||||
|
|||||||
@@ -11,11 +11,9 @@
|
|||||||
#include "network/OtaUpdater.h"
|
#include "network/OtaUpdater.h"
|
||||||
|
|
||||||
void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
||||||
exitActivity();
|
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
LOG_ERR("OTA", "WiFi connection failed, exiting");
|
LOG_ERR("OTA", "WiFi connection failed, exiting");
|
||||||
goBack();
|
activityManager.popActivity();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +32,6 @@ void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
|||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
state = FAILED;
|
state = FAILED;
|
||||||
}
|
}
|
||||||
requestUpdate();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +41,6 @@ void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
|||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
state = NO_UPDATE;
|
state = NO_UPDATE;
|
||||||
}
|
}
|
||||||
requestUpdate();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,11 +48,10 @@ void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
|||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
state = WAITING_CONFIRMATION;
|
state = WAITING_CONFIRMATION;
|
||||||
}
|
}
|
||||||
requestUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OtaUpdateActivity::onEnter() {
|
void OtaUpdateActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
// Turn on WiFi immediately
|
// Turn on WiFi immediately
|
||||||
LOG_DBG("OTA", "Turning on WiFi...");
|
LOG_DBG("OTA", "Turning on WiFi...");
|
||||||
@@ -64,12 +59,12 @@ void OtaUpdateActivity::onEnter() {
|
|||||||
|
|
||||||
// Launch WiFi selection subactivity
|
// Launch WiFi selection subactivity
|
||||||
LOG_DBG("OTA", "Launching WifiSelectionActivity...");
|
LOG_DBG("OTA", "Launching WifiSelectionActivity...");
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void OtaUpdateActivity::onExit() {
|
void OtaUpdateActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
// Turn off wifi
|
// Turn off wifi
|
||||||
WiFi.disconnect(false); // false = don't erase credentials, send disconnect frame
|
WiFi.disconnect(false); // false = don't erase credentials, send disconnect frame
|
||||||
@@ -78,12 +73,7 @@ void OtaUpdateActivity::onExit() {
|
|||||||
delay(100); // Allow WiFi hardware to fully power down
|
delay(100); // Allow WiFi hardware to fully power down
|
||||||
}
|
}
|
||||||
|
|
||||||
void OtaUpdateActivity::render(Activity::RenderLock&&) {
|
void OtaUpdateActivity::render(RenderLock&&) {
|
||||||
if (subActivity) {
|
|
||||||
// Subactivity handles its own rendering
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
const auto pageHeight = renderer.getScreenHeight();
|
||||||
@@ -154,11 +144,6 @@ void OtaUpdateActivity::loop() {
|
|||||||
requestUpdate();
|
requestUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == WAITING_CONFIRMATION) {
|
if (state == WAITING_CONFIRMATION) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
||||||
LOG_DBG("OTA", "New update available, starting download...");
|
LOG_DBG("OTA", "New update available, starting download...");
|
||||||
@@ -166,7 +151,6 @@ void OtaUpdateActivity::loop() {
|
|||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
state = UPDATE_IN_PROGRESS;
|
state = UPDATE_IN_PROGRESS;
|
||||||
}
|
}
|
||||||
requestUpdate();
|
|
||||||
requestUpdateAndWait();
|
requestUpdateAndWait();
|
||||||
const auto res = updater.installUpdate();
|
const auto res = updater.installUpdate();
|
||||||
|
|
||||||
@@ -188,7 +172,7 @@ void OtaUpdateActivity::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
goBack();
|
activityManager.popActivity();
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -196,14 +180,14 @@ void OtaUpdateActivity::loop() {
|
|||||||
|
|
||||||
if (state == FAILED) {
|
if (state == FAILED) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
goBack();
|
activityManager.popActivity();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (state == NO_UPDATE) {
|
if (state == NO_UPDATE) {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
goBack();
|
activityManager.popActivity();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
#include "network/OtaUpdater.h"
|
#include "network/OtaUpdater.h"
|
||||||
|
|
||||||
class OtaUpdateActivity : public ActivityWithSubactivity {
|
class OtaUpdateActivity : public Activity {
|
||||||
enum State {
|
enum State {
|
||||||
WIFI_SELECTION,
|
WIFI_SELECTION,
|
||||||
CHECKING_FOR_UPDATE,
|
CHECKING_FOR_UPDATE,
|
||||||
@@ -18,7 +18,6 @@ class OtaUpdateActivity : public ActivityWithSubactivity {
|
|||||||
// Can't initialize this to 0 or the first render doesn't happen
|
// Can't initialize this to 0 or the first render doesn't happen
|
||||||
static constexpr unsigned int UNINITIALIZED_PERCENTAGE = 111;
|
static constexpr unsigned int UNINITIALIZED_PERCENTAGE = 111;
|
||||||
|
|
||||||
const std::function<void()> goBack;
|
|
||||||
State state = WIFI_SELECTION;
|
State state = WIFI_SELECTION;
|
||||||
unsigned int lastUpdaterPercentage = UNINITIALIZED_PERCENTAGE;
|
unsigned int lastUpdaterPercentage = UNINITIALIZED_PERCENTAGE;
|
||||||
OtaUpdater updater;
|
OtaUpdater updater;
|
||||||
@@ -26,13 +25,12 @@ class OtaUpdateActivity : public ActivityWithSubactivity {
|
|||||||
void onWifiSelectionComplete(bool success);
|
void onWifiSelectionComplete(bool success);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit OtaUpdateActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit OtaUpdateActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& goBack)
|
: Activity("OtaUpdate", renderer, mappedInput), updater() {}
|
||||||
: ActivityWithSubactivity("OtaUpdate", renderer, mappedInput), goBack(goBack), updater() {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
bool preventAutoSleep() override { return state == CHECKING_FOR_UPDATE || state == UPDATE_IN_PROGRESS; }
|
bool preventAutoSleep() override { return state == CHECKING_FOR_UPDATE || state == UPDATE_IN_PROGRESS; }
|
||||||
bool skipLoopDelay() override { return true; } // Prevent power-saving mode
|
bool skipLoopDelay() override { return true; } // Prevent power-saving mode
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -67,16 +67,12 @@ void SettingsActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SettingsActivity::onExit() {
|
void SettingsActivity::onExit() {
|
||||||
ActivityWithSubactivity::onExit();
|
Activity::onExit();
|
||||||
|
|
||||||
UITheme::getInstance().reload(); // Re-apply theme in case it was changed
|
UITheme::getInstance().reload(); // Re-apply theme in case it was changed
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsActivity::loop() {
|
void SettingsActivity::loop() {
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
bool hasChangedCategory = false;
|
bool hasChangedCategory = false;
|
||||||
|
|
||||||
// Handle actions with early return
|
// Handle actions with early return
|
||||||
@@ -164,50 +160,38 @@ void SettingsActivity::toggleCurrentSetting() {
|
|||||||
SETTINGS.*(setting.valuePtr) = currentValue + setting.valueRange.step;
|
SETTINGS.*(setting.valuePtr) = currentValue + setting.valueRange.step;
|
||||||
}
|
}
|
||||||
} else if (setting.type == SettingType::ACTION) {
|
} else if (setting.type == SettingType::ACTION) {
|
||||||
auto enterSubActivity = [this](Activity* activity) {
|
auto resultHandler = [this](const ActivityResult&) { SETTINGS.saveToFile(); };
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(activity);
|
|
||||||
};
|
|
||||||
|
|
||||||
auto onComplete = [this] {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
};
|
|
||||||
|
|
||||||
auto onCompleteBool = [this](bool) {
|
|
||||||
exitActivity();
|
|
||||||
requestUpdate();
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (setting.action) {
|
switch (setting.action) {
|
||||||
case SettingAction::RemapFrontButtons:
|
case SettingAction::RemapFrontButtons:
|
||||||
enterSubActivity(new ButtonRemapActivity(renderer, mappedInput, onComplete));
|
startActivityForResult(std::make_unique<ButtonRemapActivity>(renderer, mappedInput), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::CustomiseStatusBar:
|
case SettingAction::CustomiseStatusBar:
|
||||||
enterSubActivity(new StatusBarSettingsActivity(renderer, mappedInput, onComplete));
|
startActivityForResult(std::make_unique<StatusBarSettingsActivity>(renderer, mappedInput), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::KOReaderSync:
|
case SettingAction::KOReaderSync:
|
||||||
enterSubActivity(new KOReaderSettingsActivity(renderer, mappedInput, onComplete));
|
startActivityForResult(std::make_unique<KOReaderSettingsActivity>(renderer, mappedInput), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::OPDSBrowser:
|
case SettingAction::OPDSBrowser:
|
||||||
enterSubActivity(new CalibreSettingsActivity(renderer, mappedInput, onComplete));
|
startActivityForResult(std::make_unique<CalibreSettingsActivity>(renderer, mappedInput), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::Network:
|
case SettingAction::Network:
|
||||||
enterSubActivity(new WifiSelectionActivity(renderer, mappedInput, onCompleteBool, false));
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput, false), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::ClearCache:
|
case SettingAction::ClearCache:
|
||||||
enterSubActivity(new ClearCacheActivity(renderer, mappedInput, onComplete));
|
startActivityForResult(std::make_unique<ClearCacheActivity>(renderer, mappedInput), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::CheckForUpdates:
|
case SettingAction::CheckForUpdates:
|
||||||
enterSubActivity(new OtaUpdateActivity(renderer, mappedInput, onComplete));
|
startActivityForResult(std::make_unique<OtaUpdateActivity>(renderer, mappedInput), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::Language:
|
case SettingAction::Language:
|
||||||
enterSubActivity(new LanguageSelectActivity(renderer, mappedInput, onComplete));
|
startActivityForResult(std::make_unique<LanguageSelectActivity>(renderer, mappedInput), resultHandler);
|
||||||
break;
|
break;
|
||||||
case SettingAction::None:
|
case SettingAction::None:
|
||||||
// Do nothing
|
// Do nothing
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
return; // Results will be handled in the result handler, so we can return early here
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -215,7 +199,7 @@ void SettingsActivity::toggleCurrentSetting() {
|
|||||||
SETTINGS.saveToFile();
|
SETTINGS.saveToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingsActivity::render(Activity::RenderLock&&) {
|
void SettingsActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
#include "activities/Activity.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
class CrossPointSettings;
|
class CrossPointSettings;
|
||||||
@@ -134,7 +134,7 @@ struct SettingInfo {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SettingsActivity final : public ActivityWithSubactivity {
|
class SettingsActivity final : public Activity {
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
|
|
||||||
int selectedCategoryIndex = 0; // Currently selected category
|
int selectedCategoryIndex = 0; // Currently selected category
|
||||||
@@ -148,8 +148,6 @@ class SettingsActivity final : public ActivityWithSubactivity {
|
|||||||
std::vector<SettingInfo> systemSettings;
|
std::vector<SettingInfo> systemSettings;
|
||||||
const std::vector<SettingInfo>* currentSettings = nullptr;
|
const std::vector<SettingInfo>* currentSettings = nullptr;
|
||||||
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
static constexpr int categoryCount = 4;
|
static constexpr int categoryCount = 4;
|
||||||
static const StrId categoryNames[categoryCount];
|
static const StrId categoryNames[categoryCount];
|
||||||
|
|
||||||
@@ -157,11 +155,10 @@ class SettingsActivity final : public ActivityWithSubactivity {
|
|||||||
void toggleCurrentSetting();
|
void toggleCurrentSetting();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit SettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit SettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onGoHome)
|
: Activity("Settings", renderer, mappedInput) {}
|
||||||
: ActivityWithSubactivity("Settings", renderer, mappedInput), onGoHome(onGoHome) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ void StatusBarSettingsActivity::onExit() { Activity::onExit(); }
|
|||||||
|
|
||||||
void StatusBarSettingsActivity::loop() {
|
void StatusBarSettingsActivity::loop() {
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
onBack();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ void StatusBarSettingsActivity::handleSelection() {
|
|||||||
SETTINGS.saveToFile();
|
SETTINGS.saveToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusBarSettingsActivity::render(Activity::RenderLock&&) {
|
void StatusBarSettingsActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
auto metrics = UITheme::getInstance().getMetrics();
|
auto metrics = UITheme::getInstance().getMetrics();
|
||||||
|
|||||||
@@ -9,22 +9,18 @@
|
|||||||
// Reader status bar configuration activity
|
// Reader status bar configuration activity
|
||||||
class StatusBarSettingsActivity final : public Activity {
|
class StatusBarSettingsActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit StatusBarSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit StatusBarSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||||
const std::function<void()>& onBack)
|
: Activity("StatusBarSettings", renderer, mappedInput) {}
|
||||||
: Activity("StatusBarSettings", renderer, mappedInput), onBack(onBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
|
|
||||||
int selectedIndex = 0;
|
int selectedIndex = 0;
|
||||||
|
|
||||||
const std::function<void()> onBack;
|
|
||||||
|
|
||||||
static void taskTrampoline(void* param);
|
|
||||||
void handleSelection();
|
void handleSelection();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,9 +8,8 @@
|
|||||||
#include "components/UITheme.h"
|
#include "components/UITheme.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
BmpViewerActivity::BmpViewerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string path,
|
BmpViewerActivity::BmpViewerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string path)
|
||||||
std::function<void()> onGoBack)
|
: Activity("BmpViewer", renderer, mappedInput), filePath(std::move(path)) {}
|
||||||
: Activity("BmpViewer", renderer, mappedInput), filePath(std::move(path)), onGoBack(std::move(onGoBack)) {}
|
|
||||||
|
|
||||||
void BmpViewerActivity::onEnter() {
|
void BmpViewerActivity::onEnter() {
|
||||||
Activity::onEnter();
|
Activity::onEnter();
|
||||||
@@ -95,7 +94,7 @@ void BmpViewerActivity::loop() {
|
|||||||
Activity::loop();
|
Activity::loop();
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
if (onGoBack) onGoBack();
|
onGoHome();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,8 +8,7 @@
|
|||||||
|
|
||||||
class BmpViewerActivity final : public Activity {
|
class BmpViewerActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
BmpViewerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string filePath,
|
BmpViewerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string filePath);
|
||||||
std::function<void()> onGoBack);
|
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
@@ -17,5 +16,4 @@ class BmpViewerActivity final : public Activity {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::string filePath;
|
std::string filePath;
|
||||||
std::function<void()> onGoBack;
|
|
||||||
};
|
};
|
||||||
@@ -57,13 +57,13 @@ char KeyboardEntryActivity::getSelectedChar() const {
|
|||||||
return layout[selectedRow][selectedCol];
|
return layout[selectedRow][selectedCol];
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyboardEntryActivity::handleKeyPress() {
|
bool KeyboardEntryActivity::handleKeyPress() {
|
||||||
// Handle special row (bottom row with shift, space, backspace, done)
|
// Handle special row (bottom row with shift, space, backspace, done)
|
||||||
if (selectedRow == SPECIAL_ROW) {
|
if (selectedRow == SPECIAL_ROW) {
|
||||||
if (selectedCol >= SHIFT_COL && selectedCol < SPACE_COL) {
|
if (selectedCol >= SHIFT_COL && selectedCol < SPACE_COL) {
|
||||||
// Shift toggle (0 = lower case, 1 = upper case, 2 = shift lock)
|
// Shift toggle (0 = lower case, 1 = upper case, 2 = shift lock)
|
||||||
shiftState = (shiftState + 1) % 3;
|
shiftState = (shiftState + 1) % 3;
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL) {
|
if (selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL) {
|
||||||
@@ -71,7 +71,7 @@ void KeyboardEntryActivity::handleKeyPress() {
|
|||||||
if (maxLength == 0 || text.length() < maxLength) {
|
if (maxLength == 0 || text.length() < maxLength) {
|
||||||
text += ' ';
|
text += ' ';
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedCol >= BACKSPACE_COL && selectedCol < DONE_COL) {
|
if (selectedCol >= BACKSPACE_COL && selectedCol < DONE_COL) {
|
||||||
@@ -79,22 +79,20 @@ void KeyboardEntryActivity::handleKeyPress() {
|
|||||||
if (!text.empty()) {
|
if (!text.empty()) {
|
||||||
text.pop_back();
|
text.pop_back();
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedCol >= DONE_COL) {
|
if (selectedCol >= DONE_COL) {
|
||||||
// Done button
|
// Done button
|
||||||
if (onComplete) {
|
onComplete(text);
|
||||||
onComplete(text);
|
return false;
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regular character
|
// Regular character
|
||||||
const char c = getSelectedChar();
|
const char c = getSelectedChar();
|
||||||
if (c == '\0') {
|
if (c == '\0') {
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxLength == 0 || text.length() < maxLength) {
|
if (maxLength == 0 || text.length() < maxLength) {
|
||||||
@@ -104,6 +102,8 @@ void KeyboardEntryActivity::handleKeyPress() {
|
|||||||
shiftState = 0;
|
shiftState = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyboardEntryActivity::loop() {
|
void KeyboardEntryActivity::loop() {
|
||||||
@@ -177,20 +177,19 @@ void KeyboardEntryActivity::loop() {
|
|||||||
|
|
||||||
// Selection
|
// Selection
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
||||||
handleKeyPress();
|
if (handleKeyPress()) {
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
|
}
|
||||||
|
// If handleKeyPress returns false, it means onComplete was triggered, no update needed
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cancel
|
// Cancel
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
if (onCancel) {
|
onCancel();
|
||||||
onCancel();
|
|
||||||
}
|
|
||||||
requestUpdate();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyboardEntryActivity::render(Activity::RenderLock&&) {
|
void KeyboardEntryActivity::render(RenderLock&&) {
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
@@ -321,3 +320,15 @@ void KeyboardEntryActivity::render(Activity::RenderLock&&) {
|
|||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void KeyboardEntryActivity::onComplete(std::string text) {
|
||||||
|
setResult(KeyboardResult{std::move(text)});
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KeyboardEntryActivity::onCancel() {
|
||||||
|
ActivityResult result;
|
||||||
|
result.isCancelled = true;
|
||||||
|
setResult(std::move(result));
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,21 +10,10 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Reusable keyboard entry activity for text input.
|
* Reusable keyboard entry activity for text input.
|
||||||
* Can be started from any activity that needs text entry.
|
* Can be started from any activity that needs text entry via startActivityForResult()
|
||||||
*
|
|
||||||
* Usage:
|
|
||||||
* 1. Create a KeyboardEntryActivity instance
|
|
||||||
* 2. Set callbacks with setOnComplete() and setOnCancel()
|
|
||||||
* 3. Call onEnter() to start the activity
|
|
||||||
* 4. Call loop() in your main loop
|
|
||||||
* 5. When complete or cancelled, callbacks will be invoked
|
|
||||||
*/
|
*/
|
||||||
class KeyboardEntryActivity : public Activity {
|
class KeyboardEntryActivity : public Activity {
|
||||||
public:
|
public:
|
||||||
// Callback types
|
|
||||||
using OnCompleteCallback = std::function<void(const std::string&)>;
|
|
||||||
using OnCancelCallback = std::function<void()>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param renderer Reference to the GfxRenderer for drawing
|
* @param renderer Reference to the GfxRenderer for drawing
|
||||||
@@ -33,26 +22,21 @@ class KeyboardEntryActivity : public Activity {
|
|||||||
* @param initialText Initial text to show in the input field
|
* @param initialText Initial text to show in the input field
|
||||||
* @param maxLength Maximum length of input text (0 for unlimited)
|
* @param maxLength Maximum length of input text (0 for unlimited)
|
||||||
* @param isPassword If true, display asterisks instead of actual characters
|
* @param isPassword If true, display asterisks instead of actual characters
|
||||||
* @param onComplete Callback invoked when input is complete
|
|
||||||
* @param onCancel Callback invoked when input is cancelled
|
|
||||||
*/
|
*/
|
||||||
explicit KeyboardEntryActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit KeyboardEntryActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
std::string title = "Enter Text", std::string initialText = "",
|
std::string title = "Enter Text", std::string initialText = "",
|
||||||
const size_t maxLength = 0, const bool isPassword = false,
|
const size_t maxLength = 0, const bool isPassword = false)
|
||||||
OnCompleteCallback onComplete = nullptr, OnCancelCallback onCancel = nullptr)
|
|
||||||
: Activity("KeyboardEntry", renderer, mappedInput),
|
: Activity("KeyboardEntry", renderer, mappedInput),
|
||||||
title(std::move(title)),
|
title(std::move(title)),
|
||||||
text(std::move(initialText)),
|
text(std::move(initialText)),
|
||||||
maxLength(maxLength),
|
maxLength(maxLength),
|
||||||
isPassword(isPassword),
|
isPassword(isPassword) {}
|
||||||
onComplete(std::move(onComplete)),
|
|
||||||
onCancel(std::move(onCancel)) {}
|
|
||||||
|
|
||||||
// Activity overrides
|
// Activity overrides
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
void render(Activity::RenderLock&&) override;
|
void render(RenderLock&&) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string title;
|
std::string title;
|
||||||
@@ -67,9 +51,9 @@ class KeyboardEntryActivity : public Activity {
|
|||||||
int selectedCol = 0;
|
int selectedCol = 0;
|
||||||
int shiftState = 0; // 0 = lower case, 1 = upper case, 2 = shift lock)
|
int shiftState = 0; // 0 = lower case, 1 = upper case, 2 = shift lock)
|
||||||
|
|
||||||
// Callbacks
|
// Handlers
|
||||||
OnCompleteCallback onComplete;
|
void onComplete(std::string text);
|
||||||
OnCancelCallback onCancel;
|
void onCancel();
|
||||||
|
|
||||||
// Keyboard layout
|
// Keyboard layout
|
||||||
static constexpr int NUM_ROWS = 5;
|
static constexpr int NUM_ROWS = 5;
|
||||||
@@ -86,6 +70,6 @@ class KeyboardEntryActivity : public Activity {
|
|||||||
static constexpr int DONE_COL = 9;
|
static constexpr int DONE_COL = 9;
|
||||||
|
|
||||||
char getSelectedChar() const;
|
char getSelectedChar() const;
|
||||||
void handleKeyPress();
|
bool handleKeyPress(); // false if onComplete was triggered
|
||||||
int getRowLength(int row) const;
|
int getRowLength(int row) const;
|
||||||
};
|
};
|
||||||
|
|||||||
101
src/main.cpp
101
src/main.cpp
@@ -18,16 +18,8 @@
|
|||||||
#include "KOReaderCredentialStore.h"
|
#include "KOReaderCredentialStore.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "RecentBooksStore.h"
|
#include "RecentBooksStore.h"
|
||||||
#include "activities/boot_sleep/BootActivity.h"
|
#include "activities/Activity.h"
|
||||||
#include "activities/boot_sleep/SleepActivity.h"
|
#include "activities/ActivityManager.h"
|
||||||
#include "activities/browser/OpdsBookBrowserActivity.h"
|
|
||||||
#include "activities/home/HomeActivity.h"
|
|
||||||
#include "activities/home/MyLibraryActivity.h"
|
|
||||||
#include "activities/home/RecentBooksActivity.h"
|
|
||||||
#include "activities/network/CrossPointWebServerActivity.h"
|
|
||||||
#include "activities/reader/ReaderActivity.h"
|
|
||||||
#include "activities/settings/SettingsActivity.h"
|
|
||||||
#include "activities/util/FullScreenMessageActivity.h"
|
|
||||||
#include "components/UITheme.h"
|
#include "components/UITheme.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
@@ -37,8 +29,8 @@ HalDisplay display;
|
|||||||
HalGPIO gpio;
|
HalGPIO gpio;
|
||||||
MappedInputManager mappedInputManager(gpio);
|
MappedInputManager mappedInputManager(gpio);
|
||||||
GfxRenderer renderer(display);
|
GfxRenderer renderer(display);
|
||||||
|
ActivityManager activityManager(renderer, mappedInputManager);
|
||||||
FontDecompressor fontDecompressor;
|
FontDecompressor fontDecompressor;
|
||||||
Activity* currentActivity;
|
|
||||||
|
|
||||||
// Fonts
|
// Fonts
|
||||||
EpdFont bookerly14RegularFont(&bookerly_14_regular);
|
EpdFont bookerly14RegularFont(&bookerly_14_regular);
|
||||||
@@ -133,19 +125,6 @@ EpdFontFamily ui12FontFamily(&ui12RegularFont, &ui12BoldFont);
|
|||||||
unsigned long t1 = 0;
|
unsigned long t1 = 0;
|
||||||
unsigned long t2 = 0;
|
unsigned long t2 = 0;
|
||||||
|
|
||||||
void exitActivity() {
|
|
||||||
if (currentActivity) {
|
|
||||||
currentActivity->onExit();
|
|
||||||
delete currentActivity;
|
|
||||||
currentActivity = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void enterNewActivity(Activity* activity) {
|
|
||||||
currentActivity = activity;
|
|
||||||
currentActivity->onEnter();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify power button press duration on wake-up from deep sleep
|
// Verify power button press duration on wake-up from deep sleep
|
||||||
// Pre-condition: isWakeupByPowerButton() == true
|
// Pre-condition: isWakeupByPowerButton() == true
|
||||||
void verifyPowerButtonDuration() {
|
void verifyPowerButtonDuration() {
|
||||||
@@ -201,10 +180,10 @@ void waitForPowerRelease() {
|
|||||||
// Enter deep sleep mode
|
// Enter deep sleep mode
|
||||||
void enterDeepSleep() {
|
void enterDeepSleep() {
|
||||||
HalPowerManager::Lock powerLock; // Ensure we are at normal CPU frequency for sleep preparation
|
HalPowerManager::Lock powerLock; // Ensure we are at normal CPU frequency for sleep preparation
|
||||||
APP_STATE.lastSleepFromReader = currentActivity && currentActivity->isReaderActivity();
|
APP_STATE.lastSleepFromReader = activityManager.isReaderActivity();
|
||||||
APP_STATE.saveToFile();
|
APP_STATE.saveToFile();
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new SleepActivity(renderer, mappedInputManager));
|
activityManager.goToSleep();
|
||||||
|
|
||||||
display.deepSleep();
|
display.deepSleep();
|
||||||
LOG_DBG("MAIN", "Power button press calibration value: %lu ms", t2 - t1);
|
LOG_DBG("MAIN", "Power button press calibration value: %lu ms", t2 - t1);
|
||||||
@@ -213,54 +192,10 @@ void enterDeepSleep() {
|
|||||||
powerManager.startDeepSleep(gpio);
|
powerManager.startDeepSleep(gpio);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onGoHome();
|
|
||||||
void onGoToMyLibraryWithPath(const std::string& path);
|
|
||||||
void onGoToRecentBooks();
|
|
||||||
void onGoToReader(const std::string& initialEpubPath) {
|
|
||||||
const std::string bookPath = initialEpubPath; // Copy before exitActivity() invalidates the reference
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new ReaderActivity(renderer, mappedInputManager, bookPath, onGoHome, onGoToMyLibraryWithPath));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoToFileTransfer() {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new CrossPointWebServerActivity(renderer, mappedInputManager, onGoHome));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoToSettings() {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new SettingsActivity(renderer, mappedInputManager, onGoHome));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoToMyLibrary() {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new MyLibraryActivity(renderer, mappedInputManager, onGoHome, onGoToReader));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoToRecentBooks() {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new RecentBooksActivity(renderer, mappedInputManager, onGoHome, onGoToReader));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoToMyLibraryWithPath(const std::string& path) {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new MyLibraryActivity(renderer, mappedInputManager, onGoHome, onGoToReader, path));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoToBrowser() {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new OpdsBookBrowserActivity(renderer, mappedInputManager, onGoHome));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoHome() {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new HomeActivity(renderer, mappedInputManager, onGoToReader, onGoToMyLibrary, onGoToRecentBooks,
|
|
||||||
onGoToSettings, onGoToFileTransfer, onGoToBrowser));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setupDisplayAndFonts() {
|
void setupDisplayAndFonts() {
|
||||||
display.begin();
|
display.begin();
|
||||||
renderer.begin();
|
renderer.begin();
|
||||||
|
activityManager.begin();
|
||||||
LOG_DBG("MAIN", "Display initialized");
|
LOG_DBG("MAIN", "Display initialized");
|
||||||
|
|
||||||
// Initialize font decompressor for compressed reader fonts
|
// Initialize font decompressor for compressed reader fonts
|
||||||
@@ -310,8 +245,7 @@ void setup() {
|
|||||||
if (!Storage.begin()) {
|
if (!Storage.begin()) {
|
||||||
LOG_ERR("MAIN", "SD card initialization failed");
|
LOG_ERR("MAIN", "SD card initialization failed");
|
||||||
setupDisplayAndFonts();
|
setupDisplayAndFonts();
|
||||||
exitActivity();
|
activityManager.goToFullScreenMessage("SD card error", EpdFontFamily::BOLD);
|
||||||
enterNewActivity(new FullScreenMessageActivity(renderer, mappedInputManager, "SD card error", EpdFontFamily::BOLD));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,8 +278,7 @@ void setup() {
|
|||||||
|
|
||||||
setupDisplayAndFonts();
|
setupDisplayAndFonts();
|
||||||
|
|
||||||
exitActivity();
|
activityManager.goToBoot();
|
||||||
enterNewActivity(new BootActivity(renderer, mappedInputManager));
|
|
||||||
|
|
||||||
APP_STATE.loadFromFile();
|
APP_STATE.loadFromFile();
|
||||||
RECENT_BOOKS.loadFromFile();
|
RECENT_BOOKS.loadFromFile();
|
||||||
@@ -354,14 +287,14 @@ void setup() {
|
|||||||
// crashed (indicated by readerActivityLoadCount > 0)
|
// crashed (indicated by readerActivityLoadCount > 0)
|
||||||
if (APP_STATE.openEpubPath.empty() || !APP_STATE.lastSleepFromReader ||
|
if (APP_STATE.openEpubPath.empty() || !APP_STATE.lastSleepFromReader ||
|
||||||
mappedInputManager.isPressed(MappedInputManager::Button::Back) || APP_STATE.readerActivityLoadCount > 0) {
|
mappedInputManager.isPressed(MappedInputManager::Button::Back) || APP_STATE.readerActivityLoadCount > 0) {
|
||||||
onGoHome();
|
activityManager.goHome();
|
||||||
} else {
|
} else {
|
||||||
// Clear app state to avoid getting into a boot loop if the epub doesn't load
|
// Clear app state to avoid getting into a boot loop if the epub doesn't load
|
||||||
const auto path = APP_STATE.openEpubPath;
|
const auto path = APP_STATE.openEpubPath;
|
||||||
APP_STATE.openEpubPath = "";
|
APP_STATE.openEpubPath = "";
|
||||||
APP_STATE.readerActivityLoadCount++;
|
APP_STATE.readerActivityLoadCount++;
|
||||||
APP_STATE.saveToFile();
|
APP_STATE.saveToFile();
|
||||||
onGoToReader(path);
|
activityManager.goToReader(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we're not still holding the power button before leaving setup
|
// Ensure we're not still holding the power button before leaving setup
|
||||||
@@ -401,7 +334,7 @@ void loop() {
|
|||||||
|
|
||||||
// Check for any user activity (button press or release) or active background work
|
// Check for any user activity (button press or release) or active background work
|
||||||
static unsigned long lastActivityTime = millis();
|
static unsigned long lastActivityTime = millis();
|
||||||
if (gpio.wasAnyPressed() || gpio.wasAnyReleased() || (currentActivity && currentActivity->preventAutoSleep())) {
|
if (gpio.wasAnyPressed() || gpio.wasAnyReleased() || activityManager.preventAutoSleep()) {
|
||||||
lastActivityTime = millis(); // Reset inactivity timer
|
lastActivityTime = millis(); // Reset inactivity timer
|
||||||
powerManager.setPowerSaving(false); // Restore normal CPU frequency on user activity
|
powerManager.setPowerSaving(false); // Restore normal CPU frequency on user activity
|
||||||
}
|
}
|
||||||
@@ -410,8 +343,8 @@ void loop() {
|
|||||||
if (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.isPressed(HalGPIO::BTN_DOWN)) {
|
if (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.isPressed(HalGPIO::BTN_DOWN)) {
|
||||||
if (screenshotButtonsReleased) {
|
if (screenshotButtonsReleased) {
|
||||||
screenshotButtonsReleased = false;
|
screenshotButtonsReleased = false;
|
||||||
if (currentActivity) {
|
{
|
||||||
Activity::RenderLock lock(*currentActivity);
|
RenderLock lock;
|
||||||
ScreenshotUtil::takeScreenshot(renderer);
|
ScreenshotUtil::takeScreenshot(renderer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -439,9 +372,7 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const unsigned long activityStartTime = millis();
|
const unsigned long activityStartTime = millis();
|
||||||
if (currentActivity) {
|
activityManager.loop();
|
||||||
currentActivity->loop();
|
|
||||||
}
|
|
||||||
const unsigned long activityDuration = millis() - activityStartTime;
|
const unsigned long activityDuration = millis() - activityStartTime;
|
||||||
|
|
||||||
const unsigned long loopDuration = millis() - loopStartTime;
|
const unsigned long loopDuration = millis() - loopStartTime;
|
||||||
@@ -455,7 +386,7 @@ void loop() {
|
|||||||
// Add delay at the end of the loop to prevent tight spinning
|
// Add delay at the end of the loop to prevent tight spinning
|
||||||
// When an activity requests skip loop delay (e.g., webserver running), use yield() for faster response
|
// When an activity requests skip loop delay (e.g., webserver running), use yield() for faster response
|
||||||
// Otherwise, use longer delay to save power
|
// Otherwise, use longer delay to save power
|
||||||
if (currentActivity && currentActivity->skipLoopDelay()) {
|
if (activityManager.skipLoopDelay()) {
|
||||||
powerManager.setPowerSaving(false); // Make sure we're at full performance when skipLoopDelay is requested
|
powerManager.setPowerSaving(false); // Make sure we're at full performance when skipLoopDelay is requested
|
||||||
yield(); // Give FreeRTOS a chance to run tasks, but return immediately
|
yield(); // Give FreeRTOS a chance to run tasks, but return immediately
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user