refactor: consolidate Epub blank strings, simplify BookInfo buildLayout
Replace 13 per-accessor static std::string blank locals with a single file-scope kBlank (~384 bytes DRAM saved). Add Epub::getMetadata() returning the full BookMetadata struct. Refactor buildLayout from 14 individual parameters to a single BookMetadata const ref + fileSize. Made-with: Cursor
This commit is contained in:
@@ -59,8 +59,7 @@ void BookInfoActivity::onEnter() {
|
||||
}
|
||||
}
|
||||
|
||||
std::string title, author, series, seriesIndex, description, language;
|
||||
std::string publisher, date, subjects, rights, contributor, identifier, rating;
|
||||
BookMetadataCache::BookMetadata meta;
|
||||
|
||||
if (FsHelpers::hasEpubExtension(fileName)) {
|
||||
Epub epub(filePath, "/.crosspoint");
|
||||
@@ -73,19 +72,8 @@ void BookInfoActivity::onEnter() {
|
||||
GUI.fillPopupProgress(renderer, popupRect, 50);
|
||||
}
|
||||
|
||||
title = epub.getTitle();
|
||||
author = epub.getAuthor();
|
||||
series = epub.getSeries();
|
||||
seriesIndex = epub.getSeriesIndex();
|
||||
description = normalizeWhitespace(epub.getDescription());
|
||||
language = epub.getLanguage();
|
||||
publisher = epub.getPublisher();
|
||||
date = epub.getDate();
|
||||
subjects = epub.getSubjects();
|
||||
rights = epub.getRights();
|
||||
contributor = epub.getContributor();
|
||||
identifier = epub.getIdentifier();
|
||||
rating = epub.getRating();
|
||||
meta = epub.getMetadata();
|
||||
meta.description = normalizeWhitespace(meta.description);
|
||||
|
||||
const int coverH = renderer.getScreenHeight() * 2 / 5;
|
||||
if (epub.generateThumbBmp(coverH)) {
|
||||
@@ -93,8 +81,8 @@ void BookInfoActivity::onEnter() {
|
||||
} else {
|
||||
const int thumbW = static_cast<int>(coverH * 0.6);
|
||||
const std::string placeholderPath = epub.getCachePath() + "/placeholder_" + std::to_string(coverH) + ".bmp";
|
||||
if (PlaceholderCoverGenerator::generate(placeholderPath, title.empty() ? fileName : title, author, thumbW,
|
||||
coverH)) {
|
||||
if (PlaceholderCoverGenerator::generate(placeholderPath, meta.title.empty() ? fileName : meta.title, meta.author,
|
||||
thumbW, coverH)) {
|
||||
coverBmpPath = placeholderPath;
|
||||
}
|
||||
}
|
||||
@@ -113,8 +101,8 @@ void BookInfoActivity::onEnter() {
|
||||
if (needsBuild) {
|
||||
GUI.fillPopupProgress(renderer, popupRect, 50);
|
||||
}
|
||||
title = xtc.getTitle();
|
||||
author = xtc.getAuthor();
|
||||
meta.title = xtc.getTitle();
|
||||
meta.author = xtc.getAuthor();
|
||||
|
||||
const int coverH = renderer.getScreenHeight() * 2 / 5;
|
||||
if (xtc.generateThumbBmp(coverH)) {
|
||||
@@ -122,8 +110,8 @@ void BookInfoActivity::onEnter() {
|
||||
} else {
|
||||
const int thumbW = static_cast<int>(coverH * 0.6);
|
||||
const std::string placeholderPath = xtc.getCachePath() + "/placeholder_" + std::to_string(coverH) + ".bmp";
|
||||
if (PlaceholderCoverGenerator::generate(placeholderPath, title.empty() ? fileName : title, author, thumbW,
|
||||
coverH)) {
|
||||
if (PlaceholderCoverGenerator::generate(placeholderPath, meta.title.empty() ? fileName : meta.title, meta.author,
|
||||
thumbW, coverH)) {
|
||||
coverBmpPath = placeholderPath;
|
||||
}
|
||||
}
|
||||
@@ -133,23 +121,17 @@ void BookInfoActivity::onEnter() {
|
||||
}
|
||||
}
|
||||
|
||||
if (title.empty()) {
|
||||
title = fileName;
|
||||
if (meta.title.empty()) {
|
||||
meta.title = fileName;
|
||||
}
|
||||
|
||||
buildLayout(title, author, series, seriesIndex, description, language, fileSize, publisher, date, subjects, rights,
|
||||
contributor, identifier, rating);
|
||||
buildLayout(meta, fileSize);
|
||||
requestUpdate();
|
||||
}
|
||||
|
||||
void BookInfoActivity::onExit() { Activity::onExit(); }
|
||||
|
||||
void BookInfoActivity::buildLayout(const std::string& title, const std::string& author, const std::string& series,
|
||||
const std::string& seriesIndex, const std::string& description,
|
||||
const std::string& language, size_t fileSize, const std::string& publisher,
|
||||
const std::string& date, const std::string& subjects, const std::string& rights,
|
||||
const std::string& contributor, const std::string& identifier,
|
||||
const std::string& rating) {
|
||||
void BookInfoActivity::buildLayout(const BookMetadataCache::BookMetadata& meta, size_t fileSize) {
|
||||
const int contentW = renderer.getScreenWidth() - MARGIN * 2;
|
||||
fields.reserve(13);
|
||||
|
||||
@@ -162,23 +144,23 @@ void BookInfoActivity::buildLayout(const std::string& title, const std::string&
|
||||
fields.push_back(std::move(field));
|
||||
};
|
||||
|
||||
addField(nullptr, title, true, EpdFontFamily::BOLD);
|
||||
addField(tr(STR_AUTHOR), author, false, EpdFontFamily::REGULAR);
|
||||
addField(nullptr, meta.title, true, EpdFontFamily::BOLD);
|
||||
addField(tr(STR_AUTHOR), meta.author, false, EpdFontFamily::REGULAR);
|
||||
|
||||
if (!series.empty()) {
|
||||
std::string seriesStr = series;
|
||||
if (!seriesIndex.empty()) {
|
||||
seriesStr += " #" + seriesIndex;
|
||||
if (!meta.series.empty()) {
|
||||
std::string seriesStr = meta.series;
|
||||
if (!meta.seriesIndex.empty()) {
|
||||
seriesStr += " #" + meta.seriesIndex;
|
||||
}
|
||||
addField(tr(STR_SERIES), seriesStr, false, EpdFontFamily::REGULAR);
|
||||
}
|
||||
|
||||
addField(tr(STR_PUBLISHER), publisher, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_DATE), date, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_SUBJECTS), subjects, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_PUBLISHER), meta.publisher, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_DATE), meta.date, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_SUBJECTS), meta.subjects, false, EpdFontFamily::REGULAR);
|
||||
|
||||
if (!rating.empty()) {
|
||||
int ratingVal = atoi(rating.c_str());
|
||||
if (!meta.rating.empty()) {
|
||||
int ratingVal = atoi(meta.rating.c_str());
|
||||
if (ratingVal > 0 && ratingVal <= 10) {
|
||||
char ratingBuf[8];
|
||||
snprintf(ratingBuf, sizeof(ratingBuf), "%d / 5", (ratingVal + 1) / 2);
|
||||
@@ -186,16 +168,16 @@ void BookInfoActivity::buildLayout(const std::string& title, const std::string&
|
||||
}
|
||||
}
|
||||
|
||||
addField(tr(STR_LANGUAGE), language, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_ISBN), identifier, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_CONTRIBUTOR), contributor, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_LANGUAGE), meta.language, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_ISBN), meta.identifier, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_CONTRIBUTOR), meta.contributor, false, EpdFontFamily::REGULAR);
|
||||
|
||||
if (fileSize > 0) {
|
||||
addField(tr(STR_FILE_SIZE), formatFileSize(fileSize), false, EpdFontFamily::REGULAR);
|
||||
}
|
||||
|
||||
addField(tr(STR_RIGHTS), rights, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_DESCRIPTION), description, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_RIGHTS), meta.rights, false, EpdFontFamily::REGULAR);
|
||||
addField(tr(STR_DESCRIPTION), meta.description, false, EpdFontFamily::REGULAR);
|
||||
|
||||
const int lineH10 = renderer.getLineHeight(UI_10_FONT_ID);
|
||||
const int lineH12 = renderer.getLineHeight(UI_12_FONT_ID);
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <Epub/BookMetadataCache.h>
|
||||
|
||||
#include "../Activity.h"
|
||||
|
||||
class BookInfoActivity final : public Activity {
|
||||
@@ -31,10 +33,6 @@ class BookInfoActivity final : public Activity {
|
||||
int coverDisplayHeight = 0;
|
||||
int coverDisplayWidth = 0;
|
||||
|
||||
void buildLayout(const std::string& title, const std::string& author, const std::string& series,
|
||||
const std::string& seriesIndex, const std::string& description, const std::string& language,
|
||||
size_t fileSize, const std::string& publisher, const std::string& date,
|
||||
const std::string& subjects, const std::string& rights, const std::string& contributor,
|
||||
const std::string& identifier, const std::string& rating);
|
||||
void buildLayout(const BookMetadataCache::BookMetadata& meta, size_t fileSize);
|
||||
static std::string formatFileSize(size_t bytes);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user