root / GES_PAC / ViewModel / CreatePersonViewModel.cs @ 5d673ce0
Historique | Voir | Annoter | Télécharger (2,542 ko)
1 | 65ad7e66 | Lucas Bihannic | using GES_PAC.Model; |
---|---|---|---|
2 | using System.Windows.Input; |
||
3 | |||
4 | namespace GES_PAC.ViewModel |
||
5 | { |
||
6 | public class CreatePersonViewModel : BaseViewModel |
||
7 | { |
||
8 | 18f910c6 | Lucas Bihannic | #region Attributs |
9 | 65ad7e66 | Lucas Bihannic | private string _nom; |
10 | 18f910c6 | Lucas Bihannic | private string _prenom; |
11 | private string _email; |
||
12 | private bool _isFormValid; |
||
13 | #endregion |
||
14 | |||
15 | #region Commandes |
||
16 | public ICommand CreatePersonCommand { get; } |
||
17 | #endregion |
||
18 | |||
19 | #region Propriétés |
||
20 | 65ad7e66 | Lucas Bihannic | public string Nom |
21 | { |
||
22 | get => _nom; |
||
23 | set |
||
24 | { |
||
25 | _nom = value; |
||
26 | OnPropertyChanged(); |
||
27 | ValidateForm(); |
||
28 | } |
||
29 | } |
||
30 | 18f910c6 | Lucas Bihannic | |
31 | 65ad7e66 | Lucas Bihannic | public string Prenom |
32 | { |
||
33 | get => _prenom; |
||
34 | set |
||
35 | { |
||
36 | _prenom = value; |
||
37 | OnPropertyChanged(); |
||
38 | ValidateForm(); |
||
39 | } |
||
40 | } |
||
41 | 18f910c6 | Lucas Bihannic | |
42 | 65ad7e66 | Lucas Bihannic | public string Email |
43 | { |
||
44 | get => _email; |
||
45 | set |
||
46 | { |
||
47 | _email = value; |
||
48 | OnPropertyChanged(); |
||
49 | ValidateForm(); |
||
50 | } |
||
51 | } |
||
52 | 18f910c6 | Lucas Bihannic | |
53 | 65ad7e66 | Lucas Bihannic | public bool IsFormValid |
54 | { |
||
55 | get => _isFormValid; |
||
56 | set |
||
57 | { |
||
58 | _isFormValid = value; |
||
59 | OnPropertyChanged(); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public bool NomError { get; private set; } = true; |
||
64 | public bool PrenomError { get; private set; } = true; |
||
65 | public bool EmailError { get; private set; } = true; |
||
66 | 18f910c6 | Lucas Bihannic | #endregion |
67 | 65ad7e66 | Lucas Bihannic | |
68 | 18f910c6 | Lucas Bihannic | #region Constructeurs |
69 | 65ad7e66 | Lucas Bihannic | public CreatePersonViewModel() |
70 | { |
||
71 | CreatePersonCommand = new Command(async () => await CreatePerson()); |
||
72 | } |
||
73 | 18f910c6 | Lucas Bihannic | #endregion |
74 | |||
75 | #region Méthodes |
||
76 | 65ad7e66 | Lucas Bihannic | private async Task CreatePerson() |
77 | { |
||
78 | var newTechnician = new Personne(Nom, Prenom, Email); |
||
79 | PersonViewModel.Instance.Persons.Add(newTechnician); |
||
80 | 18f910c6 | Lucas Bihannic | await Shell.Current.GoToAsync("//Main"); |
81 | 65ad7e66 | Lucas Bihannic | } |
82 | |||
83 | private void ValidateForm() |
||
84 | { |
||
85 | NomError = string.IsNullOrWhiteSpace(Nom) || Nom.Length < 3; |
||
86 | PrenomError = string.IsNullOrWhiteSpace(Prenom) || Prenom.Length < 3; |
||
87 | EmailError = string.IsNullOrWhiteSpace(Email) || Email.Length < 10; |
||
88 | |||
89 | OnPropertyChanged(nameof(NomError)); |
||
90 | OnPropertyChanged(nameof(PrenomError)); |
||
91 | OnPropertyChanged(nameof(EmailError)); |
||
92 | |||
93 | IsFormValid = !NomError && !PrenomError && !EmailError; |
||
94 | |||
95 | (CreatePersonCommand as Command)?.ChangeCanExecute(); |
||
96 | } |
||
97 | 18f910c6 | Lucas Bihannic | #endregion |
98 | 65ad7e66 | Lucas Bihannic | } |
99 | } |