Skip to content

Commit

Permalink
show download progress on updates page
Browse files Browse the repository at this point in the history
  • Loading branch information
mgn-norm committed Mar 13, 2022
1 parent 110a573 commit ea7710b
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/models/ChapterModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void ChapterModel::gotChapter(const QJsonDocument& reply)
return;
}

disconnect(_networkManager, &NetworkManager::recievedReply, this, nullptr);
disconnect(_networkManager, &NetworkManager::receivedReply, this, nullptr);

auto insert_sorted = [&](auto& vec, const ChapterInfo& item) {
vec.insert(std::upper_bound(vec.begin(), vec.end(), item,
Expand Down Expand Up @@ -241,7 +241,7 @@ void ChapterModel::requestChapter(quint32 chapter)

connect(
_networkManager,
&NetworkManager::recievedReply,
&NetworkManager::receivedReply,
this,
&ChapterModel::gotChapter);
}
2 changes: 1 addition & 1 deletion src/models/ChaptersModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void ChaptersModel::componentComplete()
{
connect(
_networkManager,
&NetworkManager::recieveChapters,
&NetworkManager::receiveChapters,
this,
&ChaptersModel::gotChapters);

Expand Down
Empty file added src/models/Downloads.cpp
Empty file.
Empty file added src/models/Downloads.h
Empty file.
13 changes: 12 additions & 1 deletion src/models/DownloadsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,20 @@ void DownloadsModel::classBegin()
*
*****************************************************************************/
void DownloadsModel::componentComplete()
{
setupWebsocket();
}

/******************************************************************************
*
* setupWebsocket
*
*****************************************************************************/
void DownloadsModel::setupWebsocket()
{
connect(
_networkManager,
&NetworkManager::recievedReply,
&NetworkManager::receivedReply,
this,
&DownloadsModel::gotDetails);

Expand Down Expand Up @@ -110,6 +120,7 @@ void DownloadsModel::onTextMessageReceived(const QString& message)

endResetModel();

emit downloadsUpdated(_queue);
}

/******************************************************************************
Expand Down
21 changes: 12 additions & 9 deletions src/models/DownloadsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
#include "../networkmanager.h"
#include "MangaDetailsModel.h"

struct QueueInfo {
quint32 chapterIndex;
quint32 mangaId;
QString state;
quint32 progress;
quint32 tries;
ChapterInfo chapterInfo;
};

class DownloadsModel : public QAbstractListModel, public QQmlParserStatus
{
Q_OBJECT
Expand All @@ -20,15 +29,6 @@ class DownloadsModel : public QAbstractListModel, public QQmlParserStatus
NetworkManager* _networkManager = nullptr;

QWebSocket _webSocket;

struct QueueInfo {
quint32 chapterIndex;
quint32 mangaId;
QString state;
quint32 progress;
quint32 tries;
ChapterInfo chapterInfo;
};
std::vector<QueueInfo> _queue;

typedef quint32 MangaId;
Expand Down Expand Up @@ -87,13 +87,16 @@ class DownloadsModel : public QAbstractListModel, public QQmlParserStatus
networkManagerChanged();
}

void setupWebsocket();

Q_INVOKABLE void clear();
Q_INVOKABLE void pause();
Q_INVOKABLE void start();
Q_INVOKABLE void cancel(qint32 index);
signals:
void statusChanged();
void networkManagerChanged();
void downloadsUpdated(const std::vector<QueueInfo>& info);

public slots:
void onConnected();
Expand Down
10 changes: 5 additions & 5 deletions src/models/ExtensionModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ void ExtensionModel::componentComplete()

connect(
_networkManager,
&NetworkManager::recievedReply,
&NetworkManager::receivedReply,
this,
&ExtensionModel::recievedReply);
&ExtensionModel::receivedReply);
}

/******************************************************************************
*
* Method: recieveReply()
* Method: receiveReply()
*
*****************************************************************************/
void ExtensionModel::recievedReply(const QJsonDocument& reply)
void ExtensionModel::receivedReply(const QJsonDocument& reply)
{
disconnect(_networkManager, &NetworkManager::recievedReply, this, nullptr);
disconnect(_networkManager, &NetworkManager::receivedReply, this, nullptr);

beginResetModel();
_extensions.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/models/ExtensionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ExtensionModel : public QAbstractListModel, public QQmlParserStatus
networkManagerChanged();
}

void recievedReply(const QJsonDocument& reply);
void receivedReply(const QJsonDocument& reply);

Q_INVOKABLE void update(const QString& pkgName, qint32 index);
Q_INVOKABLE void install(const QString& pkgName, qint32 index);
Expand Down
2 changes: 1 addition & 1 deletion src/models/LibraryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void LibraryModel::classBegin()

/******************************************************************************
*
* Method: recieveReply()
* Method: receiveReply()
*
*****************************************************************************/
gotCategoryId = [&](const QJsonDocument& reply)
Expand Down
4 changes: 2 additions & 2 deletions src/models/MangaDetailsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void MangaDetailsModel::classBegin()
*****************************************************************************/
void MangaDetailsModel::gotDetails(const QJsonDocument& reply)
{
disconnect(_networkManager, &NetworkManager::recievedReply, this, nullptr);
disconnect(_networkManager, &NetworkManager::receivedReply, this, nullptr);

beginResetModel();
_entries.clear();
Expand All @@ -79,7 +79,7 @@ void MangaDetailsModel::componentComplete()
{
connect(
_networkManager,
&NetworkManager::recievedReply,
&NetworkManager::receivedReply,
this,
&MangaDetailsModel::gotDetails);

Expand Down
14 changes: 7 additions & 7 deletions src/models/SourcesLibraryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ void SourcesLibraryModel::componentComplete()

/******************************************************************************
*
* Method: recieveReply()
* Method: receiveReply()
*
*****************************************************************************/
void SourcesLibraryModel::recievedReply(const QJsonDocument& reply)
void SourcesLibraryModel::receivedReply(const QJsonDocument& reply)
{
disconnect(_networkManager, &NetworkManager::recievedReply, this, nullptr);
disconnect(_networkManager, &NetworkManager::receivedReply, this, nullptr);

const auto& mangaList = reply["mangaList"].toArray();

Expand Down Expand Up @@ -171,9 +171,9 @@ void SourcesLibraryModel::search(const QString& searchTerm)

connect(
_networkManager,
&NetworkManager::recievedReply,
&NetworkManager::receivedReply,
this,
&SourcesLibraryModel::recievedReply);
&SourcesLibraryModel::receivedReply);
}

/******************************************************************************
Expand All @@ -187,7 +187,7 @@ void SourcesLibraryModel::next()

connect(
_networkManager,
&NetworkManager::recievedReply,
&NetworkManager::receivedReply,
this,
&SourcesLibraryModel::recievedReply);
&SourcesLibraryModel::receivedReply);
}
2 changes: 1 addition & 1 deletion src/models/SourcesLibraryModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SourcesLibraryModel : public QAbstractListModel, public QQmlParserStatus
networkManagerChanged();
}

void recievedReply(const QJsonDocument& reply);
void receivedReply(const QJsonDocument& reply);

Q_INVOKABLE void search(const QString& searchTerm);
Q_INVOKABLE void next();
Expand Down
10 changes: 5 additions & 5 deletions src/models/SourcesModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ void SourcesModel::componentComplete()

connect(
_networkManager,
&NetworkManager::recievedReply,
&NetworkManager::receivedReply,
this,
&SourcesModel::recievedReply);
&SourcesModel::receivedReply);
}

/******************************************************************************
*
* Method: recieveReply()
* Method: receiveReply()
*
*****************************************************************************/
void SourcesModel::recievedReply(const QJsonDocument& reply)
void SourcesModel::receivedReply(const QJsonDocument& reply)
{
disconnect(_networkManager, &NetworkManager::recievedReply, this, nullptr);
disconnect(_networkManager, &NetworkManager::receivedReply, this, nullptr);

beginResetModel();
_sources.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/models/SourcesModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SourcesModel : public QAbstractListModel, public QQmlParserStatus
networkManagerChanged();
}

void recievedReply(const QJsonDocument& reply);
void receivedReply(const QJsonDocument& reply);

signals:
void networkManagerChanged();
Expand Down
56 changes: 47 additions & 9 deletions src/models/UpdatesModel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "UpdatesModel.h"
#include "DownloadsModel.h"

#include <QJsonArray>
#include <QJsonDocument>
Expand Down Expand Up @@ -83,6 +84,12 @@ void UpdatesModel::componentComplete()
{
connect(&_webSocket, &QWebSocket::connected, this, &UpdatesModel::onConnected);
connect(&_webSocket, &QWebSocket::disconnected, this, &UpdatesModel::closed);
connect(
_networkManager,
&NetworkManager::receiveUpdates,
this,
&UpdatesModel::receivedReply);

auto resolved = _networkManager->resolvedPath().arg("/api/v1/update");
bool ssl = resolved.startsWith("https", Qt::CaseInsensitive);
resolved = resolved.mid(resolved.indexOf('/', resolved.indexOf(':'))+2);
Expand All @@ -95,17 +102,46 @@ void UpdatesModel::componentComplete()
next();
}

/******************************************************************************
*
* Method: onDownloadsUpdated()
*
*****************************************************************************/
void UpdatesModel::onDownloadsUpdated(const std::vector<QueueInfo>& queueInfo)
{
for (auto& info : queueInfo) {
int row = 0;
for (auto& source : _sources) {
if (info.mangaId == source.id &&
info.chapterInfo.chapterNumber == source.chapterInfo.chapterNumber)
{
source.queueInfo = std::make_shared<QueueInfo>(info);
source.chapterInfo.downloaded = info.progress >= 100;
emit dataChanged(createIndex(row, 0), createIndex(row, 0), { RoleDownloadProgress, RoleDownloaded });
break;
}
row++;
}
}
}

/******************************************************************************
*
* Method: receiveReply()
*
*****************************************************************************/
void UpdatesModel::receivedReply(const QJsonDocument& reply)
{
if (!downloads) {
downloads = std::make_shared<DownloadsModel>();
downloads->setNetworkManager(_networkManager);
downloads->setupWebsocket();
connect(downloads.get(), &DownloadsModel::downloadsUpdated, this, &UpdatesModel::onDownloadsUpdated);
}

if (reply.isEmpty()) {
return;
}
disconnect(_networkManager, &NetworkManager::recievedReply, this, nullptr);

_hasNext = reply["hasNextPage"].toBool();

Expand Down Expand Up @@ -233,7 +269,14 @@ QVariant UpdatesModel::data(const QModelIndex &index, int role) const {
{
return entry.chapterInfo.fetchedAt;
}
//case Role

case RoleDownloadProgress:
{
if (!entry.queueInfo) {
return -1;
}
return entry.queueInfo->progress;
}
default:
return {};
}
Expand Down Expand Up @@ -264,6 +307,7 @@ QHash<int, QByteArray> UpdatesModel::roleNames() const {
{RoleLastPageRead, "lastPageRead"},
{RoleChapterCount, "chapterCount"},
{RoleFetchedAt, "fetchedAt"},
{RoleDownloadProgress,"downloadProgress"},
};

return roles;
Expand All @@ -280,13 +324,7 @@ void UpdatesModel::next()
return;
}

_networkManager->get(QStringLiteral("update/recentChapters/%1").arg(_pageNumber++));

connect(
_networkManager,
&NetworkManager::recievedReply,
this,
&UpdatesModel::receivedReply);
_networkManager->getUpdates(QStringLiteral("update/recentChapters/%1").arg(_pageNumber++));
}

/******************************************************************************
Expand Down
7 changes: 7 additions & 0 deletions src/models/UpdatesModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include <optional>

class DownloadsModel;
class QueueInfo;

class UpdatesModel : public QAbstractListModel, public QQmlParserStatus
{
Q_OBJECT
Expand All @@ -21,6 +24,7 @@ class UpdatesModel : public QAbstractListModel, public QQmlParserStatus

NetworkManager* _networkManager = nullptr;
QWebSocket _webSocket;
std::shared_ptr<DownloadsModel> downloads;

struct SourceInfo {
qint32 id;
Expand All @@ -33,6 +37,7 @@ class UpdatesModel : public QAbstractListModel, public QQmlParserStatus
bool freshData;

ChapterInfo chapterInfo;
std::shared_ptr<QueueInfo> queueInfo;
};
std::vector<SourceInfo> _sources;

Expand Down Expand Up @@ -70,6 +75,7 @@ class UpdatesModel : public QAbstractListModel, public QQmlParserStatus
RoleLastPageRead,
RoleDownloaded,
RoleFetchedAt,
RoleDownloadProgress,
};

UpdatesModel(QObject* parent = nullptr);
Expand Down Expand Up @@ -107,4 +113,5 @@ public slots:
void onConnected();
void closed();
void onTextMessageReceived(const QString& message);
void onDownloadsUpdated(const std::vector<QueueInfo>& info);
};
Loading

0 comments on commit ea7710b

Please sign in to comment.