From 60ad8df85bb175483e41cb83366f33412a6dcb56 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Sat, 10 Dec 2016 23:20:13 +0100 Subject: [PATCH] validation --- .../Pages/EstimatePages/EditEstimatePage.xaml | 2 +- .../EstimatePages/EditEstimatePage.xaml.cs | 29 +++++++++++++++++-- .../EditEstimateViewModel.cs | 18 +++++++++--- .../ViewModels/Validation/EditingViewModel.cs | 16 +++++----- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml b/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml index c2b07dc1..0e3bbc62 100644 --- a/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml +++ b/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml @@ -100,7 +100,7 @@ diff --git a/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml.cs b/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml.cs index 4f402d9e..9cd92e6a 100644 --- a/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml.cs +++ b/BookAStar/BookAStar/Pages/EstimatePages/EditEstimatePage.xaml.cs @@ -11,11 +11,29 @@ namespace BookAStar.Pages public partial class EditEstimatePage : ContentPage { - + public EditEstimateViewModel Model + { + get + { + return (EditEstimateViewModel)BindingContext; + } + } public EditEstimatePage(EditEstimateViewModel model) { - InitializeComponent(); 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() @@ -56,27 +74,32 @@ namespace BookAStar.Pages } protected void OnEditLine(object sender, ItemTappedEventArgs e) { + var line = (BillingLineViewModel)e.Item; + var evm = ((EditEstimateViewModel)BindingContext); // update the validation command, that // was creating a new line in the bill at creation time, // now one only wants to update the line line.ValidateCommand = new Command(() => { + evm.Check(); DataManager.Current.EstimationCache.SaveEntity(); }); // and setup a removal command, that was not expected at creation time - var evm = (EditEstimateViewModel)BindingContext; line.RemoveCommand = new Command(() => { evm.Bill.Remove(line); + evm.Check(); DataManager.Current.EstimationCache.SaveEntity(); }); App.NavigationService.NavigateTo( true, line ); + BillListView.SelectedItem = null; } protected async void OnEstimateValidated(object sender, EventArgs e) { + var thisPage = this; var evm = (EditEstimateViewModel) BindingContext; var cmd = new Command( async (validated) => diff --git a/BookAStar/BookAStar/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs b/BookAStar/BookAStar/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs index f02d087e..c30ea636 100644 --- a/BookAStar/BookAStar/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs +++ b/BookAStar/BookAStar/ViewModels/EstimateAndBilling/EditEstimateViewModel.cs @@ -24,7 +24,15 @@ namespace BookAStar.ViewModels.EstimateAndBilling { SyncData(); } - + public override void OnViewAppearing() + { + base.OnViewAppearing(); + SyncData(); + } + /// + /// Called to synchronyze this view on target model, + /// at accepting a new representation for this model + /// private void SyncData() { if (Data.AttachedFiles == null) Data.AttachedFiles = new List(); @@ -41,12 +49,13 @@ namespace BookAStar.ViewModels.EstimateAndBilling NotifyPropertyChanged("FormattedTotal"); NotifyPropertyChanged("Query"); NotifyPropertyChanged("CLient"); + NotifyPropertyChanged("ModelState"); } protected override void OnPropertyChanged(PropertyChangedEventArgs e) { base.OnPropertyChanged(e); - if (e.PropertyName == "Data") + if (e.PropertyName.StartsWith("Data")) { SyncData(); } @@ -61,6 +70,7 @@ namespace BookAStar.ViewModels.EstimateAndBilling Data.Bill = Bill.Select(l => l.Data).ToList(); NotifyPropertyChanged("FormattedTotal"); NotifyPropertyChanged("Bill"); + NotifyPropertyChanged("ViewModelState"); } [JsonIgnore] @@ -127,7 +137,7 @@ namespace BookAStar.ViewModels.EstimateAndBilling get { OnPlatform lfs = (OnPlatform)App.Current.Resources["MediumFontSize"]; - OnPlatform etc = (OnPlatform)App.Current.Resources["EmphasisTextColor"]; + Color etc = (Color) App.Current.Resources["EmphasisTextColor"]; return new FormattedString { @@ -135,7 +145,7 @@ namespace BookAStar.ViewModels.EstimateAndBilling Spans = { new Span { Text = "Total TTC: " }, new Span { Text = Data.Total.ToString(), - ForegroundColor = etc.Android , + ForegroundColor = etc, FontSize = (double) lfs.Android.FontSize }, new Span { Text = "€", FontSize = (double) lfs.Android.FontSize } } diff --git a/BookAStar/BookAStar/ViewModels/Validation/EditingViewModel.cs b/BookAStar/BookAStar/ViewModels/Validation/EditingViewModel.cs index 36563df5..a8f78b85 100644 --- a/BookAStar/BookAStar/ViewModels/Validation/EditingViewModel.cs +++ b/BookAStar/BookAStar/ViewModels/Validation/EditingViewModel.cs @@ -1,12 +1,7 @@ -using BookAStar.Data; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Input; +using System; using XLabs.Forms.Mvvm; using System.ComponentModel; +using Newtonsoft.Json; namespace BookAStar.ViewModels.Validation { @@ -16,7 +11,7 @@ namespace BookAStar.ViewModels.Validation /// public class EditingViewModel: ViewModel { - + [JsonIgnore] public Action CheckCommand { set; get; } public DataType Data { get; set; } @@ -44,13 +39,16 @@ namespace BookAStar.ViewModels.Validation protected override void OnPropertyChanged(PropertyChangedEventArgs e) { base.OnPropertyChanged(e); + Check(); + } + public virtual void Check() + { if (CheckCommand != null) { ViewModelState.Clear(); CheckCommand(Data, ViewModelState); } } - /* NOTE : I had a dream. bool existsRemotely;