From 194f7d53a6c936fe6f2ea0fc1ae64bc530f1882b Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Wed, 22 Feb 2017 14:37:13 +0100 Subject: [PATCH] refabrique du model d'erreurs --- .../EstimatePages/EditBillingLinePage.xaml.cs | 17 +++++++++-- .../EstimatePages/EditEstimatePage.xaml.cs | 15 ++-------- .../BillingLineViewModel.cs | 22 +++++++------- .../EditEstimateViewModel.cs | 27 ++++++++++++++++- .../ViewModels/Validation/EditingViewModel.cs | 29 +++++++++---------- .../Validation/{Error.cs => InputError.cs} | 0 .../{ModelState.cs => ViewModelState.cs} | 8 ++--- .../WorkFlow/WorkflowBookViewModel.cs | 5 ++++ 8 files changed, 77 insertions(+), 46 deletions(-) rename ZicMoove/ZicMoove/ViewModels/Validation/{Error.cs => InputError.cs} (100%) rename ZicMoove/ZicMoove/ViewModels/Validation/{ModelState.cs => ViewModelState.cs} (91%) diff --git a/ZicMoove/ZicMoove/Pages/EstimatePages/EditBillingLinePage.xaml.cs b/ZicMoove/ZicMoove/Pages/EstimatePages/EditBillingLinePage.xaml.cs index 5006e97b..0e77d7ff 100644 --- a/ZicMoove/ZicMoove/Pages/EstimatePages/EditBillingLinePage.xaml.cs +++ b/ZicMoove/ZicMoove/Pages/EstimatePages/EditBillingLinePage.xaml.cs @@ -4,17 +4,28 @@ using ZicMoove.ViewModels.EstimateAndBilling; using System; using System.Collections.Generic; using Xamarin.Forms; +using ZicMoove.Model.Workflow; namespace ZicMoove.Pages { public partial class EditBillingLinePage : ContentPage { - public EditBillingLinePage(BillingLineViewModel model) + public void Initialize() { InitializeComponent(); - foreach - (string du in Enum.GetNames(typeof(BillingLineViewModel.DurationUnits))) + foreach (string du in Enum.GetNames(typeof(BillingLineViewModel.DurationUnits))) picker.Items.Add(du); + BindingContext = new BillingLineViewModel(new BillingLine()); + } + + public EditBillingLinePage() + { + Initialize(); + } + + public EditBillingLinePage(BillingLineViewModel model) + { + Initialize(); BindingContext = model; } diff --git a/ZicMoove/ZicMoove/Pages/EstimatePages/EditEstimatePage.xaml.cs b/ZicMoove/ZicMoove/Pages/EstimatePages/EditEstimatePage.xaml.cs index 9d6da291..286cb981 100644 --- a/ZicMoove/ZicMoove/Pages/EstimatePages/EditEstimatePage.xaml.cs +++ b/ZicMoove/ZicMoove/Pages/EstimatePages/EditEstimatePage.xaml.cs @@ -22,25 +22,16 @@ namespace ZicMoove.Pages public EditEstimatePage(EditEstimateViewModel model) { BindingContext = model; - Model.CheckCommand = new Action( - (e, m) => - { - foreach (var line in model.Bill) - { - line.Check(); - if (!line.ViewModelState.IsValid) - model.ViewModelState.AddError("Bill", "invalid line"); - } - }); InitializeComponent(); - Model.Check(); } protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); - ((EditEstimateViewModel)BindingContext).PropertyChanged += EditEstimatePage_PropertyChanged; + if (Model == null) return; + Model.PropertyChanged += EditEstimatePage_PropertyChanged; + Model.Check(); } private void EditEstimatePage_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) diff --git a/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/BillingLineViewModel.cs b/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/BillingLineViewModel.cs index d4cb9337..8e284105 100644 --- a/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/BillingLineViewModel.cs +++ b/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/BillingLineViewModel.cs @@ -16,15 +16,6 @@ namespace ZicMoove.ViewModels.EstimateAndBilling public BillingLineViewModel(BillingLine data): base(data) { - CheckCommand = new Action( - (l,s) => { - if (string.IsNullOrWhiteSpace(l.Description)) - { - s.AddError("Description",Strings.NoDescription); - } - if (l.UnitaryCost < 0) { s.AddError("UnitaryCost", Strings.InvalidValue); } - if (l.Count < 0) { s.AddError("Count", Strings.InvalidValue); } - }); SyncData(); } @@ -39,7 +30,7 @@ namespace ZicMoove.ViewModels.EstimateAndBilling description = Data.Description; unitaryCostText = Data.UnitaryCost.ToString("G", CultureInfo.InvariantCulture); } - CheckCommand(Data, ViewModelState); + Check(); } protected override void OnPropertyChanged(PropertyChangedEventArgs e) @@ -51,6 +42,17 @@ namespace ZicMoove.ViewModels.EstimateAndBilling } } + public override void Check() + { + ModelState.Clear(); + if (string.IsNullOrWhiteSpace(Data.Description)) + { + ModelState.AddError("Description", Strings.NoDescription); + } + if (Data.UnitaryCost < 0) { ModelState.AddError("UnitaryCost", Strings.InvalidValue); } + if (Data.Count < 0) { ModelState.AddError("Count", Strings.InvalidValue); } + } + private int count; public int Count { diff --git a/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs b/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs index 82dc34d1..e942a1fd 100644 --- a/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs +++ b/ZicMoove/ZicMoove/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs @@ -12,6 +12,7 @@ namespace ZicMoove.ViewModels.EstimateAndBilling using Model.Social; using Validation; using Model.Musical; + using System; public class EditEstimateViewModel : EditingViewModel { @@ -52,7 +53,7 @@ namespace ZicMoove.ViewModels.EstimateAndBilling NotifyPropertyChanged("Query"); NotifyPropertyChanged("CLient"); NotifyPropertyChanged("ModelState"); - + Check(); } protected override void OnPropertyChanged(PropertyChangedEventArgs e) @@ -76,6 +77,30 @@ namespace ZicMoove.ViewModels.EstimateAndBilling NotifyPropertyChanged("ViewModelState"); } + public override void Check() + { + ModelState.Clear(); + if (Data == null) return; + if (string.IsNullOrWhiteSpace(Data.Title)) + ModelState.AddError("Title", "Spécifier un titre"); + if (string.IsNullOrWhiteSpace(Data.Description)) + ModelState.AddError("Description", "Veuillez décrire l'objet de cette facture"); + if (Data.Bill==null) + ModelState.AddError("Bill", "Veuillez ajouter au moins une ligne de facture"); + else + { + if (Data.Bill.Count==0) + ModelState.AddError("Bill", "Veuillez ajouter au moins une ligne de facture"); + var ilc = (Bill.Count(l => !l.ModelState.IsValid)); + if (ilc > 0) + { + var pluriel = (ilc > 1) ? "les lignes" : "la ligne"; + ModelState.AddError("Bill", "Veuillez corriger {pluriel} de facture"); + } + } + NotifyPropertyChanged("ModelState"); + } + [JsonIgnore] public ObservableCollection AttachedFiles { diff --git a/ZicMoove/ZicMoove/ViewModels/Validation/EditingViewModel.cs b/ZicMoove/ZicMoove/ViewModels/Validation/EditingViewModel.cs index bd3f9a1f..68b493b4 100644 --- a/ZicMoove/ZicMoove/ViewModels/Validation/EditingViewModel.cs +++ b/ZicMoove/ZicMoove/ViewModels/Validation/EditingViewModel.cs @@ -9,16 +9,14 @@ namespace ZicMoove.ViewModels.Validation /// Used to make the DataManager know how /// to sync local and remote data /// - public class EditingViewModel: ViewModel + public abstract class EditingViewModel: ViewModel { - [JsonIgnore] - public Action CheckCommand { set; get; } public DataType Data { get; set; } - private ModelState viewModelState = new ModelState(); + private ViewModelState viewModelState = new ViewModelState(); - public ModelState ViewModelState + public ViewModelState ModelState { get { @@ -26,29 +24,28 @@ namespace ZicMoove.ViewModels.Validation } set { - base.SetProperty(ref viewModelState, value); + base.SetProperty(ref viewModelState, value); } } public EditingViewModel(DataType data) { this.Data = data; - ViewModelState = new ModelState(); + ModelState = new ViewModelState(); } protected override void OnPropertyChanged(PropertyChangedEventArgs e) { base.OnPropertyChanged(e); - Check(); - } - public virtual void Check() - { - if (CheckCommand != null) - { - ViewModelState.Clear(); - CheckCommand(Data, ViewModelState); - } + if (e.PropertyName != "ModelState") + Check(); } + /// + /// Must compute the ModelState property + /// from the Data one. + /// + public abstract void Check(); + /* NOTE : I had a dream. bool existsRemotely; diff --git a/ZicMoove/ZicMoove/ViewModels/Validation/Error.cs b/ZicMoove/ZicMoove/ViewModels/Validation/InputError.cs similarity index 100% rename from ZicMoove/ZicMoove/ViewModels/Validation/Error.cs rename to ZicMoove/ZicMoove/ViewModels/Validation/InputError.cs diff --git a/ZicMoove/ZicMoove/ViewModels/Validation/ModelState.cs b/ZicMoove/ZicMoove/ViewModels/Validation/ViewModelState.cs similarity index 91% rename from ZicMoove/ZicMoove/ViewModels/Validation/ModelState.cs rename to ZicMoove/ZicMoove/ViewModels/Validation/ViewModelState.cs index eac25a5e..4a47534c 100644 --- a/ZicMoove/ZicMoove/ViewModels/Validation/ModelState.cs +++ b/ZicMoove/ZicMoove/ViewModels/Validation/ViewModelState.cs @@ -8,15 +8,15 @@ using Xamarin.Forms; namespace ZicMoove.ViewModels.Validation { - public class ModelState : BindableObject + public class ViewModelState : BindableObject { public static readonly BindableProperty IsValidProperty = - BindableProperty.Create("IsValid", typeof(bool), typeof(ModelState), false); + BindableProperty.Create("IsValid", typeof(bool), typeof(ViewModelState), false); public static readonly BindableProperty ErrorsProperty = - BindableProperty.Create("Errors", typeof(Dictionary>), typeof(ModelState), null); + BindableProperty.Create("Errors", typeof(Dictionary>), typeof(ViewModelState), null); - public ModelState() + public ViewModelState() { Errors = new Dictionary>(); } diff --git a/ZicMoove/ZicMoove/ViewModels/WorkFlow/WorkflowBookViewModel.cs b/ZicMoove/ZicMoove/ViewModels/WorkFlow/WorkflowBookViewModel.cs index 30ea51de..fa8e3953 100644 --- a/ZicMoove/ZicMoove/ViewModels/WorkFlow/WorkflowBookViewModel.cs +++ b/ZicMoove/ZicMoove/ViewModels/WorkFlow/WorkflowBookViewModel.cs @@ -15,5 +15,10 @@ namespace ZicMoove.ViewModels.WorkFlow public WorkflowBookViewModel(BookQuery data) : base(data) { } + + public override void Check() + { + throw new NotImplementedException(); + } } }