* Move Anonymous/FinalizableDisposable to own file * Rename DisposableCombo -> ImmutableDisposable and move it to its own file * Clean up IDisposable utils impl, but keep changes to calling code minimial
117 lines
4.9 KiB
C#
117 lines
4.9 KiB
C#
using bts.collections;
|
|
using bts.utils;
|
|
|
|
using mbts;
|
|
using mbts.analytics;
|
|
using mbts.ui.shell;
|
|
using mbts.ui.utility;
|
|
|
|
using Spark.Core;
|
|
using Spark.Core.Disposables;
|
|
using Spark.Core.Collections;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
|
|
namespace mbts.spark {
|
|
public interface IExpirationBasedRow {
|
|
VolSurfaceDisplayGridExpiration expiration { get; }
|
|
}
|
|
|
|
abstract class ExpSelectionGridVM<TRow> : ViewModelBase, IDisposable where TRow : IExpirationBasedRow {
|
|
protected VolSurfaceDisplayModel _model;
|
|
protected VolSurfaceEventDemux _demux;
|
|
|
|
readonly IDisposable _exps_changed_subscription;
|
|
readonly List<VolSurfaceExpiration> _hidden_exps = new();
|
|
|
|
public ExpSelectionGridVM(VolSurfaceDisplayModel model, IEnumerable<VolSurfaceExpiration> hidden_expirations, object startup_data = null) {
|
|
_model = model;
|
|
_demux = new VolSurfaceEventDemux(model);
|
|
_hidden_exps = hidden_expirations.ToList();
|
|
visible_rows = new ObservableCollection<object>();
|
|
visible_rows.CollectionChanged += _visible_rows_changed;
|
|
var expirations = new ObservableCollection<VolSurfaceDisplayGridExpiration>();
|
|
|
|
_initialize(startup_data);
|
|
_demux.on_expirations_change += update_exps;
|
|
rows = new ObservableAutoWrapper<TRow, VolSurfaceDisplayGridExpiration>(expirations, _row_from_exp);
|
|
update_exps(_model.get_ordered_expirations(), new List<VolSurfaceDisplayGridExpiration>());
|
|
_exps_changed_subscription = Disposable.Create(() => _demux.on_expirations_change -= update_exps);
|
|
|
|
void update_exps(
|
|
IEnumerable<VolSurfaceDisplayGridExpiration> added,
|
|
IEnumerable<VolSurfaceDisplayGridExpiration> removed) {
|
|
expirations.sync_with(_model.get_ordered_expirations());
|
|
|
|
if (added.Any() && !removed.Any()) {
|
|
visible_rows.sync_with(rows.Where(r => !_hidden_exps.Contains(r.expiration.ToVSExp())).Cast<object>());
|
|
} else {
|
|
var vs_exps = expirations.Select(exp => exp.ToVSExp());
|
|
var vs_adds = added.Select(exp => exp.ToVSExp());
|
|
var vs_removes = removed.Select(exp => exp.ToVSExp());
|
|
|
|
var true_removed = removed.Where(rmv => !vs_adds.Contains(rmv.ToVSExp()));
|
|
var true_added = added.Except(vs_removes.Concat(_hidden_exps), v => v.ToVSExp());
|
|
var changed_calc_modes = added.Where(add => vs_removes.Contains(add.ToVSExp()));
|
|
|
|
foreach (var tr in true_removed) {
|
|
int index = visible_rows.Cast<IExpirationBasedRow>().Select(r => r.expiration).indexOf(tr);
|
|
if (index >= 0 && index < visible_rows.Count) {
|
|
visible_rows.RemoveAt(index);
|
|
}
|
|
}
|
|
|
|
var comp = ComparerWrapper<object>.wrap_cast(
|
|
NullCheckComparer.create<IExpirationBasedRow>(
|
|
(r1, r2) => r1.expiration.CompareTo(r2.expiration)));
|
|
visible_rows.InsertSorted(true_added, comp);
|
|
|
|
foreach (var ccm in changed_calc_modes) {
|
|
int index = visible_rows
|
|
.Cast<IExpirationBasedRow>()
|
|
.Select(r => r.expiration.ToVSExp())
|
|
.indexOf(ccm.ToVSExp());
|
|
if (index < 0 || index >= visible_rows.Count) {
|
|
continue;
|
|
}
|
|
visible_rows.RemoveAt(index);
|
|
if (rows.TryGetFirst(r => r.expiration.Equals(ccm), out var row)) {
|
|
visible_rows.Insert(index, row);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public ObservableCollection<object> visible_rows { get; set; }
|
|
public ObservableAutoWrapper<TRow, VolSurfaceDisplayGridExpiration> rows { get; set; }
|
|
|
|
public List<VolSurfaceExpiration> get_hidden_exps() { return _hidden_exps; }
|
|
|
|
public virtual void Dispose() {
|
|
_exps_changed_subscription.Dispose();
|
|
visible_rows.CollectionChanged -= _visible_rows_changed;
|
|
Disposable.dispose(ref _model);
|
|
}
|
|
|
|
protected abstract TRow _row_from_exp(VolSurfaceDisplayGridExpiration exp);
|
|
|
|
protected virtual void _initialize(object startup_data) { }
|
|
|
|
void _visible_rows_changed(object sender, NotifyCollectionChangedEventArgs e) {
|
|
if (e.Action == NotifyCollectionChangedAction.Remove) {
|
|
foreach (IExpirationBasedRow row in e.OldItems)
|
|
if (!_hidden_exps.Contains(row.expiration.ToVSExp()))
|
|
_hidden_exps.Add(row.expiration.ToVSExp());
|
|
} else if (e.Action == NotifyCollectionChangedAction.Add)
|
|
foreach (IExpirationBasedRow row in e.NewItems)
|
|
if (_hidden_exps.Contains(row.expiration.ToVSExp()))
|
|
_hidden_exps.Remove(row.expiration.ToVSExp());
|
|
}
|
|
}
|
|
}
|