This commit is contained in:
Rene Schwarz
2021-08-06 12:35:46 +02:00
parent 2240d30570
commit 4828f95ebb
59 changed files with 476 additions and 829 deletions

View File

@@ -4,6 +4,10 @@ using System.ComponentModel;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// Base View Model all the other view models inherit from
/// Makes me write the INotifyPropertyChanged only once
/// </summary>
class BaseViewModel : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

View File

@@ -4,19 +4,14 @@ using System.Collections.ObjectModel;
using System.Text;
namespace Server_Dashboard {
/// <summary>
/// View Model for the modules
/// </summary>
class DashboardModuleViewModel : BaseViewModel {
//List with all Modules inside
public ObservableCollection<DashboardModule> Modules { get; set; }
private int gpuUsage;
public int GPUUsage {
get { return gpuUsage; }
set {
if(value != gpuUsage)
gpuUsage = value;
OnPropertyChanged(nameof(gpuUsage));
}
}
//Creates Default Modules, remove before release and when implementing the actual data comming from the socket
public DashboardModuleViewModel() {
Modules = new ObservableCollection<DashboardModule>();
for (int i = 0; i < 10; i++) {

View File

@@ -6,10 +6,17 @@ using System.Windows.Input;
using Server_Dashboard_Socket;
namespace Server_Dashboard {
/// <summary>
/// View Model for the Dashboard
/// </summary>
class DashboardViewModel : BaseViewModel {
private string userName = "Username";
private DashboardModuleViewModel dmvm = new DashboardModuleViewModel();
#region Private Values
private readonly DashboardModuleViewModel dmvm = new DashboardModuleViewModel();
#endregion
#region Properties
//The Username displayed defaults to Username
private string userName = "Username";
public string UserName {
get { return userName; }
set {
@@ -19,8 +26,8 @@ namespace Server_Dashboard {
}
}
//List that contains every Module
private ObservableCollection<DashboardModule> modules;
public ObservableCollection<DashboardModule> Modules {
get { return modules; }
set {
@@ -29,32 +36,59 @@ namespace Server_Dashboard {
OnPropertyChanged(nameof(modules));
}
}
#endregion
#region Constructor
public DashboardViewModel() {
//Creates a new echo server, remove b4 release
EchoServer echoServer = new EchoServer();
echoServer.Start();
//Command inits
OpenLinkCommand = new RelayCommand(OpenLink);
OpenNewModuleWindowCommand = new RelayCommand(OpenNewModuleWindow);
CreateModuleCommand = new RelayCommand(CreateModule);
//Sets the local module to the dashboardviewmodule modules
Modules = dmvm.Modules;
}
#endregion
#region ICommands
public ICommand OpenLinkCommand { get; set; }
public ICommand OpenNewModuleWindowCommand { get; set; }
public ICommand CreateModuleCommand { get; set; }
#endregion
#region Commands
/// <summary>
/// Opens a given link in the default browser
/// </summary>
/// <param name="param">The Link to be opened e.g. https://github.com/Crylia/Server-Dashboard </param>
private void OpenLink(object param) {
Process.Start(new ProcessStartInfo((string)param) { UseShellExecute = true });
}
/// <summary>
/// Creates a new window to create a new Module
/// </summary>
/// <param name="param">Nothing</param>
private void OpenNewModuleWindow(object param) {
//Creates a new CreateModulePopup and sets this view model as datacontext
CreateModulePopup cmp = new CreateModulePopup {
DataContext = this
};
//Opens it in the middle of the screen, setting the parent window as owner causes the
//application to crash when NOT in debug mode(???)
cmp.WindowStartupLocation = WindowStartupLocation.CenterScreen;
cmp.ShowDialog();
}
/// <summary>
/// No function yes
/// </summary>
/// <param name="param">Nothing</param>
private void CreateModule(object param) {
}
#endregion
}
}

View File

@@ -5,10 +5,13 @@ using System.Windows.Input;
using System.Threading.Tasks;
namespace Server_Dashboard {
/// <summary>
/// View Model for the Login Window
/// </summary>
class LoginViewModel : BaseViewModel, IWindowHelper {
#region Properties
//Username Property
private string username;
public string Username {
get { return username; }
set {
@@ -17,9 +20,8 @@ namespace Server_Dashboard {
OnPropertyChanged(nameof(username));
}
}
//Error Text displays an error to help the user to fill the form
private string errorText;
public string ErrorText {
get { return errorText; }
set {
@@ -28,9 +30,8 @@ namespace Server_Dashboard {
OnPropertyChanged(nameof(errorText));
}
}
//Remember me button
private bool rememberUser;
public bool RememberUser {
get { return rememberUser; }
set {
@@ -39,9 +40,8 @@ namespace Server_Dashboard {
OnPropertyChanged(nameof(rememberUser));
}
}
//Loading circle, gets hidden and shown when logging in
private string loading;
public string Loading {
get { return loading; }
set {
@@ -50,76 +50,126 @@ namespace Server_Dashboard {
OnPropertyChanged(nameof(loading));
}
}
#endregion
#region Public Values
//Close action for the Window to close properly
public Action Close { get ; set; }
#endregion
#region Constructor
public LoginViewModel() {
//Loading circle is hidden on startup
Loading = "Hidden";
//Command inits
LoginCommand = new RelayCommand(LoginAsync);
if (!String.IsNullOrEmpty(Settings.Default.Username)) {
//Checks if the Username and Cookie is saved in the Settings.settings
if (!String.IsNullOrEmpty(Settings.Default.Username) && !String.IsNullOrEmpty(Settings.Default.Cookies)) {
//Takes the saved Username and Remember me button status and prefills the username and checks the Remember me button
Username = Settings.Default.Username;
RememberUser = Settings.Default.RememberMe;
}
AutoLoginAsync();
//TODO: Autologin
//AutoLoginAsync();
}
#endregion
#region ICommands
public ICommand LoginCommand { get; set; }
#endregion
#region Commands
/// <summary>
/// Async login
/// </summary>
/// <param name="parameter">Secure password string</param>
private async void LoginAsync(object parameter) {
//Checks if the Username and Password field has content
if (!String.IsNullOrWhiteSpace(Username) && !String.IsNullOrWhiteSpace((parameter as IHavePassword).SecurePassword.Unsecure())) {
//Sets loading to true to show the loading icon
Loading = "Visible";
//Sends the Username and Password to the database and saved the result, 1 successfull, 0 wrong username or password
int result = await Task.Run(() => DatabaseHandler.CheckLogin(Username, (parameter as IHavePassword).SecurePassword.Unsecure()));
//hides the loading again
Loading = "Hidden";
/*result can be:
0 for Wrong username or password
1 for true username or password
2 for network or database unreachable
default error for everything else
*/
switch (result) {
case 0:
//Sets the error text and exits function
ErrorText = "Username or password is wrong.";
return;
case 1:
/*No idea why this is here, gonna let it be till the remember me and autologin is 100% done
if (RememberUser && !String.IsNullOrEmpty(Settings.Default.Cookies)) {
DatabaseHandler.CheckCookie(Settings.Default.Cookies, Username);
}
}*/
//If the remember me is not checked and a cookie exists the local storage gets cleared
if (!RememberUser && !String.IsNullOrEmpty(Settings.Default.Cookies)) {
Settings.Default.Cookies = null;
Settings.Default.Username = "";
Settings.Default.RememberMe = false;
Settings.Default.Password = "";
Settings.Default.Save();
//Also deletes the cookie from the database
DatabaseHandler.DeleteCookie(Username);
}
//If the remember user option is checked and the cookie is not set save everything locally
if (RememberUser && String.IsNullOrEmpty(Settings.Default.Cookies)) {
var guid = new Guid().ToString() + Username;
Settings.Default.Cookies = guid;
//Creates a new GUID with the username at the end, this is the cookie
var cookie = new Guid().ToString() + Username;
//Saves cookie, Username Remember me option to the local storage (Settings.settings)
Settings.Default.Cookies = cookie;
Settings.Default.Username = Username;
Settings.Default.RememberMe = true;
Settings.Default.Password = "*****";
Settings.Default.Save();
DatabaseHandler.AddCookie(Username, guid);
//Saves the cookie in the database
DatabaseHandler.AddCookie(Username, cookie);
}
//Creates a new Dashboard window and shows it
DashboardWindow window = new DashboardWindow();
window.Show();
//When closed, close it correctly
Close?.Invoke();
return;
case 2:
//Sets the error text
ErrorText = "Server unreachable, connection timeout.";
return;
default:
//Sets the error text
ErrorText = "An unknown error has occured";
return;
}
//If the Username and password is blank
//All these IF's could be one but i made multiple for the different errors so the user knows whats wrong
} else if (String.IsNullOrWhiteSpace(Username) && String.IsNullOrWhiteSpace((parameter as IHavePassword).SecurePassword.Unsecure())) {
//Sets the error text
ErrorText = "Please provide a username and password";
return;
}
//Only if the Username is empty
if (String.IsNullOrWhiteSpace(Username)) {
//Sets the error text
ErrorText = "Username cannot be empty.";
return;
}
//Only if the password is empty
if (String.IsNullOrWhiteSpace((parameter as IHavePassword).SecurePassword.Unsecure())) {
//Sets the error text
ErrorText = "Password cannot be empty.";
return;
}
//If there is no error, clear the error text
ErrorText = "";
}
#endregion
#region private functions
//TODO: Add autologin function that locks the UI untill the user hits the abort button or the login completes
/*private async void AutoLoginAsync() {
if (Settings.Default.RememberMe && !String.IsNullOrEmpty(Settings.Default.Username) && !String.IsNullOrEmpty(Settings.Default.Cookies)) {
@@ -134,5 +184,6 @@ namespace Server_Dashboard {
}
}
}*/
#endregion
}
}

View File

@@ -7,7 +7,7 @@ namespace Server_Dashboard {
class RelayCommand : ICommand {
//New Action
private Action<object> execAction;
private readonly Action<object> execAction;
//Never gets fires, but needs to be implemented
public event EventHandler CanExecuteChanged = (sender, e) => { };