Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (2,719 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 1019554c lbihannic
            if (IsBusy) return;
86 09d4a0de lbihannic
            IsBusy = true;
87
            var date = DateTime.Now;
88
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
89 ba296a27 lbihannic
            var newAnimalSet = new SerieAnimal(ChamberId, Poids ?? 0, date, NumRFID);
90
            journeeActuelle.GetCurrentSet().AddSerieAnimal(newAnimalSet);
91 09d4a0de lbihannic
92 fff89fc5 lbihannic
            await Shell.Current.GoToAsync($"{nameof(CreateMeasureView)}?chamberId={ChamberId}");
93 09d4a0de lbihannic
            IsBusy = false;
94
        }
95
96
        private void ValidateForm()
97
        {
98 ba296a27 lbihannic
            NumRFIDError = NumRFID.Length < 5 ;
99 09d4a0de lbihannic
            PoidsError = Poids == null;
100
101
            OnPropertyChanged(nameof(NumRFIDError));
102
            OnPropertyChanged(nameof(PoidsError));
103
104
            IsFormValid = !NumRFIDError && !PoidsError;
105
106
            (EnterAnimalCommand as Command)?.ChangeCanExecute();
107
        }
108
        #endregion
109
    }
110
}