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:
@@ -16,7 +16,7 @@ constexpr const char* HOSTNAME = "crosspoint";
|
||||
} // namespace
|
||||
|
||||
void CalibreConnectActivity::onEnter() {
|
||||
ActivityWithSubactivity::onEnter();
|
||||
Activity::onEnter();
|
||||
|
||||
requestUpdate();
|
||||
state = CalibreConnectState::WIFI_SELECTION;
|
||||
@@ -32,8 +32,15 @@ void CalibreConnectActivity::onEnter() {
|
||||
exitRequested = false;
|
||||
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
||||
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||
[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 {
|
||||
connectedIP = WiFi.localIP().toString().c_str();
|
||||
connectedSSID = WiFi.SSID().c_str();
|
||||
@@ -42,7 +49,7 @@ void CalibreConnectActivity::onEnter() {
|
||||
}
|
||||
|
||||
void CalibreConnectActivity::onExit() {
|
||||
ActivityWithSubactivity::onExit();
|
||||
Activity::onExit();
|
||||
|
||||
stopWebServer();
|
||||
MDNS.end();
|
||||
@@ -56,18 +63,10 @@ void CalibreConnectActivity::onExit() {
|
||||
|
||||
void CalibreConnectActivity::onWifiSelectionComplete(const bool connected) {
|
||||
if (!connected) {
|
||||
exitActivity();
|
||||
onComplete();
|
||||
activityManager.popActivity();
|
||||
return;
|
||||
}
|
||||
|
||||
if (subActivity) {
|
||||
connectedIP = static_cast<WifiSelectionActivity*>(subActivity.get())->getConnectedIP();
|
||||
} else {
|
||||
connectedIP = WiFi.localIP().toString().c_str();
|
||||
}
|
||||
connectedSSID = WiFi.SSID().c_str();
|
||||
exitActivity();
|
||||
startWebServer();
|
||||
}
|
||||
|
||||
@@ -100,11 +99,6 @@ void CalibreConnectActivity::stopWebServer() {
|
||||
}
|
||||
|
||||
void CalibreConnectActivity::loop() {
|
||||
if (subActivity) {
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||
exitRequested = true;
|
||||
}
|
||||
@@ -168,12 +162,12 @@ void CalibreConnectActivity::loop() {
|
||||
}
|
||||
|
||||
if (exitRequested) {
|
||||
onComplete();
|
||||
activityManager.popActivity();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void CalibreConnectActivity::render(Activity::RenderLock&&) {
|
||||
void CalibreConnectActivity::render(RenderLock&&) {
|
||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
#include "activities/Activity.h"
|
||||
#include "network/CrossPointWebServer.h"
|
||||
|
||||
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,
|
||||
* 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;
|
||||
const std::function<void()> onComplete;
|
||||
|
||||
std::unique_ptr<CrossPointWebServer> webServer;
|
||||
std::string connectedIP;
|
||||
@@ -36,13 +35,12 @@ class CalibreConnectActivity final : public ActivityWithSubactivity {
|
||||
void stopWebServer();
|
||||
|
||||
public:
|
||||
explicit CalibreConnectActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
const std::function<void()>& onComplete)
|
||||
: ActivityWithSubactivity("CalibreConnect", renderer, mappedInput), onComplete(onComplete) {}
|
||||
explicit CalibreConnectActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||
: Activity("CalibreConnect", renderer, mappedInput) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
void render(Activity::RenderLock&&) override;
|
||||
void render(RenderLock&&) override;
|
||||
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
||||
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
||||
};
|
||||
|
||||
@@ -33,7 +33,7 @@ constexpr uint16_t DNS_PORT = 53;
|
||||
} // namespace
|
||||
|
||||
void CrossPointWebServerActivity::onEnter() {
|
||||
ActivityWithSubactivity::onEnter();
|
||||
Activity::onEnter();
|
||||
|
||||
LOG_DBG("WEBACT", "Free heap at onEnter: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
@@ -48,14 +48,18 @@ void CrossPointWebServerActivity::onEnter() {
|
||||
|
||||
// Launch network mode selection subactivity
|
||||
LOG_DBG("WEBACT", "Launching NetworkModeSelectionActivity...");
|
||||
enterNewActivity(new NetworkModeSelectionActivity(
|
||||
renderer, mappedInput, [this](const NetworkMode mode) { onNetworkModeSelected(mode); },
|
||||
[this]() { onGoBack(); } // Cancel goes back to home
|
||||
));
|
||||
startActivityForResult(std::make_unique<NetworkModeSelectionActivity>(renderer, mappedInput),
|
||||
[this](const ActivityResult& result) {
|
||||
if (result.isCancelled) {
|
||||
onGoHome();
|
||||
} else {
|
||||
onNetworkModeSelected(std::get<NetworkModeResult>(result.data).mode);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::onExit() {
|
||||
ActivityWithSubactivity::onExit();
|
||||
Activity::onExit();
|
||||
|
||||
LOG_DBG("WEBACT", "Free heap at onExit start: %d bytes", ESP.getFreeHeap());
|
||||
|
||||
@@ -107,18 +111,20 @@ void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode)
|
||||
networkMode = mode;
|
||||
isApMode = (mode == NetworkMode::CREATE_HOTSPOT);
|
||||
|
||||
// Exit mode selection subactivity
|
||||
exitActivity();
|
||||
|
||||
if (mode == NetworkMode::CONNECT_CALIBRE) {
|
||||
exitActivity();
|
||||
enterNewActivity(new CalibreConnectActivity(renderer, mappedInput, [this] {
|
||||
exitActivity();
|
||||
state = WebServerActivityState::MODE_SELECTION;
|
||||
enterNewActivity(new NetworkModeSelectionActivity(
|
||||
renderer, mappedInput, [this](const NetworkMode nextMode) { onNetworkModeSelected(nextMode); },
|
||||
[this]() { onGoBack(); }));
|
||||
}));
|
||||
startActivityForResult(
|
||||
std::make_unique<CalibreConnectActivity>(renderer, mappedInput), [this](const ActivityResult& result) {
|
||||
state = WebServerActivityState::MODE_SELECTION;
|
||||
|
||||
startActivityForResult(std::make_unique<NetworkModeSelectionActivity>(renderer, mappedInput),
|
||||
[this](const ActivityResult& result) {
|
||||
if (result.isCancelled) {
|
||||
onGoHome();
|
||||
} else {
|
||||
onNetworkModeSelected(std::get<NetworkModeResult>(result.data).mode);
|
||||
}
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -129,8 +135,15 @@ void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode)
|
||||
|
||||
state = WebServerActivityState::WIFI_SELECTION;
|
||||
LOG_DBG("WEBACT", "Launching WifiSelectionActivity...");
|
||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
||||
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||
[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 {
|
||||
// AP mode - start access point
|
||||
state = WebServerActivityState::AP_STARTING;
|
||||
@@ -144,12 +157,8 @@ void CrossPointWebServerActivity::onWifiSelectionComplete(const bool connected)
|
||||
|
||||
if (connected) {
|
||||
// Get connection info before exiting subactivity
|
||||
connectedIP = static_cast<WifiSelectionActivity*>(subActivity.get())->getConnectedIP();
|
||||
connectedSSID = WiFi.SSID().c_str();
|
||||
isApMode = false;
|
||||
|
||||
exitActivity();
|
||||
|
||||
// Start mDNS for hostname resolution
|
||||
if (MDNS.begin(AP_HOSTNAME)) {
|
||||
LOG_DBG("WEBACT", "mDNS started: http://%s.local/", AP_HOSTNAME);
|
||||
@@ -159,11 +168,16 @@ void CrossPointWebServerActivity::onWifiSelectionComplete(const bool connected)
|
||||
startWebServer();
|
||||
} else {
|
||||
// User cancelled - go back to mode selection
|
||||
exitActivity();
|
||||
state = WebServerActivityState::MODE_SELECTION;
|
||||
enterNewActivity(new NetworkModeSelectionActivity(
|
||||
renderer, mappedInput, [this](const NetworkMode mode) { onNetworkModeSelected(mode); },
|
||||
[this]() { onGoBack(); }));
|
||||
|
||||
startActivityForResult(std::make_unique<NetworkModeSelectionActivity>(renderer, mappedInput),
|
||||
[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) {
|
||||
LOG_ERR("WEBACT", "ERROR: Failed to start Access Point!");
|
||||
onGoBack();
|
||||
onGoHome();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -236,16 +250,12 @@ void CrossPointWebServerActivity::startWebServer() {
|
||||
|
||||
// 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.
|
||||
{
|
||||
RenderLock lock(*this);
|
||||
render(std::move(lock));
|
||||
}
|
||||
LOG_DBG("WEBACT", "Rendered File Transfer screen");
|
||||
requestUpdate();
|
||||
} else {
|
||||
LOG_ERR("WEBACT", "ERROR: Failed to start web server!");
|
||||
webServer.reset();
|
||||
// Go back on error
|
||||
onGoBack();
|
||||
onGoHome();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,12 +269,6 @@ void CrossPointWebServerActivity::stopWebServer() {
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::loop() {
|
||||
if (subActivity) {
|
||||
// Forward loop to subactivity
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle different states
|
||||
if (state == WebServerActivityState::SERVER_RUNNING) {
|
||||
// Handle DNS requests for captive portal (AP mode only)
|
||||
@@ -322,7 +326,7 @@ void CrossPointWebServerActivity::loop() {
|
||||
mappedInput.update();
|
||||
// Check for exit button inside loop for responsiveness
|
||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||
onGoBack();
|
||||
onGoHome();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -332,13 +336,13 @@ void CrossPointWebServerActivity::loop() {
|
||||
|
||||
// Handle exit on Back button (also check outside loop)
|
||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||
onGoBack();
|
||||
onGoHome();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::render(Activity::RenderLock&&) {
|
||||
void CrossPointWebServerActivity::render(RenderLock&&) {
|
||||
// Only render our own UI when server is running
|
||||
// Subactivities handle their own rendering
|
||||
if (state == WebServerActivityState::SERVER_RUNNING || state == WebServerActivityState::AP_STARTING) {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "NetworkModeSelectionActivity.h"
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
#include "activities/Activity.h"
|
||||
#include "network/CrossPointWebServer.h"
|
||||
|
||||
// Web server activity states
|
||||
@@ -27,9 +27,8 @@ enum class WebServerActivityState {
|
||||
* - Handles client requests in its loop() function
|
||||
* - 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;
|
||||
const std::function<void()> onGoBack;
|
||||
|
||||
// Network mode
|
||||
NetworkMode networkMode = NetworkMode::JOIN_NETWORK;
|
||||
@@ -54,13 +53,12 @@ class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
||||
void stopWebServer();
|
||||
|
||||
public:
|
||||
explicit CrossPointWebServerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
const std::function<void()>& onGoBack)
|
||||
: ActivityWithSubactivity("CrossPointWebServer", renderer, mappedInput), onGoBack(onGoBack) {}
|
||||
explicit CrossPointWebServerActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||
: Activity("CrossPointWebServer", renderer, mappedInput) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
void render(Activity::RenderLock&&) override;
|
||||
void render(RenderLock&&) override;
|
||||
bool skipLoopDelay() 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();
|
||||
|
||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||
@@ -83,3 +83,15 @@ void NetworkModeSelectionActivity::render(Activity::RenderLock&&) {
|
||||
|
||||
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 "util/ButtonNavigator.h"
|
||||
|
||||
// Enum for network mode selection
|
||||
enum class NetworkMode { JOIN_NETWORK, CONNECT_CALIBRE, CREATE_HOTSPOT };
|
||||
|
||||
/**
|
||||
@@ -22,16 +21,14 @@ class NetworkModeSelectionActivity final : public Activity {
|
||||
|
||||
int selectedIndex = 0;
|
||||
|
||||
const std::function<void(NetworkMode)> onModeSelected;
|
||||
const std::function<void()> onCancel;
|
||||
|
||||
public:
|
||||
explicit NetworkModeSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
const std::function<void(NetworkMode)>& onModeSelected,
|
||||
const std::function<void()>& onCancel)
|
||||
: Activity("NetworkModeSelection", renderer, mappedInput), onModeSelected(onModeSelected), onCancel(onCancel) {}
|
||||
explicit NetworkModeSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
|
||||
: Activity("NetworkModeSelection", renderer, mappedInput) {}
|
||||
void onEnter() override;
|
||||
void onExit() 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
|
||||
state = WifiSelectionState::PASSWORD_ENTRY;
|
||||
// Don't allow screen updates while changing activity
|
||||
enterNewActivity(new KeyboardEntryActivity(
|
||||
renderer, mappedInput, tr(STR_ENTER_WIFI_PASSWORD),
|
||||
"", // No initial text
|
||||
64, // Max password length
|
||||
false, // Show password by default (hard keyboard to use)
|
||||
[this](const std::string& text) {
|
||||
enteredPassword = text;
|
||||
exitActivity();
|
||||
},
|
||||
[this] {
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
exitActivity();
|
||||
requestUpdate();
|
||||
}));
|
||||
startActivityForResult(
|
||||
std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_ENTER_WIFI_PASSWORD),
|
||||
"", // No initial text
|
||||
64, // Max password length
|
||||
false // Show password by default (hard keyboard to use)
|
||||
),
|
||||
[this](const ActivityResult& result) {
|
||||
if (result.isCancelled) {
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
} else {
|
||||
enteredPassword = std::get<KeyboardResult>(result.data).text;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Connect directly for open networks
|
||||
attemptConnection();
|
||||
@@ -291,11 +290,6 @@ void WifiSelectionActivity::checkConnectionStatus() {
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::loop() {
|
||||
if (subActivity) {
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check scan progress
|
||||
if (state == WifiSelectionState::SCANNING) {
|
||||
processWifiScanResults();
|
||||
@@ -467,7 +461,7 @@ std::string WifiSelectionActivity::getSignalStrengthIndicator(const int32_t rssi
|
||||
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
|
||||
// from the keyboard subactivity back to the main activity
|
||||
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));
|
||||
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 <vector>
|
||||
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
#include "activities/Activity.h"
|
||||
#include "util/ButtonNavigator.h"
|
||||
|
||||
// Structure to hold WiFi network information
|
||||
@@ -15,6 +15,7 @@ struct WifiNetworkInfo {
|
||||
int32_t rssi;
|
||||
bool isEncrypted;
|
||||
bool hasSavedPassword; // Whether we have saved credentials for this network
|
||||
std::string ipAddress; // Populated after connection for display
|
||||
};
|
||||
|
||||
// WiFi selection states
|
||||
@@ -41,13 +42,12 @@ enum class WifiSelectionState {
|
||||
*
|
||||
* The onComplete callback receives true if connected successfully, false if cancelled.
|
||||
*/
|
||||
class WifiSelectionActivity final : public ActivityWithSubactivity {
|
||||
class WifiSelectionActivity final : public Activity {
|
||||
ButtonNavigator buttonNavigator;
|
||||
|
||||
WifiSelectionState state = WifiSelectionState::SCANNING;
|
||||
size_t selectedNetworkIndex = 0;
|
||||
std::vector<WifiNetworkInfo> networks;
|
||||
const std::function<void(bool connected)> onComplete;
|
||||
|
||||
// Selected network for connection
|
||||
std::string selectedSSID;
|
||||
@@ -95,17 +95,13 @@ class WifiSelectionActivity final : public ActivityWithSubactivity {
|
||||
void checkConnectionStatus();
|
||||
std::string getSignalStrengthIndicator(int32_t rssi) const;
|
||||
|
||||
void onComplete(bool connected);
|
||||
|
||||
public:
|
||||
explicit WifiSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
const std::function<void(bool connected)>& onComplete, bool autoConnect = true)
|
||||
: ActivityWithSubactivity("WifiSelection", renderer, mappedInput),
|
||||
onComplete(onComplete),
|
||||
allowAutoConnect(autoConnect) {}
|
||||
explicit WifiSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, bool autoConnect = true)
|
||||
: Activity("WifiSelection", renderer, mappedInput), allowAutoConnect(autoConnect) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
void render(Activity::RenderLock&&) override;
|
||||
|
||||
// Get the IP address after successful connection
|
||||
const std::string& getConnectedIP() const { return connectedIP; }
|
||||
void render(RenderLock&&) override;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user