Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Avalonia.Threading;
- using Diary.Models;
- using Diary.Services;
- using ReactiveUI;
- using System.Reactive;
- using System.Threading.Tasks;
- namespace Diary.ViewModels;
- public class LoginViewModel : ViewModelBase
- {
- private Database _db;
- private Helpers _hp;
- private GithubService _gvs;
- private string _username = "";
- private string _password = "";
- private string _background = "";
- private string _foreground = "";
- private string _border = "";
- private string _fontSize = "";
- private string _fontFamily = "";
- private string _fontStyle = "";
- private string _textAlign = "";
- private User _user = new();
- private StyleModel _userStyles = new();
- private string _errorText = "";
- private bool _isVisible = false;
- public LoginViewModel()
- {
- _db = new Database();
- _gvs = new GithubService();
- _hp = new Helpers(_db, _gvs);
- _user = new User();
- SetupUi();
- }
- public LoginViewModel(Database db, Helpers hp, User user)
- {
- _db = db;
- _hp = hp;
- _user = user;
- Login = ReactiveCommand.Create(CheckInputs);
- Exit = ReactiveCommand.Create(() => { });
- SetupUi();
- }
- public void SetupUi()
- {
- _userStyles = User.UserSettings.StylesModel;
- Background = _userStyles.BackgroundColor;
- Foreground = _userStyles.FontColor;
- Border = _userStyles.BorderColor;
- FontSize = _userStyles.FontSize;
- FontFamily = _userStyles.FontName;
- FontStyle = _userStyles.FontStyle;
- TextAlign = _userStyles.TextAlign;
- }
- public User CheckInputs()
- {
- if (!_hp.ValidateUsername(Username))
- {
- IsVisible = true;
- Background = "#E2EB23";
- ErrorText = "Invalid Username/Password Format. Try Again!";
- Task.Delay(3000).ContinueWith((task) =>
- {
- Dispatcher.UIThread.Invoke(() =>
- {
- Background = _userStyles.BackgroundColor;
- Username = "";
- Password = "";
- IsVisible = false;
- });
- }, TaskScheduler.FromCurrentSynchronizationContext());
- }
- if (!_hp.ValidatePassword(Password))
- {
- IsVisible = true;
- Background = "#E2EB23";
- ErrorText = "Invalid Username/Password Format. Try Again!";
- Task.Delay(3000).ContinueWith((task) =>
- {
- Dispatcher.UIThread.Invoke(() =>
- {
- Background = _userStyles.BackgroundColor;
- Username = "";
- Password = "";
- IsVisible = false;
- });
- });
- }
- if (!_db.CheckForUser(Username))
- {
- IsVisible = true;
- Background = "#E2EB23";
- ErrorText = "Invalid Username/Password. Try Again!";
- Task.Delay(3000).ContinueWith((task) =>
- {
- Dispatcher.UIThread.Invoke(() =>
- {
- Background = _userStyles.BackgroundColor;
- Username = "";
- Password = "";
- IsVisible = false;
- });
- });
- }
- if (!_db.CheckForPassword(Username, Password))
- {
- IsVisible = true;
- Background = "#E2EB23";
- ErrorText = "Invalid Username/Password. Try Again!";
- Task.Delay(3000).ContinueWith((task) =>
- {
- Dispatcher.UIThread.Invoke(() =>
- {
- Background = _userStyles.BackgroundColor;
- Username = "";
- Password = "";
- IsVisible = false;
- });
- });
- }
- User returnUser = _db.GetUser(Username);
- return returnUser;
- }
- public ReactiveCommand<Unit, User> Login { get; }
- public ReactiveCommand<Unit, Unit> Exit { get; }
- #region Getters/Setters
- public string Username
- {
- get => _username;
- set => this.RaiseAndSetIfChanged(ref _username, value);
- }
- public string Password
- {
- get => _password;
- set => this.RaiseAndSetIfChanged(ref _password, value);
- }
- public User User
- {
- get => _user;
- set => this.RaiseAndSetIfChanged(ref _user, value);
- }
- public string Background
- {
- get => _background;
- set => this.RaiseAndSetIfChanged(ref _background, value);
- }
- public string Foreground
- {
- get => _foreground;
- set => this.RaiseAndSetIfChanged(ref _foreground, value);
- }
- public string Border
- {
- get => _border;
- set => this.RaiseAndSetIfChanged(ref _border, value);
- }
- public string FontSize
- {
- get => _fontSize;
- set => this.RaiseAndSetIfChanged(ref _fontSize, value);
- }
- public string FontFamily
- {
- get => _fontFamily;
- set => this.RaiseAndSetIfChanged(ref _fontFamily, value);
- }
- public string FontStyle
- {
- get => _fontStyle;
- set => this.RaiseAndSetIfChanged(ref _fontStyle, value);
- }
- public string TextAlign
- {
- get => _textAlign;
- set => this.RaiseAndSetIfChanged(ref _textAlign, value);
- }
- public bool IsVisible
- {
- get => _isVisible;
- set => this.RaiseAndSetIfChanged(ref _isVisible, value);
- }
- public string ErrorText
- {
- get => _errorText;
- set => this.RaiseAndSetIfChanged(ref _errorText, value);
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment