Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateBehaviourViewModel.cs @ 9fd69a0e

Historique | Voir | Annoter | Télécharger (2,929 ko)

1
using GES_PAC.Model;
2
using GES_PAC.View;
3
using System.Collections.ObjectModel;
4
using System.Windows.Input;
5

    
6
namespace GES_PAC.ViewModel
7
{
8
    [QueryProperty(nameof(ChamberId), "chamberId")]
9
    public class CreateBehaviourViewModel : BaseViewModel
10
    {
11

    
12
        #region Attributs
13
        private int _chamberId;
14
        private TypeComportement _selectedType;
15
        private string _description;
16
        private bool _isFormValid;
17
        private bool _isAnimalOut;
18
        #endregion
19

    
20
        #region Commandes
21
        public ICommand CreateBehaviourCommand { get; }
22
        #endregion
23

    
24
        #region Propriétés
25
        public int ChamberId
26
        {
27
            get => _chamberId;
28
            set
29
            {
30
                _chamberId = value;
31
                OnPropertyChanged();
32
                OnPropertyChanged(nameof(Titre));
33
            }
34
        }
35
        public ObservableCollection<TypeComportement> TypeComp { get; }
36
        public TypeComportement SelectedType
37
        {
38
            get => _selectedType;
39
            set
40
            {
41
                _selectedType = value;
42
                OnPropertyChanged();
43
                ValidateForm();
44
            }
45
        }
46
        public string Description
47
        {
48
            get => _description;
49
            set
50
            {
51
                _description = value;
52
                OnPropertyChanged();
53
                ValidateForm();
54
            }
55
        }
56
        public bool IsAnimalOut
57
        {
58
            get => _isAnimalOut;
59
            set => SetProperty(ref _isAnimalOut, value);
60
        }
61

    
62
        public bool IsFormValid
63
        {
64
            get => _isFormValid;
65
            set
66
            {
67
                _isFormValid = value;
68
                OnPropertyChanged();
69
            }
70
        }
71
        public string Titre => $"Comportement chambre {ChamberId}";
72

    
73
        public bool TypeError { get; private set; } = true;
74
        #endregion
75

    
76
        #region Constructeurs
77
        public CreateBehaviourViewModel()
78
        {
79
            CreateBehaviourCommand = new Command(async () => await CreateBehaviour());
80

    
81
            TypeComp = new ObservableCollection<TypeComportement>(TypeComportement.All);
82
        }
83
        #endregion
84

    
85
        #region Méthodes
86
        private async Task CreateBehaviour()
87
        {
88
            if (IsBusy) return;
89
            IsBusy = true;
90

    
91
            var date = DateTime.Now;
92
            var set = JourneeViewModel.Instance.GetCurrentSet();
93
            var newBehaviour = new Comportement(date, SelectedType, Description);
94
            set.AddBehaviour(newBehaviour, ChamberId, IsAnimalOut);
95

    
96
            await Shell.Current.GoToAsync(nameof(ChambersView));
97
            IsBusy = false;
98
        }
99

    
100
        private void ValidateForm()
101
        {
102
            TypeError = SelectedType == null;
103

    
104
            OnPropertyChanged(nameof(TypeError));
105

    
106
            IsFormValid = !TypeError;
107

    
108
            (CreateBehaviourCommand as Command)?.ChangeCanExecute();
109
        }
110
        #endregion
111
    }
112
}