Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateCalibrationViewModel.cs @ 9601eaf0

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