From b76db3b06e8b82cff105af54c393ee3cd2c7b702 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 20 Mar 2018 16:22:43 -0400 Subject: [PATCH] SPARK-700 disallow changes to synth if working orders exist --- .../Spreader/ShowSyntheticEditorHandler.cs | 11 +++--- .../Spreader/SyntheticManagementWindow.cs | 9 +++-- .../Modules/Spreader/VM/SyntheticEditorVM.cs | 30 +++++++++++----- .../Orders/OrderTypeVMs/SpreaderOrderVM.cs | 6 ++-- src/client/spark/Orders/OrdersModel.cs | 6 ++++ .../spark/core/Utility/ObservantCommand.cs | 35 +++++++++++++++++++ 6 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 src/client/spark/core/Utility/ObservantCommand.cs diff --git a/src/client/spark/Modules/Spreader/ShowSyntheticEditorHandler.cs b/src/client/spark/Modules/Spreader/ShowSyntheticEditorHandler.cs index 5966316bb..ba41f2962 100644 --- a/src/client/spark/Modules/Spreader/ShowSyntheticEditorHandler.cs +++ b/src/client/spark/Modules/Spreader/ShowSyntheticEditorHandler.cs @@ -15,13 +15,13 @@ using System.Threading.Tasks; namespace bts.shell { [Export(typeof(IInitializable))] partial class ShowSyntheticEditorHandler : IInitializable, IDisposable { - readonly SpreaderModel _model; + readonly OrdersModel _model; readonly IBus _bus; IDisposable _dispose; [ImportingConstructor] - public ShowSyntheticEditorHandler(IBus bus, SpreaderModel model) { + public ShowSyntheticEditorHandler(IBus bus, OrdersModel model) { _bus = bus; _model = model; } @@ -56,8 +56,9 @@ namespace bts.shell { } async Task _show_synthetic_strategy_async(SecurityImmutable verified_synthetic) { - var lookup = await _model.lookup_strategy_async(verified_synthetic); - if (lookup.success) _show_synthetic_editor(new SyntheticEditorVM(_model, verified_synthetic, lookup.result)); + var lookup = await _model.spreader.lookup_strategy_async(verified_synthetic); + if (lookup.success) + _show_synthetic_editor(new SyntheticEditorVM(_model.spreader, verified_synthetic, lookup.result, _model.security_has_working_orders(verified_synthetic.Id))); } void _show_synthetic_create(IEnumerable legs) { @@ -67,7 +68,7 @@ namespace bts.shell { Symbol = legs.FirstOrDefault()?.Security.Symbol ?? string.Empty, ContractSpec = SecurityDataHelper.Defaults.ContractSpecification }); - _show_synthetic_editor(new SyntheticEditorVM(_bus, _model, sec, new ExecutionStrategyImmutable(SyntheticDataHelper.Defaults.ExecutionStrategy(sec)))); + _show_synthetic_editor(new SyntheticEditorVM(_bus, _model.spreader, sec, new ExecutionStrategyImmutable(SyntheticDataHelper.Defaults.ExecutionStrategy(sec)))); } #region IInitializable diff --git a/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs b/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs index 77521b354..f81a15a17 100644 --- a/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs +++ b/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs @@ -32,10 +32,10 @@ namespace mbts.spark { () => new SecurityCriteria(_vm.Current.selected_securities), () => true ), SparkCommand.create("Remove", Icons.RemoveSynthetic, SparkTags.SyntheticDeleteTag).complete( _vm.Observable.SelectMany(vm => vm.current_row_observable), - r => r != null && r.n_working_orders == 0, r => r == null ? 0 : r.security_key + can_row_change, r => r == null ? 0 : r.security_key ), SparkCommand.create("Change", Icons.ModifySynthetic, SparkTags.ShowSyntheticStrategyTag).complete( - _vm.Observable.SelectMany(vm => vm.current_security_observable), - s => s.HasValue, ObjectHelper.ValueOrDefault + _vm.Observable.SelectMany(vm => vm.current_row_observable), + can_row_change, r => r == null ? SecurityDataHelper.Defaults.SecurityImmutable : r.security ), SparkCommands.ShowLadder.complete( _vm.Observable.SelectMany(vm => vm.current_security_md), md => md.HasValue, ObjectHelper.ValueOrDefault ), SparkCommands.ShowOrders.complete(() => new OrderBookWindowFactory.OrderBookStartupData { @@ -44,6 +44,9 @@ namespace mbts.spark { } }) }; + + bool can_row_change(SyntheticManagementDialogVM.Row r) => + r != null && r.n_working_orders == 0; } public void initialize(StartupData state) { diff --git a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs index 030064e43..e009ad56c 100644 --- a/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs +++ b/src/client/spark/Modules/Spreader/VM/SyntheticEditorVM.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; +using System.Reactive.Linq; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; @@ -20,10 +21,12 @@ namespace mbts.spark { /// /// The view model designed to serve as the DataContext for the SyntheticEditorView /// - partial class SyntheticEditorVM : ViewModelBaseWithValidation, IRequestViewClose, IRequestBindingSourceUpdate, IDisposable { + partial class SyntheticEditorVM : DisposableViewModelBase, IRequestViewClose, IRequestBindingSourceUpdate, IDisposable { readonly ObservableCollection _available_tick_factors = new ObservableCollection(TickUtils.all_present_ticks()); readonly QuantityModel _quantity_model; readonly PriceMultiplierModel _price_mult_model; + readonly List _resources = new List(); + readonly Property _has_working_orders; Security _synthetic; bool _set_name_automatically, _set_ticks_automatically, _set_quantities_automatically, _set_price_mults_automatically, _is_setting_quantities, _is_setting_price_mults; TickFactors _tick_factors; @@ -34,16 +37,24 @@ namespace mbts.spark { /// /// The already-existing synthetic security who's strategy may be modified /// The already-existing strategy that may be modified - public SyntheticEditorVM(SpreaderModel model, SecurityImmutable synthetic, ExecutionStrategyImmutable strategy) + public SyntheticEditorVM(SpreaderModel model, SecurityImmutable synthetic, ExecutionStrategyImmutable strategy, IObservable security_has_working_orders) : this(synthetic, strategy, false) { ExceptionAssert.InvalidOperation.That(synthetic.Id != 0, "This constructor may only be used for already-existing synthetic securities (id must be non-zero)."); _tick_factors = TickUtils.get_regional_ticks(synthetic.ContractSpec).low_tick; title = $"Edit '{synthetic.Synthetic.Name}'"; icon = Icons.ModifySynthetic; - commit = new DelegateCommand(async () => { + _has_working_orders = backing(nameof(has_working_orders), security_has_working_orders.ObserveOnDispatcher()); + var commit = new ObservantCommand(async () => { await model.save_strategy(_synthetic.Id, get_strategy()); RequestClose?.Invoke(this, new EventArgs()); }); + _resources.Add(security_has_working_orders.Select(v => !v).ObserveOnDispatcher().Subscribe(commit)); + _resources.Add(_has_working_orders.ObserveOnDispatcher().Subscribe(vals => { + if (vals.newval) + set_error(nameof(has_working_orders), "Security has working orders"); + else clear_error(nameof(has_working_orders)); + })); + this.commit = commit; } /// /// Initializes a new instance of SyntheticEditorVM class. This constructor may only be used for new synthetic securities (id must be zero). @@ -210,6 +221,7 @@ namespace mbts.spark { RaisePropertyChanged(); } } + public bool has_working_orders => _has_working_orders.Value; public bool is_new { get; } public bool is_new_leg_popup_open { get; set; } public SyntheticContract.Types.PriceCalc price_calc { get => _synthetic.Synthetic.PriceCalc; set => _synthetic.Synthetic.PriceCalc = value; } @@ -221,11 +233,6 @@ namespace mbts.spark { public ContractPickerModel contract_picker_model { get; } public ICollectionView available_tick_factors_view { get; } - public void Dispose() { - foreach (var r in rows) r.Dispose(); - contract_picker_model?.Dispose(); - } - public ExecutionStrategyImmutable get_strategy() { var mutable_strategy = _strategy.CloneToMutable(); mutable_strategy.LegParamsMap.Clear(); @@ -241,6 +248,13 @@ namespace mbts.spark { RaisePropertyChanged(nameof(name), nameof(tick_factors)); } + protected override void Dispose(bool disposing) { + foreach (var r in rows) r.Dispose(); + contract_picker_model?.Dispose(); + Disposable.dispose(_resources); + base.Dispose(disposing); + } + static string _deduce_symbol(SecurityImmutable security) { if (!string.IsNullOrWhiteSpace(security.Symbol)) return security.Symbol; if (security.Synthetic.Legs.Count() > 0) return security.Synthetic.Legs.First().Security.Symbol; diff --git a/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs b/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs index 267913ea5..9a96b1446 100644 --- a/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs +++ b/src/client/spark/Orders/OrderTypeVMs/SpreaderOrderVM.cs @@ -45,8 +45,10 @@ namespace mbts.spark { public SyntheticEditorVM strategy_editor { get { - if (_strategy_editor == null && template.HasValue) - _strategy_editor = new SyntheticEditorVM(model.spreader, template.Value.Security, template.Value.ExecutionStrategy); + if (_strategy_editor == null && template.HasValue) { + var def = template.Value; + _strategy_editor = new SyntheticEditorVM(model.spreader, def.Security, def.ExecutionStrategy, model.security_has_working_orders(def.Security.Id)); + } return _strategy_editor; } } diff --git a/src/client/spark/Orders/OrdersModel.cs b/src/client/spark/Orders/OrdersModel.cs index 97702869c..f1e7153fe 100644 --- a/src/client/spark/Orders/OrdersModel.cs +++ b/src/client/spark/Orders/OrdersModel.cs @@ -262,6 +262,12 @@ namespace mbts.spark { } #endregion + public static IObservable security_has_working_orders(this OrdersModel orders_model, uint security_key) => + orders_model + .on_security_orders(security_key) + .Select(t => t.state.Any(kv => kv.Value.is_active())) + .StartWith(false); + public static async Task<(OrderRecordImmutable result, bool success)> order_from_key(this OrdersModel orders_model, uint order_key) { var lookup = await orders_model.on_orders.FirstAsyncTask(t => t.state.ContainsKey(order_key), TimeSpan.FromSeconds(3)); return lookup.success ? (lookup.result.state[order_key], true) : (default, false); diff --git a/src/client/spark/core/Utility/ObservantCommand.cs b/src/client/spark/core/Utility/ObservantCommand.cs new file mode 100644 index 000000000..821fb7f48 --- /dev/null +++ b/src/client/spark/core/Utility/ObservantCommand.cs @@ -0,0 +1,35 @@ +using System; +using System.Windows.Input; + +namespace mbts.ui { + class ObservantCommand : ICommand, IRaiseCanExecuteChangedCommand, IObserver { + readonly Action _execute; + bool _can_execute; + + public ObservantCommand(Action execute) => + _execute = execute; + + public event EventHandler CanExecuteChanged; + + public bool CanExecute(object parameter) => _can_execute; + public void Execute(object parameter) => _execute(ObjectHelper.try_convert(parameter)); + + public void OnCompleted() { } + public void OnError(Exception error) { } + public void OnNext(bool value) { + _can_execute = value; + _raise_can_execute_changed(); + } + + void _raise_can_execute_changed() => + CanExecuteChanged.Invoke(this, new EventArgs()); + + void IRaiseCanExecuteChangedCommand.RaiseCanExecuteChanged() => + _raise_can_execute_changed(); + } + + class ObservantCommand : ObservantCommand { + public ObservantCommand(Action execute) : base(_ => execute()) { } + public ObservantCommand(Action execute) : base(execute) { } + } +} \ No newline at end of file