Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (6,982 ko)

1
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
    [QueryProperty(nameof(PhaseName), "phase")]
9
    public class CreateCalibrationViewModel : BaseViewModel
10
    {
11

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

    
28
        #region Commandes
29
        public ICommand CreateCalibrationCommand { get; }
30
        #endregion
31

    
32
        #region Propriétés
33
        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
        public ObservableCollection<TypeCalibration> TypeCalibrations { get; }
45
        public TypeCalibration SelectedType
46
        {
47
            get => _selectedType;
48
            set
49
            {
50
                _selectedType = value;
51
                IsRefBouteilleVisible = _selectedType != TypeCalibration.Air;
52
                OnPropertyChanged();
53
                OnTypeChanged();
54
                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
            get => _refBouteille;
93
            set
94
            {
95
                _refBouteille = value;
96
                OnPropertyChanged();
97
                ValidateForm();
98
            }
99
        }
100
        public bool IsRefBouteilleVisible
101
        {
102
            get => _isRefBouteilleVisible;
103
            set
104
            {
105
                _isRefBouteilleVisible = value;
106
                OnPropertyChanged();
107
            }
108
        }
109
        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

    
155
        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
        public bool RefBouteilleError { get; private set; } = true;
170
        #endregion
171

    
172
        #region Constructeurs
173
        public CreateCalibrationViewModel()
174
        {
175
            CreateCalibrationCommand = new Command(async () => await CreateCalibration());
176

    
177
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
178
            var typesManquants = journeeActuelle.GetCurrentMissingCalibration();
179
            var phaseCalibration = journeeActuelle.GetCurrentPhase();
180

    
181
            TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants);
182
            SelectedType = TypeCalibrations[0];
183

    
184
            if (journeeActuelle.GetCurrentPhase() == PhaseCalibration.FIN)
185
            { 
186
                LastCalibration = journeeActuelle.GetDebutCalibration();
187
                HasLastMeasure = true;
188
            }
189
        }
190
        #endregion
191

    
192
        #region Méthodes
193
        private async Task CreateCalibration()
194
        {
195
            if (IsBusy) return;
196
            IsBusy = true;
197

    
198
            var date = DateTime.Now;
199
            var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
200
            var isComplete = JourneeViewModel.Instance.GetCurrentDay().AddCalibration(newCalib, Phase);
201

    
202
            var targetView = $"{nameof(CreateCalibrationView)}?phase={PhaseName}";
203
            if (isComplete) {
204
                targetView = Phase == PhaseCalibration.DEBUT ? nameof(SetListView) : nameof(MainView);
205
            }
206
            await Shell.Current.GoToAsync(targetView);
207

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

    
219
            OnPropertyChanged(nameof(TypeError));
220
            OnPropertyChanged(nameof(ConcO2Error));
221
            OnPropertyChanged(nameof(ConcCO2Error));
222
            OnPropertyChanged(nameof(ConcCH4Error));
223
            OnPropertyChanged(nameof(RefBouteilleError));
224

    
225
            IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error && !RefBouteilleError;
226

    
227
            (CreateCalibrationCommand as Command)?.ChangeCanExecute();
228
        }
229

    
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
        #endregion
240
    }
241
}