Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / EnterAnimalViewModel.cs @ a7fee243

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

1
using GES_PAC.Model;
2
using GES_PAC.View;
3
using System.Windows.Input;
4

    
5
namespace GES_PAC.ViewModel
6
{
7
    [QueryProperty(nameof(ChamberId), "chamberId")]
8
    public class EnterAnimalViewModel : BaseViewModel
9
    {
10

    
11
        #region Attributs
12
        private int _chamberId;
13

    
14
        private string _numrfid;
15
        private double? _poids;
16
        private DateOnly? _datePesee;
17
        private bool _isFormValid;
18
        private bool _isLoadEnable;
19
        private Color _dateColor;
20
        #endregion
21

    
22
        #region Commandes
23
        public ICommand EnterAnimalCommand { get; }
24
        public ICommand LoadWeightCommand { get; }
25
        #endregion
26

    
27
        #region Propriétés
28

    
29

    
30
        public int ChamberId
31
        {
32
            get => _chamberId;
33
            set
34
            {
35
                _chamberId = value;
36
                OnPropertyChanged();
37
                OnPropertyChanged(nameof(Titre));
38
            }
39
        }
40

    
41
        public string NumRFID
42
        {
43
            get => _numrfid;
44
            set
45
            {
46
                _numrfid = value;
47
                OnPropertyChanged();
48
                DatePesee = null;
49
                ValidateForm();
50
            }
51
        }
52

    
53
        public double? Poids
54
        {
55
            get => _poids;
56
            set
57
            {
58
                _poids = value;
59
                OnPropertyChanged();
60
                ValidateForm();
61
            }
62
        }
63
        public DateOnly? DatePesee
64
        {
65
            get => _datePesee;
66
            set
67
            {
68
                _datePesee = value;
69
                OnPropertyChanged();
70
                ValidateForm();
71
            }
72
        }
73

    
74
        public bool IsFormValid
75
        {
76
            get => _isFormValid;
77
            set
78
            {
79
                _isFormValid = value;
80
                OnPropertyChanged();
81
            }
82
        }
83
        public bool IsLoadEnable
84
        {
85
            get => _isLoadEnable;
86
            set
87
            {
88
                _isLoadEnable = value;
89
                OnPropertyChanged();
90
            }
91
        }
92
        public Color DateColor
93
        {
94
            get => _dateColor;
95
            set
96
            {
97
                _dateColor = value;
98
                OnPropertyChanged();
99
            }
100
        }
101

    
102
        public bool NumRFIDError { get; private set; } = true;
103
        public string Titre => $"Informations animal chambre {ChamberId}";
104

    
105
        #endregion
106

    
107
        #region Constructeurs
108
        public EnterAnimalViewModel()
109
        {
110
            EnterAnimalCommand = new Command(async () => await EnterAnimal());
111
            LoadWeightCommand = new Command(async () => await LoadWeight());
112
        }
113
        #endregion
114

    
115
        #region Méthodes
116
        private async Task EnterAnimal()
117
        {
118
            if (IsBusy) return;
119
            IsBusy = true;
120
            var date = DateTime.Now;
121
            var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay();
122
            var newAnimalSet = new SerieAnimal(ChamberId, Poids ?? 0, date, NumRFID);
123
            journeeActuelle.GetCurrentSet().AddSerieAnimal(newAnimalSet);
124

    
125
            App.Db.SerieAnimal.Add(newAnimalSet);
126
            await App.Db.SaveChangesAsync();
127

    
128
            await Shell.Current.GoToAsync($"{nameof(CreateMeasureView)}?chamberId={ChamberId}");
129
            IsBusy = false;
130
        }
131

    
132
        private void ValidateForm()
133
        {
134
            NumRFIDError = NumRFID == null || NumRFID.Length < 5;
135

    
136
            IsLoadEnable = !NumRFIDError && App.Db.Animal.Any(a => a.Rfid.EndsWith(NumRFID));
137

    
138
            OnPropertyChanged(nameof(NumRFIDError));
139

    
140
            IsFormValid = !NumRFIDError;
141

    
142
            (EnterAnimalCommand as Command)?.ChangeCanExecute();
143
        }
144

    
145
        async public Task LoadWeight()
146
        {
147
            var animal = App.Db.Animal.FirstOrDefault(a => a.Rfid.EndsWith(NumRFID));
148
            NumRFID = animal?.Rfid ?? "";
149
            Poids = animal?.Poids / 1000;
150
            DatePesee = DateOnly.FromDateTime(animal?.DateDernierePesee ?? default);
151
            DateColor = animal?.DateDernierePesee >= DateTime.Now.AddDays(-14) ? Colors.Green : Colors.Orange;
152
        }
153

    
154
        #endregion
155
    }
156
}