Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateDayViewModel.cs @ 18f910c6

Historique | Voir | Annoter | Télécharger (3,792 ko)

1 65ad7e66 Lucas Bihannic
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
    public class CreateDayViewModel : BaseViewModel
9
    {
10
11 18f910c6 Lucas Bihannic
        #region Attributs
12 65ad7e66 Lucas Bihannic
        private Personne _selectedPerson;
13 18f910c6 Lucas Bihannic
        private string _espece;
14
        private string _regime;
15
        private bool _isFormValid;
16
        #endregion
17
18
        #region Commandes
19
        public ICommand CreateDayCommand { get; }
20
        public ICommand AddPersonCommand { get; }
21
        public ICommand AddPlaceCommand { get; }
22
        #endregion
23
24
        #region Propriétés
25
        public ObservableCollection<Personne> Persons => PersonViewModel.Instance.Persons;
26 65ad7e66 Lucas Bihannic
        public Personne SelectedPerson
27
        {
28
            get => _selectedPerson;
29
            set
30
            {
31
                _selectedPerson = value;
32
                OnPropertyChanged();
33
                ValidateForm();
34
            }
35
        }
36
37
        public ObservableCollection<Lieu> Places => PlaceViewModel.Instance.Places;
38
39
        private Lieu _selectedPlace;
40
        public Lieu SelectedPlace
41
        {
42
            get => _selectedPlace;
43
            set {
44
                _selectedPlace = value;
45
                OnPropertyChanged();
46
                ValidateForm();
47
            }
48
        }
49
50 18f910c6 Lucas Bihannic
        
51 65ad7e66 Lucas Bihannic
        public string Espece
52
        {
53
            get => _espece;
54
            set
55
            {
56
                _espece = value;
57
                OnPropertyChanged();
58
                ValidateForm();
59
            }
60
        }
61
62 18f910c6 Lucas Bihannic
        
63 65ad7e66 Lucas Bihannic
        public string Regime
64
        {
65
            get => _regime;
66
            set
67
            {
68
                _regime = value;
69
                OnPropertyChanged();
70
                ValidateForm();
71
            }
72
        }
73 18f910c6 Lucas Bihannic
        
74 65ad7e66 Lucas Bihannic
        public bool IsFormValid
75
        {
76
            get => _isFormValid;
77
            set
78
            {
79
                _isFormValid = value;
80
                OnPropertyChanged();
81
            }
82
        }
83
84
        public bool PlaceError { get; private set; } = true;
85
        public bool PersonError { get; private set; } = true;
86
        public bool EspeceError { get; private set; } = true;
87
        public bool RegimeError { get; private set; } = true;
88 18f910c6 Lucas Bihannic
        #endregion
89 65ad7e66 Lucas Bihannic
90 18f910c6 Lucas Bihannic
        #region Constructeurs
91 65ad7e66 Lucas Bihannic
        public CreateDayViewModel()
92
        {
93
            CreateDayCommand = new Command(async () => await CreateDay());
94
            AddPersonCommand = new Command(async () => await AddPerson());
95
            AddPlaceCommand = new Command(async () => await AddPlace());
96
        }
97 18f910c6 Lucas Bihannic
        #endregion
98 65ad7e66 Lucas Bihannic
99 18f910c6 Lucas Bihannic
        #region Méthodes
100 65ad7e66 Lucas Bihannic
        private async Task AddPerson() => await Shell.Current.GoToAsync(nameof(CreatePersonView));
101
        private async Task AddPlace() => await Shell.Current.GoToAsync(nameof(CreatePlaceView));
102
        private async Task CreateDay()
103
        {
104 18f910c6 Lucas Bihannic
            IsBusy = true;
105 65ad7e66 Lucas Bihannic
            var date = DateTime.Now;
106
            var newDay = new Journee(date, SelectedPerson, SelectedPlace, Espece, Regime);
107
            JourneeViewModel.Instance.Journees.Add(newDay);
108 18f910c6 Lucas Bihannic
            await Shell.Current.GoToAsync("//Main"); // A changer plus tard
109
            IsBusy = false;
110 65ad7e66 Lucas Bihannic
        }
111
112
        private void ValidateForm()
113
        {
114
            PersonError = SelectedPerson == null;
115
            PlaceError = SelectedPlace == null;
116
            EspeceError = string.IsNullOrWhiteSpace(Espece) || Espece.Length < 3;
117
            RegimeError = string.IsNullOrWhiteSpace(Regime) || Regime.Length < 3;
118
119
            OnPropertyChanged(nameof(PlaceError));
120
            OnPropertyChanged(nameof(PersonError));
121
            OnPropertyChanged(nameof(EspeceError));
122
            OnPropertyChanged(nameof(RegimeError));
123
124
            IsFormValid = !PlaceError && !PersonError && !EspeceError && !RegimeError;
125
126
            (CreateDayCommand as Command)?.ChangeCanExecute();
127
        }
128 18f910c6 Lucas Bihannic
        #endregion
129 65ad7e66 Lucas Bihannic
    }
130
}