root / GES_PAC / ViewModel / CreatePlaceViewModel.cs @ 0a91fd76
Historique | Voir | Annoter | Télécharger (2,037 ko)
1 | 65ad7e66 | Lucas Bihannic | using GES_PAC.Model; |
---|---|---|---|
2 | using System.Windows.Input; |
||
3 | |||
4 | namespace GES_PAC.ViewModel |
||
5 | { |
||
6 | public class CreatePlaceViewModel : BaseViewModel |
||
7 | { |
||
8 | private string _nom; |
||
9 | public string Nom |
||
10 | { |
||
11 | get => _nom; |
||
12 | set |
||
13 | { |
||
14 | _nom = value; |
||
15 | OnPropertyChanged(); |
||
16 | ValidateForm(); |
||
17 | } |
||
18 | } |
||
19 | private string _client; |
||
20 | public string Client |
||
21 | { |
||
22 | get => _client; |
||
23 | set |
||
24 | { |
||
25 | _client = value; |
||
26 | OnPropertyChanged(); |
||
27 | ValidateForm(); |
||
28 | } |
||
29 | } |
||
30 | private bool _isFormValid; |
||
31 | public bool IsFormValid |
||
32 | { |
||
33 | get => _isFormValid; |
||
34 | set |
||
35 | { |
||
36 | _isFormValid = value; |
||
37 | OnPropertyChanged(); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | public bool NomError { get; private set; } = true; |
||
42 | public bool ClientError { get; private set; } = true; |
||
43 | |||
44 | public ICommand ReturnCommand { get; } |
||
45 | public ICommand CreatePlaceCommand { get; } |
||
46 | |||
47 | public CreatePlaceViewModel() |
||
48 | { |
||
49 | ReturnCommand = new Command(async () => await Return()); |
||
50 | CreatePlaceCommand = new Command(async () => await CreatePlace()); |
||
51 | } |
||
52 | |||
53 | private void ValidateForm() |
||
54 | { |
||
55 | NomError = string.IsNullOrWhiteSpace(Nom) || Nom.Length < 3; |
||
56 | ClientError = string.IsNullOrWhiteSpace(Client) || Client.Length < 3; |
||
57 | |||
58 | OnPropertyChanged(nameof(NomError)); |
||
59 | OnPropertyChanged(nameof(ClientError)); |
||
60 | |||
61 | IsFormValid = !NomError && !ClientError; |
||
62 | (CreatePlaceCommand as Command)?.ChangeCanExecute(); |
||
63 | } |
||
64 | private async Task Return() |
||
65 | { |
||
66 | await Shell.Current.GoToAsync(".."); |
||
67 | } |
||
68 | private async Task CreatePlace() |
||
69 | { |
||
70 | var newPlace = new Lieu(Nom, Client); |
||
71 | PlaceViewModel.Instance.Places.Add(newPlace); |
||
72 | await Shell.Current.GoToAsync(".."); |
||
73 | } |
||
74 | |||
75 | } |
||
76 | } |