diff --git a/src/authdialog.cpp b/src/authdialog.cpp index b5724ec..29cf954 100644 --- a/src/authdialog.cpp +++ b/src/authdialog.cpp @@ -1,118 +1,89 @@ #include "authdialog.h" +#include "ui_authdialog.h" #include "authmanager.h" AuthDialog::AuthDialog(AuthManager *authManager, QWidget *parent) - : QDialog{parent}, m_authManager(authManager), m_state(ENTER_EMAIL) + : QDialog{parent}, m_authManager(authManager), ui(new Ui::Dialog) { - ui = new Ui::Dialog; ui->setupUi(this); - // initialize form based on current state - updateForm(); + // Connect button signals + connect(ui->sendCodeButton, &QPushButton::clicked, this, &AuthDialog::onSendCodeClicked); + connect(ui->signInButton, &QPushButton::clicked, this, &AuthDialog::onSignInClicked); + connect(ui->goBackButton, &QPushButton::clicked, this, [this]() { showEmailPage(); }); - connect(ui->sendCodeButton, &QPushButton::clicked, this, [this]() { - QString emailInput = ui->emailLineEdit->text(); - if (emailInput.isEmpty()) - { - ui->errorMessage->setText("Must provide a valid email"); - return; - } - - ui->errorMessage->setText(""); - - m_authManager->requestSignInCode(emailInput); + // Connect AuthManager signals + connect(m_authManager, &AuthManager::codeSentToEmail, this, [this](const QString &) { + showCodePage(); }); - - connect(ui->signInButton, &QPushButton::clicked, this, [this]() { - QString codeInput = ui->codeLineEdit->text(); - if (codeInput.isEmpty()) - { - ui->errorMessage->setText("Must provide a valid code"); - return; - } - - ui->errorMessage->setText(""); - - m_authManager->signIn(codeInput); - }); - - connect(m_authManager, &AuthManager::codeSentToEmail, this, [this](const QString &email) { - m_state = ENTER_CODE; - updateForm(); - }); - - connect(m_authManager, &AuthManager::isSignedInChanged, this, [this]() { - if (m_authManager->isSignedIn()) - { - m_state = SIGNED_IN; - } - else - { - m_state = ENTER_EMAIL; - } - - updateForm(); - }); - connect(m_authManager, &AuthManager::errorOccurred, this, [this](const QString &message) { - ui->errorMessage->setText(message); - ui->errorMessage->show(); + setError(message); }); -} -void AuthDialog::reset() -{ - m_state = ENTER_EMAIL; - ui->emailLineEdit->clear(); - ui->codeLineEdit->clear(); - updateForm(); -} - -void AuthDialog::updateForm() -{ - ui->errorMessage->setText(""); - - switch (m_state) - { - case ENTER_EMAIL: - ui->successLabel->hide(); - - ui->sendCodeButton->show(); - ui->emailLineEdit->show(); - ui->emailLabel->show(); - - ui->signInButton->hide(); - ui->codeLabel->hide(); - ui->codeLineEdit->hide(); - break; - case ENTER_CODE: - ui->successLabel->hide(); - - ui->sendCodeButton->hide(); - ui->emailLineEdit->hide(); - ui->emailLabel->hide(); - - ui->signInButton->show(); - ui->codeLabel->show(); - ui->codeLineEdit->show(); - - break; - case SIGNED_IN: - ui->successLabel->show(); - - ui->sendCodeButton->hide(); - ui->emailLineEdit->hide(); - ui->emailLabel->hide(); - ui->signInButton->hide(); - ui->codeLabel->hide(); - ui->codeLineEdit->hide(); - break; - default: - break; - } + // Start on email page + showEmailPage(); } AuthDialog::~AuthDialog() { delete ui; } + +void AuthDialog::reset() +{ + ui->emailLineEdit->clear(); + ui->codeLineEdit->clear(); + showEmailPage(); +} + +void AuthDialog::showEmailPage() +{ + ui->stackedWidget->setCurrentIndex(0); + ui->emailLineEdit->setFocus(); + setError(""); +} + +void AuthDialog::showCodePage() +{ + ui->stackedWidget->setCurrentIndex(1); + ui->codeLineEdit->setFocus(); + setError(""); +} + +void AuthDialog::setError(const QString &message) +{ + // Update error labels on both pages + ui->errorMessage->setText(message); + ui->codeErrorMessage->setText(message); + + // Show/hide based on whether there's an error + bool hasError = !message.isEmpty(); + ui->errorMessage->setVisible(hasError); + ui->codeErrorMessage->setVisible(hasError); +} + +void AuthDialog::onSendCodeClicked() +{ + QString email = ui->emailLineEdit->text().trimmed(); + if (email.isEmpty()) + { + setError("Please enter your email"); + return; + } + + setError(""); + m_authManager->requestSignInCode(email); +} + +void AuthDialog::onSignInClicked() +{ + QString code = ui->codeLineEdit->text().trimmed(); + if (code.isEmpty()) + { + setError("Please enter the code"); + return; + } + + setError(""); + m_authManager->signIn(code); +} diff --git a/src/authdialog.h b/src/authdialog.h index 23fc7f7..c4e8675 100644 --- a/src/authdialog.h +++ b/src/authdialog.h @@ -1,37 +1,34 @@ #ifndef AUTHDIALOG_H #define AUTHDIALOG_H -#include -#include - -#include "ui_authdialog.h" +#include class AuthManager; +namespace Ui { +class Dialog; +} + class AuthDialog : public QDialog { Q_OBJECT public: - explicit AuthDialog(AuthManager *, QWidget *parent = nullptr); + explicit AuthDialog(AuthManager *authManager, QWidget *parent = nullptr); ~AuthDialog(); - enum State - { - ENTER_EMAIL, - ENTER_CODE, - SIGNED_IN - }; - void reset(); -signals: +private slots: + void onSendCodeClicked(); + void onSignInClicked(); private: Ui::Dialog *ui; AuthManager *m_authManager; - State m_state; - void updateForm(); + void showEmailPage(); + void showCodePage(); + void setError(const QString &message); }; #endif // AUTHDIALOG_H diff --git a/src/authdialog.ui b/src/authdialog.ui index 54981ce..755eae5 100644 --- a/src/authdialog.ui +++ b/src/authdialog.ui @@ -13,102 +13,140 @@ Dialog - + - 80 - 30 - 231 - 183 + 40 + 20 + 321 + 241 - - Sign in - - - - - - Email - - - - - - - - 0 - 0 - - - - IBeamCursor - - - - - - - Code - - - - - - - - - - Error - - - - - - - - - 230 - 250 - 101 - 32 - - - - Send code - - - true - - - - - - 230 - 250 - 101 - 32 - - - - Sign in - - - true - - - - - - 120 - 120 - 121 - 16 - - - - Successful + + 0 + + + + + 50 + 20 + 231 + 183 + + + + Sign in + + + + + + Email + + + + + + + + 0 + 0 + + + + IBeamCursor + + + + + + + Error + + + + + + + Send code + + + true + + + + + + + + + + + 50 + 20 + 231 + 201 + + + + Verify code + + + + + + Enter the code we sent to your email + + + true + + + + + + + + 0 + 0 + + + + IBeamCursor + + + + + + + Error + + + + + + + + + Cancel + + + + + + + Sign In + + + true + + + + + + + +