rfid-cb / CodeBarreRFID / ViewModel / AnimalViewModel.cs @ 0f2b4298
Historique | Voir | Annoter | Télécharger (3,329 ko)
1 | 0f2b4298 | ajournaux | using CodeBarreRFID.DAO; |
---|---|---|---|
2 | using CodeBarreRFID.Model; |
||
3 | using CodeBarreRFID.View; |
||
4 | using CodeBarreRFID.ViewModel.Base; |
||
5 | using CommunityToolkit.Maui.Alerts; |
||
6 | using CommunityToolkit.Maui.Core; |
||
7 | using System; |
||
8 | using System.Collections.Generic; |
||
9 | using System.Collections.ObjectModel; |
||
10 | using System.Linq; |
||
11 | using System.Text; |
||
12 | using System.Threading.Tasks; |
||
13 | using System.Windows.Input; |
||
14 | |||
15 | namespace CodeBarreRFID.ViewModel |
||
16 | { |
||
17 | public class AnimalViewModel : BaseViewModel |
||
18 | { |
||
19 | #region Attributs |
||
20 | private String animalRfid; |
||
21 | private String animalCodeBarre; |
||
22 | private int nbAnimaux; |
||
23 | #endregion |
||
24 | |||
25 | #region Commandes |
||
26 | public ICommand AjouterCommand { get; private set; } |
||
27 | public ICommand EffaceRFIDCommand { get; private set; } |
||
28 | public ICommand EffaceCBCommand { get; private set; } |
||
29 | #endregion |
||
30 | |||
31 | #region Propriétés |
||
32 | public String AnimalRfid |
||
33 | { |
||
34 | get { return animalRfid; } |
||
35 | set |
||
36 | { |
||
37 | if (animalRfid != value) |
||
38 | { |
||
39 | animalRfid = value; |
||
40 | OnPropertyChanged(nameof(AnimalRfid)); |
||
41 | ((Command)AjouterCommand).ChangeCanExecute(); |
||
42 | } |
||
43 | } |
||
44 | } |
||
45 | |||
46 | public string AnimalCodeBarre |
||
47 | { |
||
48 | get { return animalCodeBarre; } |
||
49 | set |
||
50 | { |
||
51 | if (animalCodeBarre != value) |
||
52 | { |
||
53 | animalCodeBarre = value; |
||
54 | OnPropertyChanged(nameof(AnimalCodeBarre)); |
||
55 | ((Command)AjouterCommand).ChangeCanExecute(); |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | |||
60 | public int NbAnimaux |
||
61 | { |
||
62 | get { return nbAnimaux; } |
||
63 | set |
||
64 | { |
||
65 | if (nbAnimaux != value) |
||
66 | { |
||
67 | nbAnimaux = value; |
||
68 | OnPropertyChanged(nameof(NbAnimaux)); |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | |||
74 | #endregion |
||
75 | |||
76 | #region Constructeurs |
||
77 | public AnimalViewModel() |
||
78 | { |
||
79 | AnimalDAO.LoadFromCSV(); |
||
80 | NbAnimaux = AnimalDAO.ANIMAUX.Count; |
||
81 | AjouterCommand = new Command(async () => await Ajouter(), CanAjouter); |
||
82 | EffaceRFIDCommand = new Command(EffaceRFID); |
||
83 | EffaceCBCommand = new Command(EffaceCB); |
||
84 | } |
||
85 | #endregion |
||
86 | |||
87 | |||
88 | private async Task Ajouter() |
||
89 | { |
||
90 | IsBusy = true; |
||
91 | await Task.Delay(100); |
||
92 | Animal animal = new Animal(); |
||
93 | animal.RFID = AnimalRfid; |
||
94 | animal.CodeBarre = AnimalCodeBarre; |
||
95 | AnimalDAO.ANIMAUX.Add(animal); |
||
96 | AnimalDAO.SaveToCSV(); |
||
97 | var toast = Toast.Make("Enregistrement OK",ToastDuration.Long, 14); |
||
98 | await toast.Show(); |
||
99 | NbAnimaux = AnimalDAO.ANIMAUX.Count; |
||
100 | AnimalRfid = ""; |
||
101 | AnimalCodeBarre = ""; |
||
102 | IsBusy = false; |
||
103 | } |
||
104 | |||
105 | public bool CanAjouter() |
||
106 | { |
||
107 | return !String.IsNullOrEmpty(AnimalRfid) && !String.IsNullOrEmpty(AnimalCodeBarre); |
||
108 | } |
||
109 | |||
110 | private void EffaceRFID() |
||
111 | { |
||
112 | AnimalRfid = ""; |
||
113 | } |
||
114 | |||
115 | private void EffaceCB() |
||
116 | { |
||
117 | AnimalCodeBarre = ""; |
||
118 | } |
||
119 | |||
120 | |||
121 | } |
||
122 | } |