2
0

LoginViewModel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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. public string Username
  49. {
  50. get => _username;
  51. set
  52. {
  53. SetProperty (ref _username, value);
  54. UsernameLengthMessage = $"_Username ({_username.Length} characters):";
  55. ValidateLogin ();
  56. }
  57. }
  58. public void Initialized ()
  59. {
  60. Clear ();
  61. }
  62. private void Clear ()
  63. {
  64. Username = string.Empty;
  65. Password = string.Empty;
  66. SendMessage (LoginAction.Validation);
  67. SendMessage (LoginAction.LoginProgress, DEFAULT_LOGIN_PROGRESS_MESSAGE);
  68. }
  69. private async Task Login ()
  70. {
  71. SendMessage (LoginAction.LoginProgress, LOGGING_IN_PROGRESS_MESSAGE);
  72. await Task.Delay (TimeSpan.FromSeconds (1));
  73. Clear ();
  74. }
  75. private void SendMessage (LoginAction loginAction, string message = "")
  76. {
  77. switch (loginAction)
  78. {
  79. case LoginAction.LoginProgress:
  80. LoginProgressMessage = message;
  81. break;
  82. case LoginAction.Validation:
  83. ValidationMessage = CanLogin ? VALID_LOGIN_MESSAGE : INVALID_LOGIN_MESSAGE;
  84. ValidationColorScheme = CanLogin ? Colors.ColorSchemes ["Base"] : Colors.ColorSchemes ["Error"];
  85. break;
  86. }
  87. WeakReferenceMessenger.Default.Send (new Message<LoginAction> { Value = loginAction });
  88. }
  89. private void ValidateLogin ()
  90. {
  91. CanLogin = !string.IsNullOrEmpty (Username) && !string.IsNullOrEmpty (Password);
  92. SendMessage (LoginAction.Validation);
  93. }
  94. }