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:
@@ -30,7 +30,7 @@ void HomeActivity::taskTrampoline(void* param) {
|
||||
}
|
||||
|
||||
int HomeActivity::getMenuItemCount() const {
|
||||
int count = 3; // My Library, File transfer, Settings
|
||||
int count = 4; // Lists, My Library, File transfer, Settings
|
||||
if (hasContinueReading) count++;
|
||||
if (hasOpdsUrl) count++;
|
||||
return count;
|
||||
@@ -220,6 +220,7 @@ void HomeActivity::loop() {
|
||||
// Calculate dynamic indices based on which options are available
|
||||
int idx = 0;
|
||||
const int continueIdx = hasContinueReading ? idx++ : -1;
|
||||
const int listsIdx = idx++;
|
||||
const int myLibraryIdx = idx++;
|
||||
const int opdsLibraryIdx = hasOpdsUrl ? idx++ : -1;
|
||||
const int fileTransferIdx = idx++;
|
||||
@@ -227,6 +228,8 @@ void HomeActivity::loop() {
|
||||
|
||||
if (selectorIndex == continueIdx) {
|
||||
onContinueReading();
|
||||
} else if (selectorIndex == listsIdx) {
|
||||
onListsOpen();
|
||||
} else if (selectorIndex == myLibraryIdx) {
|
||||
onMyLibraryOpen();
|
||||
} else if (selectorIndex == opdsLibraryIdx) {
|
||||
@@ -274,17 +277,29 @@ void HomeActivity::render() {
|
||||
|
||||
// --- Calculate layout from bottom up ---
|
||||
|
||||
// Build lists button label (dynamic based on pinned list)
|
||||
std::string listsLabel;
|
||||
if (strlen(SETTINGS.pinnedListName) > 0) {
|
||||
listsLabel = std::string(SETTINGS.pinnedListName);
|
||||
} else {
|
||||
listsLabel = "ReadingLists";
|
||||
}
|
||||
|
||||
// Build menu items dynamically (need count for layout calculation)
|
||||
std::vector<const char*> menuItems = {"My Library", "File Transfer", "Settings"};
|
||||
// First row is split: [Lists] [My Library]
|
||||
// Rest are full width: Calibre Library (if configured), File Transfer, Settings
|
||||
std::vector<const char*> fullWidthItems = {"File Transfer", "Settings"};
|
||||
if (hasOpdsUrl) {
|
||||
menuItems.insert(menuItems.begin() + 1, "Calibre Library");
|
||||
fullWidthItems.insert(fullWidthItems.begin(), "Calibre Library");
|
||||
}
|
||||
|
||||
const int menuTileWidth = pageWidth - 2 * margin;
|
||||
constexpr int menuTileHeight = 45;
|
||||
constexpr int menuSpacing = 8;
|
||||
const int halfTileWidth = (menuTileWidth - menuSpacing) / 2; // Account for spacing between halves
|
||||
// 1 row for split buttons + full-width rows
|
||||
const int totalMenuHeight =
|
||||
static_cast<int>(menuItems.size()) * menuTileHeight + (static_cast<int>(menuItems.size()) - 1) * menuSpacing;
|
||||
menuTileHeight + static_cast<int>(fullWidthItems.size()) * (menuTileHeight + menuSpacing);
|
||||
|
||||
// Anchor menu to bottom of screen
|
||||
const int menuStartY = pageHeight - bottomMargin - totalMenuHeight - margin;
|
||||
@@ -580,10 +595,61 @@ void HomeActivity::render() {
|
||||
}
|
||||
|
||||
// --- Bottom menu tiles (anchored to bottom) ---
|
||||
for (size_t i = 0; i < menuItems.size(); ++i) {
|
||||
const int overallIndex = static_cast<int>(i) + (hasContinueReading ? 1 : 0);
|
||||
|
||||
// First row: Split buttons [Lists] [My Library]
|
||||
const int firstRowY = menuStartY;
|
||||
const int baseMenuIndex = hasContinueReading ? 1 : 0;
|
||||
|
||||
// Lists button (left half)
|
||||
{
|
||||
const int tileX = margin;
|
||||
const bool selected = selectorIndex == baseMenuIndex;
|
||||
|
||||
if (selected) {
|
||||
renderer.fillRect(tileX, firstRowY, halfTileWidth, menuTileHeight);
|
||||
} else {
|
||||
renderer.drawRect(tileX, firstRowY, halfTileWidth, menuTileHeight);
|
||||
}
|
||||
|
||||
// Truncate lists label if needed
|
||||
std::string truncatedLabel = listsLabel;
|
||||
const int maxLabelWidth = halfTileWidth - 16; // Padding
|
||||
while (renderer.getTextWidth(UI_10_FONT_ID, truncatedLabel.c_str()) > maxLabelWidth && truncatedLabel.length() > 3) {
|
||||
truncatedLabel = truncatedLabel.substr(0, truncatedLabel.length() - 4) + "...";
|
||||
}
|
||||
|
||||
const int textWidth = renderer.getTextWidth(UI_10_FONT_ID, truncatedLabel.c_str());
|
||||
const int textX = tileX + (halfTileWidth - textWidth) / 2;
|
||||
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
|
||||
const int textY = firstRowY + (menuTileHeight - lineHeight) / 2;
|
||||
renderer.drawText(UI_10_FONT_ID, textX, textY, truncatedLabel.c_str(), !selected);
|
||||
}
|
||||
|
||||
// My Library button (right half)
|
||||
{
|
||||
const int tileX = margin + halfTileWidth + menuSpacing;
|
||||
const bool selected = selectorIndex == baseMenuIndex + 1;
|
||||
|
||||
if (selected) {
|
||||
renderer.fillRect(tileX, firstRowY, halfTileWidth, menuTileHeight);
|
||||
} else {
|
||||
renderer.drawRect(tileX, firstRowY, halfTileWidth, menuTileHeight);
|
||||
}
|
||||
|
||||
const char* label = "My Library";
|
||||
const int textWidth = renderer.getTextWidth(UI_10_FONT_ID, label);
|
||||
const int textX = tileX + (halfTileWidth - textWidth) / 2;
|
||||
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
|
||||
const int textY = firstRowY + (menuTileHeight - lineHeight) / 2;
|
||||
renderer.drawText(UI_10_FONT_ID, textX, textY, label, !selected);
|
||||
}
|
||||
|
||||
// Full-width menu items (Calibre Library if configured, File Transfer, Settings)
|
||||
for (size_t i = 0; i < fullWidthItems.size(); ++i) {
|
||||
// Index offset: base + 2 (for Lists and My Library) + i
|
||||
const int overallIndex = baseMenuIndex + 2 + static_cast<int>(i);
|
||||
constexpr int tileX = margin;
|
||||
const int tileY = menuStartY + static_cast<int>(i) * (menuTileHeight + menuSpacing);
|
||||
const int tileY = firstRowY + menuTileHeight + menuSpacing + static_cast<int>(i) * (menuTileHeight + menuSpacing);
|
||||
const bool selected = selectorIndex == overallIndex;
|
||||
|
||||
if (selected) {
|
||||
@@ -592,13 +658,12 @@ void HomeActivity::render() {
|
||||
renderer.drawRect(tileX, tileY, menuTileWidth, menuTileHeight);
|
||||
}
|
||||
|
||||
const char* label = menuItems[i];
|
||||
const char* label = fullWidthItems[i];
|
||||
const int textWidth = renderer.getTextWidth(UI_10_FONT_ID, label);
|
||||
const int textX = tileX + (menuTileWidth - textWidth) / 2;
|
||||
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
|
||||
const int textY = tileY + (menuTileHeight - lineHeight) / 2; // vertically centered assuming y is top of text
|
||||
const int textY = tileY + (menuTileHeight - lineHeight) / 2;
|
||||
|
||||
// Invert text when the tile is selected, to contrast with the filled background
|
||||
renderer.drawText(UI_10_FONT_ID, textX, textY, label, !selected);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ class HomeActivity final : public Activity {
|
||||
std::string lastBookAuthor;
|
||||
std::string coverBmpPath;
|
||||
const std::function<void()> onContinueReading;
|
||||
const std::function<void()> onListsOpen; // Goes to pinned list or lists tab
|
||||
const std::function<void()> onMyLibraryOpen;
|
||||
const std::function<void()> onSettingsOpen;
|
||||
const std::function<void()> onFileTransferOpen;
|
||||
@@ -41,11 +42,13 @@ class HomeActivity final : public Activity {
|
||||
|
||||
public:
|
||||
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
const std::function<void()>& onContinueReading, const std::function<void()>& onMyLibraryOpen,
|
||||
const std::function<void()>& onSettingsOpen, const std::function<void()>& onFileTransferOpen,
|
||||
const std::function<void()>& onContinueReading, const std::function<void()>& onListsOpen,
|
||||
const std::function<void()>& onMyLibraryOpen, const std::function<void()>& onSettingsOpen,
|
||||
const std::function<void()>& onFileTransferOpen,
|
||||
const std::function<void()>& onOpdsBrowserOpen)
|
||||
: Activity("Home", renderer, mappedInput),
|
||||
onContinueReading(onContinueReading),
|
||||
onListsOpen(onListsOpen),
|
||||
onMyLibraryOpen(onMyLibraryOpen),
|
||||
onSettingsOpen(onSettingsOpen),
|
||||
onFileTransferOpen(onFileTransferOpen),
|
||||
|
||||
275
src/activities/home/ListViewActivity.cpp
Normal file
275
src/activities/home/ListViewActivity.cpp
Normal 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();
|
||||
}
|
||||
52
src/activities/home/ListViewActivity.h
Normal file
52
src/activities/home/ListViewActivity.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../Activity.h"
|
||||
#include "BookListStore.h"
|
||||
|
||||
/**
|
||||
* ListViewActivity - Displays the contents of a single book list
|
||||
*
|
||||
* Shows books in custom order with title, author, and thumbnail.
|
||||
* Allows selecting a book to open in the reader.
|
||||
*/
|
||||
class ListViewActivity final : public Activity {
|
||||
private:
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
|
||||
std::string listName;
|
||||
BookList bookList;
|
||||
int selectorIndex = 0;
|
||||
bool updateRequired = false;
|
||||
|
||||
// Callbacks
|
||||
const std::function<void()> onBack;
|
||||
const std::function<void(const std::string& path)> onSelectBook;
|
||||
|
||||
// Pagination helpers
|
||||
int getPageItems() const;
|
||||
int getTotalPages() const;
|
||||
int getCurrentPage() const;
|
||||
|
||||
// Rendering
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
|
||||
public:
|
||||
explicit ListViewActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& listName,
|
||||
const std::function<void()>& onBack,
|
||||
const std::function<void(const std::string& path)>& onSelectBook)
|
||||
: Activity("ListView", renderer, mappedInput), listName(listName), onBack(onBack), onSelectBook(onSelectBook) {}
|
||||
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
};
|
||||
@@ -5,8 +5,11 @@
|
||||
#include <SDCardManager.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#include "BookListStore.h"
|
||||
#include "BookManager.h"
|
||||
#include "CrossPointSettings.h"
|
||||
#include "MappedInputManager.h"
|
||||
#include "RecentBooksStore.h"
|
||||
#include "ScreenComponents.h"
|
||||
@@ -63,6 +66,7 @@ int MyLibraryActivity::getPageItems() const {
|
||||
const int screenHeight = renderer.getScreenHeight();
|
||||
const int bottomBarHeight = 60; // Space for button hints
|
||||
const int availableHeight = screenHeight - CONTENT_START_Y - bottomBarHeight;
|
||||
// Recent tab uses taller items (title + author), Lists and Files use single-line items
|
||||
const int lineHeight = (currentTab == Tab::Recent) ? RECENTS_LINE_HEIGHT : LINE_HEIGHT;
|
||||
int items = availableHeight / lineHeight;
|
||||
if (items < 1) {
|
||||
@@ -74,6 +78,8 @@ int MyLibraryActivity::getPageItems() const {
|
||||
int MyLibraryActivity::getCurrentItemCount() const {
|
||||
if (currentTab == Tab::Recent) {
|
||||
return static_cast<int>(recentBooks.size());
|
||||
} else if (currentTab == Tab::Lists) {
|
||||
return static_cast<int>(lists.size());
|
||||
}
|
||||
return static_cast<int>(files.size());
|
||||
}
|
||||
@@ -104,6 +110,8 @@ void MyLibraryActivity::loadRecentBooks() {
|
||||
}
|
||||
}
|
||||
|
||||
void MyLibraryActivity::loadLists() { lists = BookListStore::listAllLists(); }
|
||||
|
||||
void MyLibraryActivity::loadFiles() {
|
||||
files.clear();
|
||||
|
||||
@@ -155,8 +163,9 @@ void MyLibraryActivity::onEnter() {
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Load data for both tabs
|
||||
// Load data for all tabs
|
||||
loadRecentBooks();
|
||||
loadLists();
|
||||
loadFiles();
|
||||
|
||||
selectorIndex = 0;
|
||||
@@ -184,6 +193,7 @@ void MyLibraryActivity::onExit() {
|
||||
renderingMutex = nullptr;
|
||||
|
||||
recentBooks.clear();
|
||||
lists.clear();
|
||||
files.clear();
|
||||
}
|
||||
|
||||
@@ -259,6 +269,53 @@ void MyLibraryActivity::executeAction() {
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
void MyLibraryActivity::togglePinForSelectedList() {
|
||||
if (lists.empty() || selectorIndex >= static_cast<int>(lists.size())) return;
|
||||
|
||||
const std::string& selected = lists[selectorIndex];
|
||||
if (selected == SETTINGS.pinnedListName) {
|
||||
// Unpin - clear the pinned list
|
||||
SETTINGS.pinnedListName[0] = '\0';
|
||||
} else {
|
||||
// Pin this list (replaces any previously pinned list)
|
||||
strncpy(SETTINGS.pinnedListName, selected.c_str(), sizeof(SETTINGS.pinnedListName) - 1);
|
||||
SETTINGS.pinnedListName[sizeof(SETTINGS.pinnedListName) - 1] = '\0';
|
||||
}
|
||||
SETTINGS.saveToFile();
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
void MyLibraryActivity::openListActionMenu() {
|
||||
listActionTargetName = lists[selectorIndex];
|
||||
listMenuSelection = 0; // Default to Pin/Unpin
|
||||
uiState = UIState::ListActionMenu;
|
||||
ignoreNextConfirmRelease = true;
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
void MyLibraryActivity::executeListAction() {
|
||||
// Clear pinned status if deleting the pinned list
|
||||
if (listActionTargetName == SETTINGS.pinnedListName) {
|
||||
SETTINGS.pinnedListName[0] = '\0';
|
||||
SETTINGS.saveToFile();
|
||||
}
|
||||
|
||||
BookListStore::deleteList(listActionTargetName);
|
||||
|
||||
// Reload lists
|
||||
loadLists();
|
||||
|
||||
// Adjust selector if needed
|
||||
if (selectorIndex >= static_cast<int>(lists.size()) && !lists.empty()) {
|
||||
selectorIndex = static_cast<int>(lists.size()) - 1;
|
||||
} else if (lists.empty()) {
|
||||
selectorIndex = 0;
|
||||
}
|
||||
|
||||
uiState = UIState::Normal;
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
void MyLibraryActivity::loop() {
|
||||
// Handle action menu state
|
||||
if (uiState == UIState::ActionMenu) {
|
||||
@@ -312,6 +369,64 @@ void MyLibraryActivity::loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle list action menu state
|
||||
if (uiState == UIState::ListActionMenu) {
|
||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||
uiState = UIState::Normal;
|
||||
ignoreNextConfirmRelease = false;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mappedInput.wasReleased(MappedInputManager::Button::Up)) {
|
||||
listMenuSelection = 0; // Pin/Unpin
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mappedInput.wasReleased(MappedInputManager::Button::Down)) {
|
||||
listMenuSelection = 1; // Delete
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||
// Ignore the release from the long-press that opened this menu
|
||||
if (ignoreNextConfirmRelease) {
|
||||
ignoreNextConfirmRelease = false;
|
||||
return;
|
||||
}
|
||||
if (listMenuSelection == 0) {
|
||||
// Pin/Unpin - toggle and return to normal
|
||||
togglePinForSelectedList();
|
||||
uiState = UIState::Normal;
|
||||
} else {
|
||||
// Delete - go to confirmation
|
||||
uiState = UIState::ListConfirmingDelete;
|
||||
}
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle list delete confirmation state
|
||||
if (uiState == UIState::ListConfirmingDelete) {
|
||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||
uiState = UIState::Normal;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||
executeListAction();
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal state handling
|
||||
const int itemCount = getCurrentItemCount();
|
||||
const int pageItems = getPageItems();
|
||||
@@ -328,6 +443,15 @@ void MyLibraryActivity::loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Long press Confirm to open list action menu (only on Lists tab)
|
||||
constexpr unsigned long LIST_ACTION_MENU_MS = 700;
|
||||
if (currentTab == Tab::Lists && mappedInput.isPressed(MappedInputManager::Button::Confirm) &&
|
||||
mappedInput.getHeldTime() >= LIST_ACTION_MENU_MS && !lists.empty() &&
|
||||
selectorIndex < static_cast<int>(lists.size())) {
|
||||
openListActionMenu();
|
||||
return;
|
||||
}
|
||||
|
||||
// Long press Confirm to open action menu (only for files, not directories)
|
||||
if (mappedInput.isPressed(MappedInputManager::Button::Confirm) &&
|
||||
mappedInput.getHeldTime() >= ACTION_MENU_MS && isSelectedItemAFile()) {
|
||||
@@ -353,6 +477,13 @@ void MyLibraryActivity::loop() {
|
||||
if (!recentBooks.empty() && selectorIndex < static_cast<int>(recentBooks.size())) {
|
||||
onSelectBook(recentBooks[selectorIndex].path, currentTab);
|
||||
}
|
||||
} else if (currentTab == Tab::Lists) {
|
||||
// Lists tab - open selected list
|
||||
if (!lists.empty() && selectorIndex < static_cast<int>(lists.size())) {
|
||||
if (onSelectList) {
|
||||
onSelectList(lists[selectorIndex]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Files tab
|
||||
if (!files.empty() && selectorIndex < static_cast<int>(files.size())) {
|
||||
@@ -396,18 +527,32 @@ void MyLibraryActivity::loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Tab switching: Left/Right always control tabs
|
||||
if (leftReleased && currentTab == Tab::Files) {
|
||||
currentTab = Tab::Recent;
|
||||
selectorIndex = 0;
|
||||
updateRequired = true;
|
||||
return;
|
||||
// Tab switching: Left/Right always control tabs (Recent <-> Lists <-> Files)
|
||||
if (leftReleased) {
|
||||
if (currentTab == Tab::Files) {
|
||||
currentTab = Tab::Lists;
|
||||
selectorIndex = 0;
|
||||
updateRequired = true;
|
||||
return;
|
||||
} else if (currentTab == Tab::Lists) {
|
||||
currentTab = Tab::Recent;
|
||||
selectorIndex = 0;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (rightReleased && currentTab == Tab::Recent) {
|
||||
currentTab = Tab::Files;
|
||||
selectorIndex = 0;
|
||||
updateRequired = true;
|
||||
return;
|
||||
if (rightReleased) {
|
||||
if (currentTab == Tab::Recent) {
|
||||
currentTab = Tab::Lists;
|
||||
selectorIndex = 0;
|
||||
updateRequired = true;
|
||||
return;
|
||||
} else if (currentTab == Tab::Lists) {
|
||||
currentTab = Tab::Files;
|
||||
selectorIndex = 0;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Navigation: Up/Down moves through items only
|
||||
@@ -459,14 +604,30 @@ void MyLibraryActivity::render() const {
|
||||
return;
|
||||
}
|
||||
|
||||
if (uiState == UIState::ListActionMenu) {
|
||||
renderListActionMenu();
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (uiState == UIState::ListConfirmingDelete) {
|
||||
renderListDeleteConfirmation();
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal state - draw library view
|
||||
// Draw tab bar
|
||||
std::vector<TabInfo> tabs = {{"Recent", currentTab == Tab::Recent}, {"Files", currentTab == Tab::Files}};
|
||||
std::vector<TabInfo> tabs = {{"Recent", currentTab == Tab::Recent},
|
||||
{"Reading Lists", currentTab == Tab::Lists},
|
||||
{"Files", currentTab == Tab::Files}};
|
||||
ScreenComponents::drawTabBar(renderer, TAB_BAR_Y, tabs);
|
||||
|
||||
// Draw content based on current tab
|
||||
if (currentTab == Tab::Recent) {
|
||||
renderRecentTab();
|
||||
} else if (currentTab == Tab::Lists) {
|
||||
renderListsTab();
|
||||
} else {
|
||||
renderFilesTab();
|
||||
}
|
||||
@@ -599,6 +760,36 @@ void MyLibraryActivity::renderRecentTab() const {
|
||||
}
|
||||
}
|
||||
|
||||
void MyLibraryActivity::renderListsTab() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const int pageItems = getPageItems();
|
||||
const int listCount = static_cast<int>(lists.size());
|
||||
|
||||
if (listCount == 0) {
|
||||
renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y, "No lists found");
|
||||
renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y + LINE_HEIGHT, "Create lists in Companion App");
|
||||
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);
|
||||
|
||||
// Draw items
|
||||
for (int i = pageStartIndex; i < listCount && i < pageStartIndex + pageItems; i++) {
|
||||
// Add indicator for pinned list
|
||||
std::string displayName = lists[i];
|
||||
if (displayName == SETTINGS.pinnedListName) {
|
||||
displayName = "• " + displayName + " •";
|
||||
}
|
||||
auto item = renderer.truncatedText(UI_10_FONT_ID, displayName.c_str(), pageWidth - LEFT_MARGIN - RIGHT_MARGIN);
|
||||
renderer.drawText(UI_10_FONT_ID, LEFT_MARGIN, CONTENT_START_Y + (i % pageItems) * LINE_HEIGHT, item.c_str(),
|
||||
i != selectorIndex);
|
||||
}
|
||||
}
|
||||
|
||||
void MyLibraryActivity::renderFilesTab() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const int pageItems = getPageItems();
|
||||
@@ -688,3 +879,63 @@ void MyLibraryActivity::renderConfirmation() const {
|
||||
const auto labels = mappedInput.mapLabels("« Cancel", "Confirm", "", "");
|
||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
||||
}
|
||||
|
||||
void MyLibraryActivity::renderListActionMenu() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
// Title
|
||||
renderer.drawCenteredText(UI_12_FONT_ID, 20, "List Actions", true, EpdFontFamily::BOLD);
|
||||
|
||||
// Show list name
|
||||
auto truncatedName = renderer.truncatedText(UI_10_FONT_ID, listActionTargetName.c_str(), pageWidth - 40);
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, 70, truncatedName.c_str());
|
||||
|
||||
// Menu options
|
||||
const int menuStartY = pageHeight / 2 - 30;
|
||||
constexpr int menuLineHeight = 40;
|
||||
constexpr int menuItemWidth = 120;
|
||||
const int menuX = (pageWidth - menuItemWidth) / 2;
|
||||
|
||||
// Pin/Unpin option (dynamic label)
|
||||
const bool isPinned = (listActionTargetName == SETTINGS.pinnedListName);
|
||||
const char* pinLabel = isPinned ? "Unpin" : "Pin";
|
||||
|
||||
if (listMenuSelection == 0) {
|
||||
renderer.fillRect(menuX - 10, menuStartY - 5, menuItemWidth + 20, menuLineHeight);
|
||||
}
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, menuStartY, pinLabel, listMenuSelection != 0);
|
||||
|
||||
// Delete option
|
||||
if (listMenuSelection == 1) {
|
||||
renderer.fillRect(menuX - 10, menuStartY + menuLineHeight - 5, menuItemWidth + 20, menuLineHeight);
|
||||
}
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, menuStartY + menuLineHeight, "Delete", listMenuSelection != 1);
|
||||
|
||||
// Draw side button hints (up/down navigation)
|
||||
renderer.drawSideButtonHints(UI_10_FONT_ID, ">", "<");
|
||||
|
||||
// Draw bottom button hints
|
||||
const auto labels = mappedInput.mapLabels("« Cancel", "Select", "", "");
|
||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
||||
}
|
||||
|
||||
void MyLibraryActivity::renderListDeleteConfirmation() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
// Title
|
||||
renderer.drawCenteredText(UI_12_FONT_ID, 20, "Delete List?", true, EpdFontFamily::BOLD);
|
||||
|
||||
// Show list name
|
||||
auto truncatedName = renderer.truncatedText(UI_10_FONT_ID, listActionTargetName.c_str(), pageWidth - 40);
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 - 40, truncatedName.c_str());
|
||||
|
||||
// Warning text
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2, "List will be permanently deleted!", true, EpdFontFamily::BOLD);
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 + 25, "This cannot be undone.");
|
||||
|
||||
// Draw bottom button hints
|
||||
const auto labels = mappedInput.mapLabels("« Cancel", "Confirm", "", "");
|
||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "../Activity.h"
|
||||
#include "BookListStore.h"
|
||||
#include "RecentBooksStore.h"
|
||||
|
||||
// Cached thumbnail existence info for Recent tab
|
||||
@@ -20,8 +21,8 @@ struct ThumbExistsCache {
|
||||
|
||||
class MyLibraryActivity final : public Activity {
|
||||
public:
|
||||
enum class Tab { Recent, Files };
|
||||
enum class UIState { Normal, ActionMenu, Confirming };
|
||||
enum class Tab { Recent, Lists, Files };
|
||||
enum class UIState { Normal, ActionMenu, Confirming, ListActionMenu, ListConfirmingDelete };
|
||||
enum class ActionType { Archive, Delete };
|
||||
|
||||
private:
|
||||
@@ -47,6 +48,13 @@ class MyLibraryActivity final : public Activity {
|
||||
static constexpr int MAX_THUMB_CACHE = 10;
|
||||
static ThumbExistsCache thumbExistsCache[MAX_THUMB_CACHE];
|
||||
|
||||
// Lists tab state
|
||||
std::vector<std::string> lists;
|
||||
|
||||
// List action menu state (for Lists tab)
|
||||
int listMenuSelection = 0; // 0 = Pin/Unpin, 1 = Delete
|
||||
std::string listActionTargetName;
|
||||
|
||||
// Files tab state (from FileSelectionActivity)
|
||||
std::string basepath = "/";
|
||||
std::vector<std::string> files;
|
||||
@@ -54,6 +62,7 @@ class MyLibraryActivity final : public Activity {
|
||||
// Callbacks
|
||||
const std::function<void()> onGoHome;
|
||||
const std::function<void(const std::string& path, Tab fromTab)> onSelectBook;
|
||||
const std::function<void(const std::string& listName)> onSelectList;
|
||||
|
||||
// Number of items that fit on a page
|
||||
int getPageItems() const;
|
||||
@@ -63,6 +72,7 @@ class MyLibraryActivity final : public Activity {
|
||||
|
||||
// Data loading
|
||||
void loadRecentBooks();
|
||||
void loadLists();
|
||||
void loadFiles();
|
||||
size_t findEntry(const std::string& name) const;
|
||||
|
||||
@@ -71,6 +81,7 @@ class MyLibraryActivity final : public Activity {
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
void renderRecentTab() const;
|
||||
void renderListsTab() const;
|
||||
void renderFilesTab() const;
|
||||
void renderActionMenu() const;
|
||||
void renderConfirmation() const;
|
||||
@@ -80,16 +91,27 @@ class MyLibraryActivity final : public Activity {
|
||||
void executeAction();
|
||||
bool isSelectedItemAFile() const;
|
||||
|
||||
// List pinning
|
||||
void togglePinForSelectedList();
|
||||
|
||||
// List action menu
|
||||
void openListActionMenu();
|
||||
void executeListAction();
|
||||
void renderListActionMenu() const;
|
||||
void renderListDeleteConfirmation() const;
|
||||
|
||||
public:
|
||||
explicit MyLibraryActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||
const std::function<void()>& onGoHome,
|
||||
const std::function<void(const std::string& path, Tab fromTab)>& onSelectBook,
|
||||
const std::function<void(const std::string& listName)>& onSelectList,
|
||||
Tab initialTab = Tab::Recent, std::string initialPath = "/")
|
||||
: Activity("MyLibrary", renderer, mappedInput),
|
||||
currentTab(initialTab),
|
||||
basepath(initialPath.empty() ? "/" : std::move(initialPath)),
|
||||
onGoHome(onGoHome),
|
||||
onSelectBook(onSelectBook) {}
|
||||
onSelectBook(onSelectBook),
|
||||
onSelectList(onSelectList) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
|
||||
Reference in New Issue
Block a user