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

86 lines
2.3 KiB
C#
Raw Normal View History

2025-09-26 17:32:32 +02:00
using Adw;
using Logic;
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";
public MainWindow()
{
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);
}
}
catch (NullReferenceException e)
{
Console.WriteLine($"Failed to load UI element with ID: {e.Message}");
Window.Close();
}
}
private bool ValidateFields()
{
if (userNameField.Text_ != null && emailField.Text_ != null && passwordField.Text_ != null)
{
return true;
}
return false;
}
private void Save()
{
if (ValidateFields())
{
userService.Create(userNameField.GetText(), emailField.GetText(), passwordField.GetText());
}
}
}