more refactoring
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michele Scandura 2021-09-08 16:53:22 +01:00
parent 3b505141e6
commit f4c2ad79f2
2 changed files with 410 additions and 172 deletions

View File

@ -0,0 +1,238 @@
using System;
using System.Diagnostics;
using System.Linq;
using Exceptionless;
using Microsoft.Xna.Framework;
using Myra.Graphics2D.Brushes;
using Myra.Graphics2D.UI;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.UI
{
public partial class LoginRegisterWindow
{
private AuthenticateResponse _authResponse;
private readonly CommunicationManager CommunicationManager;
private readonly MainWidget MainWidget;
private readonly Window Window;
public LoginRegisterWindow(CommunicationManager communicationManager, MainWidget mainWidget, Window window)
{
BuildUI();
MainWidget=mainWidget;
CommunicationManager=communicationManager;
Window=window;
#if DEBUG
TxtEmail.Text = "michele.scandura@outlook.com";
TxtPassword.Text = "slePharland!79";
#endif
RdoLogin.IsPressed = true;
RdoLogin.Click += (s, e) =>
{
TxtFirstname.Visible = false;
TxtLastname.Visible = false;
TxtInitials.Visible = false;
LblFirstname.Visible = false;
LblLastname.Visible = false;
LblInitials.Visible = false;
BtnLogin.Visible = true;
BtnRegister.Visible = false;
Window.Title = "Login";
};
RdoRegister.Click += (s, e) =>
{
TxtFirstname.Visible = true;
TxtLastname.Visible = true;
TxtInitials.Visible = true;
LblFirstname.Visible = true;
LblLastname.Visible = true;
LblInitials.Visible = true;
BtnLogin.Visible = false;
BtnRegister.Visible = true;
Window.Title = "Register";
};
BtnRegister.Click += OnButtonRegisterClick;
BtnLogin.Click += OnButtonLoginClick;
//TxtEmail.SetKeyboardFocus();
}
//TODO refactor
private bool ValidateTextbox(TextBox textBox)
{
var valid = !string.IsNullOrWhiteSpace(textBox.Text);
if (!valid)
{
textBox.Background = new SolidBrush(Color.Red);
}
else
{
textBox.Background = new SolidBrush(new Color(51, 51, 51)); ;
}
return valid;
}
private async void OnButtonRegisterClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
var localContent = this;// GetParentContentInWindow<LoginRegisterWindow>(button);// localWindow.Content as PlayerWindow;
if (!button.Enabled)
{
return;
}
var isValid = true;
isValid &= ValidateTextbox(TxtEmail);
isValid &= ValidateTextbox(TxtPassword);
isValid &= ValidateTextbox(TxtFirstname);
isValid &= ValidateTextbox(TxtLastname);
isValid &= ValidateTextbox(TxtInitials);
if (!isValid)
{
LblLoginError.Text = "Please complete all the fields";
LblLoginError.Visible = true;
return;
}
var successful = false;
try
{
button.Text = "Wait...";
LblLoginError.Text = "";
LblLoginError.Visible = false;
var result = await CommunicationManager.Register(new RegisterModel
{
Username = TxtEmail.Text,
Email = TxtEmail.Text,
Password = TxtPassword.Text,
FirstName = TxtFirstname.Text,
LastName = TxtLastname.Text,
Initials = TxtInitials.Text
});
if (result.Result)
{
_authResponse = await CommunicationManager.Login(new AuthenticateModel
{
Username = TxtEmail.Text,
Password = TxtPassword.Text
});
successful = true;
}
else
{
LblLoginError.Text = result.Errors.FirstOrDefault();
LblLoginError.Visible = true;
}
}
catch (Refit.ApiException refitException)
{
ExceptionlessClient.Default.SubmitException(refitException);
LblLoginError.Text = refitException.Content;
LblLoginError.Visible = true;
}
catch (Exception ex)
{
ExceptionlessClient.Default.SubmitException(ex);
LblLoginError.Text = "Can't connect to the server";
LblLoginError.Visible = true;
Debug.Write(ex);
}
finally
{
button.Enabled = true;
button.Text = "Register";
}
if (successful)
{
MainWidget.MenuConnectNew.Enabled = true;
MainWidget.MenuConnectJoin.Enabled = true;
MainWidget.lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
localContent.Window.Close();
}
}
private async void OnButtonLoginClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
Container container = button.Parent;
while (!(container is Window))
{
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as LoginRegisterWindow;
if (!button.Enabled)
{
return;
}
var isValid = true;
isValid &= ValidateTextbox(localContent.TxtEmail);
isValid &= ValidateTextbox(localContent.TxtPassword);
if (!isValid)
{
localContent.LblLoginError.Text = "Username or password is not valid";
localContent.LblLoginError.Visible = true;
return;
}
var successful = false;
try
{
button.Text = "Wait...";
localContent.LblLoginError.Text = "";
localContent.LblLoginError.Visible = false;
_authResponse = await CommunicationManager.Login(new AuthenticateModel
{
Username = localContent.TxtEmail.Text,
Email = localContent.TxtEmail.Text,
Password = localContent.TxtPassword.Text
});
successful = _authResponse != null;
}
catch (Refit.ApiException refitException)
{
ExceptionlessClient.Default.SubmitException(refitException);
localContent.LblLoginError.Text = refitException.Content;
localContent.LblLoginError.Visible = true;
}
catch (Exception ex)
{
ExceptionlessClient.Default.SubmitException(ex);
localContent.LblLoginError.Text = "Can't connect to the server";
localContent.LblLoginError.Visible = true;
Debug.Write(ex);
}
finally
{
button.Enabled = true;
button.Text = "Login";
}
if (successful)
{
MainWidget.MenuConnectNew.Enabled = true;
MainWidget.MenuConnectJoin.Enabled = true;
MainWidget.lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
localWindow.Close();
}
}
}
}

View File

@ -96,203 +96,203 @@ namespace Sledgemapper.UI
Title = "Login"
};
var content = new LoginRegisterWindow();
var content = new LoginRegisterWindow(CommunicationManager, this, window);
#if DEBUG
content.TxtEmail.Text = "michele.scandura@outlook.com";
content.TxtPassword.Text = "slePharland!79";
#endif
// #if DEBUG
// content.TxtEmail.Text = "michele.scandura@outlook.com";
// content.TxtPassword.Text = "slePharland!79";
// #endif
content.RdoLogin.IsPressed = true;
content.RdoLogin.Click += (s, e) =>
{
content.TxtFirstname.Visible = false;
content.TxtLastname.Visible = false;
content.TxtInitials.Visible = false;
content.LblFirstname.Visible = false;
content.LblLastname.Visible = false;
content.LblInitials.Visible = false;
content.BtnLogin.Visible = true;
content.BtnRegister.Visible = false;
window.Title = "Login";
};
// content.RdoLogin.IsPressed = true;
// content.RdoLogin.Click += (s, e) =>
// {
// content.TxtFirstname.Visible = false;
// content.TxtLastname.Visible = false;
// content.TxtInitials.Visible = false;
// content.LblFirstname.Visible = false;
// content.LblLastname.Visible = false;
// content.LblInitials.Visible = false;
// content.BtnLogin.Visible = true;
// content.BtnRegister.Visible = false;
// window.Title = "Login";
// };
content.RdoRegister.Click += (s, e) =>
{
content.TxtFirstname.Visible = true;
content.TxtLastname.Visible = true;
content.TxtInitials.Visible = true;
content.LblFirstname.Visible = true;
content.LblLastname.Visible = true;
content.LblInitials.Visible = true;
content.BtnLogin.Visible = false;
content.BtnRegister.Visible = true;
window.Title = "Register";
};
// content.RdoRegister.Click += (s, e) =>
// {
// content.TxtFirstname.Visible = true;
// content.TxtLastname.Visible = true;
// content.TxtInitials.Visible = true;
// content.LblFirstname.Visible = true;
// content.LblLastname.Visible = true;
// content.LblInitials.Visible = true;
// content.BtnLogin.Visible = false;
// content.BtnRegister.Visible = true;
// window.Title = "Register";
// };
content.BtnRegister.Click += OnButtonRegisterClick;
content.BtnLogin.Click += OnButtonLoginClick;
// content.BtnRegister.Click += OnButtonRegisterClick;
// content.BtnLogin.Click += OnButtonLoginClick;
// content.TxtEmail.SetKeyboardFocus();
window.Content = content;
window.ShowModal(Desktop);
content.TxtEmail.SetKeyboardFocus();
}
private async void OnButtonRegisterClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
// private async void OnButtonRegisterClick(object sender, EventArgs e)
// {
// var button = ((TextButton)sender);
var localContent = GetParentContentInWindow<LoginRegisterWindow>(button);// localWindow.Content as PlayerWindow;
// var localContent = GetParentContentInWindow<LoginRegisterWindow>(button);// localWindow.Content as PlayerWindow;
if (!button.Enabled)
{
return;
}
// if (!button.Enabled)
// {
// return;
// }
var isValid = true;
isValid &= ValidateTextbox(localContent.Content.TxtEmail);
isValid &= ValidateTextbox(localContent.Content.TxtPassword);
isValid &= ValidateTextbox(localContent.Content.TxtFirstname);
isValid &= ValidateTextbox(localContent.Content.TxtLastname);
isValid &= ValidateTextbox(localContent.Content.TxtInitials);
// var isValid = true;
// isValid &= ValidateTextbox(localContent.Content.TxtEmail);
// isValid &= ValidateTextbox(localContent.Content.TxtPassword);
// isValid &= ValidateTextbox(localContent.Content.TxtFirstname);
// isValid &= ValidateTextbox(localContent.Content.TxtLastname);
// isValid &= ValidateTextbox(localContent.Content.TxtInitials);
if (!isValid)
{
localContent.Content.LblLoginError.Text = "Please complete all the fields";
localContent.Content.LblLoginError.Visible = true;
return;
}
// if (!isValid)
// {
// localContent.Content.LblLoginError.Text = "Please complete all the fields";
// localContent.Content.LblLoginError.Visible = true;
// return;
// }
var successful = false;
try
{
button.Text = "Wait...";
localContent.Content.LblLoginError.Text = "";
localContent.Content.LblLoginError.Visible = false;
// var successful = false;
// try
// {
// button.Text = "Wait...";
// localContent.Content.LblLoginError.Text = "";
// localContent.Content.LblLoginError.Visible = false;
var result = await CommunicationManager.Register(new RegisterModel
{
Username = localContent.Content.TxtEmail.Text,
Email = localContent.Content.TxtEmail.Text,
Password = localContent.Content.TxtPassword.Text,
FirstName = localContent.Content.TxtFirstname.Text,
LastName = localContent.Content.TxtLastname.Text,
Initials = localContent.Content.TxtInitials.Text
});
if (result.Result)
{
_authResponse = await CommunicationManager.Login(new AuthenticateModel
{
Username = localContent.Content.TxtEmail.Text,
Password = localContent.Content.TxtPassword.Text
});
successful = true;
}
else
{
localContent.Content.LblLoginError.Text = result.Errors.FirstOrDefault();
localContent.Content.LblLoginError.Visible = true;
}
}
catch (Refit.ApiException refitException)
{
ExceptionlessClient.Default.SubmitException(refitException);
localContent.Content.LblLoginError.Text = refitException.Content;
localContent.Content.LblLoginError.Visible = true;
}
catch (Exception ex)
{
ExceptionlessClient.Default.SubmitException(ex);
localContent.Content.LblLoginError.Text = "Can't connect to the server";
localContent.Content.LblLoginError.Visible = true;
Debug.Write(ex);
}
finally
{
button.Enabled = true;
button.Text = "Register";
}
if (successful)
{
MenuConnectNew.Enabled = true;
MenuConnectJoin.Enabled = true;
// var result = await CommunicationManager.Register(new RegisterModel
// {
// Username = localContent.Content.TxtEmail.Text,
// Email = localContent.Content.TxtEmail.Text,
// Password = localContent.Content.TxtPassword.Text,
// FirstName = localContent.Content.TxtFirstname.Text,
// LastName = localContent.Content.TxtLastname.Text,
// Initials = localContent.Content.TxtInitials.Text
// });
// if (result.Result)
// {
// _authResponse = await CommunicationManager.Login(new AuthenticateModel
// {
// Username = localContent.Content.TxtEmail.Text,
// Password = localContent.Content.TxtPassword.Text
// });
// successful = true;
// }
// else
// {
// localContent.Content.LblLoginError.Text = result.Errors.FirstOrDefault();
// localContent.Content.LblLoginError.Visible = true;
// }
// }
// catch (Refit.ApiException refitException)
// {
// ExceptionlessClient.Default.SubmitException(refitException);
// localContent.Content.LblLoginError.Text = refitException.Content;
// localContent.Content.LblLoginError.Visible = true;
// }
// catch (Exception ex)
// {
// ExceptionlessClient.Default.SubmitException(ex);
// localContent.Content.LblLoginError.Text = "Can't connect to the server";
// localContent.Content.LblLoginError.Visible = true;
// Debug.Write(ex);
// }
// finally
// {
// button.Enabled = true;
// button.Text = "Register";
// }
// if (successful)
// {
// MenuConnectNew.Enabled = true;
// MenuConnectJoin.Enabled = true;
lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
// lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
localContent.Window.Close();
}
}
// localContent.Window.Close();
// }
// }
private async void OnButtonLoginClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
Container container = button.Parent;
while (!(container is Window))
{
container = container.Parent;
}
// private async void OnButtonLoginClick(object sender, EventArgs e)
// {
// var button = ((TextButton)sender);
// Container container = button.Parent;
// while (!(container is Window))
// {
// container = container.Parent;
// }
var localWindow = (Window)container;
var localContent = localWindow.Content as LoginRegisterWindow;
// var localWindow = (Window)container;
// var localContent = localWindow.Content as LoginRegisterWindow;
if (!button.Enabled)
{
return;
}
// if (!button.Enabled)
// {
// return;
// }
var isValid = true;
isValid &= ValidateTextbox(localContent.TxtEmail);
isValid &= ValidateTextbox(localContent.TxtPassword);
// var isValid = true;
// isValid &= ValidateTextbox(localContent.TxtEmail);
// isValid &= ValidateTextbox(localContent.TxtPassword);
if (!isValid)
{
localContent.LblLoginError.Text = "Username or password is not valid";
localContent.LblLoginError.Visible = true;
// if (!isValid)
// {
// localContent.LblLoginError.Text = "Username or password is not valid";
// localContent.LblLoginError.Visible = true;
return;
}
// return;
// }
var successful = false;
try
{
button.Text = "Wait...";
localContent.LblLoginError.Text = "";
localContent.LblLoginError.Visible = false;
_authResponse = await CommunicationManager.Login(new AuthenticateModel
{
Username = localContent.TxtEmail.Text,
Email = localContent.TxtEmail.Text,
Password = localContent.TxtPassword.Text
});
successful = _authResponse != null;
}
catch (Refit.ApiException refitException)
{
ExceptionlessClient.Default.SubmitException(refitException);
localContent.LblLoginError.Text = refitException.Content;
localContent.LblLoginError.Visible = true;
}
catch (Exception ex)
{
ExceptionlessClient.Default.SubmitException(ex);
localContent.LblLoginError.Text = "Can't connect to the server";
localContent.LblLoginError.Visible = true;
Debug.Write(ex);
}
finally
{
button.Enabled = true;
button.Text = "Login";
}
// var successful = false;
// try
// {
// button.Text = "Wait...";
// localContent.LblLoginError.Text = "";
// localContent.LblLoginError.Visible = false;
// _authResponse = await CommunicationManager.Login(new AuthenticateModel
// {
// Username = localContent.TxtEmail.Text,
// Email = localContent.TxtEmail.Text,
// Password = localContent.TxtPassword.Text
// });
// successful = _authResponse != null;
// }
// catch (Refit.ApiException refitException)
// {
// ExceptionlessClient.Default.SubmitException(refitException);
// localContent.LblLoginError.Text = refitException.Content;
// localContent.LblLoginError.Visible = true;
// }
// catch (Exception ex)
// {
// ExceptionlessClient.Default.SubmitException(ex);
// localContent.LblLoginError.Text = "Can't connect to the server";
// localContent.LblLoginError.Visible = true;
// Debug.Write(ex);
// }
// finally
// {
// button.Enabled = true;
// button.Text = "Login";
// }
if (successful)
{
MenuConnectNew.Enabled = true;
MenuConnectJoin.Enabled = true;
lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
localWindow.Close();
}
}
// if (successful)
// {
// MenuConnectNew.Enabled = true;
// MenuConnectJoin.Enabled = true;
// lblUsername.Text = $"{_authResponse.Username} - {_authResponse.Initials}";
// localWindow.Close();
// }
// }
private void OnMenuConnectNewSelected(object sender, EventArgs e)