85 lines
2.5 KiB
C++
85 lines
2.5 KiB
C++
//
|
|
// Created by selim on 03.01.2022.
|
|
//
|
|
|
|
#include "LoginWindow.h"
|
|
#include "MainWindow.h"
|
|
#include "../services/Api.h"
|
|
#include "../coro/GLibMainContextExecutor.h"
|
|
|
|
#include "../gtkpp/HeaderBar.h"
|
|
#include "../gtkpp/MessageDialog.h"
|
|
|
|
#include <iostream>
|
|
#include <utility>
|
|
#include <folly/experimental/coro/Task.h>
|
|
#include <folly/executors/IOThreadPoolExecutor.h>
|
|
|
|
LoginWindow::LoginWindow(std::shared_ptr<gtkpp::Application> app): gtkpp::Window(std::move(app), false) {
|
|
|
|
setDefaultSize(640, 480);
|
|
|
|
_loginEntry.setPlaceholder("Email");
|
|
_loginEntry.setPurpose(GTK_INPUT_PURPOSE_EMAIL);
|
|
_loginEntry.onChanged([this] { validateFields(); });
|
|
|
|
_passwordEntry.setPlaceholder("Password");
|
|
_passwordEntry.setPurpose(GTK_INPUT_PURPOSE_PASSWORD);
|
|
_passwordEntry.setVisibility(false);
|
|
_passwordEntry.onChanged([this] { validateFields(); });
|
|
|
|
_loginButton.setTitle("Log in");
|
|
_loginButton.setVerticalMargins(8);
|
|
_loginButton.setEnabled(false);
|
|
_loginButton.onClick([this] { loginClicked(); });
|
|
|
|
gtkpp::Box contentBox(GTK_ORIENTATION_VERTICAL, 8);
|
|
contentBox.append(_loginEntry);
|
|
contentBox.append(_passwordEntry);
|
|
contentBox.append(_loginButton);
|
|
contentBox.append(_spinner);
|
|
contentBox.setMargins(48);
|
|
contentBox.setVAlign(GTK_ALIGN_CENTER);
|
|
contentBox.setVExpand(true);
|
|
|
|
gtkpp::Box rootBox(GTK_ORIENTATION_VERTICAL, 0);
|
|
rootBox.append(gtkpp::HeaderBar("Login"));
|
|
rootBox.append(contentBox);
|
|
|
|
adw_window_set_content(ADW_WINDOW(_window), rootBox.gobj());
|
|
}
|
|
|
|
void LoginWindow::loginClicked() {
|
|
auto email = _loginEntry.text();
|
|
auto password = _passwordEntry.text();
|
|
|
|
enableControls(false);
|
|
_spinner.start();
|
|
|
|
try {
|
|
User user = co_await Api::login(email, password).scheduleOn(GLibMainContextExecutor::instance());
|
|
if(auto app = this->application()) {
|
|
auto mainWindow = std::make_shared<MainWindow>(app);
|
|
mainWindow->show();
|
|
app->addWindow(mainWindow);
|
|
app->removeWindow(this);
|
|
hide();
|
|
}
|
|
} catch (std::exception& ex) {
|
|
enableControls(true);
|
|
_spinner.stop();
|
|
gtkpp::MessageDialog::showError(this, ex.what());
|
|
}
|
|
}
|
|
|
|
void LoginWindow::validateFields() {
|
|
bool buttonEnabled = _loginEntry.textLength() > 3 && _passwordEntry.textLength() > 3;
|
|
_loginButton.setEnabled(buttonEnabled);
|
|
}
|
|
|
|
void LoginWindow::enableControls(bool enable) {
|
|
_loginButton.setEnabled(enable);
|
|
_loginEntry.setEnabled(enable);
|
|
_passwordEntry.setEnabled(enable);
|
|
}
|