2
0

LoginViewModel.cs 3.1 KB

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