diff --git a/src/RecentBooksStore.cpp b/src/RecentBooksStore.cpp index 3be81d06..08e43168 100644 --- a/src/RecentBooksStore.cpp +++ b/src/RecentBooksStore.cpp @@ -46,6 +46,15 @@ void RecentBooksStore::addBook(const std::string& path, const std::string& title saveToFile(); } +void RecentBooksStore::removeBook(const std::string& path) { + auto it = + std::find_if(recentBooks.begin(), recentBooks.end(), [&](const RecentBook& book) { return book.path == path; }); + if (it != recentBooks.end()) { + recentBooks.erase(it); + saveToFile(); + } +} + void RecentBooksStore::updateBook(const std::string& path, const std::string& title, const std::string& author, const std::string& series, const std::string& coverBmpPath) { auto it = diff --git a/src/RecentBooksStore.h b/src/RecentBooksStore.h index deb2ec3a..faca8d35 100644 --- a/src/RecentBooksStore.h +++ b/src/RecentBooksStore.h @@ -44,6 +44,9 @@ class RecentBooksStore { void updateBook(const std::string& path, const std::string& title, const std::string& author, const std::string& series, const std::string& coverBmpPath); + // Remove a book from the recent list by path + void removeBook(const std::string& path); + // Get the list of recent books (most recent first) const std::vector& getBooks() const { return recentBooks; } diff --git a/src/activities/home/RecentBooksActivity.cpp b/src/activities/home/RecentBooksActivity.cpp index ddf8d468..4627cc6c 100644 --- a/src/activities/home/RecentBooksActivity.cpp +++ b/src/activities/home/RecentBooksActivity.cpp @@ -8,15 +8,12 @@ #include #include "BookInfoActivity.h" +#include "../util/ConfirmationActivity.h" #include "MappedInputManager.h" #include "RecentBooksStore.h" #include "components/UITheme.h" #include "fontIds.h" -namespace { -constexpr unsigned long GO_HOME_MS = 1000; -} // namespace - void RecentBooksActivity::loadRecentBooks() { recentBooks.clear(); const auto& books = RECENT_BOOKS.getBooks(); @@ -61,6 +58,32 @@ void RecentBooksActivity::loop() { onGoHome(); } + // Left button: remove selected book from recent list + if (!recentBooks.empty() && selectorIndex < recentBooks.size() && mappedInput.wasReleased(MappedInputManager::Button::Left)) { + const std::string bookPath = recentBooks[selectorIndex].path; + const std::string bookTitle = recentBooks[selectorIndex].title; + + auto handler = [this, bookPath](const ActivityResult& res) { + if (!res.isCancelled) { + LOG_DBG("RBA", "Removing from recent books: %s", bookPath.c_str()); + RECENT_BOOKS.removeBook(bookPath); + loadRecentBooks(); + if (recentBooks.empty()) { + selectorIndex = 0; + } else if (selectorIndex >= recentBooks.size()) { + selectorIndex = recentBooks.size() - 1; + } + requestUpdate(true); + } else { + LOG_DBG("RBA", "Remove cancelled by user"); + } + }; + + std::string heading = tr(STR_DELETE) + std::string("? "); + startActivityForResult(std::make_unique(renderer, mappedInput, heading, bookTitle), handler); + return; + } + if (mappedInput.wasReleased(MappedInputManager::Button::Right)) { if (!recentBooks.empty() && selectorIndex < static_cast(recentBooks.size())) { const std::string& path = recentBooks[selectorIndex].path; @@ -79,20 +102,10 @@ void RecentBooksActivity::loop() { requestUpdate(); }); - buttonNavigator.onPreviousRelease([this, listSize] { - selectorIndex = ButtonNavigator::previousIndex(static_cast(selectorIndex), listSize); - requestUpdate(); - }); - buttonNavigator.onNextContinuous([this, listSize, pageItems] { selectorIndex = ButtonNavigator::nextPageIndex(static_cast(selectorIndex), listSize, pageItems); requestUpdate(); }); - - buttonNavigator.onPreviousContinuous([this, listSize, pageItems] { - selectorIndex = ButtonNavigator::previousPageIndex(static_cast(selectorIndex), listSize, pageItems); - requestUpdate(); - }); } void RecentBooksActivity::render(RenderLock&&) { @@ -128,7 +141,7 @@ void RecentBooksActivity::render(RenderLock&&) { const bool hasInfo = !recentBooks.empty() && selectorIndex < recentBooks.size() && (FsHelpers::hasEpubExtension(recentBooks[selectorIndex].path) || FsHelpers::hasXtcExtension(recentBooks[selectorIndex].path)); - const auto labels = mappedInput.mapLabels(tr(STR_HOME), tr(STR_OPEN), "", hasInfo ? tr(STR_INFO) : ""); + const auto labels = mappedInput.mapLabels(tr(STR_HOME), tr(STR_OPEN), tr(STR_DELETE), hasInfo ? tr(STR_INFO) : ""); GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4); // Side buttons (Up/Down) navigate; show their hints on the side