## Summary Ref discussion: https://github.com/crosspoint-reader/crosspoint-reader/pull/1222#discussion_r2865402110 Important note that this is a bug-for-bug fix. In reality, this branch `WiFi.status() == WL_CONNECTED` is pretty much a dead code because the entry point of these 2 activities don't use wifi. It is better to refactor the management of network though, but it's better to be a dedicated PR. --- ### 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? **NO**
109 lines
3.0 KiB
C++
109 lines
3.0 KiB
C++
#include "KOReaderAuthActivity.h"
|
|
|
|
#include <GfxRenderer.h>
|
|
#include <I18n.h>
|
|
#include <WiFi.h>
|
|
|
|
#include "KOReaderCredentialStore.h"
|
|
#include "KOReaderSyncClient.h"
|
|
#include "MappedInputManager.h"
|
|
#include "activities/network/WifiSelectionActivity.h"
|
|
#include "components/UITheme.h"
|
|
#include "fontIds.h"
|
|
|
|
void KOReaderAuthActivity::onWifiSelectionComplete(const bool success) {
|
|
if (!success) {
|
|
{
|
|
RenderLock lock(*this);
|
|
state = FAILED;
|
|
errorMessage = tr(STR_WIFI_CONN_FAILED);
|
|
}
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
|
|
{
|
|
RenderLock lock(*this);
|
|
state = AUTHENTICATING;
|
|
statusMessage = tr(STR_AUTHENTICATING);
|
|
}
|
|
requestUpdate();
|
|
|
|
performAuthentication();
|
|
}
|
|
|
|
void KOReaderAuthActivity::performAuthentication() {
|
|
const auto result = KOReaderSyncClient::authenticate();
|
|
|
|
{
|
|
RenderLock lock(*this);
|
|
if (result == KOReaderSyncClient::OK) {
|
|
state = SUCCESS;
|
|
statusMessage = tr(STR_AUTH_SUCCESS);
|
|
} else {
|
|
state = FAILED;
|
|
errorMessage = KOReaderSyncClient::errorString(result);
|
|
}
|
|
}
|
|
requestUpdate();
|
|
}
|
|
|
|
void KOReaderAuthActivity::onEnter() {
|
|
Activity::onEnter();
|
|
|
|
// Check if already connected
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
onWifiSelectionComplete(true);
|
|
return;
|
|
}
|
|
|
|
// Launch WiFi selection
|
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
|
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
|
|
}
|
|
|
|
void KOReaderAuthActivity::onExit() {
|
|
Activity::onExit();
|
|
|
|
// Turn off wifi
|
|
WiFi.disconnect(false);
|
|
delay(100);
|
|
WiFi.mode(WIFI_OFF);
|
|
delay(100);
|
|
}
|
|
|
|
void KOReaderAuthActivity::render(RenderLock&&) {
|
|
renderer.clearScreen();
|
|
|
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
const auto pageHeight = renderer.getScreenHeight();
|
|
|
|
GUI.drawHeader(renderer, Rect{0, metrics.topPadding, pageWidth, metrics.headerHeight}, tr(STR_KOREADER_AUTH));
|
|
const auto height = renderer.getLineHeight(UI_10_FONT_ID);
|
|
const auto top = (pageHeight - height) / 2;
|
|
|
|
if (state == AUTHENTICATING) {
|
|
renderer.drawCenteredText(UI_10_FONT_ID, top, statusMessage.c_str());
|
|
} else if (state == SUCCESS) {
|
|
renderer.drawCenteredText(UI_10_FONT_ID, top, tr(STR_AUTH_SUCCESS), true, EpdFontFamily::BOLD);
|
|
renderer.drawCenteredText(UI_10_FONT_ID, top + height + 10, tr(STR_SYNC_READY));
|
|
} else if (state == FAILED) {
|
|
renderer.drawCenteredText(UI_10_FONT_ID, top, tr(STR_AUTH_FAILED), true, EpdFontFamily::BOLD);
|
|
renderer.drawCenteredText(UI_10_FONT_ID, top + height + 10, errorMessage.c_str());
|
|
}
|
|
|
|
const auto labels = mappedInput.mapLabels(tr(STR_BACK), "", "", "");
|
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
renderer.displayBuffer();
|
|
}
|
|
|
|
void KOReaderAuthActivity::loop() {
|
|
if (state == SUCCESS || state == FAILED) {
|
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back) ||
|
|
mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
|
finish();
|
|
}
|
|
}
|
|
}
|