refactor: epub chapter selection

This commit is contained in:
Istiak Tridip 2026-01-28 03:29:15 +06:00
parent 178c826b52
commit 73c5f05843
No known key found for this signature in database
4 changed files with 75 additions and 26 deletions

View File

@ -7,11 +7,6 @@
#include "MappedInputManager.h"
#include "fontIds.h"
namespace {
// Time threshold for treating a long press as a page-up/page-down
constexpr int SKIP_PAGE_MS = 700;
} // namespace
bool EpubReaderChapterSelectionActivity::hasSyncOption() const { return KOREADER_STORE.hasCredentials(); }
int EpubReaderChapterSelectionActivity::getTotalItems() const {
@ -119,12 +114,6 @@ void EpubReaderChapterSelectionActivity::loop() {
return;
}
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::Up) ||
mappedInput.wasReleased(MappedInputManager::Button::Left);
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) ||
mappedInput.wasReleased(MappedInputManager::Button::Right);
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
const int pageItems = getPageItems();
const int totalItems = getTotalItems();
@ -145,21 +134,27 @@ void EpubReaderChapterSelectionActivity::loop() {
}
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoBack();
} else if (prevReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems - 1) * pageItems + totalItems) % totalItems;
} else {
selectorIndex = (selectorIndex + totalItems - 1) % totalItems;
}
buttonNavigator.onNextRelease([this, totalItems] {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
updateRequired = true;
} else if (nextReleased) {
if (skipPage) {
selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % totalItems;
} else {
selectorIndex = (selectorIndex + 1) % totalItems;
}
});
buttonNavigator.onPreviousRelease([this, totalItems] {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
updateRequired = true;
}
});
buttonNavigator.onNextContinuous([this, totalItems, pageItems] {
selectorIndex = ButtonNavigator::nextPageIndex(selectorIndex, totalItems, pageItems);
updateRequired = true;
});
buttonNavigator.onPreviousContinuous([this, totalItems, pageItems] {
selectorIndex = ButtonNavigator::previousPageIndex(selectorIndex, totalItems, pageItems);
updateRequired = true;
});
}
void EpubReaderChapterSelectionActivity::displayTaskLoop() {

View File

@ -7,12 +7,14 @@
#include <memory>
#include "../ActivityWithSubactivity.h"
#include "util/ButtonNavigator.h"
class EpubReaderChapterSelectionActivity final : public ActivityWithSubactivity {
std::shared_ptr<Epub> epub;
std::string epubPath;
TaskHandle_t displayTaskHandle = nullptr;
SemaphoreHandle_t renderingMutex = nullptr;
ButtonNavigator buttonNavigator;
int currentSpineIndex = 0;
int currentPage = 0;
int totalPagesInSpine = 0;

View File

@ -15,6 +15,10 @@ void ButtonNavigator::onNextPress(const Callback& callback) { onPress(getNextBut
void ButtonNavigator::onPreviousPress(const Callback& callback) { onPress(getPreviousButtons(), callback); }
void ButtonNavigator::onNextRelease(const Callback& callback) const { onRelease(getNextButtons(), callback); }
void ButtonNavigator::onPreviousRelease(const Callback& callback) const { onRelease(getPreviousButtons(), callback); }
void ButtonNavigator::onNextContinuous(const Callback& callback) { onContinuous(getNextButtons(), callback); }
void ButtonNavigator::onPreviousContinuous(const Callback& callback) { onContinuous(getPreviousButtons(), callback); }
@ -34,6 +38,21 @@ void ButtonNavigator::onPress(const Buttons& buttons, const Callback& callback)
callback();
}
}
void ButtonNavigator::onRelease(const Buttons& buttons, const Callback& callback) const {
if (!mappedInput) return;
bool buttonReleased = false;
for (const MappedInputManager::Button button : buttons) {
if (mappedInput->wasReleased(button)) {
buttonReleased = true;
break;
}
}
if (buttonReleased && !recentlyNavigatedContinuously()) {
callback();
}
}
void ButtonNavigator::onContinuous(const Buttons& buttons, const Callback& callback) {
if (!mappedInput) return;
@ -83,3 +102,29 @@ int ButtonNavigator::previousIndex(const int currentIndex, const int totalItems)
// Calculate the previous index with wrap-around
return (currentIndex + totalItems - 1) % totalItems;
}
int ButtonNavigator::nextPageIndex(const int currentIndex, const int totalItems, const int itemsPerPage) {
if (totalItems <= 0 || itemsPerPage <= 0) return 0;
const int lastPageIndex = (totalItems - 1) / itemsPerPage;
const int currentPageIndex = currentIndex / itemsPerPage;
if (currentPageIndex < lastPageIndex) {
return (currentPageIndex + 1) * itemsPerPage;
}
return 0;
}
int ButtonNavigator::previousPageIndex(const int currentIndex, const int totalItems, const int itemsPerPage) {
if (totalItems <= 0 || itemsPerPage <= 0) return 0;
const int lastPageIndex = (totalItems - 1) / itemsPerPage;
const int currentPageIndex = currentIndex / itemsPerPage;
if (currentPageIndex > 0) {
return (currentPageIndex - 1) * itemsPerPage;
}
return lastPageIndex * itemsPerPage;
}

View File

@ -38,10 +38,17 @@ class ButtonNavigator final {
void onPreviousPress(const Callback& callback);
void onPress(const Buttons& buttons, const Callback& callback);
void onNextRelease(const Callback& callback) const;
void onPreviousRelease(const Callback& callback) const;
void onRelease(const Buttons& buttons, const Callback& callback) const;
void onNextContinuous(const Callback& callback);
void onPreviousContinuous(const Callback& callback);
void onContinuous(const Buttons& buttons, const Callback& callback);
[[nodiscard]] static int nextIndex(int currentIndex, int totalItems);
[[nodiscard]] static int previousIndex(int currentIndex, int totalItems);
[[nodiscard]] static int nextPageIndex(int currentIndex, int totalItems, int itemsPerPage);
[[nodiscard]] static int previousPageIndex(int currentIndex, int totalItems, int itemsPerPage);
};