diff --git a/src/controllers/accounts/accountcontroller.cpp b/src/controllers/accounts/accountcontroller.cpp index 3a3fc64af..c70d4587d 100644 --- a/src/controllers/accounts/accountcontroller.cpp +++ b/src/controllers/accounts/accountcontroller.cpp @@ -43,17 +43,6 @@ void AccountController::load() AccountModel *AccountController::createModel(QObject *parent) { AccountModel *model = new AccountModel(parent); - // model->accountRemoved.connect([this](const auto &account) { - // switch (account->getProviderId()) { - // case ProviderId::Twitch: { - // auto &accs = this->twitch.accounts.getVector(); - // auto it = std::find(accs.begin(), accs.end(), account); - // assert(it != accs.end()); - - // this->twitch.accounts.removeItem(it - accs.begin()); - // } break; - // } - // }); model->init(&this->accounts); return model; diff --git a/src/providers/twitch/twitchaccountmanager.cpp b/src/providers/twitch/twitchaccountmanager.cpp index f86f75bf7..1b8b24e8a 100644 --- a/src/providers/twitch/twitchaccountmanager.cpp +++ b/src/providers/twitch/twitchaccountmanager.cpp @@ -15,6 +15,9 @@ TwitchAccountManager::TwitchAccountManager() auto currentUser = this->getCurrent(); currentUser->loadIgnores(); }); + + this->accounts.itemRemoved.connect( + [this](const auto &acc) { this->removeUser(acc.item.get()); }); } std::shared_ptr TwitchAccountManager::getCurrent() @@ -134,35 +137,16 @@ void TwitchAccountManager::load() }); } -bool TwitchAccountManager::removeUser(const QString &username) +bool TwitchAccountManager::removeUser(TwitchAccount *account) { - if (!this->userExists(username)) { - return false; + const auto &accs = this->accounts.getVector(); + + std::string userID(account->getUserId().toStdString()); + if (!userID.empty()) { + pajlada::Settings::SettingManager::removeSetting("/accounts/uid" + userID); } - { - std::lock_guard guard(this->mutex); - - const auto &accs = this->accounts.getVector(); - - while (true) { - auto it = std::find_if(accs.begin(), accs.end(), - [&](const auto &acc) { return acc->getUserName() == username; }); - - if (it == accs.end()) { - break; - } - - std::string userID(it->get()->getUserId().toStdString()); - if (!userID.empty()) { - pajlada::Settings::SettingManager::removeSetting("/accounts/uid" + userID); - } - - this->accounts.removeItem(int(it - accs.begin())); - } - } - - if (username == qS(this->currentUsername.getValue())) { + if (account->getUserName() == qS(this->currentUsername.getValue())) { // The user that was removed is the current user, log into the anonymous user this->currentUsername = ""; } @@ -197,7 +181,7 @@ TwitchAccountManager::AddUserResponse TwitchAccountManager::addUser( auto newUser = std::make_shared(userData.username, userData.oauthToken, userData.clientID, userData.userID); - std::lock_guard lock(this->mutex); + // std::lock_guard lock(this->mutex); this->accounts.insertItem(newUser); diff --git a/src/providers/twitch/twitchaccountmanager.hpp b/src/providers/twitch/twitchaccountmanager.hpp index b4e785934..b7fe99f0e 100644 --- a/src/providers/twitch/twitchaccountmanager.hpp +++ b/src/providers/twitch/twitchaccountmanager.hpp @@ -47,8 +47,6 @@ public: void reloadUsers(); void load(); - bool removeUser(const QString &username); - pajlada::Settings::Setting currentUsername = {"/accounts/current", ""}; pajlada::Signals::NoArgSignal currentUserChanged; pajlada::Signals::NoArgSignal userListUpdated; @@ -64,11 +62,11 @@ private: UserAdded, }; AddUserResponse addUser(const UserData &data); + bool removeUser(TwitchAccount *account); std::shared_ptr currentUser; std::shared_ptr anonymousUser; - // std::vector> users; mutable std::mutex mutex; friend class chatterino::controllers::accounts::AccountController;