Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / EnterAnimalViewModel.cs @ 4afea73d

Historique | Voir | Annoter | Télécharger (2,688 ko)

1 09d4a0de lbihannic
using GES_PAC.Model;
2
using GES_PAC.View;
3
using System.Windows.Input;
4
5
namespace GES_PAC.ViewModel
6
{
7 ba296a27 lbihannic
    [QueryProperty(nameof(ChamberId), "chamberId")]
8 09d4a0de lbihannic
    public class EnterAnimalViewModel : BaseViewModel
9
    {
10
11
        #region Attributs
12
        private int _chamberId;
13
14
        private string _numrfid;
15
        private double? _poids;
16
        private bool _isFormValid;
17
        #endregion
18
19
        #region Commandes
20
        public ICommand EnterAnimalCommand { get; }
21
        #endregion
22
23
        #region Propriétés
24
25
        
26
        public int ChamberId
27
        {
28
            get => _chamberId;
29
            set
30
            {
31
                _chamberId = value;
32
                OnPropertyChanged();
33 ba296a27 lbihannic
                OnPropertyChanged(nameof(Titre));
34 09d4a0de lbihannic
            }
35
        }
36
37
        public string NumRFID
38
        {
39
            get => _numrfid;
40
            set
41
            {
42
                _numrfid = value;
43
                OnPropertyChanged();
44
                ValidateForm();
45
            }
46
        }
47
48
        public double? Poids
49
        {
50
            get => _poids;
51
            set
52
            {
53
                _poids = value;
54
                OnPropertyChanged();
55
                ValidateForm();
56
            }
57
        }
58
59
        public bool IsFormValid
60
        {
61
            get => _isFormValid;
62
            set
63
            {
64
                _isFormValid = value;
65
                OnPropertyChanged();
66
            }
67
        }
68
69
        public bool NumRFIDError { get; private set; } = true;
70
        public bool PoidsError { get; private set; } = true;
71 fff89fc5 lbihannic
        public string Titre => $"Informations animal chambre {ChamberId}";
72 ba296a27 lbihannic
73 09d4a0de lbihannic
        #endregion
74
75
        #region Constructeurs
76
        public EnterAnimalViewModel()
77
        {
78
            EnterAnimalCommand = new Command(async () => await EnterAnimal());
79
        }
80
        #endregion
81
82
        #region Méthodes
83
        private async Task EnterAnimal()
84
        {
85
            IsBusy = true;
86
            var date = DateTime.Now;
87
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
88 ba296a27 lbihannic
            var newAnimalSet = new SerieAnimal(ChamberId, Poids ?? 0, date, NumRFID);
89
            journeeActuelle.GetCurrentSet().AddSerieAnimal(newAnimalSet);
90 09d4a0de lbihannic
91 fff89fc5 lbihannic
            await Shell.Current.GoToAsync($"{nameof(CreateMeasureView)}?chamberId={ChamberId}");
92 09d4a0de lbihannic
            IsBusy = false;
93
        }
94
95
        private void ValidateForm()
96
        {
97 ba296a27 lbihannic
            NumRFIDError = NumRFID.Length < 5 ;
98 09d4a0de lbihannic
            PoidsError = Poids == null;
99
100
            OnPropertyChanged(nameof(NumRFIDError));
101
            OnPropertyChanged(nameof(PoidsError));
102
103
            IsFormValid = !NumRFIDError && !PoidsError;
104
105
            (EnterAnimalCommand as Command)?.ChangeCanExecute();
106
        }
107
        #endregion
108
    }
109
}