Statistiques
| Branche: | Révision:

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

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

1
using CommunityToolkit.Maui.Core.Extensions;
2
using GES_PAC.Model;
3
using GES_PAC.View;
4
using System.Collections.ObjectModel;
5
using System.Diagnostics;
6
using System.Windows.Input;
7

    
8
namespace GES_PAC.ViewModel
9
{
10
    public class CreateCalibrationViewModel : BaseViewModel
11
    {
12

    
13
        #region Attributs
14
        private TypeCalibration _selectedType;
15
        private double? _conc_o2;
16
        private double? _conc_co2;
17
        private double? _conc_ch4;
18
        private string _refBouteille;
19
        private bool _isRefBouteilleVisible;
20
        private bool _isFormValid;
21
        #endregion
22

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

    
27
        #region Propriétés
28
        public ObservableCollection<TypeCalibration> TypeCalibrations { get; }
29
        public TypeCalibration SelectedType
30
        {
31
            get => _selectedType;
32
            set
33
            {
34
                _selectedType = value;
35
                IsRefBouteilleVisible = _selectedType.Name != "Air";
36
                OnPropertyChanged();
37
                ValidateForm();
38
            }
39
        }
40

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

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

    
63
        public double? ConcCH4
64
        {
65
            get => _conc_ch4;
66
            set
67
            {
68
                _conc_ch4 = value;
69
                OnPropertyChanged();
70
                ValidateForm();
71
            }
72
        }
73
        public string RefBouteille
74
        {
75
            get => _refBouteille;
76
            set
77
            {
78
                _refBouteille = value;
79
                OnPropertyChanged();
80
                ValidateForm();
81
            }
82
        }
83
        public bool IsRefBouteilleVisible
84
        {
85
            get => _isRefBouteilleVisible;
86
            set
87
            {
88
                _isRefBouteilleVisible = value;
89
                OnPropertyChanged();
90
            }
91
        }
92

    
93
        public bool IsFormValid
94
        {
95
            get => _isFormValid;
96
            set
97
            {
98
                _isFormValid = value;
99
                OnPropertyChanged();
100
            }
101
        }
102

    
103
        public bool TypeError { get; private set; } = true;
104
        public bool ConcO2Error { get; private set; } = true;
105
        public bool ConcCO2Error { get; private set; } = true;
106
        public bool ConcCH4Error { get; private set; } = true;
107
        public bool RefBouteilleError { get; private set; } = true;
108
        #endregion
109

    
110
        #region Constructeurs
111
        public CreateCalibrationViewModel()
112
        {
113

    
114
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
115
            var typesManquants = journeeActuelle.GetCurrentMissingCalibration();
116

    
117
            TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants);
118

    
119
            SelectedType = TypeCalibrations[0];
120

    
121
            CreateCalibrationCommand = new Command(async () => await CreateCalibration());
122
        }
123
        #endregion
124

    
125
        #region Méthodes
126
        private async Task CreateCalibration()
127
        {
128
            if (IsBusy) return;
129
            IsBusy = true;
130

    
131
            var date = DateTime.Now;
132
            var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
133
            var phaseCalibration = JourneeViewModel.Instance.GetCurrentPhase();
134
            var isComplete = JourneeViewModel.Instance.GetCurrentDay().AddCalibration(newCalib, phaseCalibration);
135

    
136
            if (isComplete)
137
            {
138
                await Shell.Current.GoToAsync(nameof(SetListView));
139
            }
140
            else
141
            {
142
                await Shell.Current.GoToAsync(nameof(CreateCalibrationView));
143
            }
144

    
145
            IsBusy = false;
146
        }
147

    
148
        private void ValidateForm()
149
        {
150
            TypeError = SelectedType == null;
151
            ConcO2Error = ConcO2 == null;
152
            ConcCO2Error = ConcCO2 == null;
153
            ConcCH4Error = ConcCH4 == null;
154
            RefBouteilleError = IsRefBouteilleVisible && (RefBouteille == null || RefBouteille.Length < 5);
155

    
156
            OnPropertyChanged(nameof(TypeError));
157
            OnPropertyChanged(nameof(ConcO2Error));
158
            OnPropertyChanged(nameof(ConcCO2Error));
159
            OnPropertyChanged(nameof(ConcCH4Error));
160
            OnPropertyChanged(nameof(RefBouteilleError));
161

    
162
            IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error && !RefBouteilleError;
163

    
164
            (CreateCalibrationCommand as Command)?.ChangeCanExecute();
165
        }
166
        #endregion
167
    }
168
}