Introduce common helper functions

This commit is contained in:
jpirnay
2026-04-12 20:30:14 +02:00
parent df3fec574d
commit 54807911e7
16 changed files with 790 additions and 729 deletions
@@ -3,8 +3,6 @@
#include <GfxRenderer.h>
#include <I18n.h>
#include <cstring>
#include "KOReaderAuthActivity.h"
#include "KOReaderCredentialStore.h"
#include "MappedInputManager.h"
@@ -12,51 +10,58 @@
#include "components/UITheme.h"
#include "fontIds.h"
namespace {
constexpr int MENU_ITEMS = 6;
const StrId menuNames[MENU_ITEMS] = {StrId::STR_USERNAME, StrId::STR_PASSWORD, StrId::STR_SYNC_SERVER_URL,
StrId::STR_DOCUMENT_MATCHING, StrId::STR_AUTHENTICATE, StrId::STR_REGISTER};
} // namespace
void KOReaderSettingsActivity::onEnter() {
Activity::onEnter();
selectedIndex = 0;
requestUpdate();
KOReaderSettingsActivity::KOReaderSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
: MenuListActivity("KOReaderSettings", renderer, mappedInput) {
buildMenuItems();
}
void KOReaderSettingsActivity::onExit() { Activity::onExit(); }
void KOReaderSettingsActivity::buildMenuItems() {
// Username, Password, Server URL: ACTION items with custom value display
menuItems.push_back(SettingInfo::Action(StrId::STR_USERNAME, SettingAction::None));
menuItems.push_back(SettingInfo::Action(StrId::STR_PASSWORD, SettingAction::None));
menuItems.push_back(SettingInfo::Action(StrId::STR_SYNC_SERVER_URL, SettingAction::None));
void KOReaderSettingsActivity::loop() {
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
finish();
return;
}
// Document matching: DynamicEnum toggling between Filename and Binary
menuItems.push_back(SettingInfo::DynamicEnum(
StrId::STR_DOCUMENT_MATCHING, {StrId::STR_FILENAME, StrId::STR_BINARY},
[] { return static_cast<uint8_t>(KOREADER_STORE.getMatchMethod()); },
[](uint8_t v) {
KOREADER_STORE.setMatchMethod(static_cast<DocumentMatchMethod>(v));
KOREADER_STORE.saveToFile();
}));
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
handleSelection();
return;
}
// Handle navigation
buttonNavigator.onNext([this] {
selectedIndex = (selectedIndex + 1) % MENU_ITEMS;
requestUpdate();
});
buttonNavigator.onPrevious([this] {
selectedIndex = (selectedIndex + MENU_ITEMS - 1) % MENU_ITEMS;
requestUpdate();
});
// Authenticate and Register: ACTION items
menuItems.push_back(SettingInfo::Action(StrId::STR_AUTHENTICATE, SettingAction::None));
menuItems.push_back(SettingInfo::Action(StrId::STR_REGISTER, SettingAction::None));
}
void KOReaderSettingsActivity::handleSelection() {
if (selectedIndex == 0) {
// Username
std::string KOReaderSettingsActivity::getItemValueString(int index) const {
const auto& item = menuItems[index];
if (item.nameId == StrId::STR_USERNAME) {
auto username = KOREADER_STORE.getUsername();
return username.empty() ? std::string(tr(STR_NOT_SET)) : username;
}
if (item.nameId == StrId::STR_PASSWORD) {
return KOREADER_STORE.getPassword().empty() ? std::string(tr(STR_NOT_SET)) : std::string("******");
}
if (item.nameId == StrId::STR_SYNC_SERVER_URL) {
auto serverUrl = KOREADER_STORE.getServerUrl();
return serverUrl.empty() ? std::string(tr(STR_DEFAULT_VALUE)) : serverUrl;
}
if (item.nameId == StrId::STR_AUTHENTICATE || item.nameId == StrId::STR_REGISTER) {
return KOREADER_STORE.hasCredentials() ? "" : std::string("[") + tr(STR_SET_CREDENTIALS_FIRST) + "]";
}
return MenuListActivity::getItemValueString(index);
}
void KOReaderSettingsActivity::onActionSelected(int index) {
const auto& item = menuItems[index];
if (item.nameId == StrId::STR_USERNAME) {
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_KOREADER_USERNAME),
KOREADER_STORE.getUsername(),
64, // maxLength
false), // not password
KOREADER_STORE.getUsername(), 64, false),
[this](const ActivityResult& result) {
if (!result.isCancelled) {
const auto& kb = std::get<KeyboardResult>(result.data);
@@ -64,12 +69,9 @@ void KOReaderSettingsActivity::handleSelection() {
KOREADER_STORE.saveToFile();
}
});
} else if (selectedIndex == 1) {
// Password
} else if (item.nameId == StrId::STR_PASSWORD) {
startActivityForResult(std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_KOREADER_PASSWORD),
KOREADER_STORE.getPassword(),
64, // maxLength
false), // show characters
KOREADER_STORE.getPassword(), 64, false),
[this](const ActivityResult& result) {
if (!result.isCancelled) {
const auto& kb = std::get<KeyboardResult>(result.data);
@@ -77,14 +79,11 @@ void KOReaderSettingsActivity::handleSelection() {
KOREADER_STORE.saveToFile();
}
});
} else if (selectedIndex == 2) {
// Sync Server URL - prefill with https:// if empty to save typing
} else if (item.nameId == StrId::STR_SYNC_SERVER_URL) {
const std::string currentUrl = KOREADER_STORE.getServerUrl();
const std::string prefillUrl = currentUrl.empty() ? "https://" : currentUrl;
startActivityForResult(
std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_SYNC_SERVER_URL), prefillUrl,
128, // maxLength - URLs can be long
false), // not password
std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_SYNC_SERVER_URL), prefillUrl, 128, false),
[this](const ActivityResult& result) {
if (!result.isCancelled) {
const auto& kb = std::get<KeyboardResult>(result.data);
@@ -93,28 +92,13 @@ void KOReaderSettingsActivity::handleSelection() {
KOREADER_STORE.saveToFile();
}
});
} else if (selectedIndex == 3) {
// Document Matching - toggle between Filename and Binary
const auto current = KOREADER_STORE.getMatchMethod();
const auto newMethod =
(current == DocumentMatchMethod::FILENAME) ? DocumentMatchMethod::BINARY : DocumentMatchMethod::FILENAME;
KOREADER_STORE.setMatchMethod(newMethod);
KOREADER_STORE.saveToFile();
requestUpdate();
} else if (selectedIndex == 4) {
// Authenticate
if (!KOREADER_STORE.hasCredentials()) {
// Can't authenticate without credentials - just show message briefly
return;
}
} else if (item.nameId == StrId::STR_AUTHENTICATE) {
if (!KOREADER_STORE.hasCredentials()) return;
startActivityForResult(
std::make_unique<KOReaderAuthActivity>(renderer, mappedInput, KOReaderAuthActivity::Mode::LOGIN),
[](const ActivityResult&) {});
} else if (selectedIndex == 5) {
// Register
if (!KOREADER_STORE.hasCredentials()) {
return;
}
} else if (item.nameId == StrId::STR_REGISTER) {
if (!KOREADER_STORE.hasCredentials()) return;
startActivityForResult(
std::make_unique<KOReaderAuthActivity>(renderer, mappedInput, KOReaderAuthActivity::Mode::REGISTER),
[](const ActivityResult&) {});
@@ -132,33 +116,8 @@ void KOReaderSettingsActivity::render(RenderLock&&) {
const int contentTop = metrics.topPadding + metrics.headerHeight + metrics.verticalSpacing;
const int contentHeight = contentRect.height - contentTop - metrics.verticalSpacing * 2;
GUI.drawList(
renderer, Rect{contentRect.x, contentTop, contentRect.width, contentHeight}, static_cast<int>(MENU_ITEMS),
static_cast<int>(selectedIndex), [](int index) { return std::string(I18N.get(menuNames[index])); }, nullptr,
nullptr,
[this](int index) {
// Draw status for each setting
if (index == 0) {
auto username = KOREADER_STORE.getUsername();
return username.empty() ? std::string(tr(STR_NOT_SET)) : username;
} else if (index == 1) {
return KOREADER_STORE.getPassword().empty() ? std::string(tr(STR_NOT_SET)) : std::string("******");
} else if (index == 2) {
auto serverUrl = KOREADER_STORE.getServerUrl();
return serverUrl.empty() ? std::string(tr(STR_DEFAULT_VALUE)) : serverUrl;
} else if (index == 3) {
return KOREADER_STORE.getMatchMethod() == DocumentMatchMethod::FILENAME ? std::string(tr(STR_FILENAME))
: std::string(tr(STR_BINARY));
} else if (index == 4) {
return KOREADER_STORE.hasCredentials() ? "" : std::string("[") + tr(STR_SET_CREDENTIALS_FIRST) + "]";
} else if (index == 5) {
return KOREADER_STORE.hasCredentials() ? "" : std::string("[") + tr(STR_SET_CREDENTIALS_FIRST) + "]";
}
return std::string(tr(STR_NOT_SET));
},
true);
drawMenuList(Rect{contentRect.x, contentTop, contentRect.width, contentHeight});
// Draw help text at bottom
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_SELECT), tr(STR_DIR_UP), tr(STR_DIR_DOWN));
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);