Refactor spaghetti code
This commit is contained in:
@@ -99,21 +99,25 @@ int getHomeCoverRenderHeight(const HomeScreenLayout& layout) {
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int HomeActivity::getMenuItemCount() const {
|
// Builds the menu entry list in display order. Single source of truth for both loop() (which
|
||||||
int count = 4; // File Browser, Recents, File transfer, Settings
|
// dispatches Confirm based on action) and render() (which draws labels/icons).
|
||||||
if (SETTINGS.useWeather) {
|
void HomeActivity::rebuildMenuEntries() {
|
||||||
count++;
|
menuEntries.clear();
|
||||||
}
|
menuEntries.reserve(7);
|
||||||
if (!recentBooks.empty()) {
|
|
||||||
count += recentBooks.size();
|
menuEntries.push_back({MenuAction::FileBrowser, StrId::STR_BROWSE_FILES, Folder});
|
||||||
|
menuEntries.push_back({MenuAction::Recents, StrId::STR_MENU_RECENT_BOOKS, Recent});
|
||||||
|
if (!GLOBAL_BOOKMARKS.isEmpty()) {
|
||||||
|
menuEntries.push_back({MenuAction::GlobalBookmarks, StrId::STR_GLOBAL_BOOKMARKS, Book});
|
||||||
}
|
}
|
||||||
if (hasOpdsUrl) {
|
if (hasOpdsUrl) {
|
||||||
count++;
|
menuEntries.push_back({MenuAction::OpdsBrowser, StrId::STR_OPDS_BROWSER, Library});
|
||||||
}
|
}
|
||||||
if (!GLOBAL_BOOKMARKS.isEmpty()) {
|
menuEntries.push_back({MenuAction::FileTransfer, StrId::STR_FILE_TRANSFER, Transfer});
|
||||||
count++;
|
if (SETTINGS.useWeather) {
|
||||||
|
menuEntries.push_back({MenuAction::Weather, StrId::STR_WEATHER, Weather});
|
||||||
}
|
}
|
||||||
return count;
|
menuEntries.push_back({MenuAction::Settings, StrId::STR_SETTINGS_TITLE, Settings});
|
||||||
}
|
}
|
||||||
|
|
||||||
void HomeActivity::loadRecentBooks(int maxBooks) {
|
void HomeActivity::loadRecentBooks(int maxBooks) {
|
||||||
@@ -260,57 +264,37 @@ void HomeActivity::freeCoverBuffer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HomeActivity::loop() {
|
void HomeActivity::loop() {
|
||||||
|
rebuildMenuEntries();
|
||||||
|
const int totalItems = static_cast<int>(recentBooks.size() + menuEntries.size());
|
||||||
|
|
||||||
if (firstRenderDone && !recentsLoaded && !recentsLoading) {
|
if (firstRenderDone && !recentsLoaded && !recentsLoading) {
|
||||||
const auto& metrics = UITheme::getInstance().getMetrics();
|
const auto& metrics = UITheme::getInstance().getMetrics();
|
||||||
const Rect contentRect = UITheme::getContentRect(renderer, true, false);
|
const Rect contentRect = UITheme::getContentRect(renderer, true, false);
|
||||||
const int menuItemCount = getMenuItemCount();
|
const HomeScreenLayout layout =
|
||||||
const HomeScreenLayout layout = computeHomeScreenLayout(metrics, contentRect.height, menuItemCount);
|
computeHomeScreenLayout(metrics, contentRect.height, static_cast<int>(menuEntries.size()));
|
||||||
loadRecentCovers(getHomeCoverRenderHeight(layout));
|
loadRecentCovers(getHomeCoverRenderHeight(layout));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int menuCount = getMenuItemCount();
|
buttonNavigator.onNext([this, totalItems] {
|
||||||
|
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
|
||||||
buttonNavigator.onNext([this, menuCount] {
|
|
||||||
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, menuCount);
|
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
});
|
});
|
||||||
|
|
||||||
buttonNavigator.onPrevious([this, menuCount] {
|
buttonNavigator.onPrevious([this, totalItems] {
|
||||||
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, menuCount);
|
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
|
||||||
requestUpdate();
|
requestUpdate();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
// Calculate dynamic indices based on which options are available
|
const int recentsCount = static_cast<int>(recentBooks.size());
|
||||||
int idx = 0;
|
if (selectorIndex < recentsCount) {
|
||||||
int menuSelectedIndex = selectorIndex - static_cast<int>(recentBooks.size());
|
|
||||||
const bool hasGlobalBookmarks = !GLOBAL_BOOKMARKS.isEmpty();
|
|
||||||
const bool hasWeather = SETTINGS.useWeather;
|
|
||||||
const int fileBrowserIdx = idx++;
|
|
||||||
const int recentsIdx = idx++;
|
|
||||||
const int globalBookmarksIdx = hasGlobalBookmarks ? idx++ : -1;
|
|
||||||
const int opdsLibraryIdx = hasOpdsUrl ? idx++ : -1;
|
|
||||||
const int fileTransferIdx = idx++;
|
|
||||||
const int weatherIdx = hasWeather ? idx++ : -1;
|
|
||||||
const int settingsIdx = idx;
|
|
||||||
|
|
||||||
if (selectorIndex < recentBooks.size()) {
|
|
||||||
onSelectBook(recentBooks[selectorIndex].path);
|
onSelectBook(recentBooks[selectorIndex].path);
|
||||||
} else if (menuSelectedIndex == fileBrowserIdx) {
|
} else {
|
||||||
onFileBrowserOpen();
|
const int menuIdx = selectorIndex - recentsCount;
|
||||||
} else if (menuSelectedIndex == recentsIdx) {
|
if (menuIdx >= 0 && menuIdx < static_cast<int>(menuEntries.size())) {
|
||||||
onRecentsOpen();
|
dispatchMenuAction(menuEntries[menuIdx].action);
|
||||||
} else if (menuSelectedIndex == globalBookmarksIdx) {
|
}
|
||||||
onGlobalBookmarksOpen();
|
|
||||||
} else if (menuSelectedIndex == opdsLibraryIdx) {
|
|
||||||
onOpdsBrowserOpen();
|
|
||||||
} else if (menuSelectedIndex == weatherIdx) {
|
|
||||||
onWeatherOpen();
|
|
||||||
} else if (menuSelectedIndex == fileTransferIdx) {
|
|
||||||
onFileTransferOpen();
|
|
||||||
} else if (menuSelectedIndex == settingsIdx) {
|
|
||||||
onSettingsOpen();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -324,37 +308,14 @@ void HomeActivity::render(RenderLock&&) {
|
|||||||
|
|
||||||
GUI.drawHeader(renderer, Rect{contentRect.x, metrics.topPadding, contentRect.width, metrics.homeTopPadding}, nullptr);
|
GUI.drawHeader(renderer, Rect{contentRect.x, metrics.topPadding, contentRect.width, metrics.homeTopPadding}, nullptr);
|
||||||
|
|
||||||
// Build menu items dynamically
|
rebuildMenuEntries();
|
||||||
const char* weatherMenuLabel = SETTINGS.useWeather ? tr(STR_WEATHER) : tr(STR_SETTINGS_TITLE);
|
|
||||||
const UIIcon weatherMenuIcon = SETTINGS.useWeather ? Weather : Settings;
|
|
||||||
|
|
||||||
std::vector<const char*> menuItems = {tr(STR_BROWSE_FILES), tr(STR_MENU_RECENT_BOOKS), tr(STR_FILE_TRANSFER),
|
const int totalItems = static_cast<int>(recentBooks.size() + menuEntries.size());
|
||||||
weatherMenuLabel, tr(STR_SETTINGS_TITLE)};
|
|
||||||
std::vector<UIIcon> menuIcons = {Folder, Recent, Transfer, weatherMenuIcon, Settings};
|
|
||||||
|
|
||||||
if (!SETTINGS.useWeather) {
|
|
||||||
menuItems.erase(menuItems.begin() + 3);
|
|
||||||
menuIcons.erase(menuIcons.begin() + 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
int insertAfterRecents = 2;
|
|
||||||
if (!GLOBAL_BOOKMARKS.isEmpty()) {
|
|
||||||
menuItems.insert(menuItems.begin() + insertAfterRecents, tr(STR_GLOBAL_BOOKMARKS));
|
|
||||||
menuIcons.insert(menuIcons.begin() + insertAfterRecents, Book);
|
|
||||||
insertAfterRecents++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasOpdsUrl) {
|
|
||||||
menuItems.insert(menuItems.begin() + insertAfterRecents, tr(STR_OPDS_BROWSER));
|
|
||||||
menuIcons.insert(menuIcons.begin() + insertAfterRecents, Library);
|
|
||||||
}
|
|
||||||
|
|
||||||
const int totalItems = static_cast<int>(recentBooks.size() + menuItems.size());
|
|
||||||
if (selectorIndex >= totalItems) {
|
if (selectorIndex >= totalItems) {
|
||||||
selectorIndex = std::max(0, totalItems - 1);
|
selectorIndex = std::max(0, totalItems - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int menuCount = static_cast<int>(menuItems.size());
|
const int menuCount = static_cast<int>(menuEntries.size());
|
||||||
const HomeScreenLayout layout = computeHomeScreenLayout(metrics, contentRect.height, menuCount);
|
const HomeScreenLayout layout = computeHomeScreenLayout(metrics, contentRect.height, menuCount);
|
||||||
|
|
||||||
GUI.drawRecentBookCover(renderer,
|
GUI.drawRecentBookCover(renderer,
|
||||||
@@ -367,8 +328,8 @@ void HomeActivity::render(RenderLock&&) {
|
|||||||
Rect{contentRect.x, metrics.homeTopPadding + layout.recentTileHeight + layout.recentToMenuGap, contentRect.width,
|
Rect{contentRect.x, metrics.homeTopPadding + layout.recentTileHeight + layout.recentToMenuGap, contentRect.width,
|
||||||
layout.menuHeight},
|
layout.menuHeight},
|
||||||
menuCount, selectorIndex - static_cast<int>(recentBooks.size()),
|
menuCount, selectorIndex - static_cast<int>(recentBooks.size()),
|
||||||
[&menuItems](int index) { return std::string(menuItems[index]); },
|
[this](int index) { return std::string(I18N.get(menuEntries[index].label)); },
|
||||||
[&menuIcons](int index) { return menuIcons[index]; });
|
[this](int index) { return menuEntries[index].icon; });
|
||||||
|
|
||||||
const auto labels = mappedInput.mapLabels("", tr(STR_SELECT), tr(STR_DIR_UP), tr(STR_DIR_DOWN));
|
const auto labels = mappedInput.mapLabels("", tr(STR_SELECT), tr(STR_DIR_UP), tr(STR_DIR_DOWN));
|
||||||
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
||||||
@@ -383,16 +344,28 @@ void HomeActivity::render(RenderLock&&) {
|
|||||||
|
|
||||||
void HomeActivity::onSelectBook(const std::string& path) { activityManager.pushReader(path); }
|
void HomeActivity::onSelectBook(const std::string& path) { activityManager.pushReader(path); }
|
||||||
|
|
||||||
void HomeActivity::onFileBrowserOpen() { activityManager.goToFileBrowser(); }
|
void HomeActivity::dispatchMenuAction(MenuAction action) {
|
||||||
|
switch (action) {
|
||||||
void HomeActivity::onRecentsOpen() { activityManager.goToRecentBooks(); }
|
case MenuAction::FileBrowser:
|
||||||
|
activityManager.goToFileBrowser();
|
||||||
void HomeActivity::onGlobalBookmarksOpen() { activityManager.goToGlobalBookmarks(); }
|
break;
|
||||||
|
case MenuAction::Recents:
|
||||||
void HomeActivity::onSettingsOpen() { activityManager.goToSettings(); }
|
activityManager.goToRecentBooks();
|
||||||
|
break;
|
||||||
void HomeActivity::onFileTransferOpen() { activityManager.goToFileTransfer(); }
|
case MenuAction::GlobalBookmarks:
|
||||||
|
activityManager.goToGlobalBookmarks();
|
||||||
void HomeActivity::onOpdsBrowserOpen() { activityManager.goToBrowser(); }
|
break;
|
||||||
|
case MenuAction::OpdsBrowser:
|
||||||
void HomeActivity::onWeatherOpen() { activityManager.goToWeather(); }
|
activityManager.goToBrowser();
|
||||||
|
break;
|
||||||
|
case MenuAction::FileTransfer:
|
||||||
|
activityManager.goToFileTransfer();
|
||||||
|
break;
|
||||||
|
case MenuAction::Weather:
|
||||||
|
activityManager.goToWeather();
|
||||||
|
break;
|
||||||
|
case MenuAction::Settings:
|
||||||
|
activityManager.goToSettings();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,12 +5,31 @@
|
|||||||
|
|
||||||
#include "../Activity.h"
|
#include "../Activity.h"
|
||||||
#include "./FileBrowserActivity.h"
|
#include "./FileBrowserActivity.h"
|
||||||
|
#include "components/UITheme.h"
|
||||||
#include "util/ButtonNavigator.h"
|
#include "util/ButtonNavigator.h"
|
||||||
|
|
||||||
struct RecentBook;
|
struct RecentBook;
|
||||||
struct Rect;
|
struct Rect;
|
||||||
|
|
||||||
class HomeActivity final : public Activity {
|
class HomeActivity final : public Activity {
|
||||||
|
public:
|
||||||
|
enum class MenuAction {
|
||||||
|
FileBrowser,
|
||||||
|
Recents,
|
||||||
|
GlobalBookmarks,
|
||||||
|
OpdsBrowser,
|
||||||
|
FileTransfer,
|
||||||
|
Weather,
|
||||||
|
Settings,
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct MenuEntry {
|
||||||
|
MenuAction action;
|
||||||
|
StrId label;
|
||||||
|
UIIcon icon;
|
||||||
|
};
|
||||||
|
|
||||||
ButtonNavigator buttonNavigator;
|
ButtonNavigator buttonNavigator;
|
||||||
int selectorIndex = 0;
|
int selectorIndex = 0;
|
||||||
bool recentsLoading = false;
|
bool recentsLoading = false;
|
||||||
@@ -22,16 +41,12 @@ class HomeActivity final : public Activity {
|
|||||||
size_t nextRecentCoverIndex = 0;
|
size_t nextRecentCoverIndex = 0;
|
||||||
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;
|
||||||
void onSelectBook(const std::string& path);
|
std::vector<MenuEntry> menuEntries;
|
||||||
void onFileBrowserOpen();
|
|
||||||
void onRecentsOpen();
|
|
||||||
void onGlobalBookmarksOpen();
|
|
||||||
void onSettingsOpen();
|
|
||||||
void onFileTransferOpen();
|
|
||||||
void onOpdsBrowserOpen();
|
|
||||||
void onWeatherOpen();
|
|
||||||
|
|
||||||
int getMenuItemCount() const;
|
void onSelectBook(const std::string& path);
|
||||||
|
void dispatchMenuAction(MenuAction action);
|
||||||
|
|
||||||
|
void rebuildMenuEntries();
|
||||||
bool storeCoverBuffer(); // Store frame buffer for cover image
|
bool storeCoverBuffer(); // Store frame buffer for cover image
|
||||||
bool restoreCoverBuffer(); // Restore frame buffer from stored cover
|
bool restoreCoverBuffer(); // Restore frame buffer from stored cover
|
||||||
void freeCoverBuffer(); // Free the stored cover buffer
|
void freeCoverBuffer(); // Free the stored cover buffer
|
||||||
|
|||||||
Reference in New Issue
Block a user