Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateCalibrationViewModel.cs @ ba296a27

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

1 5d673ce0 Lucas Bihannic
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 09d4a0de lbihannic
        private string _refBouteille;
19
        private bool _isRefBouteilleVisible;
20 5d673ce0 Lucas Bihannic
        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 09d4a0de lbihannic
                IsRefBouteilleVisible = _selectedType.Name != "Air";
36 5d673ce0 Lucas Bihannic
                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 09d4a0de lbihannic
            get => _refBouteille;
76 5d673ce0 Lucas Bihannic
            set
77
            {
78 09d4a0de lbihannic
                _refBouteille = value;
79 5d673ce0 Lucas Bihannic
                OnPropertyChanged();
80
                ValidateForm();
81
            }
82
        }
83 09d4a0de lbihannic
        public bool IsRefBouteilleVisible
84
        {
85
            get => _isRefBouteilleVisible;
86
            set
87
            {
88
                _isRefBouteilleVisible = value;
89
                OnPropertyChanged();
90
            }
91
        }
92
93 5d673ce0 Lucas Bihannic
        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 09d4a0de lbihannic
        public bool RefBouteilleError { get; private set; } = true;
108 5d673ce0 Lucas Bihannic
        #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
            IsBusy = true;
129
130
            var date = DateTime.Now;
131
            var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
132 09d4a0de lbihannic
            var phaseCalibration = JourneeViewModel.Instance.GetCurrentPhase();
133
            var isComplete = JourneeViewModel.Instance.GetCurrentDay().AddCalibration(newCalib, phaseCalibration);
134 5d673ce0 Lucas Bihannic
135
            if (isComplete)
136
            {
137 09d4a0de lbihannic
                await Shell.Current.GoToAsync(nameof(SetListView));
138 5d673ce0 Lucas Bihannic
            }
139
            else
140
            {
141
                await Shell.Current.GoToAsync(nameof(CreateCalibrationView));
142
            }
143
144
            IsBusy = false;
145
        }
146
147
        private void ValidateForm()
148
        {
149
            TypeError = SelectedType == null;
150
            ConcO2Error = ConcO2 == null;
151
            ConcCO2Error = ConcCO2 == null;
152
            ConcCH4Error = ConcCH4 == null;
153 09d4a0de lbihannic
            RefBouteilleError = IsRefBouteilleVisible && (RefBouteille == null || RefBouteille.Length < 5);
154 5d673ce0 Lucas Bihannic
155
            OnPropertyChanged(nameof(TypeError));
156
            OnPropertyChanged(nameof(ConcO2Error));
157
            OnPropertyChanged(nameof(ConcCO2Error));
158
            OnPropertyChanged(nameof(ConcCH4Error));
159 09d4a0de lbihannic
            OnPropertyChanged(nameof(RefBouteilleError));
160 5d673ce0 Lucas Bihannic
161 09d4a0de lbihannic
            IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error && !RefBouteilleError;
162 5d673ce0 Lucas Bihannic
163
            (CreateCalibrationCommand as Command)?.ChangeCanExecute();
164
        }
165
        #endregion
166
    }
167
}