Commit cd2b9b05 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Properly show when we are fetching reviews

parent 24005fb8
Pipeline #207430 passed with stage
in 1 minute and 13 seconds
......@@ -550,8 +550,7 @@ DiscoverPage {
Layout.alignment: Qt.AlignHCenter
Layout.maximumWidth: Kirigami.Units.gridUnit * 15
Layout.bottomMargin: appInfo.internalSpacings * 2
// FIXME: how do we distinguish between "reviews are empty because they haven't loaded" and "reviews are empty because there are no reviews"?
visible: !rep.count > 0 /*&& reviewsModel.count > 0*/
visible: reviewsModel.fetching
text: i18n("Loading reviews for %1", appInfo.application.name)
}
......
......@@ -46,6 +46,7 @@ Q_SIGNALS:
void reviewsReady(AbstractResource *app, const QVector<ReviewPtr> &reviews, bool canFetchMore);
void loginStateChanged();
void error(const QString &message);
void fetchingChanged(bool fetching);
};
#endif // ABSTRACTREVIEWSBACKEND_H
......@@ -92,12 +92,14 @@ void ReviewsModel::setResource(AbstractResource *app)
if (m_backend) {
disconnect(m_backend, &AbstractReviewsBackend::reviewsReady, this, &ReviewsModel::addReviews);
disconnect(m_backend, &AbstractReviewsBackend::fetchingChanged, this, &ReviewsModel::fetchingChanged);
disconnect(m_app, &AbstractResource::versionsChanged, this, &ReviewsModel::restartFetching);
}
m_app = app;
m_backend = app ? app->backend()->reviewsBackend() : nullptr;
if (m_backend) {
connect(m_backend, &AbstractReviewsBackend::reviewsReady, this, &ReviewsModel::addReviews);
connect(m_backend, &AbstractReviewsBackend::fetchingChanged, this, &ReviewsModel::fetchingChanged);
connect(m_app, &AbstractResource::versionsChanged, this, &ReviewsModel::restartFetching);
QMetaObject::invokeMethod(this, &ReviewsModel::restartFetching, Qt::QueuedConnection);
......@@ -135,7 +137,7 @@ void ReviewsModel::addReviews(AbstractResource *app, const QVector<ReviewPtr> &r
return;
m_canFetchMore = canFetchMore;
// qCDebug(LIBDISCOVER_LOG) << "reviews arrived..." << m_lastPage << reviews.size();
qCDebug(LIBDISCOVER_LOG) << "reviews arrived..." << m_lastPage << reviews.size();
if (!reviews.isEmpty()) {
beginInsertRows(QModelIndex(), rowCount(), rowCount() + reviews.size() - 1);
......@@ -172,4 +174,9 @@ void ReviewsModel::flagReview(int row, const QString &reason, const QString &tex
m_backend->flagReview(r, reason, text);
}
bool ReviewsModel::isFetching() const
{
return m_backend && m_backend->isFetching();
}
#include "moc_ReviewsModel.cpp"
......@@ -22,6 +22,7 @@ class DISCOVERCOMMON_EXPORT ReviewsModel : public QAbstractListModel
Q_PROPERTY(AbstractReviewsBackend *backend READ backend NOTIFY resourceChanged)
Q_PROPERTY(AbstractResource *resource READ resource WRITE setResource NOTIFY resourceChanged)
Q_PROPERTY(int count READ rowCount NOTIFY rowsChanged)
Q_PROPERTY(bool fetching READ isFetching NOTIFY fetchingChanged)
public:
enum Roles {
ShouldShow = Qt::UserRole + 1,
......@@ -53,6 +54,7 @@ public:
void fetchMore(const QModelIndex &parent = QModelIndex()) override;
bool canFetchMore(const QModelIndex & /*parent*/) const override;
QHash<int, QByteArray> roleNames() const override;
bool isFetching() const;
public Q_SLOTS:
void deleteReview(int row);
......@@ -66,6 +68,7 @@ private Q_SLOTS:
Q_SIGNALS:
void rowsChanged();
void resourceChanged();
void fetchingChanged(bool fetching);
private:
AbstractResource *m_app = nullptr;
......
......@@ -38,7 +38,6 @@
OdrsReviewsBackend::OdrsReviewsBackend()
: AbstractReviewsBackend(nullptr)
, m_isFetching(false)
{
bool fetchRatings = false;
const QUrl ratingsUrl(QStringLiteral(APIURL "/ratings"));
......@@ -59,7 +58,7 @@ OdrsReviewsBackend::OdrsReviewsBackend()
}
if (fetchRatings) {
m_isFetching = true;
setFetching(true);
KIO::FileCopyJob *getJob = KIO::file_copy(ratingsUrl, fileUrl, -1, KIO::Overwrite | KIO::HideProgressInfo);
connect(getJob, &KIO::FileCopyJob::result, this, &OdrsReviewsBackend::ratingsFetched);
} else {
......@@ -72,9 +71,18 @@ OdrsReviewsBackend::~OdrsReviewsBackend() noexcept
qDeleteAll(m_ratings);
}
void OdrsReviewsBackend::setFetching(bool fetching)
{
if (fetching == m_isFetching) {
return;
}
m_isFetching = fetching;
Q_EMIT fetchingChanged(fetching);
}
void OdrsReviewsBackend::ratingsFetched(KJob *job)
{
m_isFetching = false;
setFetching(false);
if (job->error()) {
qCWarning(LIBDISCOVER_LOG) << "Failed to fetch ratings " << job->errorString();
} else {
......@@ -114,7 +122,7 @@ void OdrsReviewsBackend::fetchReviews(AbstractResource *app, int page)
if (version.isEmpty()) {
return;
}
m_isFetching = true;
setFetching(true);
const QJsonDocument document(QJsonObject{
{QStringLiteral("app_id"), app->appstreamId()},
......@@ -145,7 +153,7 @@ void OdrsReviewsBackend::reviewsFetched()
if (networkError != QNetworkReply::NoError) {
qCWarning(LIBDISCOVER_LOG) << "error fetching reviews:" << reply->errorString() << data;
Q_EMIT error(i18n("Error while fetching reviews: %1", reply->errorString()));
m_isFetching = false;
setFetching(false);
return;
}
......@@ -290,7 +298,7 @@ void OdrsReviewsBackend::parseRatings()
void OdrsReviewsBackend::parseReviews(const QJsonDocument &document, AbstractResource *resource)
{
m_isFetching = false;
setFetching(false);
Q_ASSERT(resource);
if (!resource) {
return;
......
......@@ -68,12 +68,13 @@ Q_SIGNALS:
void ratingsReady();
private:
void setFetching(bool);
QNetworkAccessManager *nam();
void parseRatings();
void parseReviews(const QJsonDocument &document, AbstractResource *resource);
QHash<QString, Rating *> m_ratings;
bool m_isFetching;
bool m_isFetching = false;
CachedNetworkAccessManager *m_delayedNam = nullptr;
};
......
......@@ -60,7 +60,20 @@ void KNSReviews::fetchReviews(AbstractResource *app, int page)
job->setProperty("app", QVariant::fromValue<AbstractResource *>(app));
connect(job, &Attica::BaseJob::finished, this, &KNSReviews::commentsReceived);
job->start();
m_fetching++;
acquireFetching(true);
}
void KNSReviews::acquireFetching(bool f)
{
if (f)
m_fetching++;
else
m_fetching--;
if ((!f && m_fetching == 0) || (f && m_fetching == 1)) {
Q_EMIT fetchingChanged(m_fetching != 0);
}
Q_ASSERT(m_fetching >= 0);
}
static QVector<ReviewPtr> createReviewList(AbstractResource *app, const Attica::Comment::List comments, int depth = 0)
......@@ -92,7 +105,7 @@ static QVector<ReviewPtr> createReviewList(AbstractResource *app, const Attica::
void KNSReviews::commentsReceived(Attica::BaseJob *j)
{
m_fetching--;
acquireFetching(false);
Attica::ListJob<Attica::Comment> *job = static_cast<Attica::ListJob<Attica::Comment> *>(j);
AbstractResource *app = job->property("app").value<AbstractResource *>();
......
......@@ -46,6 +46,8 @@ private Q_SLOTS:
private:
Attica::Provider provider() const;
void acquireFetching(bool f);
KNSBackend *const m_backend;
int m_fetching = 0;
#if KNEWSTUFFCORE_VERSION < QT_VERSION_CHECK(5, 92, 0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment