refactor: Make ChatterinoBadges less of a singleton (#5103)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "ChatterinoBadges.hpp"
|
||||
#include "providers/chatterino/ChatterinoBadges.hpp"
|
||||
|
||||
#include "common/network/NetworkRequest.hpp"
|
||||
#include "common/network/NetworkResult.hpp"
|
||||
@@ -7,17 +7,13 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QThread>
|
||||
#include <QUrl>
|
||||
|
||||
namespace chatterino {
|
||||
void ChatterinoBadges::initialize(Settings &settings, const Paths &paths)
|
||||
{
|
||||
this->loadChatterinoBadges();
|
||||
}
|
||||
|
||||
ChatterinoBadges::ChatterinoBadges()
|
||||
{
|
||||
this->loadChatterinoBadges();
|
||||
}
|
||||
|
||||
std::optional<EmotePtr> ChatterinoBadges::getBadge(const UserId &id)
|
||||
@@ -44,15 +40,19 @@ void ChatterinoBadges::loadChatterinoBadges()
|
||||
std::unique_lock lock(this->mutex_);
|
||||
|
||||
int index = 0;
|
||||
for (const auto &jsonBadge_ : jsonRoot.value("badges").toArray())
|
||||
for (const auto &jsonBadgeValue :
|
||||
jsonRoot.value("badges").toArray())
|
||||
{
|
||||
auto jsonBadge = jsonBadge_.toObject();
|
||||
auto jsonBadge = jsonBadgeValue.toObject();
|
||||
auto emote = Emote{
|
||||
EmoteName{},
|
||||
ImageSet{Url{jsonBadge.value("image1").toString()},
|
||||
Url{jsonBadge.value("image2").toString()},
|
||||
Url{jsonBadge.value("image3").toString()}},
|
||||
Tooltip{jsonBadge.value("tooltip").toString()}, Url{}};
|
||||
.name = EmoteName{},
|
||||
.images =
|
||||
ImageSet{Url{jsonBadge.value("image1").toString()},
|
||||
Url{jsonBadge.value("image2").toString()},
|
||||
Url{jsonBadge.value("image3").toString()}},
|
||||
.tooltip = Tooltip{jsonBadge.value("tooltip").toString()},
|
||||
.homePage = Url{},
|
||||
};
|
||||
|
||||
emotes.push_back(
|
||||
std::make_shared<const Emote>(std::move(emote)));
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/Aliases.hpp"
|
||||
#include "common/Singleton.hpp"
|
||||
#include "util/QStringHash.hpp"
|
||||
|
||||
#include <memory>
|
||||
@@ -15,20 +14,48 @@ namespace chatterino {
|
||||
struct Emote;
|
||||
using EmotePtr = std::shared_ptr<const Emote>;
|
||||
|
||||
class ChatterinoBadges : public Singleton
|
||||
class IChatterinoBadges
|
||||
{
|
||||
public:
|
||||
void initialize(Settings &settings, const Paths &paths) override;
|
||||
IChatterinoBadges() = default;
|
||||
virtual ~IChatterinoBadges() = default;
|
||||
|
||||
IChatterinoBadges(const IChatterinoBadges &) = delete;
|
||||
IChatterinoBadges(IChatterinoBadges &&) = delete;
|
||||
IChatterinoBadges &operator=(const IChatterinoBadges &) = delete;
|
||||
IChatterinoBadges &operator=(IChatterinoBadges &&) = delete;
|
||||
|
||||
virtual std::optional<EmotePtr> getBadge(const UserId &id) = 0;
|
||||
};
|
||||
|
||||
class ChatterinoBadges : public IChatterinoBadges
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Makes a network request to load Chatterino user badges
|
||||
*/
|
||||
ChatterinoBadges();
|
||||
|
||||
std::optional<EmotePtr> getBadge(const UserId &id);
|
||||
/**
|
||||
* Returns the Chatterino badge for the given user
|
||||
*/
|
||||
std::optional<EmotePtr> getBadge(const UserId &id) override;
|
||||
|
||||
private:
|
||||
void loadChatterinoBadges();
|
||||
|
||||
std::shared_mutex mutex_;
|
||||
|
||||
/**
|
||||
* Maps Twitch user IDs to their badge index
|
||||
* Guarded by mutex_
|
||||
*/
|
||||
std::unordered_map<QString, int> badgeMap;
|
||||
|
||||
/**
|
||||
* Keeps a list of badges.
|
||||
* Indexes in here are referred to by badgeMap
|
||||
*/
|
||||
std::vector<EmotePtr> emotes;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user