feat (rec rep): Add button to launch Ladder
This commit is contained in:
1 parent
57a4745e29
commit
851654c8a5
6 files changed
+71
-3
No files matched your search
@@ -30,6 +30,7 @@ namespace Spark.Trading {
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
|
||||
using static mbts.ui.controls.DIYGrid;
|
||||
@@ -40,9 +41,12 @@ namespace Spark.Trading {
|
||||
using RowKey = bts.AccountSecurityKey;
|
||||
using RowVM = ReconcileReportRowVM;
|
||||
|
||||
class ReconcileReportVM : DisposableViewModelBase, IHandleGridSorting {
|
||||
class ReconcileReportVM : DisposableViewModelBase, IHandleGridSorting, IHandleRowSelection {
|
||||
readonly IWindowSettingsManager<GridSettings> _settings_manager;
|
||||
readonly DIYGridRowsModel<RowData, ColKey, ReconcileReportFilterSettings> _rows_model;
|
||||
/// Don't mutate <see cref="_selected_rows" /> outside of
|
||||
/// <see cref="IHandleRowSelection.row_selection_changed(SelectionChangedEventArgs)" />
|
||||
readonly ObservableCollection<RowVM> _selected_rows = new();
|
||||
readonly ObservableKeyedCollection<IReconcileReportColumn, ColKey> _columns = new(c => c.id);
|
||||
readonly IRaiseCanExecuteChangedCommand _sync_with_exchange_cmd;
|
||||
|
||||
@@ -54,6 +58,7 @@ namespace Spark.Trading {
|
||||
IWindowSettingsManager<GridSettings> settings_manager,
|
||||
ISecurityMetadataStore security_metadata) {
|
||||
_settings_manager = settings_manager;
|
||||
selected_rows = ObservableAutoWrapper.create_read_only_wrapper(_selected_rows);
|
||||
columns = _columns.WrapSelector(c => c.column_view);
|
||||
column_models = _columns.AsReadOnlyKeyedCollection().WrapSelector(
|
||||
c => (IDIYGridColumnModel)c);
|
||||
@@ -162,6 +167,7 @@ namespace Spark.Trading {
|
||||
public IReadOnlyObservableCollection<IDIYGridColumnModel> column_models { get; }
|
||||
public IReadOnlyObservableCollection<ColumnBase> columns { get; }
|
||||
public ObservableCollection<RowVM> rows { get; } = new();
|
||||
public IReadOnlyObservableCollection<RowVM> selected_rows { get; }
|
||||
public SimpleGridSettingsManager<RowVM> grid_settings_manager { get; }
|
||||
|
||||
public ICommand sync_with_exchange_cmd => _sync_with_exchange_cmd;
|
||||
@@ -229,5 +235,11 @@ namespace Spark.Trading {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void IHandleRowSelection.row_selection_changed(SelectionChangedEventArgs e) {
|
||||
var comp = ComparerWrapper<RowVM>.wrap(r => r.row_key);
|
||||
_selected_rows.RemoveSorted(e.RemovedItems.OfType<RowVM>(), comp);
|
||||
_selected_rows.InsertSorted(e.AddedItems.OfType<RowVM>(), comp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ namespace Spark.Trading {
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
@@ -108,6 +109,10 @@ namespace Spark.Trading {
|
||||
_security_metadata);
|
||||
_vm.load_settings(_settings);
|
||||
|
||||
var has_selected_rows = _vm.selected_rows.ObserveHasItems();
|
||||
var show_ladder_cmd = SparkCommands.show_ladder_lazy(
|
||||
_bus, has_selected_rows, get_selected_sec_ids, SparkCommands.KeyGestures.ShowLadder);
|
||||
|
||||
var settings_cmd = SparkCommands.window_settings_command(new DialogCommand(() => {
|
||||
var vm = new ShellSettingsDialogVM<GridSettings, SimpleGridSettingsVM>(
|
||||
_settings_manager,
|
||||
@@ -125,6 +130,7 @@ namespace Spark.Trading {
|
||||
}));
|
||||
|
||||
_commands.AddRange(
|
||||
show_ladder_cmd,
|
||||
new SparkCommandCommand(
|
||||
"Sync with Exchange Positions", Icons.Refresh, _vm.sync_with_exchange_cmd),
|
||||
settings_cmd,
|
||||
@@ -133,6 +139,9 @@ namespace Spark.Trading {
|
||||
_view = new ReconcileReportView {
|
||||
DataContext = _vm
|
||||
};
|
||||
|
||||
IEnumerable<uint> get_selected_sec_ids() =>
|
||||
_vm.selected_rows.Select(r => r.security?.Id).SelectWhereValues();
|
||||
}
|
||||
|
||||
protected override void OnParentViewCreated(SparkWindowView parent_view) =>
|
||||
|
||||
@@ -64,5 +64,9 @@ namespace System.Reactive.Linq {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static IObservable<TResult> SelectInline<T, TResult>(
|
||||
this IObservable<T> source, Func<T, TResult> selector) =>
|
||||
Observable.Create<TResult>(o => source.Subscribe(v => o.OnNext(selector(v))));
|
||||
}
|
||||
}
|
||||
@@ -19,8 +19,6 @@ namespace bts.spark {
|
||||
using static mbts.spark.SparkTags;
|
||||
|
||||
public abstract class SparkCommand {
|
||||
public readonly KeyGesture key_gesture;
|
||||
|
||||
readonly IObservableValue<ImageSource> _icon =
|
||||
ObservableValue.create<ImageSource>(multi_threaded: true);
|
||||
|
||||
@@ -44,6 +42,8 @@ namespace bts.spark {
|
||||
remove => _icon.value_changed -= value;
|
||||
}
|
||||
|
||||
public KeyGesture key_gesture { get; init; }
|
||||
|
||||
public string group {
|
||||
get => _group;
|
||||
set {
|
||||
|
||||
@@ -14,6 +14,7 @@ using Spark.Trading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Reactive.Linq;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace bts.spark {
|
||||
@@ -54,6 +55,28 @@ namespace bts.spark {
|
||||
public static PartialCommand<SecurityMD> ShowLadder(KeyGesture key_gesture = null) =>
|
||||
SparkCommand.create("Ladder", Icons.Ladder, SparkTags.ShowLadderTag, key_gesture);
|
||||
|
||||
public static ReactiveSparkCommand show_ladder_lazy(
|
||||
IBus bus,
|
||||
IObservable<bool> can_exe,
|
||||
Func<IEnumerable<uint>> get_selected_sec_ids,
|
||||
KeyGesture key_gesture = null,
|
||||
string group = null) {
|
||||
return new(Observable.Return("Ladder"), Icons.Ladder, exe, can_exe) {
|
||||
key_gesture = key_gesture,
|
||||
group = group
|
||||
};
|
||||
|
||||
void exe() {
|
||||
var sec_ids = get_selected_sec_ids();
|
||||
var sec_ids_seti = sec_ids as IImmutableSet<uint> ?? sec_ids?.ToImmutableHashSet();
|
||||
if (sec_ids_seti is null || sec_ids_seti.Count == 0) {
|
||||
Log.Ui.warn("Skipping request to 'ShowLadder' (no securities specified).");
|
||||
return;
|
||||
}
|
||||
bus.post(SparkTags.ShowLadderAutoLookupTag, sec_ids_seti);
|
||||
}
|
||||
}
|
||||
|
||||
public static PartialCommand<IReadOnlyList<SecurityMD>> ShowLadders(KeyGesture key_gesture = null) =>
|
||||
SparkCommand.create("Ladder", Icons.Ladder, SparkTags.ShowLaddersTag, key_gesture);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ using bts.collection;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core;
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
using System;
|
||||
@@ -210,6 +211,25 @@ namespace System.Collections.Specialized {
|
||||
return Observable.FromEvent<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(cb => (s, e) => cb(e), h => source.CollectionChanged += h, h => source.CollectionChanged -= h);
|
||||
}
|
||||
|
||||
public static IObservable<bool> ObserveHasItems(this INotifyCollectionChanged source) {
|
||||
var source_collection = ExceptionAssert.of_type<IEnumerable>(source);
|
||||
return Observable.Create<bool>(o => {
|
||||
bool last_out = get_has_items();
|
||||
o.OnNext(last_out);
|
||||
source.CollectionChanged += on_change;
|
||||
return Disposable.Create(() => source.CollectionChanged -= on_change);
|
||||
|
||||
void on_change(object sender, NotifyCollectionChangedEventArgs e) {
|
||||
if (last_out.toggle_to(get_has_items())) {
|
||||
o.OnNext(last_out);
|
||||
}
|
||||
}
|
||||
|
||||
bool get_has_items() =>
|
||||
source_collection.GetEnumerator().MoveNext();
|
||||
});
|
||||
}
|
||||
|
||||
public static IObservable<ImmutableList<T>> observe_immutable<T>(this ObservableCollection<T> source) {
|
||||
// TODO: unit tests
|
||||
return Observable.Create<ImmutableList<T>>(o => {
|
||||
|
||||
Reference in new issue
Block a user