mekasu0124

Untitled

Mar 16th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.06 KB | None | 0 0
  1. using MakeMyProject.Services;
  2. using MakeMyProject.Models;
  3. using ReactiveUI;
  4. using System.Reactive;
  5. using System.Threading;
  6. using System;
  7.  
  8. namespace MakeMyProject.ViewModels;
  9.  
  10. public class LoginViewModel : ViewModelBase
  11. {
  12.     private string _title = "";
  13.     private string _username = "";
  14.     private string _password = "";
  15.     private bool _isError = false;
  16.     private string _errorText = "";
  17.     private bool _success = false;
  18.     private string _successText = "";
  19.     private static readonly string specialCharacters = "!@#$%^&_";
  20.  
  21.     public LoginViewModel()
  22.     {
  23.         Title = "Login To Get Started";
  24.  
  25.         IObservable<bool> isEnabled = this.WhenAnyValue(
  26.             x => x.Title,
  27.             x => !string.IsNullOrEmpty(x) && !string.IsNullOrWhiteSpace(x));
  28.  
  29.         Ok = ReactiveCommand.Create(ReturnUser, isEnabled);
  30.         Cancel = ReactiveCommand.Create(ExitProgram, isEnabled);
  31.     }
  32.  
  33.     public void ExitProgram() => Environment.Exit(0);
  34.  
  35.     public UserModel ReturnUser()
  36.     {
  37.         UserModel model = new()
  38.         {
  39.             Username = Username,
  40.             Password = Password
  41.         };
  42.  
  43.         bool usernameValid = Helpers.ValidateUsername(Username);
  44.         bool createPasswordValid = Helpers.ValidatePassword(Password);
  45.         bool userFound = JsonEngine.GetUser(model);
  46.  
  47.         if (!usernameValid)
  48.         {
  49.             HandleInvalidInput("Invalid Username: Username Must Be " +
  50.                                 "At least 8 Characters In Length, " +
  51.                                 "Have At least 1 lower case letter, " +
  52.                                 "Have At least 1 upper case letter, " +
  53.                                 "Have At least 1 number", "un");
  54.         }
  55.  
  56.         if (!createPasswordValid)
  57.         {
  58.             HandleInvalidInput("Invalid Password: Password Must Be " +
  59.                                 "At least 8 Characters In Length And " +
  60.                                 "Have At Least: 1 lower case letter, " +
  61.                                 "1 upper case letter, 1 number, 1 Special " +
  62.                                 "Character: " + string.Join(", ", specialCharacters), "pw");
  63.         }
  64.  
  65.         if (!userFound)
  66.         {
  67.             HandleInvalidInput("Invalid Username/Password. Try again", "nm");
  68.         }
  69.  
  70.         Success = true;
  71.         SuccessText = "Login Successful";
  72.  
  73.         Thread.Sleep(3000);
  74.         return model;
  75.     }
  76.  
  77.     public void HandleInvalidInput(string errorText, string trigger)
  78.     {
  79.         IsError = true;
  80.         ErrorText = errorText;
  81.        
  82.         Thread.Sleep(3000);
  83.  
  84.         switch (trigger)
  85.         {
  86.             case "un":
  87.                 Username = "";
  88.                 break;
  89.  
  90.             case "pw":
  91.                 Password = "";
  92.                 break;
  93.  
  94.             case "nm":
  95.                 Username = "";
  96.                 Password = "";
  97.                 break;
  98.  
  99.             default:
  100.                 throw new Exception("Couldn't Determine Which New User Item To Reset");
  101.         }
  102.     }
  103.  
  104.     public ReactiveCommand<Unit, UserModel> Ok { get; }
  105.     public ReactiveCommand<Unit, Unit> Cancel { get; }
  106.  
  107.     public string Title
  108.     {
  109.         get => _title;
  110.         set => this.RaiseAndSetIfChanged(ref _title, value);
  111.     }
  112.  
  113.     public string Username
  114.     {
  115.         get => _username;
  116.         set => this.RaiseAndSetIfChanged(ref _username, value);
  117.     }
  118.  
  119.     public string Password
  120.     {
  121.         get => _password;
  122.         set => this.RaiseAndSetIfChanged(ref _password, value);
  123.     }
  124.  
  125.     public bool IsError
  126.     {
  127.         get => _isError;
  128.         set => this.RaiseAndSetIfChanged(ref _isError, value);
  129.     }
  130.  
  131.     public string ErrorText
  132.     {
  133.         get => _errorText;
  134.         set => this.RaiseAndSetIfChanged(ref _errorText, value);
  135.     }
  136.  
  137.     public bool Success
  138.     {
  139.         get => _success;
  140.         set => this.RaiseAndSetIfChanged(ref _success, value);
  141.     }
  142.  
  143.     public string SuccessText
  144.     {
  145.         get => _successText;
  146.         set => this.RaiseAndSetIfChanged(ref _successText, value);
  147.     }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment