Ladder - sort AlgoDefinitionVMs

This commit is contained in:
John Colvin committed 2019-01-30 17:24:26 -06:00
1 parent 5d1e2ac2f2
commit 7ef31ef3b2
3 files changed
+54 -4

No files matched your search

@@ -1,12 +1,24 @@
using bts.api.core;
using bts.utils;
using mbts.collections;
using mbts.ui.utilities;
using mbts.utils;
using System;
using System.Collections.Generic;
namespace mbts.spark {
static class AlgoDefinitionVMCollectionHelper {
public static readonly IComparer<AlgoDefinitionVM> default_comparer = Comparer<AlgoDefinitionVM>.Create((a, b) => DisplayNameHelper.compare_alpha_numeric(a?.name, b?.name));
public static readonly IComparer<AlgoDefinitionVM> default_comparer =
new ComparerBuilder<AlgoDefinitionVM>(
new [] {
Comparer<AlgoDefinitionVM>.Create(_CompareUtils.compare_modes),
Comparer<AlgoDefinitionVM>.Create(_CompareUtils.compare_names)
}).to_comparer().to_safe();
static AlgoDefinitionVMCollectionHelper() {
var ordered_def_modes = new[] { AlgoVMKey.Mode.LegacyFlex, AlgoVMKey.Mode.Persisted, AlgoVMKey.Mode.Unpersisted };
var default_comp_builder = new ComparerBuilder<AlgoDefinitionVM>();
}
public static void update_persisted_definitions(ObservableKeyedCollection<AlgoDefinitionVM, AlgoVMKey> target, TableUpdate<AlgoDefinitionData, AlgoPersistedKey> update, Func<AlgoDefinitionData, AlgoDefinitionVM> factory) {
foreach (var d in update.removed)
@@ -31,5 +43,23 @@ namespace mbts.spark {
pdef.Dispose();
}
}
static class _CompareUtils {
public static int compare_modes(AlgoDefinitionVM a, AlgoDefinitionVM b) =>
_mode_sort_index(a.vm_key.mode).CompareTo(_mode_sort_index(b.vm_key.mode));
public static int compare_names(AlgoDefinitionVM a, AlgoDefinitionVM b) =>
DisplayNameHelper.compare_alpha_numeric(a.name, b.name);
static int _mode_sort_index(AlgoVMKey.Mode mode) {
switch (mode) {
case AlgoVMKey.Mode.LegacyFlex: return 0;
case AlgoVMKey.Mode.Persisted: return 1;
case AlgoVMKey.Mode.Unpersisted: return 2;
case AlgoVMKey.Mode.None: return -1;
default: throw ExceptionBuilder.Argument.unexpected_case(mode, nameof(mode));
}
}
}
}
}
@@ -4,6 +4,7 @@ using bts.api.core;
using bts.api.orderman;
using bts.collections;
using bts.spark;
using bts.utils;
using mbts.collections;
using mbts.ui;
using mbts.ui.controls;
@@ -56,6 +57,7 @@ namespace mbts.spark {
readonly SelectorProperty<TimeInForce> _time_in_force;
readonly SelectionModel<AlgoDefinitionVM> _selected_algo_definition;
readonly ObservableKeyedCollection<AlgoDefinitionVM, AlgoVMKey> _algo_definitions = new ObservableKeyedCollection<AlgoDefinitionVM, AlgoVMKey>(d => d.vm_key);
readonly ListCollectionView<AlgoDefinitionVM> _algo_definitions_view;
readonly TableUpdateBuffer<AlgoDefinitionData, AlgoPersistedKey> _definitions_buffer = new TableUpdateBuffer<AlgoDefinitionData, AlgoPersistedKey>(d => d.key);
readonly EditableCollectionModel<AlgoDefinitionTemplateVM, AlgoDefinitionVM> _algo_template_collection_model;
readonly ObservableKeyedCollection<AlgoDefinitionTemplateVM, AlgoVMKey> _favorite_templates = new ObservableKeyedCollection<AlgoDefinitionTemplateVM, AlgoVMKey>(tvm => tvm.vm_key);
@@ -63,7 +65,9 @@ namespace mbts.spark {
public OrderPropertiesEditorVM(OrdersModel model, IObservable<SecurityMD> security_md_source, IObservable<InlineOrderProperties> order_properties) {
_model = model;
algo_definitions_view = new ListCollectionView<AlgoDefinitionVM>(_algo_definitions);
_algo_definitions_view = new ListCollectionView<AlgoDefinitionVM>(_algo_definitions) {
CustomSort = AlgoDefinitionVMCollectionHelper.default_comparer.to_non_generic()
};
algo_definitions_view.Filter = d => !(d.vm_key.mode == AlgoVMKey.Mode.Persisted && _algo_metadata.TryGetValue(d.vm_key.persisted_id, out AlgoDefinitionMetadata meta) && meta.is_hidden);
var security_source = security_md_source.Select(smd => smd.security);
_security = backing(nameof(security), security_source, SecurityDataHelper.Defaults.SecurityImmutable);
@@ -198,7 +202,7 @@ namespace mbts.spark {
// items sources
public ObservableCollection<AlgoDefinitionVM> algo_definitions => _algo_definitions;
public ObservableCollection<AlgoDefinitionTemplateVM> algo_templates { get; } = new ObservableCollection<AlgoDefinitionTemplateVM>();
public ICollectionView<AlgoDefinitionVM> algo_definitions_view { get; }
public ICollectionView<AlgoDefinitionVM> algo_definitions_view => _algo_definitions_view;
public ICollectionView algo_templates_view { get; }
public ICollectionView favorite_algo_templates_view { get; }
// selected items
+17 -1
View File
@@ -32,6 +32,9 @@ namespace bts.utils {
public static IComparer to_non_generic<T>(this IComparer<T> comparer) =>
new NonGenericComparerWrapper<T>(comparer);
public static IComparer<T> to_safe<T>(this IComparer<T> comparer) where T : class =>
new SafeComparerWrapper<T>(comparer);
public static IComparer<TOutter> wrap<TInner, TOutter>(this IComparer<TInner> inner_comparer, Func<TOutter, TInner> selector) =>
Comparer<TOutter>.Create((a, b) => inner_comparer.Compare(a == null ? default : selector(a), b == null ? default : selector(b)));
@@ -49,6 +52,19 @@ namespace bts.utils {
public int Compare(object x, object y) =>
_comparer.Compare(ConvertUtils.try_convert<T>(x), ConvertUtils.try_convert<T>(y));
}
class SafeComparerWrapper<T> : IComparer<T> where T : class{
readonly IComparer<T> _comparer;
public SafeComparerWrapper(IComparer<T> comparer) =>
_comparer = comparer;
public int Compare(T x, T y) {
if (x == null) return y == null ? 0 : -1;
if (y == null) return 1;
return _comparer.Compare(x, y);
}
}
#endregion
}
}
}