467 lines
22 KiB
C#
467 lines
22 KiB
C#
using bts.collections;
|
|
using bts.utils;
|
|
using mbts;
|
|
using mbts.ui;
|
|
using mbts.ui.shell;
|
|
using mbts.ui.utility;
|
|
using mbts.utils;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Collections.ObjectModel;
|
|
using System.Collections.Specialized;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reactive.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows.Data;
|
|
using CellAnimation = mbts.ui.spark.CellAnimation;
|
|
using ColumnKeys = bts.shell.pnl.PNLView.Columns;
|
|
|
|
namespace bts.shell.pnl {
|
|
class PNLRowCollectionView : ListCollectionView {
|
|
ReadOnlyObservableCollection<object> _base_groups, _groups_wrapper;
|
|
ObservableAutoWrapper<object, object> _groups;
|
|
// appearance/triggers
|
|
ImmutableDictionary<ColumnKeys, PNLCellAppearance> _defaults_appearances;
|
|
IReadOnlyList<IPNLAnimationTrigger<PNLNormalizedData>> _triggers;
|
|
|
|
public PNLRowCollectionView(IList list, ImmutableDictionary<ColumnKeys, PNLCellAppearance> default_appearances, IReadOnlyList<IPNLAnimationTrigger<PNLNormalizedData>> triggers) : base(list) {
|
|
_defaults_appearances = default_appearances;
|
|
_triggers = triggers;
|
|
}
|
|
|
|
public override bool CanFilter => true;
|
|
public override bool CanGroup => true;
|
|
|
|
// Must return null if there are no GroupDescriptions (i.e., instead of an empty collection).
|
|
// If an empty collection is returned, the view assumes grouping is active and there are no items.
|
|
public override ReadOnlyObservableCollection<object> Groups {
|
|
get {
|
|
if ((GroupDescriptions?.Count ?? 0) == 0)
|
|
return null;
|
|
if (_groups_wrapper == null) {
|
|
_base_groups = base.Groups;
|
|
if (_base_groups == null)
|
|
return null;
|
|
_groups = new ObservableAutoWrapper<object, object>(_base_groups, obj => new PNLRowCollectionViewGroup((CollectionViewGroup)obj, this, _defaults_appearances, _triggers));
|
|
_groups_wrapper = new ReadOnlyObservableCollection<object>(_groups);
|
|
}
|
|
return _groups_wrapper;
|
|
}
|
|
}
|
|
|
|
public void on_settings(ImmutableDictionary<ColumnKeys, PNLCellAppearance> default_appearances, IReadOnlyList<IPNLAnimationTrigger<PNLNormalizedData>> triggers) {
|
|
_defaults_appearances = default_appearances;
|
|
_triggers = triggers;
|
|
foreach (var g in _groups_wrapper.OfTypeSafe<PNLRowCollectionViewGroup>())
|
|
g.on_settings(default_appearances, triggers);
|
|
}
|
|
|
|
public void set_all_expanded(bool is_expanded) {
|
|
foreach (var g in Groups.OfTypeSafe<PNLRowCollectionViewGroup>())
|
|
g.set_all_expanded(is_expanded);
|
|
}
|
|
}
|
|
|
|
class PNLRowCollectionViewGroup : CollectionViewGroup, IRaiseNotifyPropertyChanged, IEquatable<PNLRowCollectionViewGroup> {
|
|
readonly PNLRowCollectionView _root;
|
|
readonly CollectionViewGroup _from;
|
|
readonly Dictionary<object, InnerGroupSubscription> _inner_groups = new Dictionary<object, InnerGroupSubscription>();
|
|
readonly Dictionary<PNLKey, IDisposable> _rows = new Dictionary<PNLKey, IDisposable>();
|
|
readonly VersionContainer<bool?> _is_all_selected;
|
|
readonly DeferrableActionWrapper _refresh_is_all_selected;
|
|
readonly HashSet<PNLRowVM> _selected_rows = new HashSet<PNLRowVM>();
|
|
readonly PNLCellIndexBuilder<PNLNormalizedData> _cell_map = new PNLCellIndexBuilder<PNLNormalizedData>();
|
|
readonly PNLCellEmbeddedAnimationModel<PNLNormalizedData> _animator;
|
|
|
|
ImmutableDictionary<ColumnKeys, PNLCellAppearance> _defaults_appearances;
|
|
IReadOnlyList<IPNLAnimationTrigger<PNLNormalizedData>> _triggers;
|
|
IDisposable _dispose;
|
|
bool _is_expanded = true;
|
|
int _selected_item_count_backing;
|
|
PNLNormalizedData _data;
|
|
|
|
public PNLRowCollectionViewGroup(CollectionViewGroup from, PNLRowCollectionView root, ImmutableDictionary<ColumnKeys, PNLCellAppearance> default_appearances, IReadOnlyList<IPNLAnimationTrigger<PNLNormalizedData>> triggers) : base(from.Name) {
|
|
_root = root;
|
|
_from = from;
|
|
_defaults_appearances = default_appearances;
|
|
_triggers = triggers;
|
|
_animator = new PNLCellEmbeddedAnimationModel<PNLNormalizedData>(default_appearances, triggers);
|
|
_animator.on_row(data);
|
|
data_changed += () => {
|
|
_cell_map.on_row(data);
|
|
_animator.on_row(data);
|
|
_cell_map.set_appearances(_animator.cell_appearances);
|
|
};
|
|
//sod_net_pos
|
|
_is_all_selected = new VersionContainer<bool?>(() => {
|
|
if (_selected_item_count == 0)
|
|
return false;
|
|
if (_selected_item_count == ProtectedItemCount)
|
|
return true;
|
|
return null;
|
|
});
|
|
bool? last_published_all_selected = _is_all_selected.value;
|
|
_refresh_is_all_selected = new DeferrableActionWrapper(() => {
|
|
if (_is_all_selected.value != last_published_all_selected) {
|
|
last_published_all_selected = is_all_selected;
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(is_all_selected)));
|
|
}
|
|
});
|
|
// sod
|
|
sod_pnl = _cell_map.add_pnl(ColumnKeys.SODPNL, d => d.sod_pnl);
|
|
sod_net_pos = _cell_map.add_net_pos(ColumnKeys.SODNetPos, d => d.sod_net_pos);
|
|
sod_long_pos = _cell_map.add_long_pos(ColumnKeys.SODLongPos, d => d.sod_long_pos);
|
|
sod_short_pos = _cell_map.add_short_pos(ColumnKeys.SODShortPos, d => d.sod_short_pos);
|
|
// day
|
|
day_pnl = _cell_map.add_pnl(ColumnKeys.DayPNL, d => d.day_pnl);
|
|
day_net_pos = _cell_map.add_net_pos(ColumnKeys.DayNetPos, d => d.day_net_pos);
|
|
day_long_pos = _cell_map.add_long_pos(ColumnKeys.DayLongPos, d => d.day_long_pos);
|
|
day_short_pos = _cell_map.add_short_pos(ColumnKeys.DayShortPos, d => d.day_short_pos);
|
|
// raw day
|
|
raw_day_net_pos = _cell_map.add_net_pos(ColumnKeys.RawDayNetPos, d => d.raw_day_net_pos);
|
|
raw_day_long_pos = _cell_map.add_long_pos(ColumnKeys.RawDayLongPos, d => d.raw_day_long_pos);
|
|
raw_day_short_pos = _cell_map.add_short_pos(ColumnKeys.RawDayShortPos, d => d.raw_day_short_pos);
|
|
// total
|
|
total_pnl = _cell_map.add(ColumnKeys.TotalPNL, d => d.total_pnl, FormattingDelegates.Price<decimal>.Value);
|
|
total_net_pos = _cell_map.add(ColumnKeys.TotalNetPos, d => d.total_net_pos);
|
|
total_long_pos = _cell_map.add(ColumnKeys.TotalLongPos, d => d.total_long_pos);
|
|
total_short_pos = _cell_map.add(ColumnKeys.TotalShortPos, d => d.total_short_pos);
|
|
// working
|
|
working_net_pos = _cell_map.add(ColumnKeys.WorkingNetPos, d => d.working_net_pos);
|
|
working_long_pos = _cell_map.add(ColumnKeys.WorkingLongPos, d => d.working_long_pos);
|
|
working_short_pos = _cell_map.add(ColumnKeys.WorkingShortPos, d => d.working_short_pos);
|
|
|
|
_cell_map.on_row(data);
|
|
_cell_map.set_appearances(_animator.cell_appearances);
|
|
((INotifyCollectionChanged)_from.Items).CollectionChanged += _from_items_CollectionChanged;
|
|
_from_items_CollectionChanged(_from.Items, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _from.Items, 0));
|
|
|
|
_dispose = _cell_map.animation_model.OnDispatcher().Subscribe(new_animations => {
|
|
animations = new_animations.Select(kv => new CellAnimation(kv.Key.column, kv.Key.type, kv.Value.color.to_brush())).ToList();
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(animations)));
|
|
});
|
|
}
|
|
|
|
public IList<CellAnimation> animations { get; private set; }
|
|
|
|
public event Action
|
|
data_changed,
|
|
ProtectedItemCountChanged,
|
|
selected_item_count_changed;
|
|
|
|
public PNLNormalizedData data {
|
|
get => _data;
|
|
set {
|
|
if (_data != value) {
|
|
_data = value;
|
|
data_changed?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
// sod
|
|
public PNLCellVM<PNLNormalizedData, decimal> sod_pnl { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> sod_net_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> sod_long_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> sod_short_pos { get; }
|
|
// day
|
|
public PNLCellVM<PNLNormalizedData, decimal> day_pnl { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> day_net_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> day_long_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> day_short_pos { get; }
|
|
// raw day
|
|
public PNLCellVM<PNLNormalizedData, decimal> raw_day_net_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> raw_day_long_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> raw_day_short_pos { get; }
|
|
// total
|
|
public PNLCellVM<PNLNormalizedData, decimal> total_pnl { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> total_net_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> total_long_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> total_short_pos { get; }
|
|
// working
|
|
public PNLCellVM<PNLNormalizedData, decimal> working_net_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> working_long_pos { get; }
|
|
public PNLCellVM<PNLNormalizedData, decimal> working_short_pos { get; }
|
|
//
|
|
public PNLRowCollectionViewGroup parent { get; private set; }
|
|
public int level => parent == null ? 1 : parent.level + 1;
|
|
public override bool IsBottomLevel => _from.IsBottomLevel;
|
|
|
|
public IEnumerable<PNLRowCollectionViewGroup> ancestors {
|
|
get {
|
|
if (parent != null) {
|
|
var p = parent;
|
|
do {
|
|
yield return p;
|
|
p = p.parent;
|
|
} while (p != null);
|
|
}
|
|
}
|
|
}
|
|
|
|
public IReadOnlyList<object> GroupNamePath {
|
|
get {
|
|
var path = new List<object> { Name };
|
|
foreach (var a in ancestors)
|
|
path.Add(a.Name);
|
|
return path.WrapReverseRO();
|
|
}
|
|
}
|
|
|
|
public bool is_expanded { get => _is_expanded; set => _set(ref _is_expanded, value); }
|
|
public bool? is_all_selected {
|
|
get => _is_all_selected.value;
|
|
set {
|
|
// The setter should not be called while is_all_selected deferred.
|
|
// However, it is good to be defensive, so let's add a conditional check anyways:
|
|
// if 'is_all_selected' is deferred, the value must be blindly propagated,
|
|
// bc the equality comparison is erroneous, as the current state is unknown.
|
|
if (value.HasValue && value != is_all_selected) {
|
|
using (_refresh_is_all_selected.defer_execution()) {
|
|
foreach (var i in Items) {
|
|
if (i is PNLRowCollectionViewGroup g)
|
|
g.is_all_selected = value;
|
|
else if (i is PNLRowVM r)
|
|
r.is_selected = value.Value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
new int ProtectedItemCount {
|
|
get => base.ProtectedItemCount;
|
|
set {
|
|
if (base.ProtectedItemCount != value) {
|
|
base.ProtectedItemCount = value;
|
|
_is_all_selected.increment_version();
|
|
ProtectedItemCountChanged?.Invoke();
|
|
_refresh_is_all_selected.execute();
|
|
}
|
|
}
|
|
}
|
|
|
|
int _selected_item_count {
|
|
get => _selected_item_count_backing;
|
|
set {
|
|
Log.assert(value >= 0, $"{nameof(_selected_item_count)} cannot be negative.");
|
|
_selected_item_count_backing = value;
|
|
_is_all_selected.increment_version();
|
|
_refresh_is_all_selected.execute();
|
|
selected_item_count_changed?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void on_settings(ImmutableDictionary<ColumnKeys, PNLCellAppearance> default_appearances, IReadOnlyList<IPNLAnimationTrigger<PNLNormalizedData>> triggers) {
|
|
_defaults_appearances = default_appearances;
|
|
_triggers = triggers;
|
|
_animator.on_settings(default_appearances, triggers);
|
|
_cell_map.set_appearances(_animator.cell_appearances);
|
|
foreach (var g in ProtectedItems.OfTypeSafe<PNLRowCollectionViewGroup>())
|
|
g.on_settings(default_appearances, triggers);
|
|
}
|
|
|
|
public void set_all_expanded(bool is_expanded) {
|
|
this.is_expanded = is_expanded;
|
|
foreach (var g in Items.OfTypeSafe<PNLRowCollectionViewGroup>())
|
|
g.set_all_expanded(is_expanded);
|
|
}
|
|
|
|
public bool Equals(PNLRowCollectionViewGroup other) =>
|
|
other != null &&
|
|
level == other.level &&
|
|
Equals(Name, other.Name);
|
|
|
|
void _select_all() {
|
|
foreach (var cvg in Items.OfType<PNLRowCollectionViewGroup>())
|
|
cvg._select_all();
|
|
}
|
|
|
|
IDisposable _detect_is_all_selected_changed() {
|
|
bool? was_all_selected = is_all_selected;
|
|
return Disposable.Create(() => {
|
|
if (is_all_selected != was_all_selected)
|
|
OnPropertyChanged(new PropertyChangedEventArgs(nameof(is_all_selected)));
|
|
});
|
|
}
|
|
|
|
void _add_item(int index, object item) {
|
|
Log.assert(index >= 0, "Index is less than 0.");
|
|
if (item is CollectionViewGroup group) {
|
|
var inner_group = new PNLRowCollectionViewGroup(group, _root, _defaults_appearances, _triggers);
|
|
_inner_groups.Add(group.Name, new InnerGroupSubscription(this, inner_group));
|
|
ProtectedItems.Insert(index, inner_group);
|
|
} else {
|
|
var row = (PNLRowVM)item;
|
|
ProtectedItems.Insert(index, item);
|
|
_rows.Add(row.key, _connect_row(row));
|
|
}
|
|
}
|
|
|
|
void _remove_item(int index, object item) {
|
|
if (item is PNLRowCollectionViewGroup inner_group) {
|
|
if (!_inner_groups.TryPopValue(inner_group.Name, out var ig_sub))
|
|
throw new InvalidOperationException($"Inner groups cache does not have an entry for the specified name '{inner_group.Name}' (entire tree structure may be compromised).");
|
|
ig_sub.Dispose();
|
|
Disposable.dispose(ref inner_group._dispose);
|
|
} else {
|
|
var row = (PNLRowVM)item;
|
|
if (!_rows.TryPopValue(row.key, out var row_sub))
|
|
throw new InvalidOperationException($"Inner rows cache does not have an entry for the specified irowstem key '{row.key}' (entire tree structure may be compromised).");
|
|
row_sub.Dispose();
|
|
}
|
|
ProtectedItems.RemoveAt(index);
|
|
}
|
|
|
|
void _from_items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
|
|
using (_refresh_is_all_selected.defer_execution()) {
|
|
switch (e.Action) {
|
|
case NotifyCollectionChangedAction.Add:
|
|
for (int i = 0; i < e.NewItems.Count; i++)
|
|
_add_item(e.NewStartingIndex + i, e.NewItems[i]);
|
|
break;
|
|
case NotifyCollectionChangedAction.Remove:
|
|
for (int i = 0; i < e.OldItems.Count; i++)
|
|
_remove_item(e.OldStartingIndex, ProtectedItems[e.OldStartingIndex]);
|
|
break;
|
|
case NotifyCollectionChangedAction.Replace:
|
|
for (int i = 0; i < e.OldItems.Count; i++)
|
|
_remove_item(e.OldStartingIndex, ProtectedItems[e.OldStartingIndex]);
|
|
for (int i = 0; i < e.NewItems.Count; i++)
|
|
_add_item(e.NewStartingIndex + i, e.NewItems[i]);
|
|
break;
|
|
case NotifyCollectionChangedAction.Reset:
|
|
while (ProtectedItems.Count > 0)
|
|
_remove_item(0, ProtectedItems[0]);
|
|
for (int i = 0; i < _from.Items.Count; i++)
|
|
_add_item(i, _from.Items[i]);
|
|
break;
|
|
}
|
|
_is_all_selected.increment_version();
|
|
_refresh_is_all_selected.execute();
|
|
}
|
|
}
|
|
|
|
IDisposable _connect_row(PNLRowVM row) {
|
|
using (_detect_is_all_selected_changed()) {
|
|
var last_data = PNLNormalizedData.zero;
|
|
on_updated(row.normalized_data);
|
|
row.updated += on_updated;
|
|
row.is_selected_changed += on_is_selected_changed;
|
|
if (row.is_selected) {
|
|
Log.assert(_selected_rows.Add(row), $"Selected Rows out of sync ({Name}): hash set already contained selected row that was just added to group ({row}).");
|
|
_selected_item_count++;
|
|
}
|
|
ProtectedItemCount++;
|
|
return Disposable.Create(() => {
|
|
row.updated -= on_updated;
|
|
row.is_selected_changed -= on_is_selected_changed;
|
|
using (_refresh_is_all_selected.defer_execution()) {
|
|
if (row.is_selected) {
|
|
Log.assert(_selected_rows.Remove(row), $"Selected Rows out of sync ({Name}): hash set did not contain selected row that was just removed from group ({row}).");
|
|
_selected_item_count--;
|
|
}
|
|
var newval = data - row.normalized_data;
|
|
data = newval;
|
|
ProtectedItemCount--;
|
|
}
|
|
});
|
|
void on_updated(PNLNormalizedData new_data) {
|
|
var less_last = data - last_data;
|
|
var plus_new = less_last + new_data;
|
|
data += new_data - last_data;
|
|
if (plus_new != data) {
|
|
Log.Ui.warn($"Detected PNL arithmetic rounding error in the group sum for '{string.Join(" ", GroupNamePath)}' (this bug is under investigation).");
|
|
}
|
|
last_data = new_data;
|
|
}
|
|
void on_is_selected_changed(bool is_selected) {
|
|
if (is_selected) {
|
|
Log.assert(_selected_rows.Add(row), $"Selected Rows out of sync ({Name}): hash set already contained row that was just selected ({row}).");
|
|
_selected_item_count++;
|
|
} else {
|
|
Log.assert(_selected_rows.Remove(row), $"Selected Rows out of sync ({Name}): hash set did not contain row that was just unselected ({row}).");
|
|
_selected_item_count--;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void _set<T>(ref T backing, T new_value, [CallerMemberName]string prop = "") =>
|
|
_set(ref backing, new_value, EqualityComparer<T>.Default, prop);
|
|
|
|
void _set<T>(ref T backing, T new_value, IEqualityComparer<T> comparer, [CallerMemberName]string prop = "") {
|
|
if (!comparer.Equals(backing, new_value)) {
|
|
backing = new_value;
|
|
OnPropertyChanged(new PropertyChangedEventArgs(prop));
|
|
}
|
|
}
|
|
|
|
void IRaiseNotifyPropertyChanged.raise_notify_property_changed(string prop) =>
|
|
OnPropertyChanged(new PropertyChangedEventArgs(prop));
|
|
|
|
class InnerGroupSubscription : IDisposable {
|
|
public readonly PNLRowCollectionViewGroup _inner_group;
|
|
readonly PNLRowCollectionViewGroup _parent;
|
|
PNLNormalizedData _last_data = PNLNormalizedData.zero;
|
|
int _last_count, _last_selected_item_count;
|
|
|
|
public InnerGroupSubscription(PNLRowCollectionViewGroup parent, PNLRowCollectionViewGroup inner_group) {
|
|
_parent = parent;
|
|
_inner_group = inner_group;
|
|
_inner_group.parent = parent;
|
|
// init item count
|
|
_on_item_count_changed();
|
|
// listen to item count changes
|
|
_inner_group.ProtectedItemCountChanged += _on_item_count_changed;
|
|
// init selected item count
|
|
_on_selected_item_count_changed();
|
|
// listen to selected item count changes
|
|
_inner_group.selected_item_count_changed += _on_selected_item_count_changed;
|
|
// init data
|
|
_on_normalized_data_changed();
|
|
// listen to data changes
|
|
_inner_group.data_changed += _on_normalized_data_changed;
|
|
_parent._selected_item_count += _inner_group._selected_item_count;
|
|
}
|
|
|
|
public void Dispose() {
|
|
_inner_group.ProtectedItemCountChanged -= _on_item_count_changed;
|
|
_inner_group.selected_item_count_changed -= _on_selected_item_count_changed;
|
|
_inner_group.data_changed -= _on_normalized_data_changed;
|
|
_inner_group.parent = null;
|
|
// make sure the state is valid (if a bad state goes by undetected here, nefarious/difficult-to-track bugs could arise)
|
|
if (_last_count != _inner_group.ProtectedItemCount) {
|
|
Log.Ui.warn("Cached inner count is not equal to the inner group's ItemCount (entire tree structure may be compromised).");
|
|
}
|
|
if (_last_data != _inner_group.data) {
|
|
Log.Ui.warn($"Cached normalized data is not equal to the inner group's normalized data (aggregated pnl data may be compromised).");
|
|
}
|
|
_parent.ProtectedItemCount -= _last_count;
|
|
_parent.data -= _last_data;
|
|
_parent._selected_item_count -= _last_selected_item_count;
|
|
}
|
|
|
|
void _on_item_count_changed() {
|
|
// add the delta
|
|
_parent.ProtectedItemCount += (_inner_group.ProtectedItemCount - _last_count);
|
|
// reset the local cache
|
|
_last_count = _inner_group.ProtectedItemCount;
|
|
}
|
|
|
|
void _on_selected_item_count_changed() {
|
|
_parent._selected_item_count += (_inner_group._selected_item_count - _last_selected_item_count);
|
|
_last_selected_item_count = _inner_group._selected_item_count;
|
|
}
|
|
|
|
void _on_normalized_data_changed() {
|
|
var new_data = _inner_group.data;
|
|
_parent.data += new_data - _last_data;
|
|
_last_data = new_data;
|
|
}
|
|
}
|
|
}
|
|
} |