LoginViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using CommunityToolkit.Mvvm.Messaging;
  4. using Terminal.Gui;
  5. namespace CommunityToolkitExample;
  6. internal partial class LoginViewModel : ObservableObject
  7. {
  8. private const string DEFAULT_LOGIN_PROGRESS_MESSAGE = "Press 'Login' to log in.";
  9. private const string INVALID_LOGIN_MESSAGE = "Please enter a valid user name and password.";
  10. private const string LOGGING_IN_PROGRESS_MESSAGE = "Logging in...";
  11. private const string VALID_LOGIN_MESSAGE = "The input is valid!";
  12. [ObservableProperty]
  13. private bool _canLogin;
  14. [ObservableProperty]
  15. private string _loginProgressMessage;
  16. private string _password;
  17. [ObservableProperty]
  18. private string _passwordLengthMessage;
  19. private string _username;
  20. [ObservableProperty]
  21. private string _usernameLengthMessage;
  22. [ObservableProperty]
  23. private ColorScheme? _validationColorScheme;
  24. [ObservableProperty]
  25. private string _validationMessage;
  26. public LoginViewModel ()
  27. {
  28. _loginProgressMessage = string.Empty;
  29. _password = string.Empty;
  30. _passwordLengthMessage = string.Empty;
  31. _username = string.Empty;
  32. _usernameLengthMessage = string.Empty;
  33. _validationMessage = string.Empty;
  34. Username = string.Empty;
  35. Password = string.Empty;
  36. ClearCommand = new (Clear);
  37. LoginCommand = new (Execute);
  38. Clear ();
  39. return;
  40. async void Execute () { await Login (); }
  41. }
  42. public RelayCommand ClearCommand { get; }
  43. public RelayCommand LoginCommand { get; }
  44. public string Password
  45. {
  46. get => _password;
  47. set
  48. {
  49. SetProperty (ref _password, value);
  50. PasswordLengthMessage = $"_Password ({_password.Length} characters):";
  51. ValidateLogin ();
  52. }
  53. }
  54. public string Username
  55. {
  56. get => _username;
  57. set
  58. {
  59. SetProperty (ref _username, value);
  60. UsernameLengthMessage = $"_Username ({_username.Length} characters):";
  61. ValidateLogin ();
  62. }
  63. }
  64. public void Initialized ()
  65. {
  66. Clear ();
  67. }
  68. private void Clear ()
  69. {
  70. Username = string.Empty;
  71. Password = string.Empty;
  72. SendMessage (LoginActions.Clear, DEFAULT_LOGIN_PROGRESS_MESSAGE);
  73. }
  74. private async Task Login ()
  75. {
  76. SendMessage (LoginActions.LoginProgress, LOGGING_IN_PROGRESS_MESSAGE);
  77. await Task.Delay (TimeSpan.FromSeconds (1));
  78. Clear ();
  79. }
  80. private void SendMessage (LoginActions loginAction, string message = "")
  81. {
  82. switch (loginAction)
  83. {
  84. case LoginActions.Clear:
  85. LoginProgressMessage = message;
  86. ValidationMessage = INVALID_LOGIN_MESSAGE;
  87. ValidationColorScheme = Colors.ColorSchemes ["Error"];
  88. break;
  89. case LoginActions.LoginProgress:
  90. LoginProgressMessage = message;
  91. break;
  92. case LoginActions.Validation:
  93. ValidationMessage = CanLogin ? VALID_LOGIN_MESSAGE : INVALID_LOGIN_MESSAGE;
  94. ValidationColorScheme = CanLogin ? Colors.ColorSchemes ["Base"] : Colors.ColorSchemes ["Error"];
  95. break;
  96. }
  97. WeakReferenceMessenger.Default.Send (new Message<LoginActions> { Value = loginAction });
  98. }
  99. private void ValidateLogin ()
  100. {
  101. CanLogin = !string.IsNullOrEmpty (Username) && !string.IsNullOrEmpty (Password);
  102. SendMessage (LoginActions.Validation);
  103. }
  104. }