Tracking of update time in DownloadState

If download info becomes stale (due to slow responses from aria)
the download speed is shown as ---.

Known issues:
  - A stale download is refreshed only due to a GUI event (such
    as scrolling or mouse hover).
This commit is contained in:
Veloman Yunkan 2024-06-04 12:34:41 +04:00 committed by Kelson
parent e86324d242
commit 018d913955
3 changed files with 37 additions and 12 deletions

View File

@ -129,7 +129,7 @@ void showDownloadProgress(QPainter *painter, QRect box, const DownloadState& dow
double progress = (double) (downloadInfo.progress) / 100; double progress = (double) (downloadInfo.progress) / 100;
progress = -progress; progress = -progress;
auto completedLength = downloadInfo.completedLength; auto completedLength = downloadInfo.completedLength;
auto downloadSpeed = downloadInfo.downloadSpeed; auto downloadSpeed = downloadInfo.getDownloadSpeed();
if (downloadInfo.status == DownloadState::PAUSED) { if (downloadInfo.status == DownloadState::PAUSED) {
createResumeSymbol(painter, dcl.pauseResumeButtonRect); createResumeSymbol(painter, dcl.pauseResumeButtonRect);

View File

@ -37,15 +37,29 @@ DownloadState::Status getDownloadStatus(QString status)
} // unnamed namespace } // unnamed namespace
void DownloadState::update(const DownloadInfo& downloadInfos) void DownloadState::update(const DownloadInfo& info)
{ {
double percent = downloadInfos["completedLength"].toDouble() / downloadInfos["totalLength"].toDouble(); const auto completedBytes = info["completedLength"].toDouble();
percent *= 100; const double percentage = completedBytes / info["totalLength"].toDouble();
percent = QString::number(percent, 'g', 3).toDouble();
auto completedLength = convertToUnits(downloadInfos["completedLength"].toDouble()); progress = QString::number(100 * percentage, 'g', 3).toDouble();
auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toDouble()) + "/s"; completedLength = convertToUnits(completedBytes);
const auto status = getDownloadStatus(downloadInfos["status"].toString()); downloadSpeed = convertToUnits(info["downloadSpeed"].toDouble()) + "/s";
*this = {percent, completedLength, downloadSpeed, status}; status = getDownloadStatus(info["status"].toString());
lastUpdated = std::chrono::steady_clock::now();
}
double DownloadState::timeSinceLastUpdate() const
{
typedef std::chrono::duration<double> Seconds;
const auto dt = std::chrono::steady_clock::now() - lastUpdated;
return std::chrono::duration_cast<Seconds>(dt).count();
}
QString DownloadState::getDownloadSpeed() const
{
return timeSinceLastUpdate() > 2.0 ? "---" : downloadSpeed;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -8,6 +8,7 @@
#include <QString> #include <QString>
#include <QVariant> #include <QVariant>
#include <chrono>
#include <memory> #include <memory>
#include <kiwix/downloader.h> #include <kiwix/downloader.h>
@ -18,7 +19,7 @@ typedef QMap<QString, QVariant> DownloadInfo;
class DownloadState class DownloadState
{ {
public: public: // types
enum Status { enum Status {
UNKNOWN, UNKNOWN,
WAITING, WAITING,
@ -27,13 +28,23 @@ public:
PAUSED PAUSED
}; };
public: // data
double progress = 0; double progress = 0;
QString completedLength; QString completedLength;
QString downloadSpeed;
Status status = UNKNOWN; Status status = UNKNOWN;
public: public: // functions
void update(const DownloadInfo& info); void update(const DownloadInfo& info);
QString getDownloadSpeed() const;
// time in seconds since last update
double timeSinceLastUpdate() const;
private: // data
QString downloadSpeed;
std::chrono::steady_clock::time_point lastUpdated;
}; };
class DownloadManager : public QObject class DownloadManager : public QObject