Move some variables into const.hpp

Clean up some code in the AccountManager
This commit is contained in:
Rasmus Karlsson 2017-12-19 16:13:02 +01:00
parent 324dfc9ee9
commit be3c85d72d
7 changed files with 49 additions and 25 deletions

View file

@ -175,7 +175,8 @@ HEADERS += \
src/util/irchelpers.hpp \ src/util/irchelpers.hpp \
src/util/helpers.hpp \ src/util/helpers.hpp \
src/widgets/accountswitchwidget.hpp \ src/widgets/accountswitchwidget.hpp \
src/widgets/accountswitchpopupwidget.hpp src/widgets/accountswitchpopupwidget.hpp \
src/const.hpp
PRECOMPILED_HEADER = PRECOMPILED_HEADER =

View file

@ -1,5 +1,6 @@
#include "accountmanager.hpp" #include "accountmanager.hpp"
#include "common.hpp" #include "common.hpp"
#include "const.hpp"
#include "debug/log.hpp" #include "debug/log.hpp"
namespace chatterino { namespace chatterino {
@ -18,6 +19,27 @@ inline QString getEnvString(const char *target)
} // namespace } // namespace
TwitchAccountManager::TwitchAccountManager()
{
this->anonymousUser.reset(new twitch::TwitchUser(twitch::ANONYMOUS_USERNAME, "", ""));
this->currentUsername.connect([this](const auto &newValue, auto) {
QString newUsername(QString::fromStdString(newValue));
auto user = this->findUserByUsername(newUsername);
if (user) {
debug::Log("[AccountManager:currentUsernameChanged] User successfully updated to {}",
newUsername);
this->currentUser = user;
} else {
debug::Log(
"[AccountManager:currentUsernameChanged] User successfully updated to anonymous");
this->currentUser = this->anonymousUser;
}
this->userChanged.invoke();
});
}
std::shared_ptr<twitch::TwitchUser> TwitchAccountManager::getCurrent() std::shared_ptr<twitch::TwitchUser> TwitchAccountManager::getCurrent()
{ {
if (!this->currentUser) { if (!this->currentUser) {
@ -180,23 +202,6 @@ TwitchAccountManager::AddUserResponse TwitchAccountManager::addUser(
AccountManager::AccountManager() AccountManager::AccountManager()
{ {
this->Twitch.anonymousUser.reset(new twitch::TwitchUser("justinfan64537", "", ""));
this->Twitch.currentUsername.connect([this](const auto &newValue, auto) {
QString newUsername(QString::fromStdString(newValue));
auto user = this->Twitch.findUserByUsername(newUsername);
if (user) {
debug::Log("[AccountManager:currentUsernameChanged] User successfully updated to {}",
newUsername);
this->Twitch.currentUser = user;
} else {
debug::Log(
"[AccountManager:currentUsernameChanged] User successfully updated to anonymous");
this->Twitch.currentUser = this->Twitch.anonymousUser;
}
this->Twitch.userChanged.invoke();
});
} }
void AccountManager::load() void AccountManager::load()

View file

@ -14,6 +14,8 @@ class AccountManager;
class TwitchAccountManager class TwitchAccountManager
{ {
public: public:
TwitchAccountManager();
struct UserData { struct UserData {
QString username; QString username;
QString userID; QString userID;

15
src/const.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <QString>
namespace chatterino {
static const QString ANONYMOUS_USERNAME_LABEL(" - anonymous - ");
namespace twitch {
static const QString ANONYMOUS_USERNAME("justinfan64537");
} // namespace twitch
} // namespace chatterino

View file

@ -1,4 +1,5 @@
#include "twitchuser.hpp" #include "twitchuser.hpp"
#include "const.hpp"
#include "util/urlfetch.hpp" #include "util/urlfetch.hpp"
namespace chatterino { namespace chatterino {
@ -9,7 +10,7 @@ TwitchUser::TwitchUser(const QString &username, const QString &oauthToken,
: IrcUser2(username, username, username, "oauth:" + oauthToken) : IrcUser2(username, username, username, "oauth:" + oauthToken)
, _oauthClient(oauthClient) , _oauthClient(oauthClient)
, _oauthToken(oauthToken) , _oauthToken(oauthToken)
, _isAnon(username.startsWith("justinfan")) , _isAnon(username == ANONYMOUS_USERNAME)
{ {
} }

View file

@ -1,5 +1,6 @@
#include "accountswitchwidget.hpp" #include "accountswitchwidget.hpp"
#include "accountmanager.hpp" #include "accountmanager.hpp"
#include "const.hpp"
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
@ -7,9 +8,7 @@ namespace widgets {
AccountSwitchWidget::AccountSwitchWidget(QWidget *parent) AccountSwitchWidget::AccountSwitchWidget(QWidget *parent)
: QListWidget(parent) : QListWidget(parent)
{ {
static QString anonUsername(" - anonymous - "); this->addItem(ANONYMOUS_USERNAME_LABEL);
this->addItem(anonUsername);
for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) { for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) {
this->addItem(userName); this->addItem(userName);
@ -20,7 +19,7 @@ AccountSwitchWidget::AccountSwitchWidget(QWidget *parent)
this->clear(); this->clear();
this->addItem(anonUsername); this->addItem(ANONYMOUS_USERNAME_LABEL);
for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) { for (const auto &userName : AccountManager::getInstance().Twitch.getUsernames()) {
this->addItem(userName); this->addItem(userName);
@ -36,7 +35,7 @@ AccountSwitchWidget::AccountSwitchWidget(QWidget *parent)
QObject::connect(this, &QListWidget::clicked, [this] { QObject::connect(this, &QListWidget::clicked, [this] {
if (!this->selectedItems().isEmpty()) { if (!this->selectedItems().isEmpty()) {
QString newUsername = this->currentItem()->text(); QString newUsername = this->currentItem()->text();
if (newUsername.compare(anonUsername, Qt::CaseInsensitive) == 0) { if (newUsername.compare(ANONYMOUS_USERNAME_LABEL, Qt::CaseInsensitive) == 0) {
AccountManager::getInstance().Twitch.currentUsername = ""; AccountManager::getInstance().Twitch.currentUsername = "";
} else { } else {
AccountManager::getInstance().Twitch.currentUsername = newUsername.toStdString(); AccountManager::getInstance().Twitch.currentUsername = newUsername.toStdString();

View file

@ -1,5 +1,6 @@
#include "widgets/settingsdialog.hpp" #include "widgets/settingsdialog.hpp"
#include "accountmanager.hpp" #include "accountmanager.hpp"
#include "const.hpp"
#include "debug/log.hpp" #include "debug/log.hpp"
#include "twitch/twitchmessagebuilder.hpp" #include "twitch/twitchmessagebuilder.hpp"
#include "twitch/twitchuser.hpp" #include "twitch/twitchuser.hpp"
@ -130,7 +131,7 @@ QVBoxLayout *SettingsDialog::createAccountsTab()
connect(removeButton, &QPushButton::clicked, [this]() { connect(removeButton, &QPushButton::clicked, [this]() {
qDebug() << "TODO: Implement"; // qDebug() << "TODO: Implement"; //
auto selectedUser = this->ui.accountSwitchWidget->currentItem()->text(); auto selectedUser = this->ui.accountSwitchWidget->currentItem()->text();
if (selectedUser == " - anonymous - ") { if (selectedUser == ANONYMOUS_USERNAME_LABEL) {
// Do nothing // Do nothing
return; return;
} }