SPARK-1714 Ladder account - unable to pick a selection on server restart
This commit is contained in:
1 parent
d29ec26beb
commit
77854a3cf0
5 files changed
+93
-57
No files matched your search
@@ -94,7 +94,7 @@ namespace mbts.spark {
|
||||
class OrderPropertiesEditorVM : DisposableViewModelBase {
|
||||
readonly OrdersModel _model;
|
||||
readonly Property<SecurityImmutable> _security;
|
||||
readonly SelectorProperty<AccountTuple> _accounts;
|
||||
readonly SelectorProperty<AccountTuple, AccountTuple?> _accounts;
|
||||
readonly SelectorProperty<TimeInForce> _time_in_force;
|
||||
readonly SelectorProperty<IOrderTypeVM> _order_type;
|
||||
IDisposable _order_type_param_errors_sub;
|
||||
@@ -113,14 +113,21 @@ namespace mbts.spark {
|
||||
nameof(selected_account),
|
||||
model.factory.subscribe_account_tuples(),
|
||||
order_properties.Select(ops => ops.account),
|
||||
() => accounts?.FirstOrDefault() ?? default,
|
||||
val => (!string.IsNullOrWhiteSpace(val.name) && accounts.Contains(val), "Please select an account"));
|
||||
a => new AccountTuple?(a),
|
||||
() => {
|
||||
foreach (var a in accounts) {
|
||||
return a;
|
||||
}
|
||||
return new AccountTuple?();
|
||||
},
|
||||
EqualityComparer<AccountTuple?>.Default,
|
||||
new[] { new PropertyValidation<AccountTuple?>(a => (a.HasValue && !string.IsNullOrWhiteSpace(a.Value.name) && accounts.Contains(a.Value), "Please select an account")) }.ToReadOnlyList());
|
||||
_order_type = backing(
|
||||
nameof(selected_order_type),
|
||||
security_source.Select(s => model.factory.default_order_type_templates(s).Select(kv => create_order_type_vm(kv.Value, kv.Key.key.name, kv.Key.display_name))),
|
||||
order_properties.Select(get_order_type_vm_prop),
|
||||
get_default_order_type_vm,
|
||||
(a, b) => a.key() == b.key());
|
||||
EqualityComparer.null_check_wrapper(OrderTypeVMExtensions.key_comparer));
|
||||
_order_type.Changed += on_selected_order_type_changed;
|
||||
_time_in_force = backing(
|
||||
nameof(selected_time_in_force),
|
||||
@@ -172,13 +179,13 @@ namespace mbts.spark {
|
||||
|
||||
public SecurityImmutable security => _security.Value;
|
||||
public ReadOnlyObservableCollection<AccountTuple> accounts => _accounts.items;
|
||||
public AccountTuple selected_account { get => _accounts.selected_item; set => _accounts.selected_item = value; }
|
||||
public AccountTuple? selected_account { get => _accounts.selected_item; set => _accounts.selected_item = value; }
|
||||
public ReadOnlyObservableCollection<TimeInForce> time_in_forces => _time_in_force.items;
|
||||
public TimeInForce selected_time_in_force { get => _time_in_force.selected_item; set => _time_in_force.selected_item = value; }
|
||||
public ReadOnlyObservableCollection<IOrderTypeVM> order_types => _order_type.items;
|
||||
public IOrderTypeVM selected_order_type { get => _order_type.selected_item; set => _order_type.selected_item = value; }
|
||||
|
||||
public IObservable<AccountTuple> observe_account(bool start_immediately = false) =>
|
||||
public IObservable<AccountTuple?> observe_account(bool start_immediately = false) =>
|
||||
_accounts.observe_current(start_immediately);
|
||||
|
||||
public IObservable<TimeInForce> observe_time_in_force(bool start_immediately = false) =>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using bts.api.orderman;
|
||||
using bts.spark;
|
||||
using bts.utils;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Input;
|
||||
using static bts.api.orderman.CustomOrderDefinition;
|
||||
@@ -24,6 +25,9 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
static class OrderTypeVMExtensions {
|
||||
public static IEqualityComparer<IOrderTypeVM> key_comparer =>
|
||||
EqualityComparer.Create<IOrderTypeVM, OrderTemplateKey>(a => a.key());
|
||||
|
||||
public static OrderTemplateKey key(this IOrderTypeVM vm) =>
|
||||
new OrderTemplateKey(vm.order_type, vm.id);
|
||||
}
|
||||
|
||||
@@ -529,7 +529,7 @@ namespace mbts.ui.shell
|
||||
/// <para/>If the user has not selected an item, but the current items do not contain the desired item, it will re-evaluate whether or not to select the item when new items arrive.
|
||||
/// </summary>
|
||||
protected SelectorProperty<T> backing<T>(string name, IObservable<IEnumerable<T>> items, IObservable<InlineUserProperty<T>> desired_item, Func<T> select_default, IReadOnlyList<PropertyValidation<T>> validation_steps) =>
|
||||
backing(name, items, desired_item, select_default, null, validation_steps);
|
||||
backing(name, items, desired_item, select_default, EqualityComparer<T>.Default, validation_steps);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a SelectorProperty<T> that observes an items source, and a desired item. When either observable ticks, it determines whether or not to assign the selected_item property.
|
||||
@@ -541,8 +541,8 @@ namespace mbts.ui.shell
|
||||
/// <param name="desired_item">The observable sequence of a desired item.</param>
|
||||
/// <param name="select_default">The fallback function to select an item by default.</param>
|
||||
/// <param name="items_match">The item-matching function used to preserve a selected item when new items arrive.</param>
|
||||
protected SelectorProperty<T> backing<T>(string name, IObservable<IEnumerable<T>> items, IObservable<InlineUserProperty<T>> desired_item, Func<T> select_default, Func<T, T, bool> items_match) =>
|
||||
backing(name, items, desired_item, select_default, items_match, new List<PropertyValidation<T>>(0));
|
||||
protected SelectorProperty<T> backing<T>(string name, IObservable<IEnumerable<T>> items, IObservable<InlineUserProperty<T>> desired_item, Func<T> select_default, IEqualityComparer<T> comparer) =>
|
||||
backing(name, items, desired_item, select_default, comparer, new List<PropertyValidation<T>>(0));
|
||||
|
||||
/// <summary>
|
||||
/// Returns a SelectorProperty<T> that observes an items source, and a desired item. When either observable ticks, it determines whether or not to assign the selected_item property.
|
||||
@@ -555,14 +555,24 @@ namespace mbts.ui.shell
|
||||
/// <param name="select_default">The fallback function to select an item by default.</param>
|
||||
/// <param name="items_match">The item-matching function used to preserve a selected item when new items arrive.</param>
|
||||
/// <param name="validation_steps">The validation steps to take when selected_item changes.</param>
|
||||
protected SelectorProperty<T> backing<T>(string name, IObservable<IEnumerable<T>> items, IObservable<InlineUserProperty<T>> desired_item, Func<T> select_default, Func<T, T, bool> items_match, IReadOnlyList<PropertyValidation<T>> validation_steps) {
|
||||
if (_observable_properties == null) _observable_properties = new List<IDisposable>();
|
||||
var prop = new SelectorProperty<T>(this, name, select_default, items_match, validation_steps);
|
||||
_observable_properties.Add(items.OnDispatcher().Subscribe(prop));
|
||||
_observable_properties.Add(desired_item.OnDispatcher().Subscribe(prop));
|
||||
protected SelectorProperty<T> backing<T>(string name, IObservable<IEnumerable<T>> items, IObservable<InlineUserProperty<T>> desired_item, Func<T> select_default, IEqualityComparer<T> comparer, IReadOnlyList<PropertyValidation<T>> validation_steps) {
|
||||
var prop = new SelectorProperty<T>(this, name, select_default, comparer, validation_steps);
|
||||
attach(prop, items, desired_item);
|
||||
return prop;
|
||||
}
|
||||
|
||||
protected SelectorProperty<T, V> backing<T, V>(string name, IObservable<IEnumerable<T>> items, IObservable<InlineUserProperty<T>> desired_item, Func<T, V> item_to_value, Func<V> select_default, IEqualityComparer<V> comparer, IReadOnlyList<PropertyValidation<V>> validation_steps) {
|
||||
var prop = new SelectorProperty<T, V>(this, name, item_to_value, select_default, comparer, validation_steps);
|
||||
attach(prop, items, desired_item);
|
||||
return prop;
|
||||
}
|
||||
|
||||
protected void attach<T, V>(SelectorProperty<T, V> selector_property, IObservable<IEnumerable<T>> items, IObservable<InlineUserProperty<T>> desired_item) {
|
||||
if (_observable_properties == null) _observable_properties = new List<IDisposable>();
|
||||
_observable_properties.Add(items.OnDispatcher().Subscribe(selector_property));
|
||||
_observable_properties.Add(desired_item.OnDispatcher().Subscribe(selector_property));
|
||||
}
|
||||
|
||||
protected void set_dispose(IDisposable disposable) {
|
||||
if (_dispose != null) throw new InvalidOperationException("Cannot set disposable twice");
|
||||
_dispose = disposable;
|
||||
@@ -579,37 +589,44 @@ namespace mbts.ui.shell
|
||||
OnDisposed?.Invoke(true);
|
||||
}
|
||||
|
||||
protected class SelectorProperty<T> : SelectorProperty<T, T> {
|
||||
public SelectorProperty(DisposableViewModelBase owner, string prop_name, Func<T> select_default, IEqualityComparer<T> comparer, IReadOnlyList<PropertyValidation<T>> validation_steps)
|
||||
: base(owner, prop_name, i => i, select_default, comparer, validation_steps) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Designed to observe an items source, and a [settings-driven] desired item. When a new desired item is observed, it will evaluate it's current state and determine if it should be assigned to the selected_item.
|
||||
/// <para/>Notes: If the user has selected an item (ie the selected_item has been assigned through the public setter), the desired item will be ignored unless force_override is true.
|
||||
/// <para/>If the user has not selected an item, but the current items do not contain the desired item, it will re-evaluate whether or not to select the item when new items arrive.
|
||||
/// </summary>
|
||||
protected class SelectorProperty<T> : IObserver<InlineUserProperty<T>>, IObserver<IEnumerable<T>>, IObservable<(T oldval, T newval)> {
|
||||
protected class SelectorProperty<TItem, TValue> : IObserver<InlineUserProperty<TItem>>, IObserver<IEnumerable<TItem>>, IObservable<(TValue oldval, TValue newval)> {
|
||||
readonly DisposableViewModelBase _owner;
|
||||
readonly string _prop_name;
|
||||
readonly IReadOnlyList<PropertyValidation<T>> _validation_steps;
|
||||
readonly ObservableCollection<T> _items = new ObservableCollection<T>();
|
||||
readonly Func<T> _select_default;
|
||||
readonly Func<T, T, bool> _items_match; // used to maintain selected_item through an items-change
|
||||
T _selected_item;
|
||||
InlineUserProperty<T> _desired_item;
|
||||
readonly IReadOnlyList<PropertyValidation<TValue>> _validation_steps;
|
||||
readonly ObservableCollection<TItem> _items = new ObservableCollection<TItem>();
|
||||
readonly Func<TValue> _select_default;
|
||||
readonly IEqualityComparer<TValue> _value_comparer;
|
||||
readonly Func<TItem, TValue> _item_to_value;
|
||||
TValue _selected_item;
|
||||
InlineUserProperty<TItem> _desired_item = new InlineUserProperty<TItem>();
|
||||
bool _is_user_selected_item;
|
||||
|
||||
public SelectorProperty(DisposableViewModelBase owner, string prop_name, Func<T> select_default, Func<T, T, bool> items_match, IReadOnlyList<PropertyValidation<T>> validation_steps) {
|
||||
public SelectorProperty(DisposableViewModelBase owner, string prop_name, Func<TItem, TValue> item_to_value, Func<TValue> select_default, IEqualityComparer<TValue> value_comparer, IReadOnlyList<PropertyValidation<TValue>> validation_steps) {
|
||||
_owner = owner;
|
||||
_prop_name = prop_name;
|
||||
_item_to_value = item_to_value;
|
||||
_select_default = select_default;
|
||||
_items_match = items_match;
|
||||
_value_comparer = value_comparer ?? EqualityComparer<TValue>.Default;
|
||||
_validation_steps = validation_steps;
|
||||
items = new ReadOnlyObservableCollection<T>(_items);
|
||||
items = new ReadOnlyObservableCollection<TItem>(_items);
|
||||
}
|
||||
|
||||
public event PropertyChange<T> Changing;
|
||||
public event PropertyChange<T> Changed;
|
||||
public event PropertyChange<TValue> Changing;
|
||||
public event PropertyChange<TValue> Changed;
|
||||
|
||||
public ReadOnlyObservableCollection<T> items { get; }
|
||||
public ReadOnlyObservableCollection<TItem> items { get; }
|
||||
|
||||
public T selected_item {
|
||||
public TValue selected_item {
|
||||
get => _selected_item;
|
||||
set {
|
||||
_is_user_selected_item = true;
|
||||
@@ -617,31 +634,31 @@ namespace mbts.ui.shell
|
||||
}
|
||||
}
|
||||
|
||||
public IDisposable Subscribe(IObserver<(T oldval, T newval)> observer) {
|
||||
public IDisposable Subscribe(IObserver<(TValue oldval, TValue newval)> observer) {
|
||||
Changed += pch;
|
||||
return Disposable.Create(() => Changed -= pch);
|
||||
void pch(T o, T n) => observer.OnNext((o, n));
|
||||
void pch(TValue o, TValue n) => observer.OnNext((o, n));
|
||||
}
|
||||
|
||||
public IObservable<T> observe_current(bool start_now = false) {
|
||||
public IObservable<TValue> observe_current(bool start_now = false) {
|
||||
var source = this.Select(vals => vals.newval);
|
||||
if (start_now)
|
||||
source = source.StartWith(_selected_item);
|
||||
source = source.StartWith(selected_item);
|
||||
return source;
|
||||
}
|
||||
|
||||
void IObserver<InlineUserProperty<T>>.OnNext(InlineUserProperty<T> iup) {
|
||||
void IObserver<InlineUserProperty<TItem>>.OnNext(InlineUserProperty<TItem> iup) {
|
||||
_desired_item = iup;
|
||||
_try_auto_select();
|
||||
}
|
||||
void IObserver<InlineUserProperty<T>>.OnError(Exception error) { }
|
||||
void IObserver<InlineUserProperty<T>>.OnCompleted() { }
|
||||
void IObserver<InlineUserProperty<TItem>>.OnError(Exception error) { }
|
||||
void IObserver<InlineUserProperty<TItem>>.OnCompleted() { }
|
||||
|
||||
void IObserver<IEnumerable<T>>.OnNext(IEnumerable<T> value) {
|
||||
if (_items_match != null && _is_user_selected_item) {
|
||||
void IObserver<IEnumerable<TItem>>.OnNext(IEnumerable<TItem> value) {
|
||||
if (_is_user_selected_item) {
|
||||
var curr_selected_item = selected_item;
|
||||
_items.sync_with(value);
|
||||
if (_try_match_item(curr_selected_item, out T match))
|
||||
if (_try_match_item(curr_selected_item, out TValue match))
|
||||
selected_item = match;
|
||||
else {
|
||||
_is_user_selected_item = false;
|
||||
@@ -649,18 +666,13 @@ namespace mbts.ui.shell
|
||||
}
|
||||
} else {
|
||||
_items.sync_with(value);
|
||||
if (_is_user_selected_item) {
|
||||
_is_user_selected_item = _items.Contains(_selected_item);
|
||||
}
|
||||
if (!_is_user_selected_item) {
|
||||
_try_auto_select();
|
||||
}
|
||||
_try_auto_select();
|
||||
}
|
||||
}
|
||||
void IObserver<IEnumerable<T>>.OnError(Exception error) { }
|
||||
void IObserver<IEnumerable<T>>.OnCompleted() { }
|
||||
void IObserver<IEnumerable<TItem>>.OnError(Exception error) { }
|
||||
void IObserver<IEnumerable<TItem>>.OnCompleted() { }
|
||||
|
||||
void _run_validation(T val) {
|
||||
void _run_validation(TValue val) {
|
||||
var invalid_steps = _validation_steps.Select(v => v.validate(val)).Where(r => !r.valid);
|
||||
if (invalid_steps.Count() > 0) {
|
||||
var error_msg = string.Join("\n", invalid_steps.Where(r => !string.IsNullOrEmpty(r.msg)).Select(r => r.msg));
|
||||
@@ -670,15 +682,15 @@ namespace mbts.ui.shell
|
||||
|
||||
void _try_auto_select() {
|
||||
if (_is_user_selected_item) {
|
||||
if (desired_item_valid() && _desired_item.force_override && _try_match_item(_desired_item.value, out T match)) {
|
||||
if (desired_item_valid() && _desired_item.force_override && _try_match_item(_desired_item.value, out TValue match)) {
|
||||
_set_selected_item(match);
|
||||
return;
|
||||
}
|
||||
} else if (desired_item_valid() && _try_match_item(_desired_item.value, out T match)) {
|
||||
} else if (desired_item_valid() && _try_match_item(_desired_item.value, out TValue match)) {
|
||||
_set_selected_item(match);
|
||||
return;
|
||||
}
|
||||
if (_try_match_item(_select_default(), out T default_match))
|
||||
if (_try_match_item(_select_default(), out TValue default_match))
|
||||
_set_selected_item(default_match);
|
||||
else {
|
||||
// the _set_selected_item has never been entered, so validation has never ran, force it here
|
||||
@@ -688,8 +700,8 @@ namespace mbts.ui.shell
|
||||
bool desired_item_valid() => (_desired_item?.has_value).GetValueOrDefault();
|
||||
}
|
||||
|
||||
void _set_selected_item(T value) {
|
||||
if (!Equals(value, _selected_item)) {
|
||||
void _set_selected_item(TValue value) {
|
||||
if (!_value_comparer.Equals(value, _selected_item)) {
|
||||
Changing?.Invoke(_selected_item, value);
|
||||
var old = _selected_item;
|
||||
_selected_item = value;
|
||||
@@ -699,10 +711,20 @@ namespace mbts.ui.shell
|
||||
}
|
||||
}
|
||||
|
||||
bool _try_match_item(T item, out T match) =>
|
||||
_items_match != null ?
|
||||
_items.TryGetFirst(i => _items_match(i, item), out match) :
|
||||
_items.TryGetFirst(i => i.Equals(item), out match);
|
||||
bool _try_match_item(TValue value, out TValue match) {
|
||||
foreach (TItem item in _items) {
|
||||
TValue v = _item_to_value(item);
|
||||
if (_value_comparer.Equals(v, value)) {
|
||||
match = v;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
match = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool _try_match_item(TItem item, out TValue match) =>
|
||||
_try_match_item(_item_to_value(item), out match);
|
||||
}
|
||||
|
||||
public event Action<bool> OnDisposed;
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace mbts.spark {
|
||||
_dispose = Disposable.Create(
|
||||
settings_command,
|
||||
order_properties_vm,
|
||||
order_properties_vm.observe_account(true).Select(a => a.name).Subscribe(account_positions),
|
||||
order_properties_vm.observe_account(true).Select(a => a?.name ?? string.Empty).Subscribe(account_positions),
|
||||
account_positions,
|
||||
size_calculator = new CalculatorLogic(0, _settings_manager.subscribe_current().Select(s => s.calculator_buttons)),
|
||||
bus.subscribe(SparkTags.LogActivity).OnDispatcher().Subscribe(status_update),
|
||||
|
||||
@@ -27,6 +27,9 @@ namespace bts.utils {
|
||||
public static IEqualityComparer<T?> CreateNullable<T>(Func<T, T, bool> equals) where T : struct =>
|
||||
new AnonymousEqualityComparer<T?>(nullable_equals);
|
||||
|
||||
public static IEqualityComparer<T> null_check_wrapper<T>(IEqualityComparer<T> comparer) where T : class =>
|
||||
new AnonymousEqualityComparer<T>((a, b) => NullCheckEquals(a, b, comparer.Equals), comparer.GetHashCode);
|
||||
|
||||
public static bool safe_sequence_equals<T>(IEnumerable<T> source, IEnumerable<T> other) =>
|
||||
NullCheckEquals(source, other, (a, b) => a.SequenceEqual(b));
|
||||
|
||||
|
||||
Reference in new issue
Block a user