refabrique du model d'erreurs

This commit is contained in:
2017-02-22 14:37:13 +01:00
parent 390e0a47b9
commit 194f7d53a6
8 changed files with 77 additions and 46 deletions

View File

@ -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;
}

View File

@ -22,25 +22,16 @@ namespace ZicMoove.Pages
public EditEstimatePage(EditEstimateViewModel model)
{
BindingContext = model;
Model.CheckCommand = new Action<Estimate, ViewModels.Validation.ModelState>(
(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)

View File

@ -16,15 +16,6 @@ namespace ZicMoove.ViewModels.EstimateAndBilling
public BillingLineViewModel(BillingLine data): base(data)
{
CheckCommand = new Action<BillingLine, ModelState>(
(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
{

View File

@ -12,6 +12,7 @@ namespace ZicMoove.ViewModels.EstimateAndBilling
using Model.Social;
using Validation;
using Model.Musical;
using System;
public class EditEstimateViewModel : EditingViewModel<Estimate>
{
@ -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<string> AttachedFiles
{

View File

@ -9,16 +9,14 @@ namespace ZicMoove.ViewModels.Validation
/// Used to make the DataManager know how
/// to sync local and remote data
/// </summary>
public class EditingViewModel<DataType>: ViewModel
public abstract class EditingViewModel<DataType>: ViewModel
{
[JsonIgnore]
public Action<DataType, ModelState> 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<ModelState>(ref viewModelState, value);
base.SetProperty<ViewModelState>(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);
if (e.PropertyName != "ModelState")
Check();
}
public virtual void Check()
{
if (CheckCommand != null)
{
ViewModelState.Clear();
CheckCommand(Data, ViewModelState);
}
}
/// <summary>
/// Must compute the ModelState property
/// from the Data one.
/// </summary>
public abstract void Check();
/* NOTE : I had a dream.
bool existsRemotely;

View File

@ -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<string,List<InputError>>), typeof(ModelState), null);
BindableProperty.Create("Errors", typeof(Dictionary<string,List<InputError>>), typeof(ViewModelState), null);
public ModelState()
public ViewModelState()
{
Errors = new Dictionary<string, List<InputError>>();
}

View File

@ -15,5 +15,10 @@ namespace ZicMoove.ViewModels.WorkFlow
public WorkflowBookViewModel(BookQuery data) : base(data)
{
}
public override void Check()
{
throw new NotImplementedException();
}
}
}