Files
PiperFeladat/Feladat1/UI/MainWindow/MainWindow.cs

135 lines
4.0 KiB
C#
Raw Normal View History

2025-09-26 17:32:32 +02:00
using Adw;
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
{
2025-09-27 23:08:58 +02:00
private ToastOverlay toastOverlay;
private const string toastOverlayId = "toast_overlay";
2025-09-26 17:32:32 +02:00
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-27 23:08:58 +02:00
toastOverlay = builder.GetObject(toastOverlayId) as ToastOverlay;
if (toastOverlay == null)
{
throw new NullReferenceException(toastOverlayId);
}
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)
{
Toast toast = new Toast();
2025-09-27 23:08:58 +02:00
toast.SetTitle("User data exported successfully");
toastOverlay.AddToast(toast);
2025-09-27 23:01:24 +02:00
User user = userService.Read();
emailLabel.Text_ = user.Email;
usernameLabel.Text_ = user.Username;
passwordLabel.Text_ = user.Password;
}
2025-09-26 17:32:32 +02:00
}
}
}