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