mekasu0124

Untitled

Feb 24th, 2024
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1. using Avalonia.Threading;
  2. using Diary.Models;
  3. using Diary.Services;
  4. using ReactiveUI;
  5. using System.Reactive;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Diary.ViewModels;
  9.  
  10. public class LoginViewModel : ViewModelBase
  11. {
  12.     private Database _db;
  13.     private Helpers _hp;
  14.     private string _username = "";
  15.     private string _password = "";
  16.     private string _background = "";
  17.     private string _foreground = "";
  18.     private string _border = "";
  19.     private string _fontSize = "";
  20.     private string _fontFamily = "";
  21.     private string _fontStyle = "";
  22.     private string _textAlign = "";
  23.     private User _user = new();
  24.     private StyleModel _userStyles;
  25.  
  26.     private string _errorText = "";
  27.     private bool _isVisible = false;
  28.  
  29.     public LoginViewModel(Database db, Helpers hp, User user)
  30.     {
  31.         _db = db;
  32.         _hp = hp;
  33.         _user = user;
  34.         _userStyles = User.UserSettings.StylesModel;
  35.  
  36.         Background = _userStyles.BackgroundColor;
  37.         Foreground = _userStyles.FontColor;
  38.         Border = _userStyles.BorderColor;
  39.         FontSize = _userStyles.FontSize;
  40.         FontFamily = _userStyles.FontName;
  41.         FontStyle = _userStyles.FontStyle;
  42.         TextAlign = _userStyles.TextAlign;
  43.  
  44.         Login = ReactiveCommand.Create(CheckInputs);
  45.         Exit = ReactiveCommand.Create(() => { });
  46.     }
  47.  
  48.     public User ReturnUser()
  49.     {
  50.         return _hp.GetUser(Username);
  51.     }
  52.  
  53.     public User CheckInputs()
  54.     {
  55.         if (!_hp.ValidateUsername(Username))
  56.         {
  57.             IsVisible = true;
  58.             Background = "#E2EB23";
  59.             ErrorText = "Invalid Username/Password Format. Try Again!";
  60.  
  61.             Task.Delay(3000).ContinueWith((task) =>
  62.             {
  63.                 Dispatcher.UIThread.Invoke(() =>
  64.                 {
  65.                     Background = _userStyles.BackgroundColor;
  66.                     Username = "";
  67.                     Password = "";
  68.                     IsVisible = false;
  69.                 });
  70.             }, TaskScheduler.FromCurrentSynchronizationContext());
  71.         }
  72.  
  73.         if (!_hp.ValidatePassword(Password))
  74.         {
  75.             IsVisible = true;
  76.             Background = "#E2EB23";
  77.             ErrorText = "Invalid Username/Password Format. Try Again!";
  78.             Task.Delay(3000).ContinueWith((task) =>
  79.             {
  80.                 Dispatcher.UIThread.Invoke(() =>
  81.                 {
  82.                     Background = _userStyles.BackgroundColor;
  83.                     Username = "";
  84.                     Password = "";
  85.                     IsVisible = false;
  86.                 });
  87.             });
  88.         }
  89.  
  90.         if (!_db.CheckForUser(Username))
  91.         {
  92.             IsVisible = true;
  93.             Background = "#E2EB23";
  94.             ErrorText = "Invalid Username/Password. Try Again!";
  95.  
  96.             Task.Delay(3000).ContinueWith((task) =>
  97.             {
  98.                 Dispatcher.UIThread.Invoke(() =>
  99.                 {
  100.                     Background = _userStyles.BackgroundColor;
  101.                     Username = "";
  102.                     Password = "";
  103.                     IsVisible = false;
  104.                 });
  105.             });
  106.         }
  107.  
  108.         if (!_db.CheckForPassword(Username, Password))
  109.         {
  110.             IsVisible = true;
  111.             Background = "#E2EB23";
  112.             ErrorText = "Invalid Username/Password. Try Again!";
  113.  
  114.             Task.Delay(3000).ContinueWith((task) =>
  115.             {
  116.                 Dispatcher.UIThread.Invoke(() =>
  117.                 {
  118.                     Background = _userStyles.BackgroundColor;
  119.                     Username = "";
  120.                     Password = "";
  121.                     IsVisible = false;
  122.                 });
  123.             });
  124.         }
  125.  
  126.         User returnUser = _db.GetUser(Username);
  127.  
  128.         return returnUser;
  129.     }
  130.  
  131.     public ReactiveCommand<Unit, User> Login { get; }
  132.     public ReactiveCommand<Unit, Unit> Exit { get; }
  133.  
  134.     #region Getters/Setters
  135.     public string Username
  136.     {
  137.         get => _username;
  138.         set => this.RaiseAndSetIfChanged(ref _username, value);
  139.     }
  140.     public string Password
  141.     {
  142.         get => _password;
  143.         set => this.RaiseAndSetIfChanged(ref _password, value);
  144.     }
  145.     public User User
  146.     {
  147.         get => _user;
  148.         set => this.RaiseAndSetIfChanged(ref _user, value);
  149.     }
  150.     public string Background
  151.     {
  152.         get => _background;
  153.         set => this.RaiseAndSetIfChanged(ref _background, value);
  154.     }
  155.     public string Foreground
  156.     {
  157.         get => _foreground;
  158.         set => this.RaiseAndSetIfChanged(ref _foreground, value);
  159.     }
  160.     public string Border
  161.     {
  162.         get => _border;
  163.         set => this.RaiseAndSetIfChanged(ref _border, value);
  164.     }
  165.     public string FontSize
  166.     {
  167.         get => _fontSize;
  168.         set => this.RaiseAndSetIfChanged(ref _fontSize, value);
  169.     }
  170.     public string FontFamily
  171.     {
  172.         get => _fontFamily;
  173.         set => this.RaiseAndSetIfChanged(ref _fontFamily, value);
  174.     }
  175.     public string FontStyle
  176.     {
  177.         get => _fontStyle;
  178.         set => this.RaiseAndSetIfChanged(ref _fontStyle, value);
  179.     }
  180.     public string TextAlign
  181.     {
  182.         get => _textAlign;
  183.         set => this.RaiseAndSetIfChanged(ref _textAlign, value);
  184.     }
  185.     public bool IsVisible
  186.     {
  187.         get => _isVisible;
  188.         set => this.RaiseAndSetIfChanged(ref _isVisible, value);
  189.     }
  190.     public string ErrorText
  191.     {
  192.         get => _errorText;
  193.         set => this.RaiseAndSetIfChanged(ref _errorText, value);
  194.     }
  195.     #endregion
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment