crosspoint-reader/src/activities/reader/FileSelectionActivity.cpp

169 lines
5.1 KiB
C++
Raw Normal View History

#include "FileSelectionActivity.h"
2025-12-04 00:07:25 +11:00
#include <GfxRenderer.h>
2025-12-04 00:07:25 +11:00
#include <SD.h>
#include "config.h"
namespace {
constexpr int PAGE_ITEMS = 23;
constexpr int SKIP_PAGE_MS = 700;
} // namespace
2025-12-07 19:17:43 +11:00
void sortFileList(std::vector<std::string>& strs) {
2025-12-06 13:01:16 +11:00
std::sort(begin(strs), end(strs), [](const std::string& str1, const std::string& str2) {
2025-12-07 19:17:43 +11:00
if (str1.back() == '/' && str2.back() != '/') return true;
if (str1.back() != '/' && str2.back() == '/') return false;
2025-12-06 13:01:16 +11:00
return lexicographical_compare(
begin(str1), end(str1), begin(str2), end(str2),
[](const char& char1, const char& char2) { return tolower(char1) < tolower(char2); });
});
}
void FileSelectionActivity::taskTrampoline(void* param) {
auto* self = static_cast<FileSelectionActivity*>(param);
2025-12-04 00:07:25 +11:00
self->displayTaskLoop();
}
void FileSelectionActivity::loadFiles() {
2025-12-04 00:07:25 +11:00
files.clear();
2025-12-05 22:12:28 +11:00
selectorIndex = 0;
auto root = SD.open(basepath.c_str());
for (File file = root.openNextFile(); file; file = root.openNextFile()) {
2025-12-04 00:07:25 +11:00
auto filename = std::string(file.name());
2025-12-05 22:12:28 +11:00
if (filename[0] == '.') {
2025-12-04 00:07:25 +11:00
file.close();
continue;
}
2025-12-05 22:12:28 +11:00
if (file.isDirectory()) {
files.emplace_back(filename + "/");
} else if (filename.substr(filename.length() - 5) == ".epub") {
files.emplace_back(filename);
}
2025-12-04 00:07:25 +11:00
file.close();
}
root.close();
2025-12-07 19:17:43 +11:00
sortFileList(files);
2025-12-05 22:12:28 +11:00
}
void FileSelectionActivity::onEnter() {
renderingMutex = xSemaphoreCreateMutex();
2025-12-05 22:12:28 +11:00
basepath = "/";
loadFiles();
selectorIndex = 0;
2025-12-04 00:07:25 +11:00
// Trigger first update
updateRequired = true;
xTaskCreate(&FileSelectionActivity::taskTrampoline, "FileSelectionActivityTask",
2048, // Stack size
2025-12-04 00:07:25 +11:00
this, // Parameters
1, // Priority
&displayTaskHandle // Task handle
);
}
void FileSelectionActivity::onExit() {
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
xSemaphoreTake(renderingMutex, portMAX_DELAY);
if (displayTaskHandle) {
vTaskDelete(displayTaskHandle);
displayTaskHandle = nullptr;
}
vSemaphoreDelete(renderingMutex);
renderingMutex = nullptr;
files.clear();
2025-12-04 00:07:25 +11:00
}
void FileSelectionActivity::loop() {
const bool prevReleased =
inputManager.wasReleased(InputManager::BTN_UP) || inputManager.wasReleased(InputManager::BTN_LEFT);
const bool nextReleased =
inputManager.wasReleased(InputManager::BTN_DOWN) || inputManager.wasReleased(InputManager::BTN_RIGHT);
const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS;
2025-12-06 12:35:41 +11:00
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
2025-12-05 22:12:28 +11:00
if (files.empty()) {
return;
}
if (basepath.back() != '/') basepath += "/";
2025-12-05 22:12:28 +11:00
if (files[selectorIndex].back() == '/') {
basepath += files[selectorIndex].substr(0, files[selectorIndex].length() - 1);
loadFiles();
updateRequired = true;
} else {
onSelect(basepath + files[selectorIndex]);
}
} else if (inputManager.wasPressed(InputManager::BTN_BACK)) {
if (basepath != "/") {
basepath.replace(basepath.find_last_of('/'), std::string::npos, "");
if (basepath.empty()) basepath = "/";
loadFiles();
updateRequired = true;
} else {
// At root level, go back home
onGoHome();
}
} else if (prevReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + files.size()) % files.size();
} else {
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
}
2025-12-06 12:35:41 +11:00
updateRequired = true;
} else if (nextReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % files.size();
} else {
selectorIndex = (selectorIndex + 1) % files.size();
}
2025-12-06 12:35:41 +11:00
updateRequired = true;
2025-12-04 00:07:25 +11:00
}
}
void FileSelectionActivity::displayTaskLoop() {
2025-12-04 00:07:25 +11:00
while (true) {
if (updateRequired) {
updateRequired = false;
xSemaphoreTake(renderingMutex, portMAX_DELAY);
2025-12-04 00:07:25 +11:00
render();
xSemaphoreGive(renderingMutex);
2025-12-04 00:07:25 +11:00
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void FileSelectionActivity::render() const {
2025-12-06 12:56:39 +11:00
renderer.clearScreen();
2025-12-04 00:07:25 +11:00
const auto pageWidth = GfxRenderer::getScreenWidth();
renderer.drawCenteredText(READER_FONT_ID, 10, "Books", true, BOLD);
2025-12-04 00:07:25 +11:00
// Help text
renderer.drawText(SMALL_FONT_ID, 20, GfxRenderer::getScreenHeight() - 30, "Press BACK for Home");
2025-12-05 22:12:28 +11:00
if (files.empty()) {
renderer.drawText(UI_FONT_ID, 20, 60, "No EPUBs found");
renderer.displayBuffer();
return;
}
2025-12-04 00:07:25 +11:00
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 + 2, pageWidth - 1, 30);
for (int i = pageStartIndex; i < files.size() && i < pageStartIndex + PAGE_ITEMS; i++) {
auto item = files[i];
int itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
while (itemWidth > renderer.getScreenWidth() - 40 && item.length() > 8) {
item.replace(item.length() - 5, 5, "...");
itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
2025-12-05 22:12:28 +11:00
}
renderer.drawText(UI_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, item.c_str(), i != selectorIndex);
2025-12-04 00:07:25 +11:00
}
renderer.displayBuffer();
2025-12-04 00:07:25 +11:00
}