Add reading lists feature with pinning and management

Adds full support for book lists managed by the Companion App:
- New /list API endpoints (GET/POST) for uploading, retrieving, and deleting lists
- BookListStore for binary serialization of lists to /.lists/ directory
- ListViewActivity for viewing list contents with book thumbnails
- Reading Lists tab in My Library with pin/unpin and delete actions
- Pinnable list shortcut on home screen (split button layout)
- Automatic cleanup of pinned status when lists are deleted
This commit is contained in:
cottongin
2026-01-26 02:08:59 -05:00
parent cda8a5ec6d
commit 2c24ee3f81
14 changed files with 1296 additions and 33 deletions

View File

@@ -0,0 +1,275 @@
#include "ListViewActivity.h"
#include <Bitmap.h>
#include <GfxRenderer.h>
#include <SDCardManager.h>
#include <algorithm>
#include "MappedInputManager.h"
#include "ScreenComponents.h"
#include "fontIds.h"
#include "util/StringUtils.h"
namespace {
// Layout constants (matching MyLibraryActivity's Recent tab)
constexpr int HEADER_Y = 15;
constexpr int CONTENT_START_Y = 60;
constexpr int LINE_HEIGHT = 65; // Two-line items (title + author)
constexpr int LEFT_MARGIN = 20;
constexpr int RIGHT_MARGIN = 40;
constexpr int MICRO_THUMB_WIDTH = 45;
constexpr int MICRO_THUMB_HEIGHT = 60;
constexpr int THUMB_RIGHT_MARGIN = 50;
// Timing thresholds
constexpr int SKIP_PAGE_MS = 700;
// Helper function to get the micro-thumb path for a book based on its file path
std::string getMicroThumbPathForBook(const std::string& bookPath) {
const size_t hash = std::hash<std::string>{}(bookPath);
if (StringUtils::checkFileExtension(bookPath, ".epub")) {
return "/.crosspoint/epub_" + std::to_string(hash) + "/micro_thumb.bmp";
} else if (StringUtils::checkFileExtension(bookPath, ".xtc") || StringUtils::checkFileExtension(bookPath, ".xtch")) {
return "/.crosspoint/xtc_" + std::to_string(hash) + "/micro_thumb.bmp";
} else if (StringUtils::checkFileExtension(bookPath, ".txt") || StringUtils::checkFileExtension(bookPath, ".TXT")) {
return "/.crosspoint/txt_" + std::to_string(hash) + "/micro_thumb.bmp";
}
return "";
}
} // namespace
int ListViewActivity::getPageItems() const {
const int screenHeight = renderer.getScreenHeight();
const int bottomBarHeight = 60;
const int availableHeight = screenHeight - CONTENT_START_Y - bottomBarHeight;
int items = availableHeight / LINE_HEIGHT;
if (items < 1) {
items = 1;
}
return items;
}
int ListViewActivity::getTotalPages() const {
const int itemCount = static_cast<int>(bookList.books.size());
const int pageItems = getPageItems();
if (itemCount == 0) return 1;
return (itemCount + pageItems - 1) / pageItems;
}
int ListViewActivity::getCurrentPage() const {
const int pageItems = getPageItems();
return selectorIndex / pageItems + 1;
}
void ListViewActivity::taskTrampoline(void* param) {
auto* self = static_cast<ListViewActivity*>(param);
self->displayTaskLoop();
}
void ListViewActivity::onEnter() {
Activity::onEnter();
renderingMutex = xSemaphoreCreateMutex();
// Load the list
if (!BookListStore::loadList(listName, bookList)) {
Serial.printf("[%lu] [LVA] Failed to load list: %s\n", millis(), listName.c_str());
}
selectorIndex = 0;
updateRequired = true;
xTaskCreate(&ListViewActivity::taskTrampoline, "ListViewActivityTask",
4096, // Stack size
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void ListViewActivity::onExit() {
Activity::onExit();
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
bookList.books.clear();
}
void ListViewActivity::loop() {
const int itemCount = static_cast<int>(bookList.books.size());
const int pageItems = getPageItems();
// Back button - return to My Library
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
if (onBack) {
onBack();
}
return;
}
// Confirm button - open selected book
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (!bookList.books.empty() && selectorIndex < itemCount) {
const auto& book = bookList.books[selectorIndex];
// Check if file exists before opening
if (SdMan.exists(book.path.c_str())) {
if (onSelectBook) {
onSelectBook(book.path);
}
} else {
Serial.printf("[%lu] [LVA] Book file not found: %s\n", millis(), book.path.c_str());
// Could show an error message here
}
}
return;
}
const bool upReleased = mappedInput.wasReleased(MappedInputManager::Button::Up);
const bool downReleased = mappedInput.wasReleased(MappedInputManager::Button::Down);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
// Navigation: Up/Down moves through items
if (upReleased && itemCount > 0) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + itemCount) % itemCount;
} else {
selectorIndex = (selectorIndex + itemCount - 1) % itemCount;
}
updateRequired = true;
} else if (downReleased && itemCount > 0) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % itemCount;
} else {
selectorIndex = (selectorIndex + 1) % itemCount;
}
updateRequired = true;
}
}
void ListViewActivity::displayTaskLoop() {
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
render();
xSemaphoreGive(renderingMutex);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void ListViewActivity::render() const {
renderer.clearScreen();
const auto pageWidth = renderer.getScreenWidth();
const int pageItems = getPageItems();
const int bookCount = static_cast<int>(bookList.books.size());
// Draw header with list name
auto truncatedTitle = renderer.truncatedText(UI_12_FONT_ID, listName.c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN);
renderer.drawText(UI_12_FONT_ID, LEFT_MARGIN, HEADER_Y, truncatedTitle.c_str(), true, EpdFontFamily::BOLD);
if (bookCount == 0) {
renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, "This list is empty");
// Draw bottom button hints
const auto labels = mappedInput.mapLabels("« Back", "", "", "");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
return;
}
const auto pageStartIndex = selectorIndex / pageItems * pageItems;
// Draw selection highlight
renderer.fillRect(0, CONTENT_START_Y + (selectorIndex % pageItems) * LINE_HEIGHT - 2, pageWidth - RIGHT_MARGIN,
LINE_HEIGHT);
// Calculate available text width
const int textMaxWidth = pageWidth - LEFT_MARGIN - RIGHT_MARGIN - MICRO_THUMB_WIDTH - 10;
const int thumbX = pageWidth - THUMB_RIGHT_MARGIN - MICRO_THUMB_WIDTH;
// Draw items
for (int i = pageStartIndex; i < bookCount && i < pageStartIndex + pageItems; i++) {
const auto& book = bookList.books[i];
const int y = CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT;
const bool isSelected = (i == selectorIndex);
// Try to load and draw micro-thumbnail
bool hasThumb = false;
const std::string microThumbPath = getMicroThumbPathForBook(book.path);
if (!microThumbPath.empty() && SdMan.exists(microThumbPath.c_str())) {
FsFile thumbFile;
if (SdMan.openFileForRead("LVA", microThumbPath, thumbFile)) {
Bitmap bitmap(thumbFile);
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
const int bmpW = bitmap.getWidth();
const int bmpH = bitmap.getHeight();
const float scaleX = static_cast<float>(MICRO_THUMB_WIDTH) / static_cast<float>(bmpW);
const float scaleY = static_cast<float>(MICRO_THUMB_HEIGHT) / static_cast<float>(bmpH);
const float scale = std::min(scaleX, scaleY);
const int drawnW = static_cast<int>(bmpW * scale);
const int drawnH = static_cast<int>(bmpH * scale);
const int thumbY = y + (LINE_HEIGHT - drawnH) / 2;
if (isSelected) {
renderer.fillRect(thumbX, thumbY, drawnW, drawnH, false);
}
renderer.drawBitmap(bitmap, thumbX, thumbY, MICRO_THUMB_WIDTH, MICRO_THUMB_HEIGHT, 0, 0, isSelected);
hasThumb = true;
}
thumbFile.close();
}
}
// Use full width if no thumbnail
const int availableWidth = hasThumb ? textMaxWidth : (pageWidth - LEFT_MARGIN - RIGHT_MARGIN);
// Line 1: Title
std::string title = book.title;
if (title.empty()) {
// Fallback: extract filename from path
title = book.path;
const size_t lastSlash = title.find_last_of('/');
if (lastSlash != std::string::npos) {
title = title.substr(lastSlash + 1);
}
const size_t dot = title.find_last_of('.');
if (dot != std::string::npos) {
title.resize(dot);
}
}
auto truncatedBookTitle = renderer.truncatedText(UI_12_FONT_ID, title.c_str(), availableWidth);
renderer.drawText(UI_12_FONT_ID, LEFT_MARGIN, y + 2, truncatedBookTitle.c_str(), !isSelected);
// Line 2: Author
if (!book.author.empty()) {
auto truncatedAuthor = renderer.truncatedText(UI_10_FONT_ID, book.author.c_str(), availableWidth);
renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, y + 32, truncatedAuthor.c_str(), !isSelected);
}
}
// Draw scroll indicator
const int screenHeight = renderer.getScreenHeight();
const int contentHeight = screenHeight - CONTENT_START_Y - 60;
ScreenComponents::drawScrollIndicator(renderer, getCurrentPage(), getTotalPages(), CONTENT_START_Y, contentHeight);
// Draw side button hints
renderer.drawSideButtonHints(UI_10_FONT_ID, ">", "<");
// Draw bottom button hints
const auto labels = mappedInput.mapLabels("« Back", "Open", "", "");
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
renderer.displayBuffer();
}