Commit e5e8ac5f authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧 Committed by Aleix Pol Gonzalez

Have Ratings be gadgets rather than QObjects

Should make the instances much lighter, which is important as there's
tons of them.

BUG: 431364
parent 7cc0fe22
......@@ -79,8 +79,7 @@ double dampenedRating(int ratings[6], double power = 0.1)
}
Rating::Rating(const QString &packageName, quint64 ratingCount, int data[6])
: QObject()
, m_packageName(packageName)
: m_packageName(packageName)
, m_ratingCount(ratingCount)
// TODO consider storing data[] and present in UI
, m_rating(((data[1] + (data[2] * 2) +
......@@ -100,8 +99,7 @@ Rating::Rating(const QString &packageName, quint64 ratingCount, int data[6])
}
Rating::Rating(const QString &packageName, quint64 ratingCount, int rating)
: QObject()
, m_packageName(packageName)
: m_packageName(packageName)
, m_ratingCount(ratingCount)
, m_rating(rating)
, m_ratingPoints(rating)
......
......@@ -12,17 +12,18 @@
#include "discovercommon_export.h"
class DISCOVERCOMMON_EXPORT Rating : public QObject
class DISCOVERCOMMON_EXPORT Rating
{
Q_OBJECT
Q_GADGET
Q_PROPERTY(double sortableRating READ sortableRating CONSTANT)
Q_PROPERTY(float rating READ rating CONSTANT)
Q_PROPERTY(int ratingPoints READ ratingPoints CONSTANT)
Q_PROPERTY(quint64 ratingCount READ ratingCount CONSTANT)
public:
Rating() {}
explicit Rating(const QString &packageName, quint64 ratingCount, int rating);
explicit Rating(const QString &packageName, quint64 ratingCount, int data[6]);
~Rating() override;
~Rating();
QString packageName() const;
quint64 ratingCount() const;
......@@ -34,10 +35,12 @@ public:
private:
const QString m_packageName;
const quint64 m_ratingCount;
const float m_rating;
int m_ratingPoints;
double m_sortableRating;
const quint64 m_ratingCount = 0;
const float m_rating = 0;
int m_ratingPoints = 0;
double m_sortableRating = 0;
};
Q_DECLARE_METATYPE(Rating)
#endif
......@@ -64,6 +64,11 @@ OdrsReviewsBackend::OdrsReviewsBackend()
}
}
OdrsReviewsBackend::~OdrsReviewsBackend() noexcept
{
qDeleteAll(m_ratings);
}
void OdrsReviewsBackend::ratingsFetched(KJob *job)
{
m_isFetching = false;
......@@ -255,7 +260,6 @@ void OdrsReviewsBackend::parseRatings()
appJsonObject.value(QLatin1String("star5")).toInt() };
Rating *rating = new Rating(it.key(), ratingCount, ratingMap);
rating->setParent(this);
m_ratings.insert(it.key(), rating);
}
ratingsDocument.close();
......
......@@ -24,6 +24,7 @@ class DISCOVERCOMMON_EXPORT OdrsReviewsBackend : public AbstractReviewsBackend
Q_OBJECT
public:
explicit OdrsReviewsBackend();
~OdrsReviewsBackend() override;
QString userName() const override;
void login() override {}
......
......@@ -17,6 +17,11 @@ DummyReviewsBackend::DummyReviewsBackend(DummyBackend* parent)
: AbstractReviewsBackend(parent)
{}
DummyReviewsBackend::~DummyReviewsBackend() noexcept
{
qDeleteAll(m_ratings);
}
void DummyReviewsBackend::fetchReviews(AbstractResource* app, int page)
{
if (page>=5)
......@@ -46,7 +51,6 @@ void DummyReviewsBackend::initialize()
int ratings[] = {0,0,0,0,0, randomRating};
Rating* rating = new Rating(app->packageName(), ++i, ratings);
rating->setParent(this);
m_ratings.insert(app, rating);
Q_EMIT app->ratingFetched();
}
......
......@@ -17,6 +17,7 @@ class DummyReviewsBackend : public AbstractReviewsBackend
Q_OBJECT
public:
explicit DummyReviewsBackend(DummyBackend* parent = nullptr);
~DummyReviewsBackend() override;
QString userName() const override { return QStringLiteral("dummy"); }
void login() override {}
......
......@@ -243,13 +243,13 @@ Rating * KNSResource::ratingInstance()
const int noc = m_entry.numberOfComments();
const int rating = m_entry.rating();
Q_ASSERT(rating <= 100);
return new Rating(
m_rating.reset(new Rating(
packageName(),
noc,
rating / 10
);
));
}
return m_rating;
return m_rating.data();
}
QString KNSResource::author() const
......
......@@ -64,7 +64,7 @@ private:
const QStringList m_categories;
KNSCore::EntryInternal m_entry;
KNS3::Entry::Status m_lastStatus;
QPointer<Rating> m_rating;
QScopedPointer<Rating> m_rating;
};
#endif // KNSRESOURCE_H
......@@ -7,6 +7,7 @@
#include "AbstractResource.h"
#include "AbstractResourcesBackend.h"
#include <ReviewsBackend/AbstractReviewsBackend.h>
#include <ReviewsBackend/Rating.h>
#include <Category/CategoryModel.h>
#include <KLocalizedString>
#include <KFormat>
......@@ -110,6 +111,12 @@ Rating* AbstractResource::rating() const
return ratings ? ratings->ratingForApplication(const_cast<AbstractResource*>(this)) : nullptr;
}
QVariant AbstractResource::ratingVariant() const
{
auto instance = rating();
return instance ? QVariant::fromValue<Rating>(*instance) : QVariant();
}
QStringList AbstractResource::extends() const
{
return {};
......
......@@ -60,7 +60,7 @@ class DISCOVERCOMMON_EXPORT AbstractResource : public QObject
Q_PROPERTY(QString section READ section CONSTANT)
Q_PROPERTY(QStringList mimetypes READ mimetypes CONSTANT)
Q_PROPERTY(AbstractResourcesBackend* backend READ backend CONSTANT)
Q_PROPERTY(Rating* rating READ rating NOTIFY ratingFetched)
Q_PROPERTY(QVariant rating READ ratingVariant NOTIFY ratingFetched)
Q_PROPERTY(QString appstreamId READ appstreamId CONSTANT)
Q_PROPERTY(QString categoryDisplay READ categoryDisplay CONSTANT)
Q_PROPERTY(QUrl url READ url CONSTANT)
......@@ -181,6 +181,7 @@ class DISCOVERCOMMON_EXPORT AbstractResource : public QObject
* @returns the rating for the resource or null if not available
*/
Rating* rating() const;
QVariant ratingVariant() const;
/**
* @returns a string defining the categories the resource belongs to
......
......@@ -458,9 +458,9 @@ QVariant ResourcesProxyModel::roleToValue(AbstractResource* resource, int role)
const int idx = Rating::staticMetaObject.indexOfProperty(roleNames().value(role).constData());
Q_ASSERT(idx >= 0);
auto prop = Rating::staticMetaObject.property(idx);
if (rating)
return prop.read(rating);
else {
if (rating) {
return prop.readOnGadget(rating);
} else {
QVariant val(0);
val.convert(prop.type());
return val;
......
Markdown is supported
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