SPARK-1006/SPARK-1011 TimeBox params dropdown needs relative time options

This commit is contained in:
John Colvin committed 2018-04-21 12:26:32 -04:00
1 parent 9a32a5ec67
commit 0480405291
6 files changed
+340 -41

No files matched your search

@@ -32,7 +32,10 @@ namespace mbts.spark {
ObservableCollection<CustomOneofCase> _order_types = new ObservableCollection<CustomOneofCase>();
ObservableAutoWrapper<IOrderTypeVM, CustomOneofCase> _child_order_choices;
InlineTimeBoxOrderProperties _inline_props;
TimeTextTuple _start = TimeTextTuple.FromDateTime(DateTime.Now), _stop = TimeTextTuple.FromDateTime(DateTime.Now.AddMinutes(5));
TimeTextTuple _absolute_start = TimeTextTuple.FromDateTime(DateTime.Now), _absolute_stop = TimeTextTuple.FromDateTime(DateTime.Now.AddMinutes(5));
TimeSpan? _relative_start = TimeSpan.Zero, _relative_stop = TimeSpan.FromMinutes(5);
StartMode _start_mode = StartMode.Now;
StopMode _stop_mode = StopMode.After;
IOrderTypeVM _child_order;
IDisposable _dispose, _child_order_errors_sub;
@@ -57,12 +60,12 @@ namespace mbts.spark {
_select_child_order(p.selected_child_order_type.value);
}
if (p.start.force_override) {
start = TimeTextTuple.FromDateTime(p.start.value);
RaisePropertyChanged(nameof(start));
absolute_start = TimeTextTuple.FromDateTime(p.start.value);
RaisePropertyChanged(nameof(absolute_start));
}
if (p.stop.force_override) {
stop = TimeTextTuple.FromDateTime(p.stop.value);
RaisePropertyChanged(nameof(stop));
absolute_stop = TimeTextTuple.FromDateTime(p.stop.value);
RaisePropertyChanged(nameof(absolute_stop));
}
_inline_props = p;
_validate();
@@ -71,6 +74,23 @@ namespace mbts.spark {
_validate();
}
public enum StartMode {
At,
In,
[Description("Immediately")]
Now
}
public enum StopMode {
At,
[Description("In (after)")]
After,
[Description("When Completed")]
WhenComleted
}
public IEnumerable<StartMode> start_modes => EnumHelper.get_values<StartMode>();
public IEnumerable<StopMode> stop_modes => EnumHelper.get_values<StopMode>();
public ICollection<IOrderTypeVM> child_order_types {
get {
if (_child_order_choices == null) {
@@ -100,30 +120,40 @@ namespace mbts.spark {
}
}
public TimeTextTuple start {
get => _start;
public StartMode start_mode {
get => _start_mode;
set {
_start = value;
_start_mode = value;
_validate();
RaisePropertyChanged(nameof(show_absolute_start), nameof(show_relative_start));
}
}
public TimeTextTuple stop {
get => _stop;
public StopMode stop_mode {
get => _stop_mode;
set {
_stop = value;
_stop_mode = value;
_validate();
RaisePropertyChanged(nameof(show_absolute_stop), nameof(show_relative_stop));
}
}
public TimeTextTuple absolute_start { get => _absolute_start; set { _absolute_start = value; _validate(); } }
public TimeTextTuple absolute_stop { get => _absolute_stop; set { _absolute_stop = value; _validate(); } }
public TimeSpan? relative_start { get => _relative_start; set { _relative_start = value; _validate(); } }
public TimeSpan? relative_stop { get => _relative_stop; set { _relative_stop = value; _validate(); } }
public bool show_absolute_start => _start_mode == StartMode.At;
public bool show_relative_start => _start_mode == StartMode.In;
public bool show_absolute_stop => _stop_mode == StopMode.At;
public bool show_relative_stop => _stop_mode == StopMode.After;
public override void assign_to(CustomOrderDefinition value) {
if (_child_order == null)
_select_child_order(model.factory.default_order_type(_security.Value, true));
_validate();
var (start_time, stop_time) = _validate();
if (!HasErrors) {
DateTime? start_result = start.result, stop_result = stop.result;
var tb = factory.time_box_order();
tb.Start = start.result.Value.ToUniversalTime().ToGoogleTime();
tb.Stop = stop.result.Value.ToUniversalTime().ToGoogleTime();
tb.Start = start_time.Value.ToUniversalTime().ToGoogleTime();
tb.Stop = stop_time.Value.ToUniversalTime().ToGoogleTime();
child_order.assign_to(tb.Child);
value.TimeBox = tb;
}
@@ -155,19 +185,57 @@ namespace mbts.spark {
RaisePropertyChanged(nameof(child_order));
}
void _validate() {
if (_child_order == null || (_child_order is INotifyDataErrorInfo ndei && ndei.HasErrors))
set_error(nameof(child_order), string.Empty, true);
else clear_error(nameof(child_order));
if (!start.result.HasValue)
set_error(nameof(start), string.Empty, true);
else clear_error(nameof(start));
if (!stop.result.HasValue)
set_error(nameof(stop), string.Empty, true);
else clear_error(nameof(stop));
if (!HasErrors) {
if (start.result.Value > stop.result.Value)
set_error(nameof(stop), string.Empty, true);
(DateTime? start_time, DateTime? stop_time) _validate() {
assert(_child_order != null && !(_child_order is INotifyDataErrorInfo ndei && ndei.HasErrors), nameof(child_order));
// ti.start_name is null if mode is Now, stop_name if mode is WhenCompleted
var now = DateTime.Now;
var ti = get_time_info();
if (ti.start_name != null) {
if (!ti.start_time.HasValue || ti.start_time.Value < now)
set_error(ti.start_name, string.Empty, false);
else clear_error(ti.start_name);
}
if (ti.stop_name != null) {
if (!ti.stop_time.HasValue || ti.stop_time.Value <= now || ti.start_time.HasValue && ti.start_time.Value >= ti.stop_time.Value)
set_error(ti.stop_name, string.Empty, false);
else clear_error(ti.stop_name);
}
return (ti.start_time, ti.stop_time);
//
(DateTime? start_time, string start_name, DateTime? stop_time, string stop_name) get_time_info() {
DateTime? start_time = null, stop_time = null;
string start_name = null, stop_name = null;
switch (_start_mode) {
case StartMode.At:
start_time = _absolute_start.result;
start_name = nameof(absolute_start);
break;
case StartMode.In:
if (_relative_start.HasValue)
start_time = now + _relative_start.Value;
start_name = nameof(relative_start);
break;
case StartMode.Now:
default:
start_time = now;
break;
}
switch (_stop_mode) {
case StopMode.At:
stop_time = _absolute_stop.result;
stop_name = nameof(absolute_stop);
break;
case StopMode.After:
if (_relative_stop.HasValue)
stop_time = (start_time ?? now) + _relative_stop;
stop_name = nameof(relative_stop);
break;
case StopMode.WhenComleted:
default:
stop_time = DateTime.MaxValue;
break;
}
return (start_time, start_name, stop_time, stop_name);
}
}
}
@@ -6,8 +6,10 @@
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/view/Resources/Converters.xaml"/>
<ResourceDictionary Source="/view/Resources/ComboBoxes.xaml"/>
<ResourceDictionary Source="/view/Resources/DataTemplates.xaml"/>
<ResourceDictionary Source="/view/Controls/PriceEntryControl.xaml"/>
<ResourceDictionary Source="/view/Controls/Popup/PopupControl.xaml"/>
<ResourceDictionary Source="/view/Controls/Time/TimeSpanControl.xaml"/>
<ResourceDictionary Source="/Modules/Spreader/ExecutionStrategyEditor.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="uag:OrderDetailsPopupControl">
@@ -86,25 +88,65 @@
<usp:OrderModificationTemplateSelector.TimeBoxOrderTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4"/>
<RowDefinition/>
<RowDefinition Height="4"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="12"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="6"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="6"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock VerticalAlignment="Center">Start</TextBlock>
<c:DateTimePicker Grid.Column="2" TimeTuple="{Binding start}"/>
<ComboBox
Grid.Column="2"
ItemsSource="{Binding start_modes}"
ItemTemplate="{StaticResource EnumDescriptionTemplate}"
SelectedItem="{Binding start_mode}"
/>
<c:DateTimePicker
Grid.Column="4"
Margin="8,0,0,0"
TimeTuple="{Binding absolute_start}"
Visibility="{Binding show_absolute_start, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
<c:TimeSpanControl
Grid.Column="4"
Margin="8,0,0,0"
TimeSpan="{Binding relative_start, Mode=TwoWay}"
Visibility="{Binding show_relative_start, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
<TextBlock Grid.Row="2" VerticalAlignment="Center">Stop</TextBlock>
<c:DateTimePicker Grid.Row="2" Grid.Column="2" TimeTuple="{Binding stop}"/>
<TextBlock Grid.Row="4">Child</TextBlock>
<ComboBox
Grid.Column="2"
Grid.Row="2"
ItemsSource="{Binding stop_modes}"
ItemTemplate="{StaticResource EnumDescriptionTemplate}"
SelectedItem="{Binding stop_mode}"
/>
<c:DateTimePicker
Grid.Row="2"
Grid.Column="4"
Margin="8,0,0,0"
TimeTuple="{Binding absolute_stop}"
Visibility="{Binding show_absolute_stop, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
<c:TimeSpanControl
Grid.Row="2"
Grid.Column="3"
Margin="8,0,0,0"
TimeSpan="{Binding relative_stop, Mode=TwoWay}"
Visibility="{Binding show_relative_stop, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
<TextBlock Grid.Row="4" VerticalAlignment="Center">Child</TextBlock>
<uag:OrderDetailsPopupControl
Grid.Row="4" Grid.Column="2"
Grid.Column="2"
Grid.ColumnSpan="2"
Grid.Row="4"
IsOpen="{Binding child_order.editable_properties_open, FallbackValue=False}"
ItemsSource="{Binding child_order_types}"
SelectedItem="{Binding child_order}"
@@ -0,0 +1,43 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="clr-namespace:mbts.ui"
xmlns:c="clr-namespace:mbts.ui.controls"
xmlns:cv="clr-namespace:mbts.ui.converters"
xmlns:txt="clr-namespace:mbts.ui.utility.text"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/view/Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
<cv:ValueMap x:Key="NullOrEmptyVisibilityConverter" Default="{u:Visibility Hidden}">
<cv:KeyValue Key="{x:Static sys:String.Empty}" Value="{u:Visibility Visible}"/>
<cv:KeyValue Key="{x:Null}" Value="{u:Visibility Visible}"/>
</cv:ValueMap>
<Style x:Key="TextPromptStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource Text.Prompt.Foreground.Brush}"/>
<Setter Property="Margin" Value="4,0,0,0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
<Style TargetType="c:TimeSpanControl">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="c:TimeSpanControl">
<Grid>
<TextBox
x:Name="text_box"
u:TextBoxBehavior.EnableArrowKeyTraverse="True"
u:TextBoxBehavior.InputFilter="{u:RegexInputFilter {x:Static Member=TimeSpan, MemberType={u:Nested txt:TextUtils.RegexFilters.Time}}}"
u:Behavior.DisablePaste="True"
Height="20"
/>
<TextBlock
Text="hh:mm:ss"
Margin="4,0,0,0" Style="{StaticResource TextPromptStyle}" Visibility="{Binding Text, ElementName=text_box, Converter={StaticResource NullOrEmptyVisibilityConverter}}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
@@ -0,0 +1,125 @@
using mbts.ui.shell;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace mbts.ui.controls {
/// <summary>
/// Interaction logic for TimeSpanControl.xaml
/// </summary>
class TimeSpanControl : Control {
TimeSpanVM _vm;
bool _suspend_notify_vm;
static TimeSpanControl() =>
DefaultStyleKeyProperty.OverrideMetadata(typeof(TimeSpanControl), new FrameworkPropertyMetadata(typeof(TimeSpanControl)));
public static readonly DependencyProperty TimeSpanProperty =
WPFHelper.create_dp<TimeSpan?, TimeSpanControl>(
nameof(TimeSpan),
(o, val) => {
if (!o._suspend_notify_vm && o._vm != null)
o._vm.result = val;
});
public TimeSpan? TimeSpan { get => GetValue(TimeSpanProperty) as TimeSpan?; set => SetValue(TimeSpanProperty, value); }
static readonly TimeSpan
_one_hr = System.TimeSpan.FromHours(1),
_one_min = System.TimeSpan.FromMinutes(1),
_one_sec = System.TimeSpan.FromSeconds(1);
public override void OnApplyTemplate() {
if (_vm != null)
_vm.PropertyChanged -= _vm_PropertyChanged;
var text_box = this.assert_template_child<TextBox>("text_box");
_vm = new TimeSpanVM(text_box) { result = TimeSpan };
_vm.PropertyChanged += _vm_PropertyChanged;
text_box.SetBinding(TextBox.TextProperty, new Binding(nameof(TimeSpanVM.text)) { Source = _vm, Mode = BindingMode.TwoWay });
text_box.InputBindings.Clear();
text_box.InputBindings.Add(new KeyBinding(_vm.tick_up, Key.Up, ModifierKeys.None));
text_box.InputBindings.Add(new KeyBinding(_vm.tick_down, Key.Down, ModifierKeys.None));
}
private void _vm_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(TimeSpanVM.result)) {
_suspend_notify_vm = true;
TimeSpan = _vm.result;
_suspend_notify_vm = false;
}
}
class TimeSpanVM : ViewModelBase {
readonly TextBox _text_box;
TimeSpan? _result;
TimeSpan _last_well_formed_value = System.TimeSpan.Zero;
string _text;
public TimeSpanVM(TextBox text_box) {
_text_box = text_box;
tick_up = new DelegateCommand(() => {
int index = text_box.CaretIndex;
if (index <= 0)
result = _last_well_formed_value.Add(_one_min);
else {
int segment = _text.Take(index).Count(c => c == ':');
switch (segment) {
case 0: result = _last_well_formed_value.Add(_one_hr); break;
case 1: result = _last_well_formed_value.Add(_one_min); break;
default: result = _last_well_formed_value.Add(_one_sec); break;
}
}
text_box.CaretIndex = index;
RaisePropertyChanged(nameof(result));
});
tick_down = new DelegateCommand(() => {
int index = text_box.CaretIndex;
TimeSpan ts;
if (index <= 0)
ts = _last_well_formed_value.Subtract(_one_min);
else {
int segment = _text.Take(index).Count(c => c == ':');
switch (segment) {
case 0: ts = _last_well_formed_value.Subtract(_one_hr); break;
case 1: ts = _last_well_formed_value.Subtract(_one_min); break;
default: ts = _last_well_formed_value.Subtract(_one_sec); break;
}
}
result = ts > System.TimeSpan.Zero ? ts : System.TimeSpan.Zero;
text_box.CaretIndex = index;
RaisePropertyChanged(nameof(result));
});
}
public string text {
get => _text;
set {
if (System.TimeSpan.TryParse(_text = value, out TimeSpan ts))
_result = _last_well_formed_value = ts;
else _result = null;
RaisePropertyChanged(nameof(result));
}
}
public TimeSpan? result {
get => _result;
set {
if (value.HasValue) {
_text = (_last_well_formed_value = (_result = value).Value).ToString();
RaisePropertyChanged(nameof(text));
} else if (_result.HasValue) {
_result = null;
RaisePropertyChanged(nameof(text));
}
}
}
public ICommand tick_up { get; }
public ICommand tick_down { get; }
}
}
}
+2 -1
View File
@@ -81,7 +81,8 @@ namespace mbts.ui.utility.text {
MilitaryTime = new Regex(@"^(([0-1]?[0-9])|(2?[0-3]?))(:?|:([0-5]?|[0-5][0-9]?))$"),
DaysInMonth = new Regex(@"^([0-9]?|[0-2][0-9]|3[0-1])?$"),
MonthAbbrev = new Regex(@"^(?i)(a(ug?|pr?)?|d(e|ec)?|f(e?b)?|j(an?|u[nl]?)?|m(a[ry]?)?|(o(ct?)?)|s(ep?)?|n(ov?)?)?$"),
Year = new Regex(@"^\d{0,4}$");
Year = new Regex(@"^\d{0,4}$"),
TimeSpan = new Regex(@"^(\d+(:?|:([0-5]?|[0-5]\d(:?|:([0-5]?|[0-5]\d)))))?$");
}
}
}
@@ -9,6 +9,20 @@ using System.Windows.Input;
namespace mbts.ui {
static class WPFHelper {
public delegate void PropertyChangedCallback<TValue, TOwner>(TOwner owner, TValue old_value, TValue new_value);
public static DependencyProperty create_dp<TValue, TOwner>(string name, TValue default_value = default) =>
DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(default_value));
public static DependencyProperty create_dp<TValue, TOwner>(string name, Action<TOwner, TValue> callback, TValue default_value = default) =>
DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(default_value, _convert(callback)));
public static DependencyProperty create_dp<TValue, TOwner>(string name, FrameworkPropertyMetadataOptions options, Action<TOwner, TValue> callback, TValue default_value = default) =>
DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new FrameworkPropertyMetadata(default_value, options, _convert(callback)));
public static DependencyProperty create_dp<TValue, TOwner>(string name, PropertyChangedCallback<TValue, TOwner> callback, TValue default_value = default) =>
DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(default_value, _convert(callback)));
public static void manage_collection_change(DependencyPropertyChangedEventArgs e, NotifyCollectionChangedEventHandler handler) =>
manage_collection_change(e.OldValue as INotifyCollectionChanged, e.NewValue as INotifyCollectionChanged, handler, null, null);
@@ -144,5 +158,11 @@ namespace mbts.ui {
hook = rcf.subscribe(on_request);
}
}
static PropertyChangedCallback _convert<TValue, TOwner>(PropertyChangedCallback<TValue, TOwner> callback) =>
(d, e) => callback(ObjectHelper.try_convert<TOwner>(d), ObjectHelper.try_convert<TValue>(e.OldValue), ObjectHelper.try_convert<TValue>(e.NewValue));
static PropertyChangedCallback _convert<TValue, TOwner>(Action<TOwner, TValue> callback) =>
(d, e) => callback(ObjectHelper.try_convert<TOwner>(d), ObjectHelper.try_convert<TValue>(e.NewValue));
}
}