move 'RXOperableCollection' into spark_core, rename 'OperableCollection'
This commit is contained in:
1 parent
9e347a858c
commit
e84ea8b8e5
3 files changed
+78
-72
No files matched your search
@@ -1,6 +1,7 @@
|
||||
using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.api.orderman;
|
||||
using bts.collections;
|
||||
using bts.spark;
|
||||
using bts.utils;
|
||||
using mbts.collections;
|
||||
@@ -14,7 +15,6 @@ using mbts.ui.shell;
|
||||
using mbts.ui.utility;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
@@ -34,7 +34,7 @@ using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase;
|
||||
namespace mbts.spark {
|
||||
class FillsVM : DisposableViewModelBase, IHandleGridSorting {
|
||||
readonly OrdersModel _model;
|
||||
readonly RXOperableCollection<Row, FillTuple, string> _rows;
|
||||
readonly OperableCollection<Row, FillTuple, string> _rows;
|
||||
readonly DIYGridSortManager<Columns> _sort_manager;
|
||||
readonly IDIYGridFiltersManager<FillTuple, Columns, FilterStates> _filters_manager;
|
||||
readonly ObservableKeyedCollection<ColumnBase, Columns> _columns = new ObservableKeyedCollection<ColumnBase, Columns>(_col_key);
|
||||
@@ -87,7 +87,7 @@ namespace mbts.spark {
|
||||
add_col(Columns.OrderKey, DefaultColumnHeaders.OrderKey, r => r.order_key.ToString(), new RegexFilterVM<FillTuple>(r => r.fill.OrderKey.ToString()), DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.OrderKey);
|
||||
add_col(Columns.SecurityKey, DefaultColumnHeaders.SecurityKey, r => r.security_key.ToString(), new RegexFilterVM<FillTuple>(r => r.fill.SecurityKey.ToString()), DataGridFilterKnownTemplates.Regex, DefaultColumnWidths.SecurityKey);
|
||||
/// Rows
|
||||
_rows = new RXOperableCollection<Row, FillTuple, string>(
|
||||
_rows = new OperableCollection<Row, FillTuple, string>(
|
||||
(ft, fk) => new Row(ft, model),
|
||||
ft => ft.fill.FillKey,
|
||||
new ComparerBuilder<FillTuple>(new[] {
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using bts.api.core;
|
||||
using bts.collections;
|
||||
using bts.utils;
|
||||
using bts.collections;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections;
|
||||
@@ -125,69 +123,4 @@ namespace mbts.ui.utility {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RXOperableCollection<T, TModel, TKey> : ObservableCollection<T> where TKey : IEquatable<TKey> {
|
||||
readonly Func<TModel, TKey, T> _factory;
|
||||
readonly Func<TModel, TKey> _key;
|
||||
readonly IEqualityComparer<TModel> _eq_comparer;
|
||||
IReadOnlyDictionary<TKey, TModel> _map = DictionaryHelper.empty<TKey, TModel>();
|
||||
|
||||
SortedList<TModel> _sorted_items;
|
||||
Func<TModel, bool> _filter;
|
||||
IComparer<TModel> _comparer;
|
||||
|
||||
public RXOperableCollection(Func<TModel, TKey, T> factory, Func<TModel, TKey> key, IComparer<TModel> comparer) {
|
||||
_factory = factory;
|
||||
_key = key;
|
||||
_eq_comparer = EqualityComparer.Create(key);
|
||||
_comparer = comparer;
|
||||
_sorted_items = new SortedList<TModel>(comparer ?? throw new ArgumentNullException(nameof(comparer)));
|
||||
}
|
||||
|
||||
public IComparer<TModel> comparer { get => _comparer; set => _set_comparer(value); }
|
||||
public Func<TModel, bool> filter { get => _filter; set => _set_filter(value); }
|
||||
|
||||
public void reset(IComparer<TModel> comparer, Func<TModel, bool> filter) {
|
||||
_filter = filter;
|
||||
_comparer = comparer;
|
||||
_reset(new SortedList<TModel>(filter == null ? _map.Values : _map.Values.Where(filter), comparer));
|
||||
}
|
||||
|
||||
public void update(TableUpdate<TModel, TKey> table) {
|
||||
_map = table.state;
|
||||
IEnumerable<TModel> added, removed;
|
||||
if (filter == null) {
|
||||
added = table.added.Concat(table.changed);
|
||||
removed = table.removed.Concat(table.changes.Select(ch => ch.Old));
|
||||
} else {
|
||||
added = table.added.Where(filter).Concat(table.changed.Where(filter));
|
||||
removed = table.removed.Where(filter).Concat(table.changes.Select(ch => ch.Old).Where(filter));
|
||||
}
|
||||
foreach (var v in removed) {
|
||||
_sorted_items.Remove(v, out int? old_pos);
|
||||
// don't bother checking 'old_pos.HasValue' - removes should always be successful (if not, an exception should be thrown anyways bc the situation is untenable).
|
||||
RemoveAt(old_pos.Value);
|
||||
}
|
||||
foreach (var v in added) {
|
||||
_sorted_items.Add(v, out int new_pos);
|
||||
Insert(new_pos, _factory(v, _key(v)));
|
||||
}
|
||||
}
|
||||
|
||||
void _set_comparer(IComparer<TModel> new_comp) {
|
||||
_comparer = new_comp ?? throw new ArgumentNullException(nameof(new_comp));
|
||||
_reset(new SortedList<TModel>(_sorted_items, new_comp));
|
||||
}
|
||||
|
||||
void _set_filter(Func<TModel, bool> filter) {
|
||||
_filter = filter;
|
||||
_reset(new SortedList<TModel>(filter == null ? _map.Values : _map.Values.Where(filter), comparer));
|
||||
}
|
||||
|
||||
void _reset(SortedList<TModel> new_sorted_items) {
|
||||
var changes = Diff.diff(_sorted_items, new_sorted_items, _eq_comparer).ToList();
|
||||
_sorted_items = new_sorted_items;
|
||||
Diff.apply_diff(this, _sorted_items, changes, m => _factory(m, _key(m)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using bts.api.core;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace bts.collections {
|
||||
class OperableCollection<T, TModel, TKey> : ObservableCollection<T> where TKey : IEquatable<TKey> {
|
||||
readonly Func<TModel, TKey, T> _factory;
|
||||
readonly Func<TModel, TKey> _key;
|
||||
readonly IEqualityComparer<TModel> _eq_comparer;
|
||||
IReadOnlyDictionary<TKey, TModel> _map = DictionaryHelper.empty<TKey, TModel>();
|
||||
|
||||
SortedList<TModel> _sorted_items;
|
||||
Func<TModel, bool> _filter;
|
||||
IComparer<TModel> _comparer;
|
||||
|
||||
public OperableCollection(Func<TModel, TKey, T> factory, Func<TModel, TKey> key, IComparer<TModel> comparer) {
|
||||
_factory = factory;
|
||||
_key = key;
|
||||
_eq_comparer = EqualityComparer.Create(key);
|
||||
_comparer = comparer;
|
||||
_sorted_items = new SortedList<TModel>(comparer ?? throw new ArgumentNullException(nameof(comparer)));
|
||||
}
|
||||
|
||||
public IComparer<TModel> comparer { get => _comparer; set => _set_comparer(value); }
|
||||
public Func<TModel, bool> filter { get => _filter; set => _set_filter(value); }
|
||||
|
||||
public void reset(IComparer<TModel> comparer, Func<TModel, bool> filter) {
|
||||
_filter = filter;
|
||||
_comparer = comparer;
|
||||
_reset(new SortedList<TModel>(filter == null ? _map.Values : _map.Values.Where(filter), comparer));
|
||||
}
|
||||
|
||||
public void update(TableUpdate<TModel, TKey> table) {
|
||||
_map = table.state;
|
||||
IEnumerable<TModel> added, removed;
|
||||
if (filter == null) {
|
||||
added = table.added.Concat(table.changed);
|
||||
removed = table.removed.Concat(table.changes.Select(ch => ch.Old));
|
||||
} else {
|
||||
added = table.added.Where(filter).Concat(table.changed.Where(filter));
|
||||
removed = table.removed.Where(filter).Concat(table.changes.Select(ch => ch.Old).Where(filter));
|
||||
}
|
||||
foreach (var v in removed) {
|
||||
_sorted_items.Remove(v, out int? old_pos);
|
||||
// don't bother checking 'old_pos.HasValue' - removes should always be successful (if not, an exception should be thrown anyways bc the situation is untenable).
|
||||
RemoveAt(old_pos.Value);
|
||||
}
|
||||
foreach (var v in added) {
|
||||
_sorted_items.Add(v, out int new_pos);
|
||||
Insert(new_pos, _factory(v, _key(v)));
|
||||
}
|
||||
}
|
||||
|
||||
void _set_comparer(IComparer<TModel> new_comp) {
|
||||
_comparer = new_comp ?? throw new ArgumentNullException(nameof(new_comp));
|
||||
_reset(new SortedList<TModel>(_sorted_items, new_comp));
|
||||
}
|
||||
|
||||
void _set_filter(Func<TModel, bool> filter) {
|
||||
_filter = filter;
|
||||
_reset(new SortedList<TModel>(filter == null ? _map.Values : _map.Values.Where(filter), comparer));
|
||||
}
|
||||
|
||||
void _reset(SortedList<TModel> new_sorted_items) {
|
||||
var changes = Diff.diff(_sorted_items, new_sorted_items, _eq_comparer).ToList();
|
||||
_sorted_items = new_sorted_items;
|
||||
Diff.apply_diff(this, _sorted_items, changes, m => _factory(m, _key(m)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user