Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateMeasureViewModel.cs @ 957bebf1

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