fix: several QoL updates for SD font's UI (#1965)
## Summary * **What is the goal of this PR?** Improve the UI based on feedback from someone on discord > Downloading ALL fonts feature. > 1.1 Disable sleep when downloading, in my case went directly to sleep just right after downloading. > 1.2 It would be great to have and overall progress indicator as we only have the indication of each font family > 1.3 Any cancel or pause function might come in handy in case battery is running out and then resume or retry with pending fonts * **What changes are included?** - Now the UI can show overall progress across every file being downloaded in the batch, not just progress inside the current family. - Extended `HttpDownloader::downloadToFile()` to accept a cancel flag and abort the download. - Rendered a cancel button in the font download UI while a download is in progress. - `preventAutoSleep()` in `FontDownloadActivity.h` now returns true for `state_ == COMPLETE` and `state_ == ERROR` in addition to `LOADING_MANIFEST` and `DOWNLOADING` ## Additional Context Not very satisfied with how `HttpDownloader.cpp` is right now, might try to refactor it after v1.3.0 --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**PARTIALLY**_
This commit is contained in:
@@ -16,13 +16,17 @@
|
||||
namespace {
|
||||
class FileWriteStream final : public Stream {
|
||||
public:
|
||||
FileWriteStream(FsFile& file, size_t total, HttpDownloader::ProgressCallback progress)
|
||||
: file_(file), total_(total), progress_(std::move(progress)) {}
|
||||
FileWriteStream(FsFile& file, size_t total, HttpDownloader::ProgressCallback progress, bool* cancelFlag)
|
||||
: file_(file), total_(total), progress_(std::move(progress)), cancelFlag_(cancelFlag) {}
|
||||
|
||||
size_t write(uint8_t byte) override { return write(&byte, 1); }
|
||||
|
||||
size_t write(const uint8_t* buffer, size_t size) override {
|
||||
// Write-through stream for HTTPClient::writeToStream with progress tracking.
|
||||
if (cancelFlag_ && *cancelFlag_) {
|
||||
writeOk_ = false;
|
||||
return 0;
|
||||
}
|
||||
const size_t written = file_.write(buffer, size);
|
||||
if (written != size) {
|
||||
writeOk_ = false;
|
||||
@@ -48,6 +52,7 @@ class FileWriteStream final : public Stream {
|
||||
size_t downloaded_ = 0;
|
||||
bool writeOk_ = true;
|
||||
HttpDownloader::ProgressCallback progress_;
|
||||
bool* cancelFlag_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
@@ -101,8 +106,8 @@ bool HttpDownloader::fetchUrl(const std::string& url, std::string& outContent, c
|
||||
}
|
||||
|
||||
HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& url, const std::string& destPath,
|
||||
ProgressCallback progress, const std::string& username,
|
||||
const std::string& password) {
|
||||
ProgressCallback progress, bool* cancelFlag,
|
||||
const std::string& username, const std::string& password) {
|
||||
std::unique_ptr<NetworkClient> client;
|
||||
if (UrlUtils::isHttpsUrl(url)) {
|
||||
auto* secureClient = new NetworkClientSecure();
|
||||
@@ -155,12 +160,17 @@ HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string&
|
||||
}
|
||||
|
||||
// Let HTTPClient handle chunked decoding and stream body bytes into the file.
|
||||
FileWriteStream fileStream(file, contentLength, progress);
|
||||
FileWriteStream fileStream(file, contentLength, progress, cancelFlag);
|
||||
const int writeResult = http.writeToStream(&fileStream);
|
||||
|
||||
file.close();
|
||||
http.end();
|
||||
|
||||
if (cancelFlag && *cancelFlag) {
|
||||
Storage.remove(destPath.c_str());
|
||||
return ABORTED;
|
||||
}
|
||||
|
||||
if (writeResult < 0) {
|
||||
LOG_ERR("HTTP", "writeToStream error: %d", writeResult);
|
||||
Storage.remove(destPath.c_str());
|
||||
|
||||
@@ -32,6 +32,6 @@ class HttpDownloader {
|
||||
* Download a file to the SD card with optional credentials.
|
||||
*/
|
||||
static DownloadError downloadToFile(const std::string& url, const std::string& destPath,
|
||||
ProgressCallback progress = nullptr, const std::string& username = "",
|
||||
const std::string& password = "");
|
||||
ProgressCallback progress = nullptr, bool* cancelFlag = nullptr,
|
||||
const std::string& username = "", const std::string& password = "");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user