/
Shymer123
/
Messenger
Обзор
Документация
Войти
/
Shymer123
/
Messenger
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
UI_Client/SourceFiles/Processes/ContactsMenu/addContact/AddContactProcess.cpp
213 строк
7 KB
Shymer123
A lot of bugs related to the creation of dialogs and groups have been
26 мар 2025, 16:07
26 мар 2025, 16:07
6a8c3e4
Код
Авторство
О чём код?
#include "AddContactProcess.h" #include "RequestResponseHandler/dependencyManager.h" #include "Ui/ContactsMenu/AddContact/AddContact.h" #include "DataModels/user_data.h" #include "Ui/Widgets/Inputs/PhoneLineEdit/PhoneLineEdit.h" #include <QStyle> #include <QJsonObject> #include <QLineEdit> #include <QLabel> namespace Processes { AddContactProcess::AddContactProcess() { m_communicationHandler = DependencyManager::instance().communicationHandler(); m_userID = UserData::instance().getID(); } AddContactProcess::~AddContactProcess() {} void AddContactProcess::setAddContactWidget(Ui::ContactsMenu::AddContact* menu) { m_addContactWidget = menu; } void AddContactProcess::setNotExistPhoneNumberWidget(Ui::ContactsMenu::NotExistPhoneNumber *menu) { m_notExistPhoneNumberWidget = menu; } void AddContactProcess::processAddContact(const QString& firstName, const QString& lastName, const QString& phoneNumber) { if (!isFirstNameValid(firstName)) { updateFieldStyle(m_addContactWidget->getFirstNameField(), "error"); return; } QString combinedName = lastName.isEmpty() ? firstName : firstName + " " + lastName; validateContact(combinedName, phoneNumber, [this, combinedName, phoneNumber, lastName](bool nameExists, bool phoneValid) { QLabel* infoLabel = m_addContactWidget->getInfoLabel(); if (nameExists) { if(lastName.isEmpty()) { updateFieldStyle(m_addContactWidget->getFirstNameField(), "error"); updateFieldStyle(m_addContactWidget->getLastNameField(), "error"); infoLabel->setText(tr("A name with such a contact already exists")); } else { updateFieldStyle(m_addContactWidget->getFirstNameField(), "error"); infoLabel->setText(tr("A name with such a contact already exists")); } return; } if (!phoneValid) { updateFieldStyle(m_addContactWidget->getPhoneNumberField(), "error"); infoLabel->setText(tr("an incomplete phone number, or you alreadyhave a contact" "\nwith such a number, or you nentered your number")); return; } addContactOnDataBase(combinedName, phoneNumber); }); } void AddContactProcess::updateFieldStyle(QWidget* field, const QString& styleClass) { field->setProperty("class", styleClass); field->style()->unpolish(field); field->style()->polish(field); } void AddContactProcess::validateContact(const QString& combinedName, const QString& phoneNumber, std::function<void(bool, bool)> callback) { isCombinedNameExists(combinedName, [this, phoneNumber, callback](bool nameExists) { isPhoneNumberValid(phoneNumber, [callback, nameExists](bool phoneValid) { callback(nameExists, phoneValid); }); }); } bool AddContactProcess::isFirstNameValid(const QString& firstName) { if(firstName.isEmpty()) { return false; } return true; } void AddContactProcess::isCombinedNameExists(const QString& combinedName, std::function<void(bool)> callback) { m_communicationHandler->getIsCombinedNameExists(m_userID, combinedName); disconnect(m_communicationHandler, &CommunicationHandler::getIsCombinedNameExistsReceived, nullptr, nullptr); connect(m_communicationHandler, &CommunicationHandler::getIsCombinedNameExistsReceived, [callback](const QJsonObject& response) { bool isNameExists = response.value("isNameExists").toBool(); callback(isNameExists); }); } void AddContactProcess::isPhoneNumberValid(const QString& phoneNumber, std::function<void (bool)> callback) { if(UserData::instance().getPhoneNumber() == phoneNumber || phoneNumber.length() != 15) { callback(false); return; } m_communicationHandler->getIsPhoneNumberValid(m_userID, phoneNumber); disconnect(m_communicationHandler, &CommunicationHandler::getIsPhoneNumberValidReceived, nullptr, nullptr); connect(m_communicationHandler, &CommunicationHandler::getIsPhoneNumberValidReceived, this, [this, callback](const QJsonObject& response) { bool isInDatabase = response.value("isInDatabase").toBool(); bool isInMyContacts = response.value("isInMyContacts").toBool(); if(!isInDatabase) { QLineEdit* firstNameField = m_addContactWidget->getFirstNameField(); QLineEdit* lastNameField = m_addContactWidget->getLastNameField(); Ui::Widgets::PhoneLineEdit* phoneNumberField = m_addContactWidget->getPhoneNumberField(); QString firstName = firstNameField->text(); firstNameField->clear(); firstNameField->setProperty("class", ""); firstNameField->style()->unpolish(firstNameField); firstNameField->style()->polish(firstNameField); lastNameField->clear(); lastNameField->setProperty("class", ""); lastNameField->style()->unpolish(lastNameField); lastNameField->style()->polish(lastNameField); phoneNumberField->clear(); phoneNumberField->setProperty("class", ""); phoneNumberField->style()->unpolish(phoneNumberField); phoneNumberField->style()->polish(phoneNumberField); m_addContactWidget->hide(); m_notExistPhoneNumberWidget->setFirstName(firstName); m_notExistPhoneNumberWidget->setGeometry(m_addContactWidget->geometry()); m_notExistPhoneNumberWidget->show(); connect(m_notExistPhoneNumberWidget, &Ui::ContactsMenu::NotExistPhoneNumber::clickedBackSignal, this, &AddContactProcess::onClickedBackInNotExistPhoneNumber, Qt::ConnectionType::UniqueConnection); callback(false); } else if(isInMyContacts) { callback(false); } else { callback(true); } }); } void AddContactProcess::addContactOnDataBase(const QString &combinedName, const QString &phoneNumber) { receiveUser2ID(phoneNumber, [this, combinedName, phoneNumber](const int user2ID) { m_communicationHandler->addContact(m_userID, UserData::instance().getUserName(), user2ID, combinedName, phoneNumber); emit clickedCancelSignal(); }); } void AddContactProcess::receiveUser2ID(const QString& phoneNumber, std::function<void(const int user2ID)> callback) { m_communicationHandler->getParticipantIDInDialogUsePhoneNumber(phoneNumber); disconnect(m_communicationHandler, &CommunicationHandler::getParticipantIDInDialogUsePhoneNumberReceived, nullptr, nullptr); connect(m_communicationHandler, &CommunicationHandler::getParticipantIDInDialogUsePhoneNumberReceived, [callback](const QJsonObject& response) { int user2ID = response.value("participantID").toInt(); callback(user2ID); }); } void AddContactProcess::onClickedBackInNotExistPhoneNumber() { m_notExistPhoneNumberWidget->hide(); m_addContactWidget->setGeometry(m_notExistPhoneNumberWidget->geometry()); m_addContactWidget->show(); } } // namespace Processes