root / GES_PAC / ViewModel / EnterAnimalViewModel.cs @ 22c79571
Historique | Voir | Annoter | Télécharger (3,359 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 |
#endregion |
19 |
|
20 |
#region Commandes |
21 |
public ICommand EnterAnimalCommand { get; } |
22 |
public ICommand LoadWeightCommand { get; } |
23 |
#endregion |
24 |
|
25 |
#region Propriétés |
26 |
|
27 |
|
28 |
public int ChamberId |
29 |
{ |
30 |
get => _chamberId; |
31 |
set |
32 |
{ |
33 |
_chamberId = value; |
34 |
OnPropertyChanged(); |
35 |
OnPropertyChanged(nameof(Titre)); |
36 |
} |
37 |
} |
38 |
|
39 |
public string NumRFID |
40 |
{ |
41 |
get => _numrfid; |
42 |
set |
43 |
{ |
44 |
_numrfid = value; |
45 |
OnPropertyChanged(); |
46 |
ValidateForm(); |
47 |
} |
48 |
} |
49 |
|
50 |
public double? Poids |
51 |
{ |
52 |
get => _poids; |
53 |
set |
54 |
{ |
55 |
_poids = value; |
56 |
OnPropertyChanged(); |
57 |
ValidateForm(); |
58 |
} |
59 |
} |
60 |
public DateOnly? DatePesee |
61 |
{ |
62 |
get => _datePesee; |
63 |
set |
64 |
{ |
65 |
_datePesee = value; |
66 |
OnPropertyChanged(); |
67 |
ValidateForm(); |
68 |
} |
69 |
} |
70 |
|
71 |
public bool IsFormValid |
72 |
{ |
73 |
get => _isFormValid; |
74 |
set |
75 |
{ |
76 |
_isFormValid = value; |
77 |
OnPropertyChanged(); |
78 |
} |
79 |
} |
80 |
|
81 |
public bool NumRFIDError { get; private set; } = true; |
82 |
public string Titre => $"Informations animal chambre {ChamberId}"; |
83 |
|
84 |
#endregion |
85 |
|
86 |
#region Constructeurs |
87 |
public EnterAnimalViewModel() |
88 |
{ |
89 |
EnterAnimalCommand = new Command(async () => await EnterAnimal()); |
90 |
LoadWeightCommand = new Command(async () => await LoadWeight()); |
91 |
} |
92 |
#endregion |
93 |
|
94 |
#region Méthodes |
95 |
private async Task EnterAnimal() |
96 |
{ |
97 |
if (IsBusy) return; |
98 |
IsBusy = true; |
99 |
var date = DateTime.Now; |
100 |
var journeeActuelle = JourneeViewModel.Instance.GetCurrentDay(); |
101 |
var newAnimalSet = new SerieAnimal(ChamberId, Poids ?? 0, date, NumRFID); |
102 |
journeeActuelle.GetCurrentSet().AddSerieAnimal(newAnimalSet); |
103 |
|
104 |
App.Db.SerieAnimal.Add(newAnimalSet); |
105 |
await App.Db.SaveChangesAsync(); |
106 |
|
107 |
await Shell.Current.GoToAsync($"{nameof(CreateMeasureView)}?chamberId={ChamberId}"); |
108 |
IsBusy = false; |
109 |
} |
110 |
|
111 |
private void ValidateForm() |
112 |
{ |
113 |
NumRFIDError = NumRFID == null || NumRFID.Length < 5; |
114 |
|
115 |
OnPropertyChanged(nameof(NumRFIDError)); |
116 |
|
117 |
IsFormValid = !NumRFIDError; |
118 |
|
119 |
(EnterAnimalCommand as Command)?.ChangeCanExecute(); |
120 |
} |
121 |
|
122 |
async public Task LoadWeight() |
123 |
{ |
124 |
var animal = App.Db.Animal.FirstOrDefault(a => a.Rfid.EndsWith(NumRFID)); |
125 |
NumRFID = animal?.Rfid ?? ""; |
126 |
Poids = animal?.Poids / 1000; |
127 |
DatePesee = DateOnly.FromDateTime(animal?.DateDernierePesee ?? default); |
128 |
} |
129 |
|
130 |
#endregion |
131 |
} |
132 |
} |