fixed removing accounts

This commit is contained in:
fourtf 2018-05-28 08:51:39 +02:00
parent 6156b1f430
commit 5ba62997fc
3 changed files with 12 additions and 41 deletions

View file

@ -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;

View file

@ -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<TwitchAccount> 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<std::mutex> 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<TwitchAccount>(userData.username, userData.oauthToken,
userData.clientID, userData.userID);
std::lock_guard<std::mutex> lock(this->mutex);
// std::lock_guard<std::mutex> lock(this->mutex);
this->accounts.insertItem(newUser);

View file

@ -47,8 +47,6 @@ public:
void reloadUsers();
void load();
bool removeUser(const QString &username);
pajlada::Settings::Setting<std::string> 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<TwitchAccount> currentUser;
std::shared_ptr<TwitchAccount> anonymousUser;
// std::vector<std::shared_ptr<TwitchAccount>> users;
mutable std::mutex mutex;
friend class chatterino::controllers::accounts::AccountController;