Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateMeasureViewModel.cs @ 9fd69a0e

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