Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Reactive;
- using Avalonia.Threading;
- using ReactiveUI;
- using SeedFileGenerator.Models;
- using SeedFileGenerator.Services;
- namespace SeedFileGenerator.ViewModels;
- public class LoginViewModel : ViewModelBase
- {
- private string _title = "";
- private string _username = "";
- private string _password = "";
- private bool _isError = false;
- private string _errorText = "";
- private bool _success = false;
- private string _successText = "";
- private LoginModel _newUser = new();
- private string _usernameToolTip = "";
- private string _passwordToolTip = "";
- public LoginViewModel()
- {
- Title = "Login To Continue";
- UsernameToolTip = """
- Username Must Be At Least 8 Characters In Length,
- And Must Have At Least 1 Upper Case Letter,
- 1 Lower Case Letter, And 1 Number.
- """;
- PasswordToolTip = """
- Password Must Be At Least 8 Characters In Length,
- And Must Have At Least 1 Upper Case Letter,
- 1 Lower Case Letter, 1 Number, And 1 Special
- Character Such As: !, @, #, $, %, ^, or _
- """;
- IObservable<bool> isEnabled = this.WhenAnyValue(
- x => x.Title,
- x => !string.IsNullOrEmpty(x) && !string.IsNullOrWhiteSpace(x));
- Login = ReactiveCommand.Create(ReturnFoundUser, isEnabled);
- Cancel = ReactiveCommand.Create(ExitProgram, isEnabled);
- CreateNewUser = ReactiveCommand.Create(LaunchNewUser, isEnabled);
- ForgotPassword = ReactiveCommand.Create(LaunchForgotPassword, isEnabled);
- }
- public UserModel ReturnFoundUser()
- {
- if (!File.Exists("main.db"))
- {
- return new UserModel();
- }
- NewUser = new()
- {
- Username = Username,
- Password = Password
- };
- (bool canReturn, UserModel foundUser) = RunChecks();
- if (!canReturn)
- {
- return new UserModel();
- }
- return new UserModel()
- {
- FirstName = foundUser.FirstName,
- LastName = foundUser.LastName,
- EmailAddress = foundUser.EmailAddress,
- UserLogin = foundUser.UserLogin
- };
- }
- public (bool, UserModel) RunChecks()
- {
- bool usernameValid = Helpers.ValidateUsername(Username);
- bool passwordValid = Helpers.ValidatePassword(Password);
- UserModel foundUser = DatabaseEngine.GetUser(NewUser);
- if (foundUser == null)
- {
- return (false, new UserModel());
- }
- if (foundUser.UserLogin == null)
- {
- return (false, new UserModel());
- }
- LoginModel userLogin = foundUser.UserLogin;
- if (userLogin.Password == null)
- {
- return (false, new UserModel());
- }
- bool confirmPasswordMatch = Helpers.MatchPasswords(Password, userLogin.Password);
- if (!usernameValid && !passwordValid && !confirmPasswordMatch)
- {
- return (false, new UserModel());
- }
- return (true, foundUser);
- }
- public void HandleInvalidInput(string errorText)
- {
- IsError = true;
- ErrorText = errorText;
- Username = "";
- Password = "";
- int i = 0;
- DispatcherTimer timer = new()
- {
- Interval = TimeSpan.FromSeconds(5)
- };
- timer.Start();
- timer.Tick += (s, e) =>
- {
- if (i == 6)
- {
- timer.Stop();
- IsError = false;
- ErrorText = "";
- }
- else
- {
- i++;
- }
- };
- }
- internal static UserModel ExitProgram()
- {
- return new UserModel()
- {
- FirstName = "Exit"
- };
- }
- internal static UserModel LaunchNewUser()
- {
- return new UserModel()
- {
- FirstName = "NewUser"
- };
- }
- internal static UserModel LaunchForgotPassword()
- {
- return new UserModel()
- {
- FirstName = "ForgotPassword"
- };
- }
- public ReactiveCommand<Unit, UserModel> Login { get; }
- public ReactiveCommand<Unit, UserModel> Cancel { get; }
- public ReactiveCommand<Unit, UserModel> CreateNewUser { get; }
- public ReactiveCommand<Unit, UserModel> ForgotPassword { get; }
- public LoginModel NewUser
- {
- get => _newUser;
- set => this.RaiseAndSetIfChanged(ref _newUser, value);
- }
- private string Title
- {
- get => _title;
- set => this.RaiseAndSetIfChanged(ref _title, value);
- }
- public string Username
- {
- get => _username;
- set => this.RaiseAndSetIfChanged(ref _username, value);
- }
- public string Password
- {
- get => _password;
- set => this.RaiseAndSetIfChanged(ref _password, value);
- }
- public bool IsError
- {
- get => _isError;
- set => this.RaiseAndSetIfChanged(ref _isError, value);
- }
- public string ErrorText
- {
- get => _errorText;
- set => this.RaiseAndSetIfChanged(ref _errorText, value);
- }
- public bool Success
- {
- get => _success;
- set => this.RaiseAndSetIfChanged(ref _success, value);
- }
- public string SuccessText
- {
- get => _successText;
- set => this.RaiseAndSetIfChanged(ref _successText, value);
- }
- public string UsernameToolTip
- {
- get => _usernameToolTip;
- set => this.RaiseAndSetIfChanged(ref _usernameToolTip, value);
- }
- public string PasswordToolTip
- {
- get => _passwordToolTip;
- set => this.RaiseAndSetIfChanged(ref _passwordToolTip, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement