diff --git a/src/client/spark/Orders/OrderTypeVMs/TimeBoxOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/TimeBoxOrderVM.cs index ed3b559ae..093e56e32 100644 --- a/src/client/spark/Orders/OrderTypeVMs/TimeBoxOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/TimeBoxOrderVM.cs @@ -32,7 +32,10 @@ namespace mbts.spark { ObservableCollection _order_types = new ObservableCollection(); ObservableAutoWrapper _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 start_modes => EnumHelper.get_values(); + public IEnumerable stop_modes => EnumHelper.get_values(); + public ICollection 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); } } } diff --git a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml index a7bfa086c..1d42ae646 100644 --- a/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml +++ b/src/client/spark/view/Controls/Order/OrderDetailsPopupControl.xaml @@ -6,8 +6,10 @@ + + + + \ No newline at end of file diff --git a/src/client/spark/view/Controls/Time/TimeSpanControl.xaml.cs b/src/client/spark/view/Controls/Time/TimeSpanControl.xaml.cs new file mode 100644 index 000000000..bb5d0c159 --- /dev/null +++ b/src/client/spark/view/Controls/Time/TimeSpanControl.xaml.cs @@ -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 { + /// + /// Interaction logic for TimeSpanControl.xaml + /// + 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( + 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("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; } + } + } +} \ No newline at end of file diff --git a/src/client/spark/view/Utility/TextUtils.cs b/src/client/spark/view/Utility/TextUtils.cs index 3df63ccb1..c64f9a66b 100644 --- a/src/client/spark/view/Utility/TextUtils.cs +++ b/src/client/spark/view/Utility/TextUtils.cs @@ -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)))))?$"); } } } diff --git a/src/client/spark/view/Utility/WPFHelper.cs b/src/client/spark/view/Utility/WPFHelper.cs index c4eba0c35..21f2b8776 100644 --- a/src/client/spark/view/Utility/WPFHelper.cs +++ b/src/client/spark/view/Utility/WPFHelper.cs @@ -9,6 +9,20 @@ using System.Windows.Input; namespace mbts.ui { static class WPFHelper { + public delegate void PropertyChangedCallback(TOwner owner, TValue old_value, TValue new_value); + + public static DependencyProperty create_dp(string name, TValue default_value = default) => + DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(default_value)); + + public static DependencyProperty create_dp(string name, Action callback, TValue default_value = default) => + DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(default_value, _convert(callback))); + + public static DependencyProperty create_dp(string name, FrameworkPropertyMetadataOptions options, Action callback, TValue default_value = default) => + DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new FrameworkPropertyMetadata(default_value, options, _convert(callback))); + + public static DependencyProperty create_dp(string name, PropertyChangedCallback 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(PropertyChangedCallback callback) => + (d, e) => callback(ObjectHelper.try_convert(d), ObjectHelper.try_convert(e.OldValue), ObjectHelper.try_convert(e.NewValue)); + + static PropertyChangedCallback _convert(Action callback) => + (d, e) => callback(ObjectHelper.try_convert(d), ObjectHelper.try_convert(e.NewValue)); } } \ No newline at end of file