mekasu0124

Untitled

Mar 16th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 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 passwordValid = Helpers.ValidatePassword(Password);
  45.         bool userFound = JsonEngine.GetUser(model);
  46.  
  47.         if (!usernameValid || !passwordValid)
  48.         {
  49.             HandleInvalidInput("""
  50.                               Invalid Username/Password: Username and Password
  51.                               Must Be: At Least 8 Characters In Length, Have
  52.                               At Least 1 Lower Case Letter, 1 Upper Case Letter,
  53.                               And At least 1 Number. In Addition, Passwords Must
  54.                               Have At Least One Special Character.
  55.                               """, "unpw");
  56.         }
  57.         else if (!userFound)
  58.         {
  59.             HandleInvalidInput("Invalid Username/Password. Try again", "nm");
  60.         }
  61.         else
  62.         {
  63.             Success = true;
  64.             SuccessText = "Login Successful";
  65.  
  66.             Thread.Sleep(3000);
  67.             return model;
  68.         }
  69.     }
  70.  
  71.     public void HandleInvalidInput(string errorText, string trigger)
  72.     {
  73.         IsError = true;
  74.         ErrorText = errorText;
  75.     }
  76.  
  77.     public ReactiveCommand<Unit, UserModel> Ok { get; }
  78.     public ReactiveCommand<Unit, Unit> Cancel { get; }
  79.  
  80.     public string Title
  81.     {
  82.         get => _title;
  83.         set => this.RaiseAndSetIfChanged(ref _title, value);
  84.     }
  85.  
  86.     public string Username
  87.     {
  88.         get => _username;
  89.         set => this.RaiseAndSetIfChanged(ref _username, value);
  90.     }
  91.  
  92.     public string Password
  93.     {
  94.         get => _password;
  95.         set => this.RaiseAndSetIfChanged(ref _password, value);
  96.     }
  97.  
  98.     public bool IsError
  99.     {
  100.         get => _isError;
  101.         set => this.RaiseAndSetIfChanged(ref _isError, value);
  102.     }
  103.  
  104.     public string ErrorText
  105.     {
  106.         get => _errorText;
  107.         set => this.RaiseAndSetIfChanged(ref _errorText, value);
  108.     }
  109.  
  110.     public bool Success
  111.     {
  112.         get => _success;
  113.         set => this.RaiseAndSetIfChanged(ref _success, value);
  114.     }
  115.  
  116.     public string SuccessText
  117.     {
  118.         get => _successText;
  119.         set => this.RaiseAndSetIfChanged(ref _successText, value);
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment