Statistiques
| Branche: | Révision:

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

Historique | Voir | Annoter | Télécharger (4,479 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 e837cdf1 lbihannic
        public Serie CurrentSet
24
        {
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
        public SerieAnimal AnimalSet;
86
        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 12ddf7ef lbihannic
            CurrentSet = JourneeViewModel.Instance.GetCurrentSet();
115
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
            AnimalSet = CurrentSet.GetSerieAnimalByNumBoite(ChamberId);
130
            ButtonText = ChamberId.ToString();
131
132
            int measureCount = AnimalSet?.GetMeasureCount() ?? 0;
133 12ddf7ef lbihannic
134 e837cdf1 lbihannic
            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 12ddf7ef lbihannic
        }
154 e837cdf1 lbihannic
155
        private async Task GoToChamber()
156 12ddf7ef lbihannic
        {
157
            if (IsBusy) return;
158
            IsBusy = true;
159
160 e837cdf1 lbihannic
            var targetView = (CurrentSet.GetSerieAnimalByNumBoite(ChamberId) == null) ? nameof(EnterAnimalView) : nameof(CreateMeasureView);
161 957b1f34 lbihannic
162
            await Shell.Current.GoToAsync($"{targetView}?chamberId={ChamberId}");
163 12ddf7ef lbihannic
164
            IsBusy = false;
165
        }
166 e837cdf1 lbihannic
        public void OnTimerTick(object? sender, EventArgs e)
167
        {
168
            TimeIn = AnimalSet?.GetTimeInChamber() ?? "";
169
        }
170
171 12ddf7ef lbihannic
        #endregion
172
    }
173
}