From 3ed5bc367315c101e26dde20bc98d01914537696 Mon Sep 17 00:00:00 2001 From: jpirnay Date: Fri, 15 May 2026 19:22:58 +0200 Subject: [PATCH] Remove missing books from recent list --- src/RecentBooksStore.cpp | 10 ++++++++++ src/RecentBooksStore.h | 7 +++++++ src/activities/home/HomeActivity.cpp | 3 +-- src/activities/home/RecentBooksActivity.cpp | 14 +++++--------- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/RecentBooksStore.cpp b/src/RecentBooksStore.cpp index b660a3ce..19e6329f 100644 --- a/src/RecentBooksStore.cpp +++ b/src/RecentBooksStore.cpp @@ -30,6 +30,8 @@ void RecentBooksStore::addBook(const std::string& path, const std::string& title int8_t bionicReadingOverride = -1; int8_t paragraphAlignmentOverride = -1; + pruneMissing(); + // Remove existing entry if present auto it = std::find_if(recentBooks.begin(), recentBooks.end(), [&](const RecentBook& book) { return book.path == path; }); @@ -66,6 +68,14 @@ void RecentBooksStore::removeBook(const std::string& path) { } } +bool RecentBooksStore::isMissing(const RecentBook& book) { return !Storage.exists(book.path.c_str()); } + +bool RecentBooksStore::pruneMissing() { + const size_t before = recentBooks.size(); + recentBooks.erase(std::remove_if(recentBooks.begin(), recentBooks.end(), &isMissing), recentBooks.end()); + return recentBooks.size() != before; +} + 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 2df63c25..aedb21ca 100644 --- a/src/RecentBooksStore.h +++ b/src/RecentBooksStore.h @@ -62,6 +62,13 @@ class RecentBooksStore { // Get the count of recent books int getCount() const { return static_cast(recentBooks.size()); } + // Returns true if the book's file is missing from storage + static bool isMissing(const RecentBook& book); + + // Remove entries whose backing file is no longer on the SD card. + // Returns true if any entry was removed. Does not persist — caller decides. + bool pruneMissing(); + bool saveToFile() const; bool loadFromFile(); diff --git a/src/activities/home/HomeActivity.cpp b/src/activities/home/HomeActivity.cpp index 868650a4..a30358bc 100644 --- a/src/activities/home/HomeActivity.cpp +++ b/src/activities/home/HomeActivity.cpp @@ -168,8 +168,7 @@ void HomeActivity::loadRecentBooks(int maxBooks) { break; } - // Skip if file no longer exists - if (!Storage.exists(book.path.c_str())) { + if (RecentBooksStore::isMissing(book)) { continue; } diff --git a/src/activities/home/RecentBooksActivity.cpp b/src/activities/home/RecentBooksActivity.cpp index b3746614..3934416e 100644 --- a/src/activities/home/RecentBooksActivity.cpp +++ b/src/activities/home/RecentBooksActivity.cpp @@ -68,15 +68,7 @@ std::string gridThumbPath(const std::string& coverBmpPath, int tw, int th) { } } // namespace -void RecentBooksActivity::loadRecentBooks() { - recentBooks.clear(); - const auto& books = RECENT_BOOKS.getBooks(); - recentBooks.reserve(books.size()); - for (const auto& book : books) { - if (!Storage.exists(book.path.c_str())) continue; - recentBooks.push_back(book); - } -} +void RecentBooksActivity::loadRecentBooks() { recentBooks = RECENT_BOOKS.getBooks(); } bool RecentBooksActivity::loadNextCover() { const Rect contentRect = UITheme::getContentRect(renderer, true, true); @@ -134,6 +126,10 @@ bool RecentBooksActivity::loadNextCover() { void RecentBooksActivity::onEnter() { Activity::onEnter(); + if (RECENT_BOOKS.pruneMissing()) { + RECENT_BOOKS.saveToFile(); + } + loadRecentBooks(); selectorIndex = 0;