Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreateDayViewModel.cs @ 42456640

Historique | Voir | Annoter | Télécharger (3,347 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 _regime;
14
        private bool _isFormValid;
15
        #endregion
16
17
        #region Commandes
18
        public ICommand CreateDayCommand { get; }
19
        public ICommand AddPersonCommand { get; }
20
        public ICommand AddPlaceCommand { get; }
21
        #endregion
22
23
        #region Propriétés
24
        public ObservableCollection<Personne> Persons => PersonViewModel.Instance.Persons;
25 65ad7e66 Lucas Bihannic
        public Personne SelectedPerson
26
        {
27
            get => _selectedPerson;
28
            set
29
            {
30
                _selectedPerson = value;
31
                OnPropertyChanged();
32
                ValidateForm();
33
            }
34
        }
35
36
        public ObservableCollection<Lieu> Places => PlaceViewModel.Instance.Places;
37
38
        private Lieu _selectedPlace;
39
        public Lieu SelectedPlace
40
        {
41
            get => _selectedPlace;
42
            set {
43
                _selectedPlace = value;
44
                OnPropertyChanged();
45
                ValidateForm();
46
            }
47
        }
48 18f910c6 Lucas Bihannic
        
49 65ad7e66 Lucas Bihannic
        public string Regime
50
        {
51
            get => _regime;
52
            set
53
            {
54
                _regime = value;
55
                OnPropertyChanged();
56
                ValidateForm();
57
            }
58
        }
59 18f910c6 Lucas Bihannic
        
60 65ad7e66 Lucas Bihannic
        public bool IsFormValid
61
        {
62
            get => _isFormValid;
63
            set
64
            {
65
                _isFormValid = value;
66
                OnPropertyChanged();
67
            }
68
        }
69
70
        public bool PlaceError { get; private set; } = true;
71
        public bool PersonError { get; private set; } = true;
72
        public bool RegimeError { get; private set; } = true;
73 18f910c6 Lucas Bihannic
        #endregion
74 65ad7e66 Lucas Bihannic
75 18f910c6 Lucas Bihannic
        #region Constructeurs
76 65ad7e66 Lucas Bihannic
        public CreateDayViewModel()
77
        {
78
            CreateDayCommand = new Command(async () => await CreateDay());
79
            AddPersonCommand = new Command(async () => await AddPerson());
80
            AddPlaceCommand = new Command(async () => await AddPlace());
81
        }
82 18f910c6 Lucas Bihannic
        #endregion
83 65ad7e66 Lucas Bihannic
84 18f910c6 Lucas Bihannic
        #region Méthodes
85 65ad7e66 Lucas Bihannic
        private async Task AddPerson() => await Shell.Current.GoToAsync(nameof(CreatePersonView));
86
        private async Task AddPlace() => await Shell.Current.GoToAsync(nameof(CreatePlaceView));
87
        private async Task CreateDay()
88
        {
89 1019554c lbihannic
            if (IsBusy) return;
90 18f910c6 Lucas Bihannic
            IsBusy = true;
91 65ad7e66 Lucas Bihannic
            var date = DateTime.Now;
92 5d673ce0 Lucas Bihannic
            var newDay = new Journee(date, SelectedPerson, SelectedPlace, Regime);
93 65ad7e66 Lucas Bihannic
            JourneeViewModel.Instance.Journees.Add(newDay);
94 5d673ce0 Lucas Bihannic
            await Shell.Current.GoToAsync(nameof(CreateCalibrationView));
95 18f910c6 Lucas Bihannic
            IsBusy = false;
96 65ad7e66 Lucas Bihannic
        }
97
98
        private void ValidateForm()
99
        {
100
            PersonError = SelectedPerson == null;
101
            PlaceError = SelectedPlace == null;
102
            RegimeError = string.IsNullOrWhiteSpace(Regime) || Regime.Length < 3;
103
104
            OnPropertyChanged(nameof(PlaceError));
105
            OnPropertyChanged(nameof(PersonError));
106
            OnPropertyChanged(nameof(RegimeError));
107
108 5d673ce0 Lucas Bihannic
            IsFormValid = !PlaceError && !PersonError && !RegimeError;
109 65ad7e66 Lucas Bihannic
110
            (CreateDayCommand as Command)?.ChangeCanExecute();
111
        }
112 18f910c6 Lucas Bihannic
        #endregion
113 65ad7e66 Lucas Bihannic
    }
114
}