Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateCalibrationViewModel.cs @ 42456640

Historique | Voir | Annoter | Télécharger (6,542 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
    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
        private string _refBouteille;
17
        private bool _isRefBouteilleVisible;
18
        private Calibration _lastCalibration;
19
        private bool _hasLastMeasure;
20
        private double? _lastO2;
21
        private double? _lastCO2;
22
        private double? _lastCH4;
23
        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
                IsRefBouteilleVisible = _selectedType.Name != "Air";
39
                OnPropertyChanged();
40
                OnTypeChanged();
41
                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
            get => _refBouteille;
80
            set
81
            {
82
                _refBouteille = value;
83
                OnPropertyChanged();
84
                ValidateForm();
85
            }
86
        }
87
        public bool IsRefBouteilleVisible
88
        {
89
            get => _isRefBouteilleVisible;
90
            set
91
            {
92
                _isRefBouteilleVisible = value;
93
                OnPropertyChanged();
94
            }
95
        }
96
        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

    
142
        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
        public bool RefBouteilleError { get; private set; } = true;
157
        #endregion
158

    
159
        #region Constructeurs
160
        public CreateCalibrationViewModel()
161
        {
162
            CreateCalibrationCommand = new Command(async () => await CreateCalibration());
163

    
164
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
165
            var typesManquants = journeeActuelle.GetCurrentMissingCalibration();
166

    
167
            TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants);
168
            SelectedType = TypeCalibrations[0];
169

    
170
            if (journeeActuelle.GetCurrentPhase() == PhaseCalibration.Fin)
171
            { 
172
                LastCalibration = journeeActuelle.GetDebutCalibration();
173
                HasLastMeasure = true;
174
            }
175
        }
176
        #endregion
177

    
178
        #region Méthodes
179
        private async Task CreateCalibration()
180
        {
181
            if (IsBusy) return;
182
            IsBusy = true;
183

    
184
            var date = DateTime.Now;
185
            var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
186
            var phaseCalibration = JourneeViewModel.Instance.GetCurrentPhase();
187
            var isComplete = JourneeViewModel.Instance.GetCurrentDay().AddCalibration(newCalib, phaseCalibration);
188

    
189
            var targetView = nameof(CreateCalibrationView);
190
            if (isComplete) {
191
                targetView = phaseCalibration == PhaseCalibration.Debut ? nameof(SetListView) : nameof(MainView);
192
            }
193
            await Shell.Current.GoToAsync(targetView);
194

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

    
206
            OnPropertyChanged(nameof(TypeError));
207
            OnPropertyChanged(nameof(ConcO2Error));
208
            OnPropertyChanged(nameof(ConcCO2Error));
209
            OnPropertyChanged(nameof(ConcCH4Error));
210
            OnPropertyChanged(nameof(RefBouteilleError));
211

    
212
            IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error && !RefBouteilleError;
213

    
214
            (CreateCalibrationCommand as Command)?.ChangeCanExecute();
215
        }
216

    
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
        #endregion
227
    }
228
}