root / GES_PAC / ViewModel / Controls / ChamberButtonViewModel.cs @ e837cdf1
Historique | Voir | Annoter | Télécharger (4,479 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 ICommand _onClickChamberCommand; |
18 |
private readonly TimerPublisher _timer; |
19 |
|
20 |
#endregion |
21 |
|
22 |
#region Propriétés |
23 |
public Serie CurrentSet |
24 |
{ |
25 |
get => _currentSet; |
26 |
set |
27 |
{ |
28 |
_currentSet = value; |
29 |
OnPropertyChanged(); |
30 |
} |
31 |
} |
32 |
public string ButtonText { |
33 |
get => _buttonText; |
34 |
set |
35 |
{ |
36 |
_buttonText = value; |
37 |
OnPropertyChanged(); |
38 |
} |
39 |
} |
40 |
public int ChamberId |
41 |
{ |
42 |
get => _chamberId; |
43 |
set |
44 |
{ |
45 |
_chamberId = value; |
46 |
OnPropertyChanged(); |
47 |
} |
48 |
} |
49 |
public Color ButtonBackgroundColor |
50 |
{ |
51 |
get => _buttonBackgroundColor; |
52 |
set |
53 |
{ |
54 |
_buttonBackgroundColor = value; |
55 |
OnPropertyChanged(); |
56 |
} |
57 |
} |
58 |
public bool ButtonIsEnabled |
59 |
{ |
60 |
get => _buttonIsEnabled; |
61 |
set |
62 |
{ |
63 |
_buttonIsEnabled = value; |
64 |
OnPropertyChanged(); |
65 |
} |
66 |
} |
67 |
public string ButtonLabel |
68 |
{ |
69 |
get => _buttonLabel; |
70 |
set |
71 |
{ |
72 |
_buttonLabel = value; |
73 |
OnPropertyChanged(); |
74 |
} |
75 |
} |
76 |
public string TimeIn |
77 |
{ |
78 |
get => _timeIn; |
79 |
set |
80 |
{ |
81 |
_timeIn = value; |
82 |
OnPropertyChanged(); |
83 |
} |
84 |
} |
85 |
public SerieAnimal AnimalSet; |
86 |
private bool _isSubscribed = false; |
87 |
|
88 |
#endregion |
89 |
|
90 |
#region Commandes |
91 |
public ICommand OnClickChamberCommand |
92 |
{ |
93 |
get => _onClickChamberCommand; |
94 |
set |
95 |
{ |
96 |
_onClickChamberCommand = value; |
97 |
OnPropertyChanged(); |
98 |
} |
99 |
} |
100 |
#endregion |
101 |
|
102 |
#region Constructeurs |
103 |
|
104 |
public ChamberButtonViewModel(int chamberId, TimerPublisher timerPublisher) |
105 |
{ |
106 |
ChamberId = chamberId; |
107 |
_timer = timerPublisher; |
108 |
|
109 |
OnClickChamberCommand = new Command(async () => await GoToChamber()); |
110 |
ButtonBackgroundColor = Colors.White; |
111 |
ButtonIsEnabled = true; |
112 |
ButtonLabel = "0"; |
113 |
TimeIn = ""; |
114 |
CurrentSet = JourneeViewModel.Instance.GetCurrentSet(); |
115 |
|
116 |
UpdateChamberId(chamberId); |
117 |
} |
118 |
|
119 |
#endregion |
120 |
|
121 |
#region Méthodes |
122 |
public void UpdateChamberId(int chamberId) |
123 |
{ |
124 |
ChamberId = chamberId; |
125 |
UpdateProperties(); |
126 |
} |
127 |
public void UpdateProperties() |
128 |
{ |
129 |
AnimalSet = CurrentSet.GetSerieAnimalByNumBoite(ChamberId); |
130 |
ButtonText = ChamberId.ToString(); |
131 |
|
132 |
int measureCount = AnimalSet?.GetMeasureCount() ?? 0; |
133 |
|
134 |
if (measureCount == 0) |
135 |
return; |
136 |
|
137 |
if (!_isSubscribed && !AnimalSet.IsOut) |
138 |
{ |
139 |
_timer.Register(OnTimerTick); |
140 |
_isSubscribed = true; |
141 |
} |
142 |
|
143 |
if (_isSubscribed && AnimalSet.IsOut) |
144 |
{ |
145 |
_timer.Unregister(OnTimerTick); |
146 |
_isSubscribed = false; |
147 |
} |
148 |
|
149 |
ButtonIsEnabled = !AnimalSet.IsOut; |
150 |
ButtonBackgroundColor = AnimalSet.HasBehaviour() ? Colors.Yellow : Colors.LightGreen; |
151 |
ButtonLabel = measureCount.ToString(); |
152 |
TimeIn = AnimalSet.GetTimeInChamber(); |
153 |
} |
154 |
|
155 |
private async Task GoToChamber() |
156 |
{ |
157 |
if (IsBusy) return; |
158 |
IsBusy = true; |
159 |
|
160 |
var targetView = (CurrentSet.GetSerieAnimalByNumBoite(ChamberId) == null) ? nameof(EnterAnimalView) : nameof(CreateMeasureView); |
161 |
|
162 |
await Shell.Current.GoToAsync($"{targetView}?chamberId={ChamberId}"); |
163 |
|
164 |
IsBusy = false; |
165 |
} |
166 |
public void OnTimerTick(object? sender, EventArgs e) |
167 |
{ |
168 |
TimeIn = AnimalSet?.GetTimeInChamber() ?? ""; |
169 |
} |
170 |
|
171 |
#endregion |
172 |
} |
173 |
} |