From 56b855ea83cbd74499742fca54e34011ec921981 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 3 Jun 2018 18:12:54 -0400 Subject: [PATCH] SPARK-1194 modify ticket should default to original quantity (not working) --- .../spark/Orders/ExistingOrderTicketVM.cs | 7 ++-- .../spark/core/Utility/ViewModelBase.cs | 36 +++++++++++++++++++ .../spark/view/Spark/Shell/CalculatorLogic.cs | 9 +++-- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/client/spark/Orders/ExistingOrderTicketVM.cs b/src/client/spark/Orders/ExistingOrderTicketVM.cs index 22b004fe4..99a0416cb 100644 --- a/src/client/spark/Orders/ExistingOrderTicketVM.cs +++ b/src/client/spark/Orders/ExistingOrderTicketVM.cs @@ -87,11 +87,8 @@ namespace mbts.spark { key = ort.order.Key; if (DoubleHelper.IsNaNOrInfinity(price)) price = ort.order.State.Price; - if (!size_calculator.value.HasValue || size_calculator.value.Value == 0) { - int wq = ort.order.working_quantity(); - if (wq > 0) - size_calculator.value = (uint)wq; - } + if (!size_calculator.value.HasValue || !size_calculator.value_setter_called) + size_calculator.bypass_value_setter(ort.order.State.Quantity); }), Observable.Merge(_price_dirty, _size_dirty, _account_dirty, _time_in_force_dirty, _order_type_dirty).ObserveOnDispatcher().Subscribe(_ => RaisePropertyChanged(nameof(dirty_items_text))), _price_dirty.observe_current(true).CombineLatest(_size_dirty.observe_current(true), (pd, sd) => pd || sd).DistinctUntilChanged().ObserveOnDispatcher().Subscribe(modify_order)); diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index 5767ee66d..7b440fb2c 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -247,6 +247,42 @@ namespace mbts.ui.shell public void refresh() => set(_get_model()); } + + /// + /// A thin wrapper around Property<T> that trips a 'setter_called' flag when the value is set through value-property-setter (i.e., through User interaction). + /// Note: This is class is particularly useful when a default/settings-based value arrives late, and the viewmodel must decide whether or not to override the current value. + /// + protected class UserProperty : IObservable<(T oldval, T newval)> { + readonly Property _prop_model; + + public UserProperty(Property prop_model) => + _prop_model = prop_model; + + public event PropertyChange Changing { add { _prop_model.Changing += value; } remove { _prop_model.Changing -= value; } } + public event PropertyChange Changed { add { _prop_model.Changed += value; } remove { _prop_model.Changed -= value; } } + + public IDisposable Subscribe(IObserver<(T oldval, T newval)> observer) => _prop_model.Subscribe(observer); + public IObservable observe_current(bool start_immediately = false) => _prop_model.observe_current(start_immediately); + + public T Value { + get => _prop_model.Value; + set { + setter_called = true; + _prop_model.set(value); + } + } + public bool setter_called { get; private set; } + + /// + /// Sets the value without tripping the 'setter_called' flag. + /// + public void bypass_setter(T value) => _prop_model.set(value); + /// + /// Sets 'setter_called' to false. + /// + public void reset() => setter_called = false; + } + #endregion protected class ObservablePropertySink : Property, IObserver diff --git a/src/client/spark/view/Spark/Shell/CalculatorLogic.cs b/src/client/spark/view/Spark/Shell/CalculatorLogic.cs index 58af1ab3f..56a81f4fb 100644 --- a/src/client/spark/view/Spark/Shell/CalculatorLogic.cs +++ b/src/client/spark/view/Spark/Shell/CalculatorLogic.cs @@ -6,15 +6,16 @@ using System.Windows.Input; namespace mbts.spark { public class CalculatorLogic : DisposableViewModelBase { - readonly Property _value; + readonly UserProperty _value; readonly Property _buttons; public CalculatorLogic(uint? initial_size, IObservable buttons_source) { - _value = backing(nameof(value), initial_size); + _value = new UserProperty(backing(nameof(value), initial_size)); _buttons = backing(nameof(buttons), buttons_source); } - public uint? value { get => _value.Value; set => _value.set(value); } + public uint? value { get => _value.Value; set => _value.Value = value; } + public bool value_setter_called => _value.setter_called; public IObservable value_observable => _value.Select(vals => vals.newval); public int[] buttons { get => _buttons.Value; set => _buttons.set(value); } public ICommand click => new DelegateCommand(handle_calculator_click); @@ -24,6 +25,8 @@ namespace mbts.spark { public IObservable observe_current(bool start_immediately = false) => _value.observe_current(start_immediately); + public void bypass_value_setter(uint value) => _value.bypass_setter(value); + public bool validate() { if (!value.HasValue || value.Value == 0) { flash_error(nameof(value)).ignore();