fix: prune books missing form sd card in recent books list (#1959)

This commit is contained in:
KemoNine
2026-05-16 22:03:49 +03:00
committed by GitHub
parent a3e51f9b1e
commit 93e81daf41
4 changed files with 26 additions and 14 deletions
+11
View File
@@ -22,6 +22,9 @@ RecentBooksStore RecentBooksStore::instance;
void RecentBooksStore::addBook(const std::string& path, const std::string& title, const std::string& author,
const std::string& coverBmpPath) {
// Drop stale entries first so a new add can't evict a valid book in their stead.
pruneMissing();
// Remove existing entry if present
auto it =
std::find_if(recentBooks.begin(), recentBooks.end(), [&](const RecentBook& book) { return book.path == path; });
@@ -53,6 +56,14 @@ void RecentBooksStore::updateBook(const std::string& path, const std::string& ti
}
}
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;
}
bool RecentBooksStore::saveToFile() const {
Storage.mkdir("/.crosspoint");
return JsonSettingsIO::saveRecentBooks(*this, RECENT_BOOKS_FILE_JSON);
+7
View File
@@ -37,6 +37,13 @@ class RecentBooksStore {
void updateBook(const std::string& path, const std::string& title, const std::string& author,
const std::string& coverBmpPath);
// True if the book's backing file is no longer present on the SD card.
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();
// Get the list of recent books (most recent first)
const std::vector<RecentBook>& getBooks() const { return recentBooks; }
+1 -1
View File
@@ -43,7 +43,7 @@ void HomeActivity::loadRecentBooks(int maxBooks) {
}
// Skip if file no longer exists
if (!Storage.exists(book.path.c_str())) {
if (RecentBooksStore::isMissing(book)) {
continue;
}
+7 -13
View File
@@ -15,23 +15,17 @@ namespace {
constexpr unsigned long GO_HOME_MS = 1000;
} // namespace
void RecentBooksActivity::loadRecentBooks() {
recentBooks.clear();
const auto& books = RECENT_BOOKS.getBooks();
recentBooks.reserve(books.size());
for (const auto& book : books) {
// Skip if file no longer exists
if (!Storage.exists(book.path.c_str())) {
continue;
}
recentBooks.push_back(book);
}
}
void RecentBooksActivity::loadRecentBooks() { recentBooks = RECENT_BOOKS.getBooks(); }
void RecentBooksActivity::onEnter() {
Activity::onEnter();
// Prune entries whose backing files are gone; this is one of two interaction
// points where the persistent store gets cleaned (the other is addBook).
if (RECENT_BOOKS.pruneMissing()) {
RECENT_BOOKS.saveToFile();
}
// Load data
loadRecentBooks();