237 lines
11 KiB
C#
237 lines
11 KiB
C#
using mbts;
|
|
using mbts.ui;
|
|
using mbts.ui.shell;
|
|
using mbts.ui.utility;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Collections.Specialized;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows.Data;
|
|
|
|
namespace bts.shell.spreader {
|
|
class SyntheticImportRowCollectionView : ListCollectionView {
|
|
ReadOnlyObservableCollection<object> _base_groups, _groups_wrapper;
|
|
ObservableAutoWrapper<object, object> _groups;
|
|
|
|
public SyntheticImportRowCollectionView(IList list)
|
|
: base(list) { }
|
|
|
|
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 SyntheticImportRowCollectionViewGroup((CollectionViewGroup)obj, this));
|
|
_groups_wrapper = new ReadOnlyObservableCollection<object>(_groups);
|
|
}
|
|
return _groups_wrapper;
|
|
}
|
|
}
|
|
|
|
public void set_all_expanded(bool is_expanded) {
|
|
foreach (var g in Groups.OfTypeSafe<SyntheticImportRowCollectionViewGroup>())
|
|
g.set_all_expanded(is_expanded);
|
|
}
|
|
}
|
|
|
|
class SyntheticImportRowCollectionViewGroup : CollectionViewGroup, INotifyDataErrorInfo, IRaiseNotifyPropertyChanged, IEquatable<SyntheticImportRowCollectionViewGroup> {
|
|
static readonly string _desc_delim = ", ";
|
|
readonly SyntheticImportRowCollectionView _root;
|
|
readonly CollectionViewGroup _from;
|
|
readonly Dictionary<object, InnerGroupSubscription> _inner_groups = new Dictionary<object, InnerGroupSubscription>();
|
|
readonly Dictionary<int, IDisposable> _rows = new Dictionary<int, IDisposable>();
|
|
string
|
|
_leg_type_desc,
|
|
_security_desc,
|
|
_quantity_desc,
|
|
_price_multiplier_desc;
|
|
|
|
IDisposable _dispose;
|
|
bool _is_expanded;
|
|
|
|
public SyntheticImportRowCollectionViewGroup(CollectionViewGroup from, SyntheticImportRowCollectionView root) : base(from.Name) {
|
|
_root = root;
|
|
_from = from;
|
|
((INotifyCollectionChanged)_from.Items).CollectionChanged += _from_items_CollectionChanged;
|
|
_from_items_CollectionChanged(_from.Items, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _from.Items, 0));
|
|
}
|
|
|
|
public event Action ProtectedItemCountChanged;
|
|
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
|
|
|
|
public override bool IsBottomLevel => _from.IsBottomLevel;
|
|
public bool is_expanded { get => _is_expanded; set => _set(ref _is_expanded, value); }
|
|
public int level => parent == null ? 1 : parent.level + 1;
|
|
public SyntheticImportRowCollectionViewGroup parent { get; private set; }
|
|
// leg type desc
|
|
public string leg_type_desc =>
|
|
_leg_type_desc ?? (_leg_type_desc = string.Join(_desc_delim, ProtectedItems.OfType<ContractLegVM>().Select(l => l.leg_type.display_name())));
|
|
// price multiplier desc
|
|
public string price_multiplier_desc =>
|
|
_price_multiplier_desc ?? (_price_multiplier_desc = string.Join(_desc_delim, ProtectedItems.OfType<ContractLegVM>().Select(l => l.price_multiplier)));
|
|
// quantity desc
|
|
public string quantity_desc =>
|
|
_quantity_desc ?? (_quantity_desc = string.Join(_desc_delim, ProtectedItems.OfType<ContractLegVM>().Select(l => l.quantity)));
|
|
// security desc
|
|
public string security_desc =>
|
|
_security_desc ?? (_security_desc = string.Join(_desc_delim, ProtectedItems.OfType<ContractLegVM>().Select(l => l.security?.ToString() ?? "?").ExceptNull()).if_null_or_empty("(No Legs)"));
|
|
|
|
new int ProtectedItemCount {
|
|
get => base.ProtectedItemCount;
|
|
set {
|
|
if (base.ProtectedItemCount != value) {
|
|
base.ProtectedItemCount = value;
|
|
ProtectedItemCountChanged?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool HasErrors => Items.OfType<ContractLegVM>().Any(l => l.HasErrors);
|
|
public IEnumerable GetErrors(string propertyName) =>
|
|
propertyName == nameof(Items) ?
|
|
Items.OfType<ContractLegVM>().SelectMany(l => l.AllErrors) :
|
|
Enumerable.Empty<string>();
|
|
|
|
public void set_all_expanded(bool is_expanded) {
|
|
this.is_expanded = is_expanded;
|
|
foreach (var g in Items.OfTypeSafe<SyntheticImportRowCollectionViewGroup>())
|
|
g.set_all_expanded(is_expanded);
|
|
}
|
|
|
|
public bool Equals(SyntheticImportRowCollectionViewGroup other) =>
|
|
other != null &&
|
|
level == other.level &&
|
|
Equals(Name, other.Name);
|
|
|
|
void IRaiseNotifyPropertyChanged.raise_notify_property_changed(string prop) =>
|
|
OnPropertyChanged(new PropertyChangedEventArgs(prop));
|
|
|
|
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 SyntheticImportRowCollectionViewGroup(group, _root);
|
|
_inner_groups.Add(group.Name, new InnerGroupSubscription(this, inner_group));
|
|
ProtectedItems.Insert(index, inner_group);
|
|
} else {
|
|
var row = (ContractLegVM)item;
|
|
if (_rows.TryPopValue(row.id, out var sub))
|
|
sub.Dispose();
|
|
_rows[row.id] = _connect_row(row);
|
|
ProtectedItems.Insert(index, item);
|
|
}
|
|
}
|
|
|
|
void _remove_item(int index, object item) {
|
|
if (item is SyntheticImportRowCollectionViewGroup 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);
|
|
}
|
|
ProtectedItems.RemoveAt(index);
|
|
}
|
|
|
|
void _from_items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
void _invalidate_props(params string[] props) {
|
|
for (int i = 0; i < props.Length; i++)
|
|
OnPropertyChanged(new PropertyChangedEventArgs(props[i]));
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
IDisposable _connect_row(ContractLegVM row) {
|
|
row.result_changed += on_result_changed;
|
|
row.ErrorsChanged += on_errors_changed;
|
|
return Disposable.Create(() => {
|
|
row.result_changed -= on_result_changed;
|
|
row.ErrorsChanged -= on_errors_changed;
|
|
});
|
|
void on_result_changed() {
|
|
_security_desc = null;
|
|
_price_multiplier_desc = null;
|
|
_quantity_desc = null;
|
|
_leg_type_desc = null;
|
|
_invalidate_props(nameof(security_desc), nameof(price_multiplier_desc), nameof(quantity_desc), nameof(leg_type_desc));
|
|
}
|
|
void on_errors_changed(object sender, DataErrorsChangedEventArgs e) =>
|
|
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Items)));
|
|
}
|
|
|
|
class InnerGroupSubscription : IDisposable {
|
|
public readonly SyntheticImportRowCollectionViewGroup _inner_group;
|
|
readonly SyntheticImportRowCollectionViewGroup _parent;
|
|
int _last_count;
|
|
|
|
public InnerGroupSubscription(SyntheticImportRowCollectionViewGroup parent, SyntheticImportRowCollectionViewGroup 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;
|
|
}
|
|
|
|
public void Dispose() {
|
|
_inner_group.ProtectedItemCountChanged -= _on_item_count_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).");
|
|
}
|
|
_parent.ProtectedItemCount -= _last_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;
|
|
}
|
|
}
|
|
}
|
|
}
|