Enhance RecentBooksActivity to manage book paths

- Added a new vector to store book paths alongside titles, ensuring that missing files are handled correctly.
- Updated methods to utilize the new bookPaths vector for selecting and rendering recent books.
- Cleared bookPaths during activity exit to prevent memory leaks.
This commit is contained in:
Kenneth 2026-01-05 08:03:02 -05:00
parent 067a36a7b4
commit 6401f099f4
3 changed files with 8 additions and 6 deletions

View File

@ -41,13 +41,14 @@ void RecentBooksActivity::onEnter() {
// Load book titles from recent books list // Load book titles from recent books list
bookTitles.clear(); bookTitles.clear();
bookPaths.clear();
const auto& books = RECENT_BOOKS.getBooks(); const auto& books = RECENT_BOOKS.getBooks();
bookTitles.reserve(books.size()); bookTitles.reserve(books.size());
bookPaths.reserve(books.size());
for (const auto& path : books) { for (const auto& path : books) {
// Skip if file no longer exists // Skip if file no longer exists
if (!SdMan.exists(path.c_str())) { if (!SdMan.exists(path.c_str())) {
bookTitles.emplace_back("[Missing]");
continue; continue;
} }
@ -75,6 +76,7 @@ void RecentBooksActivity::onEnter() {
} }
bookTitles.push_back(title); bookTitles.push_back(title);
bookPaths.push_back(path);
} }
selectorIndex = 0; selectorIndex = 0;
@ -102,6 +104,7 @@ void RecentBooksActivity::onExit() {
vSemaphoreDelete(renderingMutex); vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr; renderingMutex = nullptr;
bookTitles.clear(); bookTitles.clear();
bookPaths.clear();
} }
void RecentBooksActivity::loop() { void RecentBooksActivity::loop() {
@ -112,12 +115,11 @@ void RecentBooksActivity::loop() {
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS; const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
const int pageItems = getPageItems(); const int pageItems = getPageItems();
const int bookCount = RECENT_BOOKS.getCount(); const int bookCount = static_cast<int>(bookTitles.size());
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) { if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (bookCount > 0 && selectorIndex < bookCount) { if (bookCount > 0 && selectorIndex < bookCount) {
const auto& books = RECENT_BOOKS.getBooks(); onSelectBook(bookPaths[selectorIndex]);
onSelectBook(books[selectorIndex]);
} }
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) { } else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack(); onGoBack();
@ -155,7 +157,7 @@ void RecentBooksActivity::render() const {
const auto pageWidth = renderer.getScreenWidth(); const auto pageWidth = renderer.getScreenWidth();
const int pageItems = getPageItems(); const int pageItems = getPageItems();
const int bookCount = RECENT_BOOKS.getCount(); const int bookCount = static_cast<int>(bookTitles.size());
// Draw header // Draw header
renderer.drawCenteredText(UI_12_FONT_ID, 15, "Recent Books", true, EpdFontFamily::BOLD); renderer.drawCenteredText(UI_12_FONT_ID, 15, "Recent Books", true, EpdFontFamily::BOLD);

View File

@ -15,6 +15,7 @@ class RecentBooksActivity final : public Activity {
int selectorIndex = 0; int selectorIndex = 0;
bool updateRequired = false; bool updateRequired = false;
std::vector<std::string> bookTitles; // Display titles for each book std::vector<std::string> bookTitles; // Display titles for each book
std::vector<std::string> bookPaths; // Paths for each visible book (excludes missing)
const std::function<void()> onGoBack; const std::function<void()> onGoBack;
const std::function<void(const std::string& path)> onSelectBook; const std::function<void(const std::string& path)> onSelectBook;

View File

@ -224,7 +224,6 @@ void onGoToSettings() {
enterNewActivity(new SettingsActivity(renderer, mappedInputManager, onGoHome)); enterNewActivity(new SettingsActivity(renderer, mappedInputManager, onGoHome));
} }
void onGoHome();
void onGoToRecentBooks() { void onGoToRecentBooks() {
exitActivity(); exitActivity();
enterNewActivity(new RecentBooksActivity(renderer, mappedInputManager, onGoHome, onGoToReader)); enterNewActivity(new RecentBooksActivity(renderer, mappedInputManager, onGoHome, onGoToReader));