fix: prune books missing form sd card in recent books list (#1959)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user