Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / CreatePlaceViewModel.cs @ 42456640

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

1 65ad7e66 Lucas Bihannic
using GES_PAC.Model;
2
using System.Windows.Input;
3
4
namespace GES_PAC.ViewModel
5
{
6
    public class CreatePlaceViewModel : BaseViewModel
7
    {
8 18f910c6 Lucas Bihannic
        #region Attributs
9 65ad7e66 Lucas Bihannic
        private string _nom;
10 18f910c6 Lucas Bihannic
        private string _client;
11
        private string _espece;
12
        private bool _isFormValid;
13
        #endregion
14
15
        #region Commandes
16
        public ICommand CreatePlaceCommand { get; }
17
        #endregion
18
19
        #region Propriétés
20 65ad7e66 Lucas Bihannic
        public string Nom
21
        {
22
            get => _nom;
23
            set
24
            {
25
                _nom = value;
26
                OnPropertyChanged();
27
                ValidateForm();
28
            }
29
        }
30 18f910c6 Lucas Bihannic
        
31 65ad7e66 Lucas Bihannic
        public string Client
32
        {
33
            get => _client;
34
            set
35
            {
36
                _client = value;
37
                OnPropertyChanged();
38
                ValidateForm();
39
            }
40
        }
41 18f910c6 Lucas Bihannic
        public string Espece
42
        {
43
            get => _espece;
44
            set
45
            {
46
                _espece = value;
47
                OnPropertyChanged();
48
                ValidateForm();
49
            }
50
        }
51
52 65ad7e66 Lucas Bihannic
        public bool IsFormValid
53
        {
54
            get => _isFormValid;
55
            set
56
            {
57
                _isFormValid = value;
58
                OnPropertyChanged();
59
            }
60
        }
61
62
        public bool NomError { get; private set; } = true;
63
        public bool ClientError { get; private set; } = true;
64 18f910c6 Lucas Bihannic
        public bool EspeceError { get; private set; } = true;
65
        #endregion
66 65ad7e66 Lucas Bihannic
67 18f910c6 Lucas Bihannic
        #region Constructeur
68 65ad7e66 Lucas Bihannic
        public CreatePlaceViewModel()
69
        {
70
            CreatePlaceCommand = new Command(async () => await CreatePlace());
71
        }
72 18f910c6 Lucas Bihannic
        #endregion
73 65ad7e66 Lucas Bihannic
74 18f910c6 Lucas Bihannic
        #region Méthodes
75 65ad7e66 Lucas Bihannic
        private void ValidateForm()
76
        {
77
            NomError = string.IsNullOrWhiteSpace(Nom) || Nom.Length < 3;
78
            ClientError = string.IsNullOrWhiteSpace(Client) || Client.Length < 3;
79 18f910c6 Lucas Bihannic
            EspeceError = string.IsNullOrWhiteSpace(Espece) || Espece.Length < 3;
80 65ad7e66 Lucas Bihannic
81
            OnPropertyChanged(nameof(NomError));
82
            OnPropertyChanged(nameof(ClientError));
83 18f910c6 Lucas Bihannic
            OnPropertyChanged(nameof(EspeceError));
84 65ad7e66 Lucas Bihannic
85 18f910c6 Lucas Bihannic
            IsFormValid = !NomError && !ClientError && !EspeceError;
86 65ad7e66 Lucas Bihannic
            (CreatePlaceCommand as Command)?.ChangeCanExecute();
87
        }
88
        private async Task CreatePlace()
89
        {
90 18f910c6 Lucas Bihannic
            var newPlace = new Lieu(Nom, Client, Espece);
91 65ad7e66 Lucas Bihannic
            PlaceViewModel.Instance.Places.Add(newPlace);
92 18f910c6 Lucas Bihannic
            await Shell.Current.GoToAsync("//Main");
93 65ad7e66 Lucas Bihannic
        }
94 18f910c6 Lucas Bihannic
        #endregion
95 65ad7e66 Lucas Bihannic
    }
96
}