2020-09-20 01:21:28 +01:00
|
|
|
#include "worker_page.hpp"
|
2021-09-11 14:48:13 +01:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "constants.hpp"
|
2021-02-10 16:28:47 +00:00
|
|
|
#include "download.hpp"
|
|
|
|
#include "extract.hpp"
|
2021-09-22 20:01:45 +01:00
|
|
|
#include "main_frame.hpp"
|
2021-02-10 16:28:47 +00:00
|
|
|
#include "progress_event.hpp"
|
2021-09-11 14:48:13 +01:00
|
|
|
#include "utils.hpp"
|
2020-09-20 01:21:28 +01:00
|
|
|
|
2021-06-27 23:46:00 +01:00
|
|
|
namespace i18n = brls::i18n;
|
|
|
|
using namespace i18n::literals;
|
2021-03-11 17:42:10 +00:00
|
|
|
|
2021-09-11 14:48:13 +01:00
|
|
|
WorkerPage::WorkerPage(brls::StagedAppletFrame* frame, const std::string& text, worker_func_t worker_func) : frame(frame), workerFunc(worker_func), text(text)
|
2020-09-20 01:21:28 +01:00
|
|
|
{
|
|
|
|
this->progressDisp = new brls::ProgressDisplay();
|
|
|
|
this->progressDisp->setParent(this);
|
|
|
|
|
|
|
|
this->label = new brls::Label(brls::LabelStyle::DIALOG, text, true);
|
|
|
|
this->label->setHorizontalAlign(NVG_ALIGN_CENTER);
|
|
|
|
this->label->setParent(this);
|
|
|
|
|
2021-02-16 00:46:39 +00:00
|
|
|
this->button = new brls::Button(brls::ButtonStyle::REGULAR);
|
2020-09-20 01:21:28 +01:00
|
|
|
this->button->setParent(this);
|
2021-02-06 17:24:47 +00:00
|
|
|
|
2021-09-22 20:01:45 +01:00
|
|
|
this->registerAction("menus/common/cancel"_i18n, brls::Key::B, [this] {
|
|
|
|
ProgressEvent::instance().setInterupt(true);
|
|
|
|
return true;
|
|
|
|
});
|
2021-02-15 18:31:06 +00:00
|
|
|
this->registerAction("", brls::Key::A, [this] { return true; });
|
2021-09-22 20:04:04 +01:00
|
|
|
this->registerAction("", brls::Key::PLUS, [this] { return true; });
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 11:54:32 +01:00
|
|
|
std::string formatLabelText(double speed, double fileSizeCurrent, double fileSizeFinal)
|
2023-07-11 19:12:40 +01:00
|
|
|
{
|
|
|
|
double fileSizeCurrentMB = fileSizeCurrent / 0x100000;
|
|
|
|
double fileSizeFinalMB = fileSizeFinal / 0x100000;
|
|
|
|
double speedMB = speed / 0x100000;
|
|
|
|
|
|
|
|
double timeRemaining = (fileSizeFinal - fileSizeCurrent) / speed;
|
|
|
|
int hours = static_cast<int>(timeRemaining / 3600);
|
|
|
|
int minutes = static_cast<int>((timeRemaining - hours * 3600) / 60);
|
|
|
|
int seconds = static_cast<int>(timeRemaining - hours * 3600 - minutes * 60);
|
|
|
|
|
2023-07-12 11:54:32 +01:00
|
|
|
std::string labelText = fmt::format("menus/worker/download_progress"_i18n, fileSizeCurrentMB, fileSizeFinalMB, speedMB);
|
|
|
|
if (speedMB > 0) {
|
|
|
|
std::string eta;
|
|
|
|
if (hours > 0)
|
|
|
|
eta += fmt::format("{}h ", hours);
|
|
|
|
if (minutes > 0)
|
|
|
|
eta += fmt::format("{}m ", minutes);
|
2023-07-11 19:12:40 +01:00
|
|
|
|
2023-07-12 11:54:32 +01:00
|
|
|
eta += fmt::format("{}s", seconds);
|
|
|
|
labelText += "\n" + fmt::format("menus/worker/time_left"_i18n, eta);
|
|
|
|
}
|
2023-07-11 19:12:40 +01:00
|
|
|
|
|
|
|
return labelText;
|
|
|
|
}
|
|
|
|
|
2020-09-20 01:21:28 +01:00
|
|
|
void WorkerPage::draw(NVGcontext* vg, int x, int y, unsigned width, unsigned height, brls::Style* style, brls::FrameContext* ctx)
|
|
|
|
{
|
2021-09-11 14:48:13 +01:00
|
|
|
if (this->draw_page) {
|
|
|
|
if (!this->workStarted) {
|
2021-09-28 13:43:48 +01:00
|
|
|
this->workStarted = true;
|
2021-06-27 23:46:00 +01:00
|
|
|
appletSetMediaPlaybackState(true);
|
2021-09-23 02:29:33 +01:00
|
|
|
appletBeginBlockingHomeButton(0);
|
2021-06-27 23:46:00 +01:00
|
|
|
ProgressEvent::instance().reset();
|
|
|
|
workerThread = new std::thread(&WorkerPage::doWork, this);
|
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
else if (ProgressEvent::instance().finished()) {
|
2021-09-23 02:29:33 +01:00
|
|
|
appletEndBlockingHomeButton();
|
2021-06-27 23:46:00 +01:00
|
|
|
appletSetMediaPlaybackState(false);
|
|
|
|
if (ProgressEvent::instance().getStatusCode() > 399) {
|
|
|
|
this->draw_page = false;
|
|
|
|
brls::Application::crash(fmt::format("menus/errors/error_message"_i18n, util::getErrorMessage(ProgressEvent::instance().getStatusCode())));
|
2021-09-11 14:48:13 +01:00
|
|
|
}
|
2021-09-22 20:01:45 +01:00
|
|
|
if (ProgressEvent::instance().getInterupt()) {
|
|
|
|
brls::Application::pushView(new MainFrame());
|
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
else {
|
2021-06-27 23:46:00 +01:00
|
|
|
ProgressEvent::instance().setStatusCode(0);
|
|
|
|
frame->nextStage();
|
|
|
|
}
|
|
|
|
}
|
2021-09-11 14:48:13 +01:00
|
|
|
else {
|
2021-06-27 23:46:00 +01:00
|
|
|
this->progressDisp->setProgress(ProgressEvent::instance().getStep(), ProgressEvent::instance().getMax());
|
|
|
|
this->progressDisp->frame(ctx);
|
2021-09-11 14:48:13 +01:00
|
|
|
if (ProgressEvent::instance().getTotal()) {
|
2023-07-11 19:12:40 +01:00
|
|
|
this->label->setText(formatLabelText(ProgressEvent::instance().getSpeed(), ProgressEvent::instance().getNow(), ProgressEvent::instance().getTotal()));
|
2021-06-27 23:46:00 +01:00
|
|
|
}
|
|
|
|
this->label->frame(ctx);
|
2021-03-11 17:42:10 +00:00
|
|
|
}
|
2020-09-20 01:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerPage::layout(NVGcontext* vg, brls::Style* style, brls::FontStash* stash)
|
|
|
|
{
|
|
|
|
this->label->setWidth(roundf((float)this->width * style->CrashFrame.labelWidth));
|
|
|
|
|
|
|
|
this->label->setBoundaries(
|
|
|
|
this->x + this->width / 2 - this->label->getWidth() / 2,
|
2023-07-11 19:12:40 +01:00
|
|
|
this->y + (this->height - style->AppletFrame.footerHeight) / 2 - 30,
|
2020-09-20 01:21:28 +01:00
|
|
|
this->label->getWidth(),
|
|
|
|
this->label->getHeight());
|
|
|
|
|
|
|
|
this->progressDisp->setBoundaries(
|
|
|
|
this->x + this->width / 2 - style->CrashFrame.buttonWidth,
|
|
|
|
this->y + this->height / 2,
|
|
|
|
style->CrashFrame.buttonWidth * 2,
|
|
|
|
style->CrashFrame.buttonHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerPage::doWork()
|
|
|
|
{
|
|
|
|
if (this->workerFunc)
|
|
|
|
this->workerFunc();
|
|
|
|
}
|
|
|
|
|
|
|
|
brls::View* WorkerPage::getDefaultFocus()
|
|
|
|
{
|
|
|
|
return this->button;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPage::~WorkerPage()
|
|
|
|
{
|
2021-09-11 14:48:13 +01:00
|
|
|
if (this->workStarted && workerThread->joinable()) {
|
2020-09-20 01:21:28 +01:00
|
|
|
this->workerThread->join();
|
|
|
|
if (this->workerThread)
|
|
|
|
delete this->workerThread;
|
|
|
|
}
|
|
|
|
delete this->progressDisp;
|
|
|
|
delete this->label;
|
|
|
|
delete this->button;
|
|
|
|
}
|