Skip to content

Commit

Permalink
refactor: Move TwitchBadges to Application
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Jan 17, 2024
1 parent 7d5967c commit 4c551b3
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
- Dev: Move `clang-tidy` checker to its own CI job. (#4996)
- Dev: Refactored the Image Uploader feature. (#4971)
- Dev: Refactored the SplitOverlay code. (#5082)
- Dev: Refactored the TwitchBadges structure, making it less of a singleton. (#5096)
- Dev: Moved the Network files to their own folder. (#5089)
- Dev: Fixed deadlock and use-after-free in tests. (#4981)
- Dev: Moved all `.clang-format` files to the root directory. (#5037)
Expand Down
6 changes: 6 additions & 0 deletions mocks/include/mocks/EmptyApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ class EmptyApplication : public IApplication
return nullptr;
}

TwitchBadges *getTwitchBadges() override
{
assert(false && "getTwitchBadges was called without being initialized");
return nullptr;
}

Logging *getChatLogger() override
{
assert(!"getChatLogger was called without being initialized");
Expand Down
10 changes: 10 additions & 0 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "controllers/notifications/NotificationController.hpp"
#include "controllers/sound/ISoundController.hpp"
#include "providers/seventv/SeventvAPI.hpp"
#include "providers/twitch/TwitchBadges.hpp"
#include "singletons/ImageUploader.hpp"
#ifdef CHATTERINO_HAVE_PLUGINS
# include "controllers/plugins/PluginController.hpp"
Expand Down Expand Up @@ -133,6 +134,7 @@ Application::Application(Settings &_settings, const Paths &paths,
, sound(&this->emplace<ISoundController>(makeSoundController(_settings)))
, twitchLiveController(&this->emplace<TwitchLiveController>())
, twitchPubSub(new PubSub(TWITCH_PUBSUB_URL))
, twitchBadges(new TwitchBadges)
, logging(new Logging(_settings))
#ifdef CHATTERINO_HAVE_PLUGINS
, plugins(&this->emplace(new PluginController(paths)))
Expand All @@ -153,6 +155,7 @@ Application::~Application() = default;
void Application::fakeDtor()
{
this->twitchPubSub.reset();
this->twitchBadges.reset();
}

void Application::initialize(Settings &settings, const Paths &paths)
Expand Down Expand Up @@ -320,6 +323,13 @@ ITwitchLiveController *Application::getTwitchLiveController()
return this->twitchLiveController;
}

TwitchBadges *Application::getTwitchBadges()
{
assert(this->twitchBadges);

return this->twitchBadges.get();
}

ITwitchIrcServer *Application::getTwitch()
{
return this->twitch;
Expand Down
4 changes: 4 additions & 0 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ISoundController;
class SoundController;
class ITwitchLiveController;
class TwitchLiveController;
class TwitchBadges;
#ifdef CHATTERINO_HAVE_PLUGINS
class PluginController;
#endif
Expand Down Expand Up @@ -78,6 +79,7 @@ class IApplication
virtual IUserDataController *getUserData() = 0;
virtual ISoundController *getSound() = 0;
virtual ITwitchLiveController *getTwitchLiveController() = 0;
virtual TwitchBadges *getTwitchBadges() = 0;
virtual ImageUploader *getImageUploader() = 0;
virtual SeventvAPI *getSeventvAPI() = 0;
virtual Updates &getUpdates() = 0;
Expand Down Expand Up @@ -141,6 +143,7 @@ class Application : public IApplication
private:
TwitchLiveController *const twitchLiveController{};
std::unique_ptr<PubSub> twitchPubSub;
std::unique_ptr<TwitchBadges> twitchBadges;
const std::unique_ptr<Logging> logging;

public:
Expand Down Expand Up @@ -215,6 +218,7 @@ class Application : public IApplication
IUserDataController *getUserData() override;
ISoundController *getSound() override;
ITwitchLiveController *getTwitchLiveController() override;
TwitchBadges *getTwitchBadges() override;
ImageUploader *getImageUploader() override
{
return this->imageUploader;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/highlights/BadgeHighlightModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void BadgeHighlightModel::getRowFromItem(const HighlightBadge &item,
setFilePathItem(row[Column::SoundPath], item.getSoundUrl());
setColorItem(row[Column::Color], *item.getColor());

TwitchBadges::instance()->getBadgeIcon(
getIApp()->getTwitchBadges()->getBadgeIcon(
item.badgeName(), [item, row](QString /*name*/, const QIconPtr pixmap) {
row[Column::Badge]->setData(QVariant(*pixmap), Qt::DecorationRole);
});
Expand Down
14 changes: 1 addition & 13 deletions src/providers/twitch/TwitchBadges.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "TwitchBadges.hpp"
#include "providers/twitch/TwitchBadges.hpp"

#include "common/network/NetworkRequest.hpp"
#include "common/network/NetworkResult.hpp"
Expand Down Expand Up @@ -279,16 +279,4 @@ void TwitchBadges::loadEmoteImage(const QString &name, ImagePtr image,
.execute();
}

TwitchBadges *TwitchBadges::instance_;

TwitchBadges *TwitchBadges::instance()
{
if (TwitchBadges::instance_ == nullptr)
{
TwitchBadges::instance_ = new TwitchBadges();
}

return TwitchBadges::instance_;
}

} // namespace chatterino
5 changes: 1 addition & 4 deletions src/providers/twitch/TwitchBadges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TwitchBadges
using BadgeIconCallback = std::function<void(QString, const QIconPtr)>;

public:
static TwitchBadges *instance();
TwitchBadges();

// Get badge from name and version
std::optional<EmotePtr> badge(const QString &set,
Expand All @@ -46,9 +46,6 @@ class TwitchBadges
BadgeIconCallback callback);

private:
static TwitchBadges *instance_;

TwitchBadges();
void loadTwitchBadges();
void parseTwitchBadges(QJsonObject root);
void loaded();
Expand Down
2 changes: 1 addition & 1 deletion src/providers/twitch/TwitchMessageBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace {
}

if (auto globalBadge =
TwitchBadges::instance()->badge(badge.key_, badge.value_))
getIApp()->getTwitchBadges()->badge(badge.key_, badge.value_))
{
return globalBadge;
}
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/dialogs/BadgePickerDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "BadgePickerDialog.hpp"

#include "Application.hpp"
#include "providers/twitch/TwitchBadges.hpp"
#include "singletons/Resources.hpp"

Expand Down Expand Up @@ -57,7 +58,7 @@ BadgePickerDialog::BadgePickerDialog(QList<DisplayBadge> badges,
updateBadge(0);

// Set icons.
TwitchBadges::instance()->getBadgeIcons(
getIApp()->getTwitchBadges()->getBadgeIcons(
badges,
[&dropdown = this->dropdown_](QString identifier, const QIconPtr icon) {
if (!dropdown)
Expand Down

0 comments on commit 4c551b3

Please sign in to comment.