validation

vnext
Paul Schneider 8 years ago
parent 3f2d69b6f8
commit 60ad8df85b
4 changed files with 48 additions and 17 deletions

@ -100,7 +100,7 @@
<StackLayout Orientation="Vertical"> <StackLayout Orientation="Vertical">
<Button Text="Ajouter une ligne de facture" Clicked="OnNewCommanLine"></Button> <Button Text="Ajouter une ligne de facture" Clicked="OnNewCommanLine"></Button>
<Label FormattedText="{Binding FormattedTotal}"/> <Label FormattedText="{Binding FormattedTotal}"/>
<Button Text="Valider ce devis" Clicked="OnEstimateValidated" ></Button> <Button x:Name="btnValidate" Text="Valider ce devis" Clicked="OnEstimateValidated" IsEnabled="{Binding ViewModelState.IsValid}" ></Button>
</StackLayout> </StackLayout>
</StackLayout> </StackLayout>
</StackLayout> </StackLayout>

@ -11,11 +11,29 @@ namespace BookAStar.Pages
public partial class EditEstimatePage : ContentPage public partial class EditEstimatePage : ContentPage
{ {
public EditEstimateViewModel Model
{
get
{
return (EditEstimateViewModel)BindingContext;
}
}
public EditEstimatePage(EditEstimateViewModel model) public EditEstimatePage(EditEstimateViewModel model)
{ {
InitializeComponent();
BindingContext = 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() protected override void OnBindingContextChanged()
@ -56,27 +74,32 @@ namespace BookAStar.Pages
} }
protected void OnEditLine(object sender, ItemTappedEventArgs e) protected void OnEditLine(object sender, ItemTappedEventArgs e)
{ {
var line = (BillingLineViewModel)e.Item; var line = (BillingLineViewModel)e.Item;
var evm = ((EditEstimateViewModel)BindingContext);
// update the validation command, that // update the validation command, that
// was creating a new line in the bill at creation time, // was creating a new line in the bill at creation time,
// now one only wants to update the line // now one only wants to update the line
line.ValidateCommand = new Command(() => line.ValidateCommand = new Command(() =>
{ {
evm.Check();
DataManager.Current.EstimationCache.SaveEntity(); DataManager.Current.EstimationCache.SaveEntity();
}); });
// and setup a removal command, that was not expected at creation time // and setup a removal command, that was not expected at creation time
var evm = (EditEstimateViewModel)BindingContext;
line.RemoveCommand = new Command(() => line.RemoveCommand = new Command(() =>
{ {
evm.Bill.Remove(line); evm.Bill.Remove(line);
evm.Check();
DataManager.Current.EstimationCache.SaveEntity(); DataManager.Current.EstimationCache.SaveEntity();
}); });
App.NavigationService.NavigateTo<EditBillingLinePage>( App.NavigationService.NavigateTo<EditBillingLinePage>(
true, line ); true, line );
BillListView.SelectedItem = null;
} }
protected async void OnEstimateValidated(object sender, EventArgs e) protected async void OnEstimateValidated(object sender, EventArgs e)
{ {
var thisPage = this; var thisPage = this;
var evm = (EditEstimateViewModel) BindingContext; var evm = (EditEstimateViewModel) BindingContext;
var cmd = new Command<bool>( async (validated) => var cmd = new Command<bool>( async (validated) =>

@ -24,7 +24,15 @@ namespace BookAStar.ViewModels.EstimateAndBilling
{ {
SyncData(); SyncData();
} }
public override void OnViewAppearing()
{
base.OnViewAppearing();
SyncData();
}
/// <summary>
/// Called to synchronyze this view on target model,
/// at accepting a new representation for this model
/// </summary>
private void SyncData() private void SyncData()
{ {
if (Data.AttachedFiles == null) Data.AttachedFiles = new List<string>(); if (Data.AttachedFiles == null) Data.AttachedFiles = new List<string>();
@ -41,12 +49,13 @@ namespace BookAStar.ViewModels.EstimateAndBilling
NotifyPropertyChanged("FormattedTotal"); NotifyPropertyChanged("FormattedTotal");
NotifyPropertyChanged("Query"); NotifyPropertyChanged("Query");
NotifyPropertyChanged("CLient"); NotifyPropertyChanged("CLient");
NotifyPropertyChanged("ModelState");
} }
protected override void OnPropertyChanged(PropertyChangedEventArgs e) protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{ {
base.OnPropertyChanged(e); base.OnPropertyChanged(e);
if (e.PropertyName == "Data") if (e.PropertyName.StartsWith("Data"))
{ {
SyncData(); SyncData();
} }
@ -61,6 +70,7 @@ namespace BookAStar.ViewModels.EstimateAndBilling
Data.Bill = Bill.Select(l => l.Data).ToList(); Data.Bill = Bill.Select(l => l.Data).ToList();
NotifyPropertyChanged("FormattedTotal"); NotifyPropertyChanged("FormattedTotal");
NotifyPropertyChanged("Bill"); NotifyPropertyChanged("Bill");
NotifyPropertyChanged("ViewModelState");
} }
[JsonIgnore] [JsonIgnore]
@ -127,7 +137,7 @@ namespace BookAStar.ViewModels.EstimateAndBilling
get get
{ {
OnPlatform<Font> lfs = (OnPlatform<Font>)App.Current.Resources["MediumFontSize"]; OnPlatform<Font> lfs = (OnPlatform<Font>)App.Current.Resources["MediumFontSize"];
OnPlatform<Color> etc = (OnPlatform<Color>)App.Current.Resources["EmphasisTextColor"]; Color etc = (Color) App.Current.Resources["EmphasisTextColor"];
return new FormattedString return new FormattedString
{ {
@ -135,7 +145,7 @@ namespace BookAStar.ViewModels.EstimateAndBilling
Spans = { Spans = {
new Span { Text = "Total TTC: " }, new Span { Text = "Total TTC: " },
new Span { Text = Data.Total.ToString(), new Span { Text = Data.Total.ToString(),
ForegroundColor = etc.Android , ForegroundColor = etc,
FontSize = (double) lfs.Android.FontSize }, FontSize = (double) lfs.Android.FontSize },
new Span { Text = "€", FontSize = (double) lfs.Android.FontSize } new Span { Text = "€", FontSize = (double) lfs.Android.FontSize }
} }

@ -1,12 +1,7 @@
using BookAStar.Data; using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using XLabs.Forms.Mvvm; using XLabs.Forms.Mvvm;
using System.ComponentModel; using System.ComponentModel;
using Newtonsoft.Json;
namespace BookAStar.ViewModels.Validation namespace BookAStar.ViewModels.Validation
{ {
@ -16,7 +11,7 @@ namespace BookAStar.ViewModels.Validation
/// </summary> /// </summary>
public class EditingViewModel<DataType>: ViewModel public class EditingViewModel<DataType>: ViewModel
{ {
[JsonIgnore]
public Action<DataType, ModelState> CheckCommand { set; get; } public Action<DataType, ModelState> CheckCommand { set; get; }
public DataType Data { get; set; } public DataType Data { get; set; }
@ -44,13 +39,16 @@ namespace BookAStar.ViewModels.Validation
protected override void OnPropertyChanged(PropertyChangedEventArgs e) protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{ {
base.OnPropertyChanged(e); base.OnPropertyChanged(e);
Check();
}
public virtual void Check()
{
if (CheckCommand != null) if (CheckCommand != null)
{ {
ViewModelState.Clear(); ViewModelState.Clear();
CheckCommand(Data, ViewModelState); CheckCommand(Data, ViewModelState);
} }
} }
/* NOTE : I had a dream. /* NOTE : I had a dream.
bool existsRemotely; bool existsRemotely;

Loading…