Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MakeMyProject.Services;
- using MakeMyProject.Models;
- using ReactiveUI;
- using System.Reactive;
- using System.Threading;
- using System;
- namespace MakeMyProject.ViewModels;
- public class LoginViewModel : ViewModelBase
- {
- private string _title = "";
- private string _username = "";
- private string _password = "";
- private bool _isError = false;
- private string _errorText = "";
- private bool _success = false;
- private string _successText = "";
- private static readonly string specialCharacters = "!@#$%^&_";
- public LoginViewModel()
- {
- Title = "Login To Get Started";
- IObservable<bool> isEnabled = this.WhenAnyValue(
- x => x.Title,
- x => !string.IsNullOrEmpty(x) && !string.IsNullOrWhiteSpace(x));
- Ok = ReactiveCommand.Create(ReturnUser, isEnabled);
- Cancel = ReactiveCommand.Create(ExitProgram, isEnabled);
- }
- public void ExitProgram() => Environment.Exit(0);
- public UserModel ReturnUser()
- {
- UserModel model = new()
- {
- Username = Username,
- Password = Password
- };
- bool usernameValid = Helpers.ValidateUsername(Username);
- bool createPasswordValid = Helpers.ValidatePassword(Password);
- bool userFound = JsonEngine.GetUser(model);
- if (!usernameValid)
- {
- HandleInvalidInput("Invalid Username: Username Must Be " +
- "At least 8 Characters In Length, " +
- "Have At least 1 lower case letter, " +
- "Have At least 1 upper case letter, " +
- "Have At least 1 number", "un");
- }
- if (!createPasswordValid)
- {
- HandleInvalidInput("Invalid Password: Password Must Be " +
- "At least 8 Characters In Length And " +
- "Have At Least: 1 lower case letter, " +
- "1 upper case letter, 1 number, 1 Special " +
- "Character: " + string.Join(", ", specialCharacters), "pw");
- }
- if (!userFound)
- {
- HandleInvalidInput("Invalid Username/Password. Try again", "nm");
- }
- Success = true;
- SuccessText = "Login Successful";
- Thread.Sleep(3000);
- return model;
- }
- public void HandleInvalidInput(string errorText, string trigger)
- {
- IsError = true;
- ErrorText = errorText;
- Thread.Sleep(3000);
- switch (trigger)
- {
- case "un":
- Username = "";
- break;
- case "pw":
- Password = "";
- break;
- case "nm":
- Username = "";
- Password = "";
- break;
- default:
- throw new Exception("Couldn't Determine Which New User Item To Reset");
- }
- }
- public ReactiveCommand<Unit, UserModel> Ok { get; }
- public ReactiveCommand<Unit, Unit> Cancel { get; }
- public string Title
- {
- get => _title;
- set => this.RaiseAndSetIfChanged(ref _title, value);
- }
- public string Username
- {
- get => _username;
- set => this.RaiseAndSetIfChanged(ref _username, value);
- }
- public string Password
- {
- get => _password;
- set => this.RaiseAndSetIfChanged(ref _password, value);
- }
- public bool IsError
- {
- get => _isError;
- set => this.RaiseAndSetIfChanged(ref _isError, value);
- }
- public string ErrorText
- {
- get => _errorText;
- set => this.RaiseAndSetIfChanged(ref _errorText, value);
- }
- public bool Success
- {
- get => _success;
- set => this.RaiseAndSetIfChanged(ref _success, value);
- }
- public string SuccessText
- {
- get => _successText;
- set => this.RaiseAndSetIfChanged(ref _successText, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment