SPARK-548 In time filter make sure to validate date
This commit is contained in:
1 parent
3bef2e64ec
commit
136acec4ba
8 files changed
+162
-39
No files matched your search
@@ -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"]; } }
|
||||
|
||||
@@ -473,6 +473,16 @@ M0,0L63.999001,0 63.999001,48.792999 0,48.792999z">
|
||||
</DrawingImage.Drawing>
|
||||
</DrawingImage>
|
||||
|
||||
<DrawingImage x:Key="FilterError">
|
||||
<DrawingImage.Drawing>
|
||||
<DrawingGroup>
|
||||
<DrawingGroup.Children>
|
||||
<GeometryDrawing Brush="Red" Geometry="M0,0L32,0 21.306,17.372993 21.272003,17.372993 21.272003,31.352 10.692001,31.352 10.692001,17.372993z"/>
|
||||
</DrawingGroup.Children>
|
||||
</DrawingGroup>
|
||||
</DrawingImage.Drawing>
|
||||
</DrawingImage>
|
||||
|
||||
<DrawingImage x:Key="CancelIcon">
|
||||
<DrawingImage.Drawing>
|
||||
<GeometryDrawing>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -18,9 +18,9 @@ using System.Text.RegularExpressions;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace mbts.spark {
|
||||
interface IDataGridColumnFilter<in TRow> {
|
||||
interface IDataGridColumnFilter<in TRow> : 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<TRow>(ICollectionView rows, params IDataGridColumnFilter<TRow>[] 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<TRow, TValue, TState> : ViewModelBase, IDataGridColumnPersistentFilter<TRow, TState> {
|
||||
readonly DelegateProperty<bool> _active;
|
||||
public abstract class FilterBase<TRow, TValue, TState> : ViewModelBaseWithValidation, IDataGridColumnPersistentFilter<TRow, TState> {
|
||||
readonly DelegateProperty<bool?> _active;
|
||||
protected readonly Func<TRow, TValue> _get_row_val;
|
||||
ObjSnapshot _snapshot;
|
||||
|
||||
protected FilterBase(Func<TRow, TValue> 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<TRow> : FilterBase<TRow, DateTime, TimeFilterTuple> {
|
||||
public interface ITime : INotifyDataErrorInfo {
|
||||
TimeFilterMode mode { get; }
|
||||
TimeTextTuple begin { get; }
|
||||
TimeTextTuple end { get; }
|
||||
}
|
||||
|
||||
public class Time<TRow> : FilterBase<TRow, DateTime, TimeFilterTuple>, ITime {
|
||||
TimeFilterMode _mode;
|
||||
TimeTextTuple _begin, _end;
|
||||
DateTime? _begin_dt, _end_dt;
|
||||
|
||||
public Time(Func<TRow, DateTime> 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<TRow, TValue1, TState1, TValue2, TState2, TCompoundState> : IDataGridColumnPersistentFilter<TRow, TCompoundState> {
|
||||
public class TupleFilter<TRow, TValue1, TState1, TValue2, TState2, TCompoundState> : ViewModelBaseWithValidation, IDataGridColumnPersistentFilter<TRow, TCompoundState> {
|
||||
readonly FilterBase<TRow, TValue1, TState1> _filter1;
|
||||
readonly FilterBase<TRow, TValue2, TState2> _filter2;
|
||||
readonly Func<TState1, TState2, TCompoundState> _get_state;
|
||||
@@ -661,13 +702,27 @@ namespace mbts.spark {
|
||||
public FilterBase<TRow, TValue1, TState1> filter1 => _filter1;
|
||||
public FilterBase<TRow, TValue2, TState2> 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));
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -7,16 +7,23 @@
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
<Style TargetType="c:TimeFilterControl">
|
||||
<Setter Property="Focusable" Value="False"/>
|
||||
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="{x:Type c:TimeFilterControl}">
|
||||
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
|
||||
<Border
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
<c:RadioButtonsControl ItemsSource="{u:EnumBindingSource c:TimeFilterMode}" SelectedItem="{Binding Mode, RelativeSource={RelativeSource TemplatedParent}}"/>
|
||||
<c:RadioButtonsControl
|
||||
ItemsSource="{u:EnumBindingSource c:TimeFilterMode}"
|
||||
SelectedItem="{Binding Mode, RelativeSource={RelativeSource TemplatedParent}}"
|
||||
/>
|
||||
<Grid x:Name="PeriodRoot" Grid.Row="1" Margin="0,6,0,0" IsEnabled="False">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition/>
|
||||
@@ -36,12 +43,22 @@
|
||||
<TextBlock Grid.Column="2" HorizontalAlignment="Left">
|
||||
<Hyperlink x:Name="ClearBeginLink" Focusable="False">(Set Anytime)</Hyperlink>
|
||||
</TextBlock>
|
||||
<c:DateTimePicker Grid.Row="2" Grid.ColumnSpan="3" TimeTuple="{Binding Begin, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
<c:DateTimePicker
|
||||
x:Name="PART_Begin_DateTimePicker"
|
||||
Grid.Row="2"
|
||||
Grid.ColumnSpan="3"
|
||||
TimeTuple="{Binding Begin, RelativeSource={RelativeSource TemplatedParent}}"
|
||||
/>
|
||||
<TextBlock Grid.Row="4" VerticalAlignment="Center">To</TextBlock>
|
||||
<TextBlock Grid.Row="4" Grid.Column="2" HorizontalAlignment="Left">
|
||||
<Hyperlink x:Name="ClearEndLink" Focusable="False">(Set Anytime)</Hyperlink>
|
||||
</TextBlock>
|
||||
<c:DateTimePicker Grid.Row="6" Grid.ColumnSpan="3" TimeTuple="{Binding End, RelativeSource={RelativeSource TemplatedParent}}" />
|
||||
<c:DateTimePicker
|
||||
x:Name="PART_End_DateTimePicker"
|
||||
Grid.Row="6"
|
||||
Grid.ColumnSpan="3"
|
||||
TimeTuple="{Binding End, RelativeSource={RelativeSource TemplatedParent}}"
|
||||
/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
using System.Windows;
|
||||
using mbts.spark;
|
||||
using mbts.ui.adorners;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
/// <summary>
|
||||
@@ -10,6 +15,9 @@ namespace mbts.ui.controls {
|
||||
static TimeFilterControl() =>
|
||||
DefaultStyleKeyProperty.OverrideMetadata(typeof(TimeFilterControl), new FrameworkPropertyMetadata(typeof(TimeFilterControl)));
|
||||
|
||||
public TimeFilterControl() =>
|
||||
new TimeFilterErrorAdorner(this);
|
||||
|
||||
public static readonly DependencyProperty ModeProperty = DependencyProperty.Register("Mode", typeof(TimeFilterMode), typeof(TimeFilterControl),
|
||||
new FrameworkPropertyMetadata(TimeFilterMode.Anytime, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
||||
public TimeFilterMode Mode { get => (TimeFilterMode)GetValue(ModeProperty); set => SetValue(ModeProperty, value); }
|
||||
@@ -28,6 +36,27 @@ namespace mbts.ui.controls {
|
||||
clear_begin_time.Command = new DelegateCommand(() => Begin = default);
|
||||
clear_end_time.Command = new DelegateCommand(() => End = default);
|
||||
}
|
||||
|
||||
class TimeFilterErrorAdorner : NotifyDataErrorAdorner<TimeFilterControl, DataGridColumnFilters.ITime>, IDisposable {
|
||||
readonly ChildProperty<DateTimePicker> _begin_picker, _end_picker;
|
||||
|
||||
public TimeFilterErrorAdorner(TimeFilterControl view) : base(view) {
|
||||
_begin_picker = child_property<DateTimePicker>("PART_Begin_DateTimePicker");
|
||||
_end_picker = child_property<DateTimePicker>("PART_End_DateTimePicker");
|
||||
}
|
||||
|
||||
protected override void OnRender(DrawingContext drawing_context) {
|
||||
const double x_offset = 99, y_offset = -3;
|
||||
if (_vm.GetErrors(nameof(DataGridColumnFilters.ITime.begin)).Cast<string>().FirstOrDefault() is string begin_msg) {
|
||||
this.draw_border(drawing_context, _begin_picker.value, null, new Pen(Brushes.Red, 1));
|
||||
this.draw_relative_text(RelativePosition.Above, begin_msg, drawing_context, _begin_picker.value, Brushes.Red, x_offset, y_offset);
|
||||
}
|
||||
if (_vm.GetErrors(nameof(DataGridColumnFilters.ITime.end)).Cast<string>().FirstOrDefault() is string end_msg) {
|
||||
this.draw_border(drawing_context, _end_picker.value, null, new Pen(Brushes.Red, 1));
|
||||
this.draw_relative_text(RelativePosition.Above, end_msg, drawing_context, _end_picker.value, Brushes.Red, x_offset, y_offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum TimeFilterMode {
|
||||
|
||||
@@ -158,6 +158,10 @@
|
||||
<Setter Property="Source" Value="{x:Static uc:Icons.FilterActive}"/>
|
||||
<Setter Property="Opacity" Value="1.0"/>
|
||||
</DataTrigger>
|
||||
<DataTrigger Binding="{Binding Path='Column.(c:DataGridFilterProperties.FilterActive)', RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}}" Value="{x:Null}">
|
||||
<Setter Property="Source" Value="{x:Static uc:Icons.FilterError}"/>
|
||||
<Setter Property="Opacity" Value="1.0"/>
|
||||
</DataTrigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Image.Style>
|
||||
|
||||
Reference in new issue
Block a user