Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / Controls / ChamberButtonViewModel.cs @ 42456640

Historique | Voir | Annoter | Télécharger (4,55 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 e837cdf1 lbihannic
        public ChamberButtonViewModel(int chamberId, TimerPublisher timerPublisher)
105 12ddf7ef lbihannic
        {
106 e837cdf1 lbihannic
            ChamberId = chamberId;
107
            _timer = timerPublisher;
108 12ddf7ef lbihannic
109 e837cdf1 lbihannic
            OnClickChamberCommand = new Command(async () => await GoToChamber());
110
            ButtonBackgroundColor = Colors.White;
111
            ButtonIsEnabled = true;
112
            ButtonLabel = "0";
113
            TimeIn = "";
114 42456640 lbihannic
            CurrentSet = JourneeViewModel.Instance?.GetCurrentSet();
115 12ddf7ef lbihannic
116 e837cdf1 lbihannic
            UpdateChamberId(chamberId);
117
        }
118
119
        #endregion
120
121
        #region Méthodes
122
        public void UpdateChamberId(int chamberId)
123
        {
124 957b1f34 lbihannic
            ChamberId = chamberId;
125 e837cdf1 lbihannic
            UpdateProperties();
126
        }
127
        public void UpdateProperties()
128
        {
129 42456640 lbihannic
            CurrentSet = JourneeViewModel.Instance?.GetCurrentSet();
130
            AnimalSet = CurrentSet?.GetSerieAnimalByNumBoite(ChamberId);
131 e837cdf1 lbihannic
            ButtonText = ChamberId.ToString();
132
133
            int measureCount = AnimalSet?.GetMeasureCount() ?? 0;
134 12ddf7ef lbihannic
135 e837cdf1 lbihannic
            if (measureCount == 0)
136
                return;
137
138
            if (!_isSubscribed && !AnimalSet.IsOut)
139
            {
140
                _timer.Register(OnTimerTick);
141
                _isSubscribed = true;
142
            }
143
144
            if (_isSubscribed && AnimalSet.IsOut)
145
            {
146
                _timer.Unregister(OnTimerTick);
147
                _isSubscribed = false;
148
            }
149
150
            ButtonIsEnabled = !AnimalSet.IsOut;
151
            ButtonBackgroundColor = AnimalSet.HasBehaviour() ? Colors.Yellow : Colors.LightGreen;
152
            ButtonLabel = measureCount.ToString();
153
            TimeIn = AnimalSet.GetTimeInChamber();
154 12ddf7ef lbihannic
        }
155 e837cdf1 lbihannic
156
        private async Task GoToChamber()
157 12ddf7ef lbihannic
        {
158
            if (IsBusy) return;
159
            IsBusy = true;
160
161 e837cdf1 lbihannic
            var targetView = (CurrentSet.GetSerieAnimalByNumBoite(ChamberId) == null) ? nameof(EnterAnimalView) : nameof(CreateMeasureView);
162 957b1f34 lbihannic
163
            await Shell.Current.GoToAsync($"{targetView}?chamberId={ChamberId}");
164 12ddf7ef lbihannic
165
            IsBusy = false;
166
        }
167 e837cdf1 lbihannic
        public void OnTimerTick(object? sender, EventArgs e)
168
        {
169
            TimeIn = AnimalSet?.GetTimeInChamber() ?? "";
170
        }
171
172 12ddf7ef lbihannic
        #endregion
173
    }
174
}