Files
PiperFeladat/Feladat1/UI/MainWindow/MainWindow.cs
2025-09-28 00:53:10 +02:00

266 lines
8.3 KiB
C#

using Adw;
using Gtk;
using Logic;
using Models;
namespace Feladat1.UI.MainWindow;
public partial class MainWindow
{
private ToastOverlay toastOverlay;
private const string toastOverlayId = "toast_overlay";
private IUserService userService;
private const string windowId = "main_window";
public Adw.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";
private EntryRow emailLabel;
private const string emailLabelId = "emailLabel";
private EntryRow usernameLabel;
private const string usernameLabelId = "usernameLabel";
private PasswordEntryRow passwordLabel;
private const string passwordLabelId = "passwordLabel";
private Button leftUpArrow;
private const string leftUpArrowId = "leftUpArrow";
private Button rightUpArrow;
private const string rightUpArrowId = "rightUpArrow";
private Button leftDownArrow;
private const string leftDownArrowId = "leftDownArrow";
private Button rightDownArrow;
private const string rightDownArrowId = "rightDownArrow";
private Button leftLeftArrow;
private const string leftLeftArrowId = "leftLeftArrow";
private Button rightLeftArrow;
private const string rightLeftArrowId = "rightLeftArrow";
private Button leftRightArrow;
private const string leftRightArrowId = "leftRightArrow";
private Button rightRightArrow;
private const string rightRightArrowId = "rightRightArrow";
public MainWindow(IUserService userService)
{
this.userService = userService;
var builder = new Gtk.Builder("Feladat1.UI.MainWindow.MainWindow.ui.xml");
Window = builder.GetObject(windowId) as Adw.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);
}
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);
}
toastOverlay = builder.GetObject(toastOverlayId) as ToastOverlay;
if (toastOverlay == null)
{
throw new NullReferenceException(toastOverlayId);
}
canvas = builder.GetObject(canvasId) as DrawingArea;
if (canvas == null)
{
throw new NullReferenceException(canvasId);
}
canvas.SetDrawFunc(Draw);
lineWidthAdjustment = builder.GetObject(lineWidthAdjustmentId) as Adjustment;
if (lineWidthAdjustment == null)
{
throw new NullReferenceException(lineWidthAdjustmentId);
}
lineWidthAdjustment.OnValueChanged += (sender, args) =>
{
lineWidth = lineWidthAdjustment.Value;
canvas.QueueDraw();
};
leftUpArrow = builder.GetObject(leftUpArrowId) as Button;
if (leftUpArrow == null)
{
throw new NullReferenceException(leftUpArrowId);
}
leftUpArrow.OnClicked += (sender, args) =>
{
pointX[1] -= 5;
canvas.QueueDraw();
};
rightUpArrow = builder.GetObject(rightUpArrowId) as Button;
if (rightUpArrow == null)
{
throw new NullReferenceException(rightUpArrowId);
}
rightUpArrow.OnClicked += (sender, args) =>
{
pointY[1] -= 5;
canvas.QueueDraw();
};
leftDownArrow = builder.GetObject(leftDownArrowId) as Button;
if (leftDownArrow == null)
{
throw new NullReferenceException(leftDownArrowId);
}
leftDownArrow.OnClicked += (sender, args) =>
{
pointX[1] += 5;
canvas.QueueDraw();
};
rightDownArrow = builder.GetObject(rightDownArrowId) as Button;
if (rightDownArrow == null)
{
throw new NullReferenceException(rightDownArrowId);
}
rightDownArrow.OnClicked += (sender, args) =>
{
pointY[1] += 5;
canvas.QueueDraw();
};
leftLeftArrow = builder.GetObject(leftLeftArrowId) as Button;
if (leftLeftArrow == null)
{
throw new NullReferenceException(leftLeftArrowId);
}
leftLeftArrow.OnClicked += (sender, args) =>
{
pointX[0] -= 5;
canvas.QueueDraw();
};
rightLeftArrow = builder.GetObject(rightLeftArrowId) as Button;
if (rightLeftArrow == null)
{
throw new NullReferenceException(rightLeftArrowId);
}
rightLeftArrow.OnClicked += (sender, args) =>
{
pointY[0] -= 5;
canvas.QueueDraw();
};
leftRightArrow = builder.GetObject(leftRightArrowId) as Button;
if (leftRightArrow == null)
{
throw new NullReferenceException(leftRightArrowId);
}
leftRightArrow.OnClicked += (sender, args) =>
{
pointX[0] += 5;
canvas.QueueDraw();
};
rightRightArrow = builder.GetObject(rightRightArrowId) as Button;
if (rightRightArrow == null)
{
throw new NullReferenceException(rightRightArrowId);
}
rightRightArrow.OnClicked += (sender, args) =>
{
pointY[0] += 5;
canvas.QueueDraw();
};
}
catch (NullReferenceException e)
{
Console.WriteLine($"Failed to load UI element with ID: {e.Message}");
Window.Close();
}
}
private bool ValidateFields()
{
if (userNameField.Text_ != string.Empty && emailField.Text_ != string.Empty && passwordField.Text_ != string.Empty)
{
return true;
}
return false;
}
private void Save()
{
if (ValidateFields())
{
int returnCode = userService.Create(userNameField.GetText(), emailField.GetText(), passwordField.GetText());
if (returnCode == 0)
{
Toast toast = new Toast();
toast.SetTitle("User data exported successfully");
toastOverlay.AddToast(toast);
User user = userService.Read();
emailLabel.Text_ = user.Email;
usernameLabel.Text_ = user.Username;
passwordLabel.Text_ = user.Password;
}
}
}
}