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