Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateMeasureViewModel.cs @ 612877b5

Historique | Voir | Annoter | Télécharger (5,956 ko)

1 fff89fc5 lbihannic
using GES_PAC.Model;
2
using GES_PAC.View;
3
using System.Windows.Input;
4
5
namespace GES_PAC.ViewModel
6
{
7
    [QueryProperty(nameof(ChamberId), "chamberId")]
8
    public class CreateMeasureViewModel : BaseViewModel
9
    {
10
11
        #region Attributs
12
        private int _chamberId;
13
        private double? _conc_o2;
14
        private double? _conc_co2;
15
        private double? _conc_ch4;
16
        private bool _isFormValid;
17 1019554c lbihannic
        private bool _hasLastMeasure;
18
        private double? _lastO2;
19
        private double? _lastCO2;
20
        private double? _lastCH4;
21 612877b5 lbihannic
        private string _lastTime;
22
        private Color _lastCH4Color;
23
        private Color _lastO2Color;
24
        private Color _lastCO2Color;
25
26 fff89fc5 lbihannic
        #endregion
27
28
        #region Commandes
29
        public ICommand CreateMeasureCommand { get; }
30
        #endregion
31
32
        #region Propriétés
33
        public int ChamberId
34
        {
35
            get => _chamberId;
36
            set
37
            {
38
                _chamberId = value;
39
                OnPropertyChanged();
40
                OnPropertyChanged(nameof(Titre));
41 1019554c lbihannic
                GetLastMeasure();
42 fff89fc5 lbihannic
            }
43
        }
44
45
        public double? ConcO2
46
        {
47
            get => _conc_o2;
48
            set
49
            {
50
                _conc_o2 = value;
51
                OnPropertyChanged();
52
                ValidateForm();
53 612877b5 lbihannic
                UpdateColors();
54 fff89fc5 lbihannic
            }
55
        }
56
57
        public double? ConcCO2
58
        {
59
            get => _conc_co2;
60
            set
61
            {
62
                _conc_co2 = value;
63
                OnPropertyChanged();
64
                ValidateForm();
65 612877b5 lbihannic
                UpdateColors();
66 fff89fc5 lbihannic
            }
67
        }
68
69
        public double? ConcCH4
70
        {
71
            get => _conc_ch4;
72
            set
73
            {
74
                _conc_ch4 = value;
75
                OnPropertyChanged();
76
                ValidateForm();
77 612877b5 lbihannic
                UpdateColors();
78 fff89fc5 lbihannic
            }
79
        }
80 1019554c lbihannic
        public bool HasLastMeasure
81
        {
82
            get => _hasLastMeasure;
83
            set
84
            {
85
                _hasLastMeasure = value;
86
                OnPropertyChanged();
87
            }
88
        }
89
        public double? LastO2
90
        {
91
            get => _lastO2;
92
            set
93
            {
94
                _lastO2 = value;
95
                OnPropertyChanged();
96
            }
97
        }
98
        public double? LastCO2
99
        {
100
            get => _lastCO2;
101
            set
102
            {
103
                _lastCO2 = value;
104
                OnPropertyChanged();
105
            }
106
        }
107
        public double? LastCH4
108
        {
109
            get => _lastCH4;
110
            set
111
            {
112
                _lastCH4 = value;
113
                OnPropertyChanged();
114
            }
115
        }
116 612877b5 lbihannic
        public string LastTime
117
        {
118
            get => _lastTime;
119
            set
120
            {
121
                _lastTime = value;
122
                OnPropertyChanged();
123
            }
124
        }
125
        public Color LastCH4Color
126
        {
127
            get => _lastCH4Color;
128
            set
129
            {
130
                _lastCH4Color = value;
131
                OnPropertyChanged();
132
            }
133
        }
134
        public Color LastO2Color
135
        {
136
            get => _lastO2Color;
137
            set
138
            {
139
                _lastO2Color = value;
140
                OnPropertyChanged();
141
            }
142
        }
143
        public Color LastCO2Color
144
        {
145
            get => _lastCO2Color;
146
            set
147
            {
148
                _lastCO2Color = value;
149
                OnPropertyChanged();
150
            }
151
        }
152 fff89fc5 lbihannic
        public bool IsFormValid
153
        {
154
            get => _isFormValid;
155
            set
156
            {
157
                _isFormValid = value;
158
                OnPropertyChanged();
159
            }
160
        }
161
162
        public bool ConcO2Error { get; private set; } = true;
163
        public bool ConcCO2Error { get; private set; } = true;
164
        public bool ConcCH4Error { get; private set; } = true;
165
        public string Titre => $"Mesures chambre {ChamberId}";
166 1019554c lbihannic
        public Mesure? LastMeasure { get; set; }
167 fff89fc5 lbihannic
        #endregion
168
169
        #region Constructeurs
170
        public CreateMeasureViewModel()
171
        {
172
            CreateMeasureCommand = new Command(async () => await CreateMeasure());
173 1019554c lbihannic
174
            HasLastMeasure = false;
175 fff89fc5 lbihannic
        }
176
        #endregion
177
178
        #region Méthodes
179
        private async Task CreateMeasure()
180
        {
181 1019554c lbihannic
            if (IsBusy) return;
182 fff89fc5 lbihannic
            IsBusy = true;
183
184
            var date = DateTime.Now;
185
            var newMeasure = new Mesure(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0);
186
187
            JourneeViewModel.Instance.AddMeasureToCurrentSet(newMeasure, ChamberId);
188
189
            await Shell.Current.GoToAsync(nameof(ChambersView));
190
191
            IsBusy = false;
192
        }
193
194
        private void ValidateForm()
195
        {
196
            ConcO2Error = ConcO2 == null;
197
            ConcCO2Error = ConcCO2 == null;
198
            ConcCH4Error = ConcCH4 == null;
199
200
            OnPropertyChanged(nameof(ConcO2Error));
201
            OnPropertyChanged(nameof(ConcCO2Error));
202
            OnPropertyChanged(nameof(ConcCH4Error));
203
204
            IsFormValid = !ConcO2Error && !ConcCO2Error && !ConcCH4Error;
205
206
            (CreateMeasureCommand as Command)?.ChangeCanExecute();
207
        }
208 1019554c lbihannic
209
        private void GetLastMeasure()
210
        {
211
            LastMeasure = JourneeViewModel.Instance.GetCurrentSet().GetLastMeasureByNumeroBoite(ChamberId);
212
            HasLastMeasure = LastMeasure != null;
213
            if (HasLastMeasure)
214
            {
215
                LastO2 = LastMeasure.Conc_O2;
216
                LastCO2 = LastMeasure.Conc_CO2;
217
                LastCH4 = LastMeasure.Conc_CH4;
218 612877b5 lbihannic
                LastTime = "Dernière mesure : " + (int)(DateTime.Now - LastMeasure.Time).TotalMinutes + " min";
219
                UpdateColors();
220 1019554c lbihannic
            }
221
        }
222 612877b5 lbihannic
        private void UpdateColors()
223
        {
224
            LastCH4Color = (ConcCH4 < LastCH4) ? Colors.Orange : Colors.Gray;
225
            LastO2Color = (ConcO2 > LastO2) ? Colors.Orange : Colors.Gray;
226
            LastCO2Color = (ConcCO2 < LastCO2) ? Colors.Orange : Colors.Gray;
227
        }
228 fff89fc5 lbihannic
        #endregion
229
    }
230
}