Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / Controls / ChamberButtonViewModel.cs @ 3fef487c

Historique | Voir | Annoter | Télécharger (5,538 ko)

1 12ddf7ef lbihannic
using GES_PAC.Model;
2
using GES_PAC.View;
3 3fef487c lbihannic
using System.ComponentModel.Design;
4 12ddf7ef lbihannic
using System.Windows.Input;
5
6
namespace GES_PAC.ViewModel.Controls
7
{
8
    public class ChamberButtonViewModel : BaseViewModel
9
    {
10
        #region Attributs
11 e837cdf1 lbihannic
        private Serie _currentSet;
12 12ddf7ef lbihannic
        private string _buttonText;
13 957b1f34 lbihannic
        private int _chamberId;
14 12ddf7ef lbihannic
        private Color _buttonBackgroundColor;
15
        private bool _buttonIsEnabled;
16
        private string _buttonLabel;
17 e837cdf1 lbihannic
        private string _timeIn;
18 9601eaf0 lbihannic
        private Color _timeColor;
19 12ddf7ef lbihannic
        private ICommand _onClickChamberCommand;
20 e837cdf1 lbihannic
        private readonly TimerPublisher _timer;
21
22 12ddf7ef lbihannic
        #endregion
23
24
        #region Propriétés
25 42456640 lbihannic
        public Serie? CurrentSet
26 e837cdf1 lbihannic
        {
27
            get => _currentSet;
28
            set
29
            {
30
                _currentSet = value;
31
                OnPropertyChanged();
32
            }
33
        }
34 12ddf7ef lbihannic
        public string ButtonText {
35
            get => _buttonText;
36
            set
37
            {
38
                _buttonText = value;
39
                OnPropertyChanged();
40
            }
41
        }
42 957b1f34 lbihannic
        public int ChamberId
43 12ddf7ef lbihannic
        {
44 957b1f34 lbihannic
            get => _chamberId;
45 12ddf7ef lbihannic
            set
46
            {
47 957b1f34 lbihannic
                _chamberId = value;
48 12ddf7ef lbihannic
                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 e837cdf1 lbihannic
        public string TimeIn
79
        {
80
            get => _timeIn;
81
            set
82
            {
83
                _timeIn = value;
84
                OnPropertyChanged();
85 9601eaf0 lbihannic
86
            }
87
        }
88
        public Color TimeColor
89
        {
90
            get => _timeColor;
91
            set
92
            {
93
                _timeColor = value;
94
                OnPropertyChanged();
95 e837cdf1 lbihannic
            }
96
        }
97 42456640 lbihannic
        public SerieAnimal? AnimalSet;
98 e837cdf1 lbihannic
        private bool _isSubscribed = false;
99
100 12ddf7ef lbihannic
        #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 957bebf1 lbihannic
        public ChamberButtonViewModel(int chamberId, ChambersViewModel parentViewModel)
117 12ddf7ef lbihannic
        {
118 e837cdf1 lbihannic
            ChamberId = chamberId;
119 957bebf1 lbihannic
            _timer = parentViewModel.Timer;
120
            parentViewModel.RegisterButton(this);
121 12ddf7ef lbihannic
122 e837cdf1 lbihannic
            OnClickChamberCommand = new Command(async () => await GoToChamber());
123 957bebf1 lbihannic
            InitButtonProperties();
124 42456640 lbihannic
            CurrentSet = JourneeViewModel.Instance?.GetCurrentSet();
125 12ddf7ef lbihannic
126 e837cdf1 lbihannic
            UpdateChamberId(chamberId);
127
        }
128
129
        #endregion
130
131
        #region Méthodes
132
        public void UpdateChamberId(int chamberId)
133
        {
134 957b1f34 lbihannic
            ChamberId = chamberId;
135 e837cdf1 lbihannic
            UpdateProperties();
136
        }
137 957bebf1 lbihannic
        public void InitButtonProperties()
138
        {
139
            ButtonBackgroundColor = Colors.White;
140
            ButtonIsEnabled = true;
141
            ButtonLabel = "0";
142
            TimeIn = "";
143 3fef487c lbihannic
            TimeColor = Colors.Black;
144 957bebf1 lbihannic
        }
145 e837cdf1 lbihannic
        public void UpdateProperties()
146
        {
147 42456640 lbihannic
            CurrentSet = JourneeViewModel.Instance?.GetCurrentSet();
148
            AnimalSet = CurrentSet?.GetSerieAnimalByNumBoite(ChamberId);
149 e837cdf1 lbihannic
            ButtonText = ChamberId.ToString();
150 957bebf1 lbihannic
            InitButtonProperties();
151 e837cdf1 lbihannic
152
            int measureCount = AnimalSet?.GetMeasureCount() ?? 0;
153 12ddf7ef lbihannic
154 e837cdf1 lbihannic
            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 3fef487c lbihannic
            UpdateTimerColor();
170 e837cdf1 lbihannic
            ButtonIsEnabled = !AnimalSet.IsOut;
171
            ButtonBackgroundColor = AnimalSet.HasBehaviour() ? Colors.Yellow : Colors.LightGreen;
172
            ButtonLabel = measureCount.ToString();
173 9601eaf0 lbihannic
            TimeIn = AnimalSet.GetFormatedTimeInChamber();
174 12ddf7ef lbihannic
        }
175 e837cdf1 lbihannic
176 9601eaf0 lbihannic
177 e837cdf1 lbihannic
        private async Task GoToChamber()
178 12ddf7ef lbihannic
        {
179
            if (IsBusy) return;
180
            IsBusy = true;
181
182 e837cdf1 lbihannic
            var targetView = (CurrentSet.GetSerieAnimalByNumBoite(ChamberId) == null) ? nameof(EnterAnimalView) : nameof(CreateMeasureView);
183 957b1f34 lbihannic
184
            await Shell.Current.GoToAsync($"{targetView}?chamberId={ChamberId}");
185 12ddf7ef lbihannic
186
            IsBusy = false;
187
        }
188 e837cdf1 lbihannic
        public void OnTimerTick(object? sender, EventArgs e)
189
        {
190 9601eaf0 lbihannic
            TimeIn = AnimalSet?.GetFormatedTimeInChamber() ?? "";
191 3fef487c lbihannic
            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 e837cdf1 lbihannic
        }
203
204 12ddf7ef lbihannic
        #endregion
205
    }
206
}