root / GES_PAC / ViewModel / CreateDayViewModel.cs @ 4afea73d
Historique | Voir | Annoter | Télécharger (3,315 ko)
1 | 65ad7e66 | Lucas Bihannic | using GES_PAC.Model; |
---|---|---|---|
2 | using GES_PAC.View; |
||
3 | using System.Collections.ObjectModel; |
||
4 | using System.Windows.Input; |
||
5 | |||
6 | namespace GES_PAC.ViewModel |
||
7 | { |
||
8 | public class CreateDayViewModel : BaseViewModel |
||
9 | { |
||
10 | |||
11 | 18f910c6 | Lucas Bihannic | #region Attributs |
12 | 65ad7e66 | Lucas Bihannic | private Personne _selectedPerson; |
13 | 18f910c6 | Lucas Bihannic | private string _regime; |
14 | private bool _isFormValid; |
||
15 | #endregion |
||
16 | |||
17 | #region Commandes |
||
18 | public ICommand CreateDayCommand { get; } |
||
19 | public ICommand AddPersonCommand { get; } |
||
20 | public ICommand AddPlaceCommand { get; } |
||
21 | #endregion |
||
22 | |||
23 | #region Propriétés |
||
24 | public ObservableCollection<Personne> Persons => PersonViewModel.Instance.Persons; |
||
25 | 65ad7e66 | Lucas Bihannic | public Personne SelectedPerson |
26 | { |
||
27 | get => _selectedPerson; |
||
28 | set |
||
29 | { |
||
30 | _selectedPerson = value; |
||
31 | OnPropertyChanged(); |
||
32 | ValidateForm(); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | public ObservableCollection<Lieu> Places => PlaceViewModel.Instance.Places; |
||
37 | |||
38 | private Lieu _selectedPlace; |
||
39 | public Lieu SelectedPlace |
||
40 | { |
||
41 | get => _selectedPlace; |
||
42 | set { |
||
43 | _selectedPlace = value; |
||
44 | OnPropertyChanged(); |
||
45 | ValidateForm(); |
||
46 | } |
||
47 | } |
||
48 | 18f910c6 | Lucas Bihannic | |
49 | 65ad7e66 | Lucas Bihannic | public string Regime |
50 | { |
||
51 | get => _regime; |
||
52 | set |
||
53 | { |
||
54 | _regime = value; |
||
55 | OnPropertyChanged(); |
||
56 | ValidateForm(); |
||
57 | } |
||
58 | } |
||
59 | 18f910c6 | Lucas Bihannic | |
60 | 65ad7e66 | Lucas Bihannic | public bool IsFormValid |
61 | { |
||
62 | get => _isFormValid; |
||
63 | set |
||
64 | { |
||
65 | _isFormValid = value; |
||
66 | OnPropertyChanged(); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | public bool PlaceError { get; private set; } = true; |
||
71 | public bool PersonError { get; private set; } = true; |
||
72 | public bool RegimeError { get; private set; } = true; |
||
73 | 18f910c6 | Lucas Bihannic | #endregion |
74 | 65ad7e66 | Lucas Bihannic | |
75 | 18f910c6 | Lucas Bihannic | #region Constructeurs |
76 | 65ad7e66 | Lucas Bihannic | public CreateDayViewModel() |
77 | { |
||
78 | CreateDayCommand = new Command(async () => await CreateDay()); |
||
79 | AddPersonCommand = new Command(async () => await AddPerson()); |
||
80 | AddPlaceCommand = new Command(async () => await AddPlace()); |
||
81 | } |
||
82 | 18f910c6 | Lucas Bihannic | #endregion |
83 | 65ad7e66 | Lucas Bihannic | |
84 | 18f910c6 | Lucas Bihannic | #region Méthodes |
85 | 65ad7e66 | Lucas Bihannic | private async Task AddPerson() => await Shell.Current.GoToAsync(nameof(CreatePersonView)); |
86 | private async Task AddPlace() => await Shell.Current.GoToAsync(nameof(CreatePlaceView)); |
||
87 | private async Task CreateDay() |
||
88 | { |
||
89 | 18f910c6 | Lucas Bihannic | IsBusy = true; |
90 | 65ad7e66 | Lucas Bihannic | var date = DateTime.Now; |
91 | 5d673ce0 | Lucas Bihannic | var newDay = new Journee(date, SelectedPerson, SelectedPlace, Regime); |
92 | 65ad7e66 | Lucas Bihannic | JourneeViewModel.Instance.Journees.Add(newDay); |
93 | 5d673ce0 | Lucas Bihannic | await Shell.Current.GoToAsync(nameof(CreateCalibrationView)); |
94 | 18f910c6 | Lucas Bihannic | IsBusy = false; |
95 | 65ad7e66 | Lucas Bihannic | } |
96 | |||
97 | private void ValidateForm() |
||
98 | { |
||
99 | PersonError = SelectedPerson == null; |
||
100 | PlaceError = SelectedPlace == null; |
||
101 | RegimeError = string.IsNullOrWhiteSpace(Regime) || Regime.Length < 3; |
||
102 | |||
103 | OnPropertyChanged(nameof(PlaceError)); |
||
104 | OnPropertyChanged(nameof(PersonError)); |
||
105 | OnPropertyChanged(nameof(RegimeError)); |
||
106 | |||
107 | 5d673ce0 | Lucas Bihannic | IsFormValid = !PlaceError && !PersonError && !RegimeError; |
108 | 65ad7e66 | Lucas Bihannic | |
109 | (CreateDayCommand as Command)?.ChangeCanExecute(); |
||
110 | } |
||
111 | 18f910c6 | Lucas Bihannic | #endregion |
112 | 65ad7e66 | Lucas Bihannic | } |
113 | } |