root / GES_PAC / ViewModel / Controls / ChamberButtonViewModel.cs @ 9601eaf0
Historique | Voir | Annoter | Télécharger (5,15 ko)
1 | 12ddf7ef | lbihannic | using GES_PAC.Model; |
---|---|---|---|
2 | using GES_PAC.View; |
||
3 | using System.Windows.Input; |
||
4 | |||
5 | namespace GES_PAC.ViewModel.Controls |
||
6 | { |
||
7 | public class ChamberButtonViewModel : BaseViewModel |
||
8 | { |
||
9 | #region Attributs |
||
10 | e837cdf1 | lbihannic | private Serie _currentSet; |
11 | 12ddf7ef | lbihannic | private string _buttonText; |
12 | 957b1f34 | lbihannic | private int _chamberId; |
13 | 12ddf7ef | lbihannic | private Color _buttonBackgroundColor; |
14 | private bool _buttonIsEnabled; |
||
15 | private string _buttonLabel; |
||
16 | e837cdf1 | lbihannic | private string _timeIn; |
17 | 9601eaf0 | lbihannic | private Color _timeColor; |
18 | 12ddf7ef | lbihannic | private ICommand _onClickChamberCommand; |
19 | e837cdf1 | lbihannic | private readonly TimerPublisher _timer; |
20 | |||
21 | 12ddf7ef | lbihannic | #endregion |
22 | |||
23 | #region Propriétés |
||
24 | 42456640 | lbihannic | public Serie? CurrentSet |
25 | e837cdf1 | lbihannic | { |
26 | get => _currentSet; |
||
27 | set |
||
28 | { |
||
29 | _currentSet = value; |
||
30 | OnPropertyChanged(); |
||
31 | } |
||
32 | } |
||
33 | 12ddf7ef | lbihannic | public string ButtonText { |
34 | get => _buttonText; |
||
35 | set |
||
36 | { |
||
37 | _buttonText = value; |
||
38 | OnPropertyChanged(); |
||
39 | } |
||
40 | } |
||
41 | 957b1f34 | lbihannic | public int ChamberId |
42 | 12ddf7ef | lbihannic | { |
43 | 957b1f34 | lbihannic | get => _chamberId; |
44 | 12ddf7ef | lbihannic | set |
45 | { |
||
46 | 957b1f34 | lbihannic | _chamberId = value; |
47 | 12ddf7ef | lbihannic | OnPropertyChanged(); |
48 | } |
||
49 | } |
||
50 | public Color ButtonBackgroundColor |
||
51 | { |
||
52 | get => _buttonBackgroundColor; |
||
53 | set |
||
54 | { |
||
55 | _buttonBackgroundColor = value; |
||
56 | OnPropertyChanged(); |
||
57 | } |
||
58 | } |
||
59 | public bool ButtonIsEnabled |
||
60 | { |
||
61 | get => _buttonIsEnabled; |
||
62 | set |
||
63 | { |
||
64 | _buttonIsEnabled = value; |
||
65 | OnPropertyChanged(); |
||
66 | } |
||
67 | } |
||
68 | public string ButtonLabel |
||
69 | { |
||
70 | get => _buttonLabel; |
||
71 | set |
||
72 | { |
||
73 | _buttonLabel = value; |
||
74 | OnPropertyChanged(); |
||
75 | } |
||
76 | } |
||
77 | e837cdf1 | lbihannic | public string TimeIn |
78 | { |
||
79 | get => _timeIn; |
||
80 | set |
||
81 | { |
||
82 | _timeIn = value; |
||
83 | OnPropertyChanged(); |
||
84 | 9601eaf0 | lbihannic | |
85 | } |
||
86 | } |
||
87 | public Color TimeColor |
||
88 | { |
||
89 | get => _timeColor; |
||
90 | set |
||
91 | { |
||
92 | _timeColor = value; |
||
93 | OnPropertyChanged(); |
||
94 | e837cdf1 | lbihannic | } |
95 | } |
||
96 | 42456640 | lbihannic | public SerieAnimal? AnimalSet; |
97 | e837cdf1 | lbihannic | private bool _isSubscribed = false; |
98 | |||
99 | 12ddf7ef | lbihannic | #endregion |
100 | |||
101 | #region Commandes |
||
102 | public ICommand OnClickChamberCommand |
||
103 | { |
||
104 | get => _onClickChamberCommand; |
||
105 | set |
||
106 | { |
||
107 | _onClickChamberCommand = value; |
||
108 | OnPropertyChanged(); |
||
109 | } |
||
110 | } |
||
111 | #endregion |
||
112 | |||
113 | #region Constructeurs |
||
114 | |||
115 | 957bebf1 | lbihannic | public ChamberButtonViewModel(int chamberId, ChambersViewModel parentViewModel) |
116 | 12ddf7ef | lbihannic | { |
117 | e837cdf1 | lbihannic | ChamberId = chamberId; |
118 | 957bebf1 | lbihannic | _timer = parentViewModel.Timer; |
119 | parentViewModel.RegisterButton(this); |
||
120 | 12ddf7ef | lbihannic | |
121 | e837cdf1 | lbihannic | OnClickChamberCommand = new Command(async () => await GoToChamber()); |
122 | 957bebf1 | lbihannic | InitButtonProperties(); |
123 | 42456640 | lbihannic | CurrentSet = JourneeViewModel.Instance?.GetCurrentSet(); |
124 | 12ddf7ef | lbihannic | |
125 | e837cdf1 | lbihannic | UpdateChamberId(chamberId); |
126 | } |
||
127 | |||
128 | #endregion |
||
129 | |||
130 | #region Méthodes |
||
131 | public void UpdateChamberId(int chamberId) |
||
132 | { |
||
133 | 957b1f34 | lbihannic | ChamberId = chamberId; |
134 | e837cdf1 | lbihannic | UpdateProperties(); |
135 | } |
||
136 | 957bebf1 | lbihannic | public void InitButtonProperties() |
137 | { |
||
138 | ButtonBackgroundColor = Colors.White; |
||
139 | ButtonIsEnabled = true; |
||
140 | ButtonLabel = "0"; |
||
141 | TimeIn = ""; |
||
142 | } |
||
143 | e837cdf1 | lbihannic | public void UpdateProperties() |
144 | { |
||
145 | 42456640 | lbihannic | CurrentSet = JourneeViewModel.Instance?.GetCurrentSet(); |
146 | AnimalSet = CurrentSet?.GetSerieAnimalByNumBoite(ChamberId); |
||
147 | e837cdf1 | lbihannic | ButtonText = ChamberId.ToString(); |
148 | 957bebf1 | lbihannic | InitButtonProperties(); |
149 | e837cdf1 | lbihannic | |
150 | int measureCount = AnimalSet?.GetMeasureCount() ?? 0; |
||
151 | 12ddf7ef | lbihannic | |
152 | e837cdf1 | lbihannic | if (measureCount == 0) |
153 | return; |
||
154 | |||
155 | if (!_isSubscribed && !AnimalSet.IsOut) |
||
156 | { |
||
157 | _timer.Register(OnTimerTick); |
||
158 | _isSubscribed = true; |
||
159 | } |
||
160 | |||
161 | if (_isSubscribed && AnimalSet.IsOut) |
||
162 | { |
||
163 | _timer.Unregister(OnTimerTick); |
||
164 | _isSubscribed = false; |
||
165 | } |
||
166 | |||
167 | ButtonIsEnabled = !AnimalSet.IsOut; |
||
168 | ButtonBackgroundColor = AnimalSet.HasBehaviour() ? Colors.Yellow : Colors.LightGreen; |
||
169 | ButtonLabel = measureCount.ToString(); |
||
170 | 9601eaf0 | lbihannic | TimeIn = AnimalSet.GetFormatedTimeInChamber(); |
171 | TimeColor = AnimalSet.IsTimeWarning() ? Colors.Red : Colors.Black; |
||
172 | 12ddf7ef | lbihannic | } |
173 | e837cdf1 | lbihannic | |
174 | 9601eaf0 | lbihannic | |
175 | e837cdf1 | lbihannic | private async Task GoToChamber() |
176 | 12ddf7ef | lbihannic | { |
177 | if (IsBusy) return; |
||
178 | IsBusy = true; |
||
179 | |||
180 | e837cdf1 | lbihannic | var targetView = (CurrentSet.GetSerieAnimalByNumBoite(ChamberId) == null) ? nameof(EnterAnimalView) : nameof(CreateMeasureView); |
181 | 957b1f34 | lbihannic | |
182 | await Shell.Current.GoToAsync($"{targetView}?chamberId={ChamberId}"); |
||
183 | 12ddf7ef | lbihannic | |
184 | IsBusy = false; |
||
185 | } |
||
186 | e837cdf1 | lbihannic | public void OnTimerTick(object? sender, EventArgs e) |
187 | { |
||
188 | 9601eaf0 | lbihannic | TimeIn = AnimalSet?.GetFormatedTimeInChamber() ?? ""; |
189 | TimeColor = AnimalSet?.IsTimeWarning() ?? false ? Colors.Red : Colors.Black; |
||
190 | e837cdf1 | lbihannic | } |
191 | |||
192 | 12ddf7ef | lbihannic | #endregion |
193 | } |
||
194 | } |