root / GES_PAC / ViewModel / Controls / ChamberButtonViewModel.cs @ 957bebf1
Historique | Voir | Annoter | Télécharger (4,741 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 | 12ddf7ef | lbihannic | private ICommand _onClickChamberCommand; |
18 | e837cdf1 | lbihannic | private readonly TimerPublisher _timer; |
19 | |||
20 | 12ddf7ef | lbihannic | #endregion |
21 | |||
22 | #region Propriétés |
||
23 | 42456640 | lbihannic | public Serie? CurrentSet |
24 | e837cdf1 | lbihannic | { |
25 | get => _currentSet; |
||
26 | set |
||
27 | { |
||
28 | _currentSet = value; |
||
29 | OnPropertyChanged(); |
||
30 | } |
||
31 | } |
||
32 | 12ddf7ef | lbihannic | public string ButtonText { |
33 | get => _buttonText; |
||
34 | set |
||
35 | { |
||
36 | _buttonText = value; |
||
37 | OnPropertyChanged(); |
||
38 | } |
||
39 | } |
||
40 | 957b1f34 | lbihannic | public int ChamberId |
41 | 12ddf7ef | lbihannic | { |
42 | 957b1f34 | lbihannic | get => _chamberId; |
43 | 12ddf7ef | lbihannic | set |
44 | { |
||
45 | 957b1f34 | lbihannic | _chamberId = value; |
46 | 12ddf7ef | lbihannic | 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 | e837cdf1 | lbihannic | public string TimeIn |
77 | { |
||
78 | get => _timeIn; |
||
79 | set |
||
80 | { |
||
81 | _timeIn = value; |
||
82 | OnPropertyChanged(); |
||
83 | } |
||
84 | } |
||
85 | 42456640 | lbihannic | public SerieAnimal? AnimalSet; |
86 | e837cdf1 | lbihannic | private bool _isSubscribed = false; |
87 | |||
88 | 12ddf7ef | lbihannic | #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 | 957bebf1 | lbihannic | public ChamberButtonViewModel(int chamberId, ChambersViewModel parentViewModel) |
105 | 12ddf7ef | lbihannic | { |
106 | e837cdf1 | lbihannic | ChamberId = chamberId; |
107 | 957bebf1 | lbihannic | _timer = parentViewModel.Timer; |
108 | parentViewModel.RegisterButton(this); |
||
109 | 12ddf7ef | lbihannic | |
110 | e837cdf1 | lbihannic | OnClickChamberCommand = new Command(async () => await GoToChamber()); |
111 | 957bebf1 | lbihannic | InitButtonProperties(); |
112 | 42456640 | lbihannic | CurrentSet = JourneeViewModel.Instance?.GetCurrentSet(); |
113 | 12ddf7ef | lbihannic | |
114 | e837cdf1 | lbihannic | UpdateChamberId(chamberId); |
115 | } |
||
116 | |||
117 | #endregion |
||
118 | |||
119 | #region Méthodes |
||
120 | public void UpdateChamberId(int chamberId) |
||
121 | { |
||
122 | 957b1f34 | lbihannic | ChamberId = chamberId; |
123 | e837cdf1 | lbihannic | UpdateProperties(); |
124 | } |
||
125 | 957bebf1 | lbihannic | public void InitButtonProperties() |
126 | { |
||
127 | ButtonBackgroundColor = Colors.White; |
||
128 | ButtonIsEnabled = true; |
||
129 | ButtonLabel = "0"; |
||
130 | TimeIn = ""; |
||
131 | } |
||
132 | e837cdf1 | lbihannic | public void UpdateProperties() |
133 | { |
||
134 | 42456640 | lbihannic | CurrentSet = JourneeViewModel.Instance?.GetCurrentSet(); |
135 | AnimalSet = CurrentSet?.GetSerieAnimalByNumBoite(ChamberId); |
||
136 | e837cdf1 | lbihannic | ButtonText = ChamberId.ToString(); |
137 | 957bebf1 | lbihannic | InitButtonProperties(); |
138 | e837cdf1 | lbihannic | |
139 | int measureCount = AnimalSet?.GetMeasureCount() ?? 0; |
||
140 | 12ddf7ef | lbihannic | |
141 | e837cdf1 | lbihannic | if (measureCount == 0) |
142 | return; |
||
143 | |||
144 | if (!_isSubscribed && !AnimalSet.IsOut) |
||
145 | { |
||
146 | _timer.Register(OnTimerTick); |
||
147 | _isSubscribed = true; |
||
148 | } |
||
149 | |||
150 | if (_isSubscribed && AnimalSet.IsOut) |
||
151 | { |
||
152 | _timer.Unregister(OnTimerTick); |
||
153 | _isSubscribed = false; |
||
154 | } |
||
155 | |||
156 | ButtonIsEnabled = !AnimalSet.IsOut; |
||
157 | ButtonBackgroundColor = AnimalSet.HasBehaviour() ? Colors.Yellow : Colors.LightGreen; |
||
158 | ButtonLabel = measureCount.ToString(); |
||
159 | TimeIn = AnimalSet.GetTimeInChamber(); |
||
160 | 12ddf7ef | lbihannic | } |
161 | e837cdf1 | lbihannic | |
162 | private async Task GoToChamber() |
||
163 | 12ddf7ef | lbihannic | { |
164 | if (IsBusy) return; |
||
165 | IsBusy = true; |
||
166 | |||
167 | e837cdf1 | lbihannic | var targetView = (CurrentSet.GetSerieAnimalByNumBoite(ChamberId) == null) ? nameof(EnterAnimalView) : nameof(CreateMeasureView); |
168 | 957b1f34 | lbihannic | |
169 | await Shell.Current.GoToAsync($"{targetView}?chamberId={ChamberId}"); |
||
170 | 12ddf7ef | lbihannic | |
171 | IsBusy = false; |
||
172 | } |
||
173 | e837cdf1 | lbihannic | public void OnTimerTick(object? sender, EventArgs e) |
174 | { |
||
175 | TimeIn = AnimalSet?.GetTimeInChamber() ?? ""; |
||
176 | } |
||
177 | |||
178 | 12ddf7ef | lbihannic | #endregion |
179 | } |
||
180 | } |