Files
Crosspoint/src/RecentBooksStore.h
T
KemoNine df53faab91 feat: allow removing book from recent list (#2045)
## Summary

Add ability to long press 'confirm' on a book in the recent books list
to be prompted to remove it from the list.

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? *Yes, Claude*

---------
2026-05-18 22:16:33 -04:00

75 lines
2.5 KiB
C++

#pragma once
#include <string>
#include <vector>
struct RecentBook {
std::string path;
std::string title;
std::string author;
std::string coverBmpPath;
bool operator==(const RecentBook& other) const { return path == other.path; }
};
class RecentBooksStore;
namespace JsonSettingsIO {
bool loadRecentBooks(RecentBooksStore& store, const char* json);
} // namespace JsonSettingsIO
class RecentBooksStore {
// Static instance
static RecentBooksStore instance;
std::vector<RecentBook> recentBooks;
friend bool JsonSettingsIO::loadRecentBooks(RecentBooksStore&, const char*);
public:
~RecentBooksStore() = default;
// Get singleton instance
static RecentBooksStore& getInstance() { return instance; }
// Add a book to the recent list (moves to front if already exists)
void addBook(const std::string& path, const std::string& title, const std::string& author,
const std::string& coverBmpPath);
void updateBook(const std::string& path, const std::string& title, const std::string& author,
const std::string& coverBmpPath);
// Remove the entry whose path matches (used when a book is removed from recents or finished/read).
// Returns true if an entry was found and removed (no-op + false otherwise).
// Persistence is best-effort: a failed save is logged, not reflected in the return.
bool removeByPath(const std::string& path);
// Repoint an entry's path (and coverBmpPath, if it lived under the old cache dir) after the
// backing file and cache dir were moved on disk. No-op if no entry matches oldPath.
// Persists on success. Keeps the entry's list position (does not reorder).
void updatePath(const std::string& oldPath, const std::string& newPath, const std::string& oldCachePath,
const std::string& newCachePath);
// 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; }
// Get the count of recent books
int getCount() const { return static_cast<int>(recentBooks.size()); }
bool saveToFile() const;
bool loadFromFile();
RecentBook getDataFromBook(std::string path) const;
private:
bool loadFromBinaryFile();
};
// Helper macro to access recent books store
#define RECENT_BOOKS RecentBooksStore::getInstance()