forked from crosspoint-reader/crosspoint-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentBooksActivity.cpp
More file actions
176 lines (151 loc) · 6.34 KB
/
Copy pathRecentBooksActivity.cpp
File metadata and controls
176 lines (151 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "RecentBooksActivity.h"
#include <GfxRenderer.h>
#include <HalStorage.h>
#include <I18n.h>
#include <algorithm>
#include <memory>
#include "MappedInputManager.h"
#include "RecentBooksStore.h"
#include "activities/util/ConfirmationActivity.h"
#include "components/UITheme.h"
#include "components/UiAppHelpers.h"
namespace fui = freeink::ui;
namespace {
// Hold threshold for the long-press "remove from list" action (firmware convention).
constexpr unsigned long LONG_PRESS_MS = 1000;
} // namespace
RecentBooksActivity::RecentBooksActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
: UiListActivity("RecentBooks", renderer, mappedInput, /*wantsTouchLongPress=*/true) {}
void RecentBooksActivity::loadRecentBooks() {
recentBooks = RECENT_BOOKS.getBooks();
rebuildRowItems();
}
// Derives rowItems from recentBooks. Called whenever recentBooks changes
// (loadRecentBooks(), i.e. load/removal) so buildScreen() reuses the cached
// rows on every repaint instead of rebuilding them per render.
void RecentBooksActivity::rebuildRowItems() {
rowItems.clear();
rowItems.reserve(recentBooks.size());
for (const auto& book : recentBooks) {
fui::ListItem item;
item.label = book.title.c_str();
if (!book.author.empty()) item.subtitle = book.author.c_str();
item.icon = listIconFor(UITheme::getFileIcon(book.path), 32); // subtitle rows carry the larger icon
item.actionValue = static_cast<int16_t>(rowItems.size());
rowItems.push_back(item);
}
}
void RecentBooksActivity::onEnter() {
UiListActivity::onEnter();
// Prune entries whose backing files are gone; this is one of two interaction
// points where the persistent store gets cleaned (the other is addBook).
if (RECENT_BOOKS.pruneMissing()) {
RECENT_BOOKS.saveToFile();
}
loadRecentBooks();
}
void RecentBooksActivity::onExit() {
Activity::onExit();
recentBooks.clear();
}
void RecentBooksActivity::activateIndex(const int index) {
// Opening the book leaves this screen; a lingering flash would gray an
// unrelated row when the list next appears.
app.clearTapFlash();
LOG_DBG("RBA", "Selected recent book: %s", recentBooks[index].path.c_str());
onSelectBook(recentBooks[index].path);
}
void RecentBooksActivity::onRowLongPress(const int index) {
// Long-press prompts removal from the list (mirrors the Confirm-button hold).
app.clearTapFlash();
promptRemoveBook(recentBooks[index].path, recentBooks[index].title);
}
bool RecentBooksActivity::handleButtons() {
// After a long-press has fired, swallow input until Confirm is physically released
// (so the release doesn't also open the book; re-arm only once the button is up).
if (longPressFired) {
if (!mappedInput.isPressed(MappedInputManager::Button::Confirm)) {
longPressFired = false;
}
return true;
}
// Long-press Confirm on the selected book: prompt to remove it from the list.
// Fires when the hold times out while still held (firmware hold-to-act pattern,
// cf. FileBrowserActivity BACK long-press).
if (!recentBooks.empty() && nav.selected < listCount() &&
mappedInput.isPressed(MappedInputManager::Button::Confirm) && mappedInput.getHeldTime() >= LONG_PRESS_MS) {
longPressFired = true;
promptRemoveBook(recentBooks[nav.selected].path, recentBooks[nav.selected].title);
return true;
}
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (!recentBooks.empty() && nav.selected < listCount()) {
activateIndex(nav.selected);
return true;
}
}
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
onGoHome();
return true;
}
return false;
}
void RecentBooksActivity::promptRemoveBook(const std::string& path, const std::string& title) {
auto handler = [this, path](const ActivityResult& res) {
if (res.isCancelled) {
LOG_DBG("RBA", "Remove from recents cancelled");
return;
}
if (RECENT_BOOKS.removeByPath(path)) {
LOG_DBG("RBA", "Removed from recents: %s", path.c_str());
loadRecentBooks();
if (recentBooks.empty()) {
nav.selected = 0;
} else if (nav.selected >= listCount()) {
nav.selected = listCount() - 1;
}
nav.follow(listCount());
requestUpdate(true);
}
};
startActivityForResult(
std::make_unique<ConfirmationActivity>(renderer, mappedInput, tr(STR_REMOVE_FROM_RECENTS), title),
std::move(handler));
}
void RecentBooksActivity::buildScreen(UiScreen& screen) {
const auto& metrics = UITheme::getInstance().getMetrics();
// Content below the GUI.drawHeader band, above the button hints.
screen.setContentMargin(fui::Insets{static_cast<int16_t>(metrics.topPadding + metrics.headerHeight), 0,
static_cast<int16_t>(metrics.buttonHintsHeight), 0});
screen.spacer(static_cast<int16_t>(metrics.verticalSpacing));
if (recentBooks.empty()) {
screen.centeredText(tr(STR_NO_RECENT_BOOKS), screen.theme().bodyText);
return;
}
// rowItems is built in loadRecentBooks() (see rebuildRowItems()) and
// reused here on every repaint.
fui::ListProps props;
props.items = rowItems.data();
props.count = static_cast<uint16_t>(rowItems.size());
props.action = ACTION_ROW;
// Tap opens; long-press prompts removal (physical buttons stay in loop()).
props.inputMask = fui::InputTouch | fui::InputLongPress;
// Titles in the small font so more of a long title fits on the line; the row
// height stays on the theme cadence. Bold keeps the title/author hierarchy
// and doubles as the caller-owned marker: an all-default smallText fails
// textStyleUnset and Screen::list() would substitute bodyText back
// (FONT_SLOT_SMALL is 0). No maxLines=2 here: on subtitle rows the label
// band is one line tall and a wrapped title would collide with the author.
fui::TextStyle label = screen.theme().smallText;
label.bold = true;
props.labelText = label;
syncListViewport(screen, props, /*hasSubtitle=*/true);
screen.list(props);
}
void RecentBooksActivity::drawFooter() {
// No rows: blank the row-action hints, same as FileBrowserActivity.
const bool empty = recentBooks.empty();
const auto labels = mappedInput.mapLabels(tr(STR_HOME), empty ? "" : tr(STR_OPEN), empty ? "" : tr(STR_DIR_UP),
empty ? "" : tr(STR_DIR_DOWN));
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
}