From 136acec4ba647325ad7f9f13b8d563d4e4a157f7 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 12 Mar 2018 21:04:47 -0400 Subject: [PATCH] SPARK-548 In time filter make sure to validate date --- src/client/spark/Images/Icons.cs | 1 + src/client/spark/Images/Vector/Icons.xaml | 10 ++ .../Filters/DataGridFilterProperties.cs | 6 +- .../Filters/IDataGridColumnFilter.cs | 111 +++++++++++++----- .../DataGridEx/Filters/Views/TimeFilter.cs | 13 +- .../Filters/Views/TimeFilterControl.xaml | 25 +++- .../Filters/Views/TimeFilterControl.xaml.cs | 31 ++++- src/client/spark/view/Resources/DataGrid.xaml | 4 + 8 files changed, 162 insertions(+), 39 deletions(-) diff --git a/src/client/spark/Images/Icons.cs b/src/client/spark/Images/Icons.cs index c0cb7dc07..c1ccd0ce7 100644 --- a/src/client/spark/Images/Icons.cs +++ b/src/client/spark/Images/Icons.cs @@ -99,6 +99,7 @@ namespace mbts.ui public static ImageSource Display { get { return (DrawingImage)_resources["Display"]; } } public static ImageSource Filter { get { return (DrawingImage)_resources["Filter"]; } } public static ImageSource FilterActive { get { return (DrawingImage)_resources["FilterActive"]; } } + public static ImageSource FilterError { get { return (DrawingImage)_resources["FilterError"]; } } public static ImageSource Knight { get { return (DrawingImage)_resources["Knight"]; } } public static ImageSource Payment { get { return (DrawingImage)_resources["Payment"]; } } public static ImageSource Spawn { get { return (DrawingImage)_resources["Spawn"]; } } diff --git a/src/client/spark/Images/Vector/Icons.xaml b/src/client/spark/Images/Vector/Icons.xaml index 34577b05e..3bb5591cf 100644 --- a/src/client/spark/Images/Vector/Icons.xaml +++ b/src/client/spark/Images/Vector/Icons.xaml @@ -473,6 +473,16 @@ M0,0L63.999001,0 63.999001,48.792999 0,48.792999z"> + + + + + + + + + + diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/DataGridFilterProperties.cs b/src/client/spark/view/Controls/DataGridEx/Filters/DataGridFilterProperties.cs index 67d0a8350..b66171db2 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/DataGridFilterProperties.cs +++ b/src/client/spark/view/Controls/DataGridEx/Filters/DataGridFilterProperties.cs @@ -15,9 +15,9 @@ namespace mbts.ui.controls { #endregion #region FilterActive - public static readonly DependencyProperty FilterActiveProperty = DependencyProperty.RegisterAttached("FilterActive", typeof(bool), typeof(DataGridFilterProperties), new PropertyMetadata(false)); - public static bool GetFilterActive(DependencyObject d) { return (bool)d.GetValue(FilterActiveProperty); } - public static void SetFilterActive(DependencyObject d, bool value) { d.SetValue(FilterActiveProperty, value); } + public static readonly DependencyProperty FilterActiveProperty = DependencyProperty.RegisterAttached("FilterActive", typeof(bool?), typeof(DataGridFilterProperties), new PropertyMetadata(false)); + public static bool? GetFilterActive(DependencyObject d) => d.GetValue(FilterActiveProperty) as bool?; + public static void SetFilterActive(DependencyObject d, bool? value) => d.SetValue(FilterActiveProperty, value); #endregion #region FilterOpen diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/IDataGridColumnFilter.cs b/src/client/spark/view/Controls/DataGridEx/Filters/IDataGridColumnFilter.cs index cfcde4220..401fe5350 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/IDataGridColumnFilter.cs +++ b/src/client/spark/view/Controls/DataGridEx/Filters/IDataGridColumnFilter.cs @@ -18,9 +18,9 @@ using System.Text.RegularExpressions; using System.Windows.Input; namespace mbts.spark { - interface IDataGridColumnFilter { + interface IDataGridColumnFilter : INotifyDataErrorInfo { string prop_name { get; } - bool active { get; } + bool? active { get; } bool open { get; } ICommand commit { get; } ICommand clear { get; } @@ -38,17 +38,8 @@ namespace mbts.spark { public static IDisposable hook_filters(ICollectionView rows, params IDataGridColumnFilter[] filters) { var icvls = (ICollectionViewLiveShaping)rows; icvls.IsLiveFiltering = true; - Action set_filter = () => { - var active_filters = filters.Where(f => f.active).ToList(); - using (rows.DeferRefresh()) { - icvls.LiveFilteringProperties.sync_with(active_filters.Select(f => f.prop_name)); - rows.Filter = r_obj => { - TRow r = (TRow)r_obj; - return active_filters.All(f => f.filter(r)); - }; - } - }; - foreach (var f in filters) f.filter_changed += set_filter; + foreach (var f in filters) + f.filter_changed += set_filter; set_filter(); return Disposable.Create(() => { foreach (var f in filters) { @@ -56,18 +47,29 @@ namespace mbts.spark { (f as IDisposable)?.Dispose(); } }); + + void set_filter() { + var active_filters = filters.Where(f => f.active.HasValue && f.active.Value).ToList(); + using (rows.DeferRefresh()) { + icvls.LiveFilteringProperties.sync_with(active_filters.Select(f => f.prop_name)); + rows.Filter = r_obj => { + TRow r = (TRow)r_obj; + return active_filters.All(f => f.filter(r)); + }; + } + } } - + #region Filters - public abstract class FilterBase : ViewModelBase, IDataGridColumnPersistentFilter { - readonly DelegateProperty _active; + public abstract class FilterBase : ViewModelBaseWithValidation, IDataGridColumnPersistentFilter { + readonly DelegateProperty _active; protected readonly Func _get_row_val; ObjSnapshot _snapshot; protected FilterBase(Func get_row_val, string prop_name) { _get_row_val = get_row_val; this.prop_name = prop_name; - _active = backing(nameof(active), _get_active); + _active = backing(nameof(active), () => HasErrors ? new bool?() : _get_active()); _snapshot = detect_property_changes(); commit = new DelegateCommand(_commit); cancel = new DelegateCommand(_cancel); @@ -77,7 +79,7 @@ namespace mbts.spark { public event Action filter_changed; public string prop_name { get; } - public bool active => _active.Value; + public bool? active => _active.Value; public bool open { get; set; } public bool filter(TRow row) => _filter(_get_row_val(row)); public ICommand commit { get; } @@ -88,6 +90,8 @@ namespace mbts.spark { public void load_state(TState state) { _load_state_internal(state); _snapshot = detect_property_changes(); + _validate(); + _raise_filter_changed(true); } protected abstract bool _get_active(); @@ -96,9 +100,12 @@ namespace mbts.spark { protected virtual void _commit() { Disposable.dispose(ref _snapshot); _snapshot = detect_property_changes(); - open = false; - RaisePropertyChanged(nameof(open)); - _raise_filter_changed(); + _validate(); + if (!HasErrors) { + open = false; + RaisePropertyChanged(nameof(open)); + } + _raise_filter_changed(true); } protected virtual void _cancel() { if (_snapshot != null) @@ -114,10 +121,12 @@ namespace mbts.spark { var dpc = detect_property_changes(); return Disposable.Create(() => { dpc.Dispose(); + _validate(); _raise_filter_changed(); _snapshot = detect_property_changes(); }); } + protected virtual void _validate() { } protected void _raise_filter_changed(bool notify_active_property_changed = true) { if (notify_active_property_changed) _active.refresh(); filter_changed?.Invoke(); @@ -331,30 +340,43 @@ namespace mbts.spark { #endregion #endregion - public class Time : FilterBase { + public interface ITime : INotifyDataErrorInfo { + TimeFilterMode mode { get; } + TimeTextTuple begin { get; } + TimeTextTuple end { get; } + } + + public class Time : FilterBase, ITime { + TimeFilterMode _mode; TimeTextTuple _begin, _end; DateTime? _begin_dt, _end_dt; public Time(Func get_row_val, string prop_name) : base(get_row_val, prop_name) => _set_default_values(); - public TimeFilterMode mode { get; set; } + public TimeFilterMode mode { + get => _mode; + set { + _mode = value; + if (HasErrors) _validate(); + } + } public TimeTextTuple begin { get => _begin; set { - bool was_active = _begin_dt.HasValue; _begin = value; _begin_dt = _begin.result; + if (HasErrors) _validate(); } } public TimeTextTuple end { get => _end; set { - bool was_active = _end_dt.HasValue; _end = value; _end_dt = _end.result; + if (HasErrors) _validate(); } } @@ -387,6 +409,25 @@ namespace mbts.spark { _set_default_values(); } + protected override void _validate() { + using (validation(nameof(begin), nameof(end))) { + if (mode == TimeFilterMode.Period) { + bool begin_valid = _begin.is_valid, end_valid = _end.is_valid; + validation_assert(begin_valid, "'From' is invalid.", nameof(begin)); + validation_assert(end_valid, "'To' is invalid.", nameof(end)); + if (begin_valid && end_valid) { + if (_begin_dt.HasValue) { + if (_end_dt.HasValue) + validation_assert(_end_dt.Value >= _begin_dt.Value, "'From' is after 'To.'", nameof(begin), nameof(end)); + } else if (!_end_dt.HasValue) { + validation_assert(false, "Please set 'From' and/or 'To.'", nameof(begin)); + validation_assert(false, string.Empty, nameof(end)); + } + } + } + } + } + void _set_default_values() { mode = TimeFilterMode.Anytime; _begin = TimeTextTuple.FromDateTime(DateTime.Today); @@ -638,7 +679,7 @@ namespace mbts.spark { } } - public class TupleFilter : IDataGridColumnPersistentFilter { + public class TupleFilter : ViewModelBaseWithValidation, IDataGridColumnPersistentFilter { readonly FilterBase _filter1; readonly FilterBase _filter2; readonly Func _get_state; @@ -661,13 +702,27 @@ namespace mbts.spark { public FilterBase filter1 => _filter1; public FilterBase filter2 => _filter2; public string prop_name { get; } - public bool active => _filter1.active || _filter2.active; + public bool? active { + get { + bool? f1_active = _filter1.active; + if (f1_active.HasValue) { + bool? f2_active = _filter2.active; + if (f2_active.HasValue) + return f1_active.Value && f2_active.Value; + } + return null; + } + } public bool open { get; set; } public ICommand commit { get; } public ICommand clear { get; } public ICommand cancel { get; } public event Action filter_changed; - public bool filter(TRow row) => (!_filter1.active || _filter1.filter(row)) && (!_filter2.active || _filter2.filter(row)); + public bool filter(TRow row) { + if (!active.HasValue || !active.Value) + return true; + return (!_filter1.active.Value || _filter1.filter(row)) && (!_filter2.active.Value || _filter2.filter(row)); + } public TCompoundState get_state() => _get_state(_filter1.get_state(), _filter2.get_state()); public void load_state(TCompoundState state) { _filter1.load_state(_get_filter1_state(state)); diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs b/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs index 077fd6286..ba3a63d27 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs +++ b/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs @@ -22,9 +22,9 @@ namespace mbts.ui { public TimeFilterMode mode => _mode; public TimeTextTuple begin => _begin; public TimeTextTuple end => _end; - public bool is_active => - _mode == TimeFilterMode.Today || - (_mode == TimeFilterMode.Period && (_begin.result.HasValue || _end.result.HasValue)); + public bool is_active => + _mode == TimeFilterMode.Today || + (_mode == TimeFilterMode.Period && (_begin.result.HasValue || _end.result.HasValue)); public bool Equals(TimeFilterTuple other) => @@ -69,6 +69,13 @@ namespace mbts.ui { public string year_text => _year_text; public string time_text => _time_text; public DateTime? result => _parse(_month_text, _day_text, _year_text, _time_text); + public bool is_valid => + result.HasValue || ( + string.IsNullOrEmpty(_month_text) && + string.IsNullOrEmpty(_day_text) && + string.IsNullOrEmpty(_year_text) && + string.IsNullOrEmpty(_time_text) + ); public static TimeTextTuple TodayEmptyTime { get { diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilterControl.xaml b/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilterControl.xaml index 500050da0..c7ccb3eef 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilterControl.xaml +++ b/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilterControl.xaml @@ -7,16 +7,23 @@