You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 satır
2.9KB

  1. using System.ComponentModel;
  2. using System.Runtime.CompilerServices;
  3. using System.Windows.Input;
  4. namespace MaUI;
  5. public partial class AuthLogin_VM : INotifyPropertyChanged
  6. {
  7. #region Variables
  8. private readonly ApiService _apiService;
  9. #endregion
  10. #region Propriétés
  11. #region Commands
  12. public ICommand ValiderCommand { get; set; }
  13. public ICommand AnnulerCommand { get; }
  14. #endregion
  15. #region Element de saisie
  16. private LoginModel _selectedItem = null!;
  17. public LoginModel SelectedItem
  18. {
  19. get => _selectedItem;
  20. set
  21. {
  22. _selectedItem = value;
  23. OnPropertyChanged();
  24. //MettreAJourSelection();
  25. }
  26. }
  27. #endregion
  28. #endregion
  29. #region Constructeur
  30. public AuthLogin_VM()
  31. {
  32. SelectedItem = new();
  33. ValiderCommand = new Command(ExecuterValidation);
  34. AnnulerCommand = new Command(ExecuterAnnulation);
  35. _apiService = new ApiService();
  36. }
  37. #endregion
  38. #region Méthodes actions
  39. private async void ExecuterAnnulation()
  40. {
  41. if (Application.Current?.MainPage != null)
  42. {
  43. bool reponse = await Application.Current.MainPage.DisplayAlert(
  44. "Confirmation",
  45. "Voulez-vous vraiment annuler les modifications ?",
  46. "Oui",
  47. "Non");
  48. if (reponse)
  49. {
  50. // Retour à la page précédente
  51. //await Application.Current.MainPage.Navigation.PopAsync();
  52. }
  53. }
  54. }
  55. private async void ExecuterValidation()
  56. {
  57. if (Application.Current?.MainPage != null)
  58. {
  59. if (SelectedItem != null)
  60. {
  61. bool rep = false;
  62. string msg = "";
  63. rep = await _apiService.Connexion(SelectedItem);
  64. if (!rep)
  65. {
  66. msg = "Votre connexion est refusée.";
  67. await Application.Current.MainPage.DisplayAlert("Erreur", msg, "OK");
  68. }
  69. else
  70. {
  71. _selectedItem = new LoginModel();
  72. await Application.Current.MainPage.Navigation.PushAsync(new MainPage());
  73. }
  74. }
  75. }
  76. }
  77. #endregion
  78. #region Implémentation de INotifyPropertyChanged
  79. public event PropertyChangedEventHandler? PropertyChanged;
  80. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  81. {
  82. if (propertyName is null)
  83. return;
  84. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  85. }
  86. #endregion
  87. }
  88. #region Autres classes
  89. public class LoginModel
  90. {
  91. public string Username { get; set; } = string.Empty;
  92. public string Password { get; set; } = string.Empty;
  93. }
  94. public class LoginResponse
  95. {
  96. public string Token { get; set; } = string.Empty;
  97. }
  98. #endregion