root / GES_PAC / ViewModel / Controls / ChamberButtonViewModel.cs @ 9601eaf0
Historique | Voir | Annoter | Télécharger (5,15 ko)
1 |
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 |
private Serie _currentSet; |
11 |
private string _buttonText; |
12 |
private int _chamberId; |
13 |
private Color _buttonBackgroundColor; |
14 |
private bool _buttonIsEnabled; |
15 |
private string _buttonLabel; |
16 |
private string _timeIn; |
17 |
private Color _timeColor; |
18 |
private ICommand _onClickChamberCommand; |
19 |
private readonly TimerPublisher _timer; |
20 |
|
21 |
#endregion |
22 |
|
23 |
#region Propriétés |
24 |
public Serie? CurrentSet |
25 |
{ |
26 |
get => _currentSet; |
27 |
set |
28 |
{ |
29 |
_currentSet = value; |
30 |
OnPropertyChanged(); |
31 |
} |
32 |
} |
33 |
public string ButtonText { |
34 |
get => _buttonText; |
35 |
set |
36 |
{ |
37 |
_buttonText = value; |
38 |
OnPropertyChanged(); |
39 |
} |
40 |
} |
41 |
public int ChamberId |
42 |
{ |
43 |
get => _chamberId; |
44 |
set |
45 |
{ |
46 |
_chamberId = value; |
47 |
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 |
public string TimeIn |
78 |
{ |
79 |
get => _timeIn; |
80 |
set |
81 |
{ |
82 |
_timeIn = value; |
83 |
OnPropertyChanged(); |
84 |
|
85 |
} |
86 |
} |
87 |
public Color TimeColor |
88 |
{ |
89 |
get => _timeColor; |
90 |
set |
91 |
{ |
92 |
_timeColor = value; |
93 |
OnPropertyChanged(); |
94 |
} |
95 |
} |
96 |
public SerieAnimal? AnimalSet; |
97 |
private bool _isSubscribed = false; |
98 |
|
99 |
#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 |
public ChamberButtonViewModel(int chamberId, ChambersViewModel parentViewModel) |
116 |
{ |
117 |
ChamberId = chamberId; |
118 |
_timer = parentViewModel.Timer; |
119 |
parentViewModel.RegisterButton(this); |
120 |
|
121 |
OnClickChamberCommand = new Command(async () => await GoToChamber()); |
122 |
InitButtonProperties(); |
123 |
CurrentSet = JourneeViewModel.Instance?.GetCurrentSet(); |
124 |
|
125 |
UpdateChamberId(chamberId); |
126 |
} |
127 |
|
128 |
#endregion |
129 |
|
130 |
#region Méthodes |
131 |
public void UpdateChamberId(int chamberId) |
132 |
{ |
133 |
ChamberId = chamberId; |
134 |
UpdateProperties(); |
135 |
} |
136 |
public void InitButtonProperties() |
137 |
{ |
138 |
ButtonBackgroundColor = Colors.White; |
139 |
ButtonIsEnabled = true; |
140 |
ButtonLabel = "0"; |
141 |
TimeIn = ""; |
142 |
} |
143 |
public void UpdateProperties() |
144 |
{ |
145 |
CurrentSet = JourneeViewModel.Instance?.GetCurrentSet(); |
146 |
AnimalSet = CurrentSet?.GetSerieAnimalByNumBoite(ChamberId); |
147 |
ButtonText = ChamberId.ToString(); |
148 |
InitButtonProperties(); |
149 |
|
150 |
int measureCount = AnimalSet?.GetMeasureCount() ?? 0; |
151 |
|
152 |
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 |
TimeIn = AnimalSet.GetFormatedTimeInChamber(); |
171 |
TimeColor = AnimalSet.IsTimeWarning() ? Colors.Red : Colors.Black; |
172 |
} |
173 |
|
174 |
|
175 |
private async Task GoToChamber() |
176 |
{ |
177 |
if (IsBusy) return; |
178 |
IsBusy = true; |
179 |
|
180 |
var targetView = (CurrentSet.GetSerieAnimalByNumBoite(ChamberId) == null) ? nameof(EnterAnimalView) : nameof(CreateMeasureView); |
181 |
|
182 |
await Shell.Current.GoToAsync($"{targetView}?chamberId={ChamberId}"); |
183 |
|
184 |
IsBusy = false; |
185 |
} |
186 |
public void OnTimerTick(object? sender, EventArgs e) |
187 |
{ |
188 |
TimeIn = AnimalSet?.GetFormatedTimeInChamber() ?? ""; |
189 |
TimeColor = AnimalSet?.IsTimeWarning() ?? false ? Colors.Red : Colors.Black; |
190 |
} |
191 |
|
192 |
#endregion |
193 |
} |
194 |
} |