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;
progress = -progress;
auto completedLength = downloadInfo.completedLength;
auto downloadSpeed = downloadInfo.downloadSpeed;
auto downloadSpeed = downloadInfo.getDownloadSpeed();
if (downloadInfo.status == DownloadState::PAUSED) {
createResumeSymbol(painter, dcl.pauseResumeButtonRect);

View File

@ -37,15 +37,29 @@ DownloadState::Status getDownloadStatus(QString status)
} // unnamed namespace
void DownloadState::update(const DownloadInfo& downloadInfos)
void DownloadState::update(const DownloadInfo& info)
{
double percent = downloadInfos["completedLength"].toDouble() / downloadInfos["totalLength"].toDouble();
percent *= 100;
percent = QString::number(percent, 'g', 3).toDouble();
auto completedLength = convertToUnits(downloadInfos["completedLength"].toDouble());
auto downloadSpeed = convertToUnits(downloadInfos["downloadSpeed"].toDouble()) + "/s";
const auto status = getDownloadStatus(downloadInfos["status"].toString());
*this = {percent, completedLength, downloadSpeed, status};
const auto completedBytes = info["completedLength"].toDouble();
const double percentage = completedBytes / info["totalLength"].toDouble();
progress = QString::number(100 * percentage, 'g', 3).toDouble();
completedLength = convertToUnits(completedBytes);
downloadSpeed = convertToUnits(info["downloadSpeed"].toDouble()) + "/s";
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 <QVariant>
#include <chrono>
#include <memory>
#include <kiwix/downloader.h>
@ -18,7 +19,7 @@ typedef QMap<QString, QVariant> DownloadInfo;
class DownloadState
{
public:
public: // types
enum Status {
UNKNOWN,
WAITING,
@ -27,13 +28,23 @@ public:
PAUSED
};
public: // data
double progress = 0;
QString completedLength;
QString downloadSpeed;
Status status = UNKNOWN;
public:
public: // functions
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