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/helpers.hpp \
src/widgets/accountswitchwidget.hpp \
src/widgets/accountswitchpopupwidget.hpp
src/widgets/accountswitchpopupwidget.hpp \
src/const.hpp
PRECOMPILED_HEADER =

View file

@ -1,5 +1,6 @@
#include "accountmanager.hpp"
#include "common.hpp"
#include "const.hpp"
#include "debug/log.hpp"
namespace chatterino {
@ -18,6 +19,27 @@ inline QString getEnvString(const char *target)
} // 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()
{
if (!this->currentUser) {
@ -180,23 +202,6 @@ TwitchAccountManager::AddUserResponse TwitchAccountManager::addUser(
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()

View file

@ -14,6 +14,8 @@ class AccountManager;
class TwitchAccountManager
{
public:
TwitchAccountManager();
struct UserData {
QString username;
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 "const.hpp"
#include "util/urlfetch.hpp"
namespace chatterino {
@ -9,7 +10,7 @@ TwitchUser::TwitchUser(const QString &username, const QString &oauthToken,
: IrcUser2(username, username, username, "oauth:" + oauthToken)
, _oauthClient(oauthClient)
, _oauthToken(oauthToken)
, _isAnon(username.startsWith("justinfan"))
, _isAnon(username == ANONYMOUS_USERNAME)
{
}

View file

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

View file

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