Advertisement
Guest User

Untitled

a guest
Apr 5th, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.00 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Reactive;
  4. using Avalonia.Threading;
  5. using ReactiveUI;
  6. using SeedFileGenerator.Models;
  7. using SeedFileGenerator.Services;
  8.  
  9. namespace SeedFileGenerator.ViewModels;
  10.  
  11. public class LoginViewModel : ViewModelBase
  12. {
  13.     private string _title = "";
  14.     private string _username = "";
  15.     private string _password = "";
  16.     private bool _isError = false;
  17.     private string _errorText = "";
  18.     private bool _success = false;
  19.     private string _successText = "";
  20.     private LoginModel _newUser = new();
  21.     private string _usernameToolTip = "";
  22.     private string _passwordToolTip = "";
  23.  
  24.     public LoginViewModel()
  25.     {
  26.         Title = "Login To Continue";
  27.         UsernameToolTip = """
  28.                            Username Must Be At Least 8 Characters In Length,
  29.                            And Must Have At Least 1 Upper Case Letter,
  30.                            1 Lower Case Letter, And 1 Number.
  31.                            """;
  32.         PasswordToolTip = """
  33.                            Password Must Be At Least 8 Characters In Length,
  34.                            And Must Have At Least 1 Upper Case Letter,
  35.                            1 Lower Case Letter, 1 Number, And 1 Special
  36.                            Character Such As: !, @, #, $, %, ^, or _
  37.                            """;
  38.  
  39.         IObservable<bool> isEnabled = this.WhenAnyValue(
  40.             x => x.Title,
  41.             x => !string.IsNullOrEmpty(x) && !string.IsNullOrWhiteSpace(x));
  42.  
  43.         Login = ReactiveCommand.Create(ReturnFoundUser, isEnabled);
  44.         Cancel = ReactiveCommand.Create(ExitProgram, isEnabled);
  45.         CreateNewUser = ReactiveCommand.Create(LaunchNewUser, isEnabled);
  46.         ForgotPassword = ReactiveCommand.Create(LaunchForgotPassword, isEnabled);
  47.     }
  48.  
  49.     public UserModel ReturnFoundUser()
  50.     {
  51.         if (!File.Exists("main.db"))
  52.         {
  53.             return new UserModel();
  54.         }
  55.  
  56.         NewUser = new()
  57.         {
  58.             Username = Username,
  59.             Password = Password
  60.         };
  61.  
  62.         (bool canReturn, UserModel foundUser) = RunChecks();
  63.  
  64.         if (!canReturn)
  65.         {
  66.             return new UserModel();
  67.         }
  68.  
  69.         return new UserModel()
  70.         {
  71.             FirstName = foundUser.FirstName,
  72.             LastName = foundUser.LastName,
  73.             EmailAddress = foundUser.EmailAddress,
  74.             UserLogin = foundUser.UserLogin
  75.         };
  76.     }
  77.  
  78.     public (bool, UserModel) RunChecks()
  79.     {
  80.         bool usernameValid = Helpers.ValidateUsername(Username);
  81.         bool passwordValid = Helpers.ValidatePassword(Password);
  82.         UserModel foundUser = DatabaseEngine.GetUser(NewUser);
  83.  
  84.         if (foundUser == null)
  85.         {
  86.             return (false, new UserModel());
  87.         }
  88.  
  89.         if (foundUser.UserLogin == null)
  90.         {
  91.             return (false, new UserModel());
  92.         }
  93.  
  94.         LoginModel userLogin = foundUser.UserLogin;
  95.  
  96.         if (userLogin.Password == null)
  97.         {
  98.             return (false, new UserModel());
  99.         }
  100.  
  101.         bool confirmPasswordMatch = Helpers.MatchPasswords(Password, userLogin.Password);
  102.  
  103.         if (!usernameValid && !passwordValid && !confirmPasswordMatch)
  104.         {
  105.             return (false, new UserModel());
  106.         }
  107.  
  108.         return (true, foundUser);
  109.     }
  110.  
  111.     public void HandleInvalidInput(string errorText)
  112.     {
  113.         IsError = true;
  114.         ErrorText = errorText;
  115.  
  116.         Username = "";
  117.         Password = "";
  118.  
  119.         int i = 0;
  120.  
  121.         DispatcherTimer timer = new()
  122.         {
  123.             Interval = TimeSpan.FromSeconds(5)
  124.         };
  125.  
  126.         timer.Start();
  127.         timer.Tick += (s, e) =>
  128.         {
  129.             if (i == 6)
  130.             {
  131.                 timer.Stop();
  132.                 IsError = false;
  133.                 ErrorText = "";
  134.             }
  135.             else
  136.             {
  137.                 i++;
  138.             }
  139.         };
  140.     }
  141.  
  142.     internal static UserModel ExitProgram()
  143.     {
  144.         return new UserModel()
  145.         {
  146.             FirstName = "Exit"
  147.         };
  148.     }
  149.  
  150.     internal static UserModel LaunchNewUser()
  151.     {
  152.         return new UserModel()
  153.         {
  154.             FirstName = "NewUser"
  155.         };
  156.     }
  157.  
  158.     internal static UserModel LaunchForgotPassword()
  159.     {
  160.         return new UserModel()
  161.         {
  162.             FirstName = "ForgotPassword"
  163.         };
  164.     }
  165.  
  166.     public ReactiveCommand<Unit, UserModel> Login { get; }
  167.     public ReactiveCommand<Unit, UserModel> Cancel { get; }
  168.     public ReactiveCommand<Unit, UserModel>   CreateNewUser { get; }
  169.     public ReactiveCommand<Unit, UserModel>   ForgotPassword { get; }
  170.  
  171.     public LoginModel NewUser
  172.     {
  173.         get => _newUser;
  174.         set => this.RaiseAndSetIfChanged(ref _newUser, value);
  175.     }
  176.  
  177.     private string Title
  178.     {
  179.         get => _title;
  180.         set => this.RaiseAndSetIfChanged(ref _title, value);
  181.     }
  182.  
  183.     public string Username
  184.     {
  185.         get => _username;
  186.         set => this.RaiseAndSetIfChanged(ref _username, value);
  187.     }
  188.  
  189.     public string Password
  190.     {
  191.         get => _password;
  192.         set => this.RaiseAndSetIfChanged(ref _password, value);
  193.     }
  194.  
  195.     public bool IsError
  196.     {
  197.         get => _isError;
  198.         set => this.RaiseAndSetIfChanged(ref _isError, value);
  199.     }
  200.  
  201.     public string ErrorText
  202.     {
  203.         get => _errorText;
  204.         set => this.RaiseAndSetIfChanged(ref _errorText, value);
  205.     }
  206.  
  207.     public bool Success
  208.     {
  209.         get => _success;
  210.         set => this.RaiseAndSetIfChanged(ref _success, value);
  211.     }
  212.  
  213.     public string SuccessText
  214.     {
  215.         get => _successText;
  216.         set => this.RaiseAndSetIfChanged(ref _successText, value);
  217.     }
  218.  
  219.     public string UsernameToolTip
  220.     {
  221.         get => _usernameToolTip;
  222.         set => this.RaiseAndSetIfChanged(ref _usernameToolTip, value);
  223.     }
  224.  
  225.     public string PasswordToolTip
  226.     {
  227.         get => _passwordToolTip;
  228.         set => this.RaiseAndSetIfChanged(ref _passwordToolTip, value);
  229.     }
  230. }
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement