Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateMeasureViewModel.cs @ 1019554c

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

1
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
        private bool _hasLastMeasure;
18
        private double? _lastO2;
19
        private double? _lastCO2;
20
        private double? _lastCH4;
21
        #endregion
22

    
23
        #region Commandes
24
        public ICommand CreateMeasureCommand { get; }
25
        #endregion
26

    
27
        #region Propriétés
28
        public int ChamberId
29
        {
30
            get => _chamberId;
31
            set
32
            {
33
                _chamberId = value;
34
                OnPropertyChanged();
35
                OnPropertyChanged(nameof(Titre));
36
                GetLastMeasure();
37
            }
38
        }
39

    
40
        public double? ConcO2
41
        {
42
            get => _conc_o2;
43
            set
44
            {
45
                _conc_o2 = value;
46
                OnPropertyChanged();
47
                ValidateForm();
48
            }
49
        }
50

    
51
        public double? ConcCO2
52
        {
53
            get => _conc_co2;
54
            set
55
            {
56
                _conc_co2 = value;
57
                OnPropertyChanged();
58
                ValidateForm();
59
            }
60
        }
61

    
62
        public double? ConcCH4
63
        {
64
            get => _conc_ch4;
65
            set
66
            {
67
                _conc_ch4 = value;
68
                OnPropertyChanged();
69
                ValidateForm();
70
            }
71
        }
72
        public bool HasLastMeasure
73
        {
74
            get => _hasLastMeasure;
75
            set
76
            {
77
                _hasLastMeasure = value;
78
                OnPropertyChanged();
79
            }
80
        }
81
        public double? LastO2
82
        {
83
            get => _lastO2;
84
            set
85
            {
86
                _lastO2 = value;
87
                OnPropertyChanged();
88
            }
89
        }
90
        public double? LastCO2
91
        {
92
            get => _lastCO2;
93
            set
94
            {
95
                _lastCO2 = value;
96
                OnPropertyChanged();
97
            }
98
        }
99
        public double? LastCH4
100
        {
101
            get => _lastCH4;
102
            set
103
            {
104
                _lastCH4 = value;
105
                OnPropertyChanged();
106
            }
107
        }
108
        public bool IsFormValid
109
        {
110
            get => _isFormValid;
111
            set
112
            {
113
                _isFormValid = value;
114
                OnPropertyChanged();
115
            }
116
        }
117

    
118
        public bool ConcO2Error { get; private set; } = true;
119
        public bool ConcCO2Error { get; private set; } = true;
120
        public bool ConcCH4Error { get; private set; } = true;
121
        public string Titre => $"Mesures chambre {ChamberId}";
122
        public Mesure? LastMeasure { get; set; }
123
        #endregion
124

    
125
        #region Constructeurs
126
        public CreateMeasureViewModel()
127
        {
128
            CreateMeasureCommand = new Command(async () => await CreateMeasure());
129

    
130
            HasLastMeasure = false;
131
        }
132
        #endregion
133

    
134
        #region Méthodes
135
        private async Task CreateMeasure()
136
        {
137
            if (IsBusy) return;
138
            IsBusy = true;
139

    
140
            var date = DateTime.Now;
141
            var newMeasure = new Mesure(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0);
142

    
143
            JourneeViewModel.Instance.AddMeasureToCurrentSet(newMeasure, ChamberId);
144

    
145
            await Shell.Current.GoToAsync(nameof(ChambersView));
146

    
147
            IsBusy = false;
148
        }
149

    
150
        private void ValidateForm()
151
        {
152
            ConcO2Error = ConcO2 == null;
153
            ConcCO2Error = ConcCO2 == null;
154
            ConcCH4Error = ConcCH4 == null;
155

    
156
            OnPropertyChanged(nameof(ConcO2Error));
157
            OnPropertyChanged(nameof(ConcCO2Error));
158
            OnPropertyChanged(nameof(ConcCH4Error));
159

    
160
            IsFormValid = !ConcO2Error && !ConcCO2Error && !ConcCH4Error;
161

    
162
            (CreateMeasureCommand as Command)?.ChangeCanExecute();
163
        }
164

    
165
        private void GetLastMeasure()
166
        {
167
            LastMeasure = JourneeViewModel.Instance.GetCurrentSet().GetLastMeasureByNumeroBoite(ChamberId);
168
            HasLastMeasure = LastMeasure != null;
169
            if (HasLastMeasure)
170
            {
171
                LastO2 = LastMeasure.Conc_O2;
172
                LastCO2 = LastMeasure.Conc_CO2;
173
                LastCH4 = LastMeasure.Conc_CH4;
174
            }
175
        }
176
        #endregion
177
    }
178
}