Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateCalibrationViewModel.cs @ 6f451cc1

Historique | Voir | Annoter | Télécharger (7,862 ko)

1 5d673ce0 Lucas Bihannic
using GES_PAC.Model;
2
using GES_PAC.View;
3 6f451cc1 lbihannic
using Microsoft.EntityFrameworkCore;
4 5d673ce0 Lucas Bihannic
using System.Collections.ObjectModel;
5
using System.Windows.Input;
6
7
namespace GES_PAC.ViewModel
8
{
9 9601eaf0 lbihannic
    [QueryProperty(nameof(PhaseName), "phase")]
10 5d673ce0 Lucas Bihannic
    public class CreateCalibrationViewModel : BaseViewModel
11
    {
12
13
        #region Attributs
14 9601eaf0 lbihannic
        public string _phaseName;
15 6f451cc1 lbihannic
        private ObservableCollection<TypeCalibration> _typeCalibrations;
16 5d673ce0 Lucas Bihannic
        private TypeCalibration _selectedType;
17
        private double? _conc_o2;
18
        private double? _conc_co2;
19
        private double? _conc_ch4;
20 09d4a0de lbihannic
        private string _refBouteille;
21
        private bool _isRefBouteilleVisible;
22 957b1f34 lbihannic
        private Calibration _lastCalibration;
23
        private bool _hasLastMeasure;
24
        private double? _lastO2;
25
        private double? _lastCO2;
26
        private double? _lastCH4;
27 5d673ce0 Lucas Bihannic
        private bool _isFormValid;
28
        #endregion
29
30
        #region Commandes
31
        public ICommand CreateCalibrationCommand { get; }
32
        #endregion
33
34
        #region Propriétés
35 9601eaf0 lbihannic
        public PhaseCalibration Phase { get; private set; }
36
        public string PhaseName
37
        {
38
            get => _phaseName;
39
            set
40
            {
41 6f451cc1 lbihannic
                if (_phaseName == value) return;
42 9601eaf0 lbihannic
                _phaseName = value;
43
                Phase = PhaseCalibration.All.FirstOrDefault(p => p.Name == value) ?? PhaseCalibration.DEBUT;
44
                OnPropertyChanged();
45 6f451cc1 lbihannic
                OnPhaseChanged();
46 9601eaf0 lbihannic
            }
47
        }
48 6f451cc1 lbihannic
        public ObservableCollection<TypeCalibration> TypeCalibrations
49
        {
50
            get => _typeCalibrations;
51
            set
52
            {
53
                _typeCalibrations = value;
54
                OnPropertyChanged();
55
            }
56
        }
57
58 5d673ce0 Lucas Bihannic
        public TypeCalibration SelectedType
59
        {
60
            get => _selectedType;
61
            set
62
            {
63
                _selectedType = value;
64 6f451cc1 lbihannic
                IsRefBouteilleVisible = _selectedType != TypeCalibration.AIR;
65 5d673ce0 Lucas Bihannic
                OnPropertyChanged();
66 957b1f34 lbihannic
                OnTypeChanged();
67 5d673ce0 Lucas Bihannic
                ValidateForm();
68
            }
69
        }
70
71
        public double? ConcO2
72
        {
73
            get => _conc_o2;
74
            set
75
            {
76
                _conc_o2 = value;
77
                OnPropertyChanged();
78
                ValidateForm();
79
            }
80
        }
81
82
        public double? ConcCO2
83
        {
84
            get => _conc_co2;
85
            set
86
            {
87
                _conc_co2 = value;
88
                OnPropertyChanged();
89
                ValidateForm();
90
            }
91
        }
92
93
        public double? ConcCH4
94
        {
95
            get => _conc_ch4;
96
            set
97
            {
98
                _conc_ch4 = value;
99
                OnPropertyChanged();
100
                ValidateForm();
101
            }
102
        }
103
        public string RefBouteille
104
        {
105 09d4a0de lbihannic
            get => _refBouteille;
106 5d673ce0 Lucas Bihannic
            set
107
            {
108 09d4a0de lbihannic
                _refBouteille = value;
109 5d673ce0 Lucas Bihannic
                OnPropertyChanged();
110
                ValidateForm();
111
            }
112
        }
113 09d4a0de lbihannic
        public bool IsRefBouteilleVisible
114
        {
115
            get => _isRefBouteilleVisible;
116
            set
117
            {
118
                _isRefBouteilleVisible = value;
119
                OnPropertyChanged();
120
            }
121
        }
122 957b1f34 lbihannic
        public Calibration LastCalibration
123
        {
124
            get => _lastCalibration;
125
            set
126
            {
127
                _lastCalibration = value;
128
                OnPropertyChanged();
129
            }
130
        }
131
        public bool HasLastMeasure
132
        {
133
            get => _hasLastMeasure;
134
            set
135
            {
136
                _hasLastMeasure = value;
137
                OnPropertyChanged();
138
            }
139
        }
140
        public double? LastO2
141
        {
142
            get => _lastO2;
143
            set
144
            {
145
                _lastO2 = value;
146
                OnPropertyChanged();
147
            }
148
        }
149
        public double? LastCO2
150
        {
151
            get => _lastCO2;
152
            set
153
            {
154
                _lastCO2 = value;
155
                OnPropertyChanged();
156
            }
157
        }
158
        public double? LastCH4
159
        {
160
            get => _lastCH4;
161
            set
162
            {
163
                _lastCH4 = value;
164
                OnPropertyChanged();
165
            }
166
        }
167 09d4a0de lbihannic
168 5d673ce0 Lucas Bihannic
        public bool IsFormValid
169
        {
170
            get => _isFormValid;
171
            set
172
            {
173
                _isFormValid = value;
174
                OnPropertyChanged();
175
            }
176
        }
177
178
        public bool TypeError { get; private set; } = true;
179
        public bool ConcO2Error { get; private set; } = true;
180
        public bool ConcCO2Error { get; private set; } = true;
181
        public bool ConcCH4Error { get; private set; } = true;
182 09d4a0de lbihannic
        public bool RefBouteilleError { get; private set; } = true;
183 5d673ce0 Lucas Bihannic
        #endregion
184
185
        #region Constructeurs
186
        public CreateCalibrationViewModel()
187
        {
188 957b1f34 lbihannic
            CreateCalibrationCommand = new Command(async () => await CreateCalibration());
189 6f451cc1 lbihannic
        }
190
        #endregion
191 5d673ce0 Lucas Bihannic
192 6f451cc1 lbihannic
        #region Méthodes
193
194
        public void OnPhaseChanged()
195
        {
196 5d673ce0 Lucas Bihannic
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
197 6f451cc1 lbihannic
            var typesManquants = journeeActuelle.GetMissingCalibrationByPhase(Phase);
198 5d673ce0 Lucas Bihannic
199
            TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants);
200
            SelectedType = TypeCalibrations[0];
201
202 6f451cc1 lbihannic
            if (Phase == PhaseCalibration.FIN)
203
            {
204 957b1f34 lbihannic
                LastCalibration = journeeActuelle.GetDebutCalibration();
205
                HasLastMeasure = true;
206 6f451cc1 lbihannic
                OnTypeChanged();
207 957b1f34 lbihannic
            }
208 5d673ce0 Lucas Bihannic
        }
209
        private async Task CreateCalibration()
210
        {
211 1019554c lbihannic
            if (IsBusy) return;
212 5d673ce0 Lucas Bihannic
            IsBusy = true;
213
214 6f451cc1 lbihannic
            var journee = JourneeViewModel.Instance.GetCurrentDay();
215 5d673ce0 Lucas Bihannic
            var date = DateTime.Now;
216
            var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
217
218 6f451cc1 lbihannic
            var calibration = journee.Calibrations.FirstOrDefault(c => c.Phase == Phase);
219
            if (calibration == null)
220
            {
221
                calibration = new Calibration(Phase, newCalib);
222
                journee.AddCalibration(calibration);
223
                App.Db.Calibration.Add(calibration);
224
            }
225
            else
226
            {
227
                calibration.AddMesure(newCalib);
228
            }
229
230
            App.Db.MesureCalibration.Add(newCalib);
231
            await App.Db.SaveChangesAsync();
232
233
            var isComplete = calibration.IsComplete();
234 9601eaf0 lbihannic
            var targetView = $"{nameof(CreateCalibrationView)}?phase={PhaseName}";
235 957b1f34 lbihannic
            if (isComplete) {
236 9601eaf0 lbihannic
                targetView = Phase == PhaseCalibration.DEBUT ? nameof(SetListView) : nameof(MainView);
237 5d673ce0 Lucas Bihannic
            }
238 957b1f34 lbihannic
            await Shell.Current.GoToAsync(targetView);
239 5d673ce0 Lucas Bihannic
240
            IsBusy = false;
241
        }
242
243
        private void ValidateForm()
244
        {
245
            TypeError = SelectedType == null;
246
            ConcO2Error = ConcO2 == null;
247
            ConcCO2Error = ConcCO2 == null;
248
            ConcCH4Error = ConcCH4 == null;
249 09d4a0de lbihannic
            RefBouteilleError = IsRefBouteilleVisible && (RefBouteille == null || RefBouteille.Length < 5);
250 5d673ce0 Lucas Bihannic
251
            OnPropertyChanged(nameof(TypeError));
252
            OnPropertyChanged(nameof(ConcO2Error));
253
            OnPropertyChanged(nameof(ConcCO2Error));
254
            OnPropertyChanged(nameof(ConcCH4Error));
255 09d4a0de lbihannic
            OnPropertyChanged(nameof(RefBouteilleError));
256 5d673ce0 Lucas Bihannic
257 09d4a0de lbihannic
            IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error && !RefBouteilleError;
258 5d673ce0 Lucas Bihannic
259
            (CreateCalibrationCommand as Command)?.ChangeCanExecute();
260
        }
261 957b1f34 lbihannic
262
        private void OnTypeChanged()
263
        {
264
            if (!HasLastMeasure)
265
                return;
266
            var mesureCalibration = LastCalibration.getMesureCalibrationByType(SelectedType);
267
            LastCH4 = mesureCalibration.Conc_CH4;
268
            LastO2 = mesureCalibration.Conc_O2;
269
            LastCO2 = mesureCalibration.Conc_CO2;
270
        }
271 5d673ce0 Lucas Bihannic
        #endregion
272
    }
273
}