Statistiques
| Branche: | Révision:

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

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

1
using GES_PAC.Model;
2
using GES_PAC.View;
3
using Microsoft.EntityFrameworkCore;
4
using System.Collections.ObjectModel;
5
using System.Windows.Input;
6

    
7
namespace GES_PAC.ViewModel
8
{
9
    [QueryProperty(nameof(PhaseName), "phase")]
10
    public class CreateCalibrationViewModel : BaseViewModel
11
    {
12

    
13
        #region Attributs
14
        public string _phaseName;
15
        private ObservableCollection<TypeCalibration> _typeCalibrations;
16
        private TypeCalibration _selectedType;
17
        private double? _conc_o2;
18
        private double? _conc_co2;
19
        private double? _conc_ch4;
20
        private string _refBouteille;
21
        private bool _isRefBouteilleVisible;
22
        private Calibration _lastCalibration;
23
        private bool _hasLastMeasure;
24
        private double? _lastO2;
25
        private double? _lastCO2;
26
        private double? _lastCH4;
27
        private bool _isFormValid;
28
        #endregion
29

    
30
        #region Commandes
31
        public ICommand CreateCalibrationCommand { get; }
32
        #endregion
33

    
34
        #region Propriétés
35
        public PhaseCalibration Phase { get; private set; }
36
        public string PhaseName
37
        {
38
            get => _phaseName;
39
            set
40
            {
41
                if (_phaseName == value) return;
42
                _phaseName = value;
43
                Phase = PhaseCalibration.All.FirstOrDefault(p => p.Name == value) ?? PhaseCalibration.DEBUT;
44
                OnPropertyChanged();
45
                OnPhaseChanged();
46
            }
47
        }
48
        public ObservableCollection<TypeCalibration> TypeCalibrations
49
        {
50
            get => _typeCalibrations;
51
            set
52
            {
53
                _typeCalibrations = value;
54
                OnPropertyChanged();
55
            }
56
        }
57

    
58
        public TypeCalibration SelectedType
59
        {
60
            get => _selectedType;
61
            set
62
            {
63
                _selectedType = value;
64
                IsRefBouteilleVisible = _selectedType != TypeCalibration.AIR;
65
                OnPropertyChanged();
66
                OnTypeChanged();
67
                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
            get => _refBouteille;
106
            set
107
            {
108
                _refBouteille = value;
109
                OnPropertyChanged();
110
                ValidateForm();
111
            }
112
        }
113
        public bool IsRefBouteilleVisible
114
        {
115
            get => _isRefBouteilleVisible;
116
            set
117
            {
118
                _isRefBouteilleVisible = value;
119
                OnPropertyChanged();
120
            }
121
        }
122
        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

    
168
        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
        public bool RefBouteilleError { get; private set; } = true;
183
        #endregion
184

    
185
        #region Constructeurs
186
        public CreateCalibrationViewModel()
187
        {
188
            CreateCalibrationCommand = new Command(async () => await CreateCalibration());
189
        }
190
        #endregion
191

    
192
        #region Méthodes
193

    
194
        public void OnPhaseChanged()
195
        {
196
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
197
            var typesManquants = journeeActuelle.GetMissingCalibrationByPhase(Phase);
198

    
199
            TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants);
200
            SelectedType = TypeCalibrations[0];
201

    
202
            if (Phase == PhaseCalibration.FIN)
203
            {
204
                LastCalibration = journeeActuelle.GetDebutCalibration();
205
                HasLastMeasure = true;
206
                OnTypeChanged();
207
            }
208
        }
209
        private async Task CreateCalibration()
210
        {
211
            if (IsBusy) return;
212
            IsBusy = true;
213

    
214
            var journee = JourneeViewModel.Instance.GetCurrentDay();
215
            var date = DateTime.Now;
216
            var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
217

    
218
            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
            var targetView = $"{nameof(CreateCalibrationView)}?phase={PhaseName}";
235
            if (isComplete) {
236
                targetView = Phase == PhaseCalibration.DEBUT ? nameof(SetListView) : nameof(MainView);
237
            }
238
            await Shell.Current.GoToAsync(targetView);
239

    
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
            RefBouteilleError = IsRefBouteilleVisible && (RefBouteille == null || RefBouteille.Length < 5);
250

    
251
            OnPropertyChanged(nameof(TypeError));
252
            OnPropertyChanged(nameof(ConcO2Error));
253
            OnPropertyChanged(nameof(ConcCO2Error));
254
            OnPropertyChanged(nameof(ConcCH4Error));
255
            OnPropertyChanged(nameof(RefBouteilleError));
256

    
257
            IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error && !RefBouteilleError;
258

    
259
            (CreateCalibrationCommand as Command)?.ChangeCanExecute();
260
        }
261

    
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
        #endregion
272
    }
273
}