Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateCalibrationViewModel.cs @ 5d673ce0

Historique | Voir | Annoter | Télécharger (4,103 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 _ref_bouteille;
19
        private bool _isFormValid;
20
        #endregion
21

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

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

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

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

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

    
91
        public bool TypeError { get; private set; } = true;
92
        public bool ConcO2Error { get; private set; } = true;
93
        public bool ConcCO2Error { get; private set; } = true;
94
        public bool ConcCH4Error { get; private set; } = true;
95
        #endregion
96

    
97
        #region Constructeurs
98
        public CreateCalibrationViewModel()
99
        {
100

    
101
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
102
            var typesManquants = journeeActuelle.GetCurrentMissingCalibration();
103

    
104
            TypeCalibrations = new ObservableCollection<TypeCalibration>(typesManquants);
105

    
106
            SelectedType = TypeCalibrations[0];
107

    
108
            CreateCalibrationCommand = new Command(async () => await CreateCalibration());
109
        }
110
        #endregion
111

    
112
        #region Méthodes
113
        private async Task CreateCalibration()
114
        {
115
            IsBusy = true;
116

    
117
            var date = DateTime.Now;
118
            var newCalib = new MesureCalibration(date, ConcO2 ?? 0, ConcCO2 ?? 0, ConcCH4 ?? 0, SelectedType, RefBouteille);
119
            var isComplete = JourneeViewModel.Instance.GetCurrentDay().AddCalibration(newCalib);
120

    
121
            if (isComplete)
122
            {
123
                await Shell.Current.GoToAsync(nameof(MainView));
124
            }
125
            else
126
            {
127
                await Shell.Current.GoToAsync(nameof(CreateCalibrationView));
128
            }
129

    
130
            IsBusy = false;
131
        }
132

    
133
        private void ValidateForm()
134
        {
135
            TypeError = SelectedType == null;
136
            ConcO2Error = ConcO2 == null;
137
            ConcCO2Error = ConcCO2 == null;
138
            ConcCH4Error = ConcCH4 == null;
139

    
140
            OnPropertyChanged(nameof(TypeError));
141
            OnPropertyChanged(nameof(ConcO2Error));
142
            OnPropertyChanged(nameof(ConcCO2Error));
143
            OnPropertyChanged(nameof(ConcCH4Error));
144

    
145
            IsFormValid = !TypeError && !ConcO2Error && !ConcCO2Error && !ConcCH4Error;
146

    
147
            (CreateCalibrationCommand as Command)?.ChangeCanExecute();
148
        }
149
        #endregion
150
    }
151
}