mekasu0124

Untitled

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