2025-09-26 17:32:32 +02:00
|
|
|
using Adw;
|
2025-09-27 23:01:24 +02:00
|
|
|
using GLib;
|
2025-09-26 17:32:32 +02:00
|
|
|
using Logic;
|
2025-09-27 23:01:24 +02:00
|
|
|
using Models;
|
2025-09-26 17:32:32 +02:00
|
|
|
|
|
|
|
|
namespace Feladat1.UI.MainWindow;
|
|
|
|
|
|
|
|
|
|
public class MainWindow
|
|
|
|
|
{
|
|
|
|
|
private IUserService userService;
|
|
|
|
|
private const string windowId = "main_window";
|
|
|
|
|
|
|
|
|
|
public Window Window;
|
|
|
|
|
|
|
|
|
|
private Gtk.Button saveBtn;
|
|
|
|
|
private const string saveBtnId = "save_btn";
|
|
|
|
|
|
|
|
|
|
private EntryRow emailField;
|
|
|
|
|
private const string emailFieldId = "email";
|
|
|
|
|
|
|
|
|
|
private EntryRow userNameField;
|
|
|
|
|
private const string userNameFieldId = "username";
|
|
|
|
|
|
|
|
|
|
private PasswordEntryRow passwordField;
|
|
|
|
|
private const string passwordFieldId = "password";
|
|
|
|
|
|
2025-09-27 23:01:24 +02:00
|
|
|
private EntryRow emailLabel;
|
|
|
|
|
private const string emailLabelId = "emailLabel";
|
|
|
|
|
|
|
|
|
|
private EntryRow usernameLabel;
|
|
|
|
|
private const string usernameLabelId = "usernameLabel";
|
|
|
|
|
|
|
|
|
|
private PasswordEntryRow passwordLabel;
|
|
|
|
|
private const string passwordLabelId = "passwordLabel";
|
|
|
|
|
|
|
|
|
|
public MainWindow(IUserService userService)
|
2025-09-26 17:32:32 +02:00
|
|
|
{
|
2025-09-27 23:01:24 +02:00
|
|
|
this.userService = userService;
|
2025-09-26 17:32:32 +02:00
|
|
|
var builder = new Gtk.Builder("Feladat1.UI.MainWindow.MainWindow.ui.xml");
|
|
|
|
|
|
|
|
|
|
Window = builder.GetObject(windowId) as Window;
|
|
|
|
|
if (Window == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Failed to load embedded resource MainWindow.ui.xml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
saveBtn = builder.GetObject(saveBtnId) as Gtk.Button;
|
|
|
|
|
if (saveBtn == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException(saveBtnId);
|
|
|
|
|
}
|
|
|
|
|
saveBtn.OnClicked += (sender, args) => Save();
|
|
|
|
|
|
|
|
|
|
emailField = builder.GetObject(emailFieldId) as EntryRow;
|
|
|
|
|
if (emailField == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException(emailFieldId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userNameField = builder.GetObject(userNameFieldId) as EntryRow;
|
|
|
|
|
if (userNameField == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException(userNameFieldId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
passwordField = builder.GetObject(passwordFieldId) as PasswordEntryRow;
|
|
|
|
|
if (passwordField == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException(passwordFieldId);
|
|
|
|
|
}
|
2025-09-27 23:01:24 +02:00
|
|
|
|
|
|
|
|
emailLabel = builder.GetObject(emailLabelId) as EntryRow;
|
|
|
|
|
if (emailLabel == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException(emailLabelId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usernameLabel = builder.GetObject(usernameLabelId) as EntryRow;
|
|
|
|
|
if (usernameLabel == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException(usernameLabelId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
passwordLabel = builder.GetObject(passwordLabelId) as PasswordEntryRow;
|
|
|
|
|
if (passwordLabel == null)
|
|
|
|
|
{
|
|
|
|
|
throw new NullReferenceException(passwordLabelId);
|
|
|
|
|
}
|
2025-09-26 17:32:32 +02:00
|
|
|
}
|
|
|
|
|
catch (NullReferenceException e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Failed to load UI element with ID: {e.Message}");
|
|
|
|
|
Window.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ValidateFields()
|
|
|
|
|
{
|
2025-09-27 23:01:24 +02:00
|
|
|
if (userNameField.Text_ != string.Empty && emailField.Text_ != string.Empty && passwordField.Text_ != string.Empty)
|
2025-09-26 17:32:32 +02:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Save()
|
|
|
|
|
{
|
|
|
|
|
if (ValidateFields())
|
|
|
|
|
{
|
2025-09-27 23:01:24 +02:00
|
|
|
int returnCode = userService.Create(userNameField.GetText(), emailField.GetText(), passwordField.GetText());
|
|
|
|
|
if (returnCode == 0)
|
|
|
|
|
{
|
|
|
|
|
ToastOverlay overlay = new ToastOverlay();
|
|
|
|
|
overlay.SetParent(Window);
|
|
|
|
|
|
|
|
|
|
Toast toast = new Toast();
|
|
|
|
|
toast.SetButtonLabel("New user exported successfully");
|
|
|
|
|
|
|
|
|
|
overlay.AddToast(toast);
|
|
|
|
|
|
|
|
|
|
User user = userService.Read();
|
|
|
|
|
emailLabel.Text_ = user.Email;
|
|
|
|
|
usernameLabel.Text_ = user.Username;
|
|
|
|
|
passwordLabel.Text_ = user.Password;
|
|
|
|
|
}
|
2025-09-26 17:32:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|