root / GES_PAC / ViewModel / CreateCalibrationViewModel.cs @ 42456640
Historique | Voir | Annoter | Télécharger (6,542 ko)
1 | 5d673ce0 | 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 CreateCalibrationViewModel : BaseViewModel |
||
9 | { |
||
10 | |||
11 | #region Attributs |
||
12 | private TypeCalibration _selectedType; |
||
13 | private double? _conc_o2; |
||
14 | private double? _conc_co2; |
||
15 | private double? _conc_ch4; |
||
16 | 09d4a0de | lbihannic | private string _refBouteille; |
17 | private bool _isRefBouteilleVisible; |
||
18 | 957b1f34 | lbihannic | private Calibration _lastCalibration; |
19 | private bool _hasLastMeasure; |
||
20 | private double? _lastO2; |
||
21 | private double? _lastCO2; |
||
22 | private double? _lastCH4; |
||
23 | 5d673ce0 | Lucas Bihannic | private bool _isFormValid; |
24 | #endregion |
||
25 | |||
26 | #region Commandes |
||
27 | public ICommand CreateCalibrationCommand { get; } |
||
28 | #endregion |
||
29 | |||
30 | #region Propriétés |
||
31 | public ObservableCollection<TypeCalibration> TypeCalibrations { get; } |
||
32 | public TypeCalibration SelectedType |
||
33 | { |
||
34 | get => _selectedType; |
||
35 | set |
||
36 | { |
||
37 | _selectedType = value; |
||
38 | 09d4a0de | lbihannic | IsRefBouteilleVisible = _selectedType.Name != "Air"; |
39 | 5d673ce0 | Lucas Bihannic | OnPropertyChanged(); |
40 | 957b1f34 | lbihannic | OnTypeChanged(); |
41 | 5d673ce0 | Lucas Bihannic | ValidateForm(); |
42 | } |
||
43 | } |
||
44 | |||
45 | public double? ConcO2 |
||
46 | { |
||
47 | get => _conc_o2; |
||
48 | set |
||
49 | { |
||
50 | _conc_o2 = value; |
||
51 | OnPropertyChanged(); |
||
52 | ValidateForm(); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public double? ConcCO2 |
||
57 | { |
||
58 | get => _conc_co2; |
||
59 | set |
||
60 | { |
||
61 | _conc_co2 = value; |
||
62 | OnPropertyChanged(); |
||
63 | ValidateForm(); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public double? ConcCH4 |
||
68 | { |
||
69 | get => _conc_ch4; |
||
70 | set |
||
71 | { |
||
72 | _conc_ch4 = value; |
||
73 | OnPropertyChanged(); |
||
74 | ValidateForm(); |
||
75 | } |
||
76 | } |
||
77 | public string RefBouteille |
||
78 | { |
||
79 | 09d4a0de | lbihannic | get => _refBouteille; |
80 | 5d673ce0 | Lucas Bihannic | set |
81 | { |
||
82 | 09d4a0de | lbihannic | _refBouteille = value; |
83 | 5d673ce0 | Lucas Bihannic | OnPropertyChanged(); |
84 | ValidateForm(); |
||
85 | } |
||
86 | } |
||
87 | 09d4a0de | lbihannic | public bool IsRefBouteilleVisible |
88 | { |
||
89 | get => _isRefBouteilleVisible; |
||
90 | set |
||
91 | { |
||
92 | _isRefBouteilleVisible = value; |
||
93 | OnPropertyChanged(); |
||
94 | } |
||
95 | } |
||
96 | 957b1f34 | lbihannic | public Calibration LastCalibration |
97 | { |
||
98 | get => _lastCalibration; |
||
99 | set |
||
100 | { |
||
101 | _lastCalibration = value; |
||
102 | OnPropertyChanged(); |
||
103 | } |
||
104 | } |
||
105 | public bool HasLastMeasure |
||
106 | { |
||
107 | get => _hasLastMeasure; |
||
108 | set |
||
109 | { |
||
110 | _hasLastMeasure = value; |
||
111 | OnPropertyChanged(); |
||
112 | } |
||
113 | } |
||
114 | public double? LastO2 |
||
115 | { |
||
116 | get => _lastO2; |
||
117 | set |
||
118 | { |
||
119 | _lastO2 = value; |
||
120 | OnPropertyChanged(); |
||
121 | } |
||
122 | } |
||
123 | public double? LastCO2 |
||
124 | { |
||
125 | get => _lastCO2; |
||
126 | set |
||
127 | { |
||
128 | _lastCO2 = value; |
||
129 | OnPropertyChanged(); |
||
130 | } |
||
131 | } |
||
132 | public double? LastCH4 |
||
133 | { |
||
134 | get => _lastCH4; |
||
135 | set |
||
136 | { |
||
137 | _lastCH4 = value; |
||
138 | OnPropertyChanged(); |
||
139 | } |
||
140 | } |
||
141 | 09d4a0de | lbihannic | |
142 | 5d673ce0 | Lucas Bihannic | public bool IsFormValid |
143 | { |
||
144 | get => _isFormValid; |
||
145 | set |
||
146 | { |
||
147 | _isFormValid = value; |
||
148 | OnPropertyChanged(); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | public bool TypeError { get; private set; } = true; |
||
153 | public bool ConcO2Error { get; private set; } = true; |
||
154 | public bool ConcCO2Error { get; private set; } = true; |
||
155 | public bool ConcCH4Error { get; private set; } = true; |
||
156 | 09d4a0de | lbihannic | public bool RefBouteilleError { get; private set; } = true; |
157 | 5d673ce0 | Lucas Bihannic | #endregion |
158 | |||
159 | #region Constructeurs |
||
160 | public CreateCalibrationViewModel() |
||
161 | { |
||
162 | 957b1f34 | lbihannic | CreateCalibrationCommand = new Command(async () => await CreateCalibration()); |
163 | 5d673ce0 | Lucas Bihannic | |
164 | var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay(); |
||
165 | var typesManquants = journeeActuelle.GetCurrentMissingCalibration(); |
||
166 | |||
167 | TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants); |
||
168 | SelectedType = TypeCalibrations[0]; |
||
169 | |||
170 | 957b1f34 | lbihannic | if (journeeActuelle.GetCurrentPhase() == PhaseCalibration.Fin) |
171 | { |
||
172 | LastCalibration = journeeActuelle.GetDebutCalibration(); |
||
173 | HasLastMeasure = true; |
||
174 | } |
||
175 | 5d673ce0 | Lucas Bihannic | } |
176 | #endregion |
||
177 | |||
178 | #region Méthodes |
||
179 | private async Task CreateCalibration() |
||
180 | { |
||
181 | 1019554c | lbihannic | if (IsBusy) return; |
182 | 5d673ce0 | Lucas Bihannic | IsBusy = true; |
183 | |||
184 | var date = DateTime.Now; |
||
185 | var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille); |
||
186 | 09d4a0de | lbihannic | var phaseCalibration = JourneeViewModel.Instance.GetCurrentPhase(); |
187 | var isComplete = JourneeViewModel.Instance.GetCurrentDay().AddCalibration(newCalib, phaseCalibration); |
||
188 | 5d673ce0 | Lucas Bihannic | |
189 | 957b1f34 | lbihannic | var targetView = nameof(CreateCalibrationView); |
190 | if (isComplete) { |
||
191 | targetView = phaseCalibration == PhaseCalibration.Debut ? nameof(SetListView) : nameof(MainView); |
||
192 | 5d673ce0 | Lucas Bihannic | } |
193 | 957b1f34 | lbihannic | await Shell.Current.GoToAsync(targetView); |
194 | 5d673ce0 | Lucas Bihannic | |
195 | IsBusy = false; |
||
196 | } |
||
197 | |||
198 | private void ValidateForm() |
||
199 | { |
||
200 | TypeError = SelectedType == null; |
||
201 | ConcO2Error = ConcO2 == null; |
||
202 | ConcCO2Error = ConcCO2 == null; |
||
203 | ConcCH4Error = ConcCH4 == null; |
||
204 | 09d4a0de | lbihannic | RefBouteilleError = IsRefBouteilleVisible && (RefBouteille == null || RefBouteille.Length < 5); |
205 | 5d673ce0 | Lucas Bihannic | |
206 | OnPropertyChanged(nameof(TypeError)); |
||
207 | OnPropertyChanged(nameof(ConcO2Error)); |
||
208 | OnPropertyChanged(nameof(ConcCO2Error)); |
||
209 | OnPropertyChanged(nameof(ConcCH4Error)); |
||
210 | 09d4a0de | lbihannic | OnPropertyChanged(nameof(RefBouteilleError)); |
211 | 5d673ce0 | Lucas Bihannic | |
212 | 09d4a0de | lbihannic | IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error && !RefBouteilleError; |
213 | 5d673ce0 | Lucas Bihannic | |
214 | (CreateCalibrationCommand as Command)?.ChangeCanExecute(); |
||
215 | } |
||
216 | 957b1f34 | lbihannic | |
217 | private void OnTypeChanged() |
||
218 | { |
||
219 | if (!HasLastMeasure) |
||
220 | return; |
||
221 | var mesureCalibration = LastCalibration.getMesureCalibrationByType(SelectedType); |
||
222 | LastCH4 = mesureCalibration.Conc_CH4; |
||
223 | LastO2 = mesureCalibration.Conc_O2; |
||
224 | LastCO2 = mesureCalibration.Conc_CO2; |
||
225 | } |
||
226 | 5d673ce0 | Lucas Bihannic | #endregion |
227 | } |
||
228 | } |