From cb07abbf4249340f24d9be1db77c90d8f90e9b0e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 3 Aug 2020 11:56:31 -0500 Subject: [PATCH] made new SelectorVM (todo: replace refs to old one) --- .../spark/core/Utility/SelectionModel.cs | 97 ++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) diff --git a/src/client/spark/core/Utility/SelectionModel.cs b/src/client/spark/core/Utility/SelectionModel.cs index 917252f0a..ef56f8d5b 100644 --- a/src/client/spark/core/Utility/SelectionModel.cs +++ b/src/client/spark/core/Utility/SelectionModel.cs @@ -1,6 +1,11 @@ -using System; +using bts.collections; +using bts.utils; +using mbts.ui.shell; +using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Collections.Specialized; +using System.Linq; using System.Reactive.Linq; namespace mbts.ui.utility { @@ -153,6 +158,94 @@ namespace mbts.ui.utility { _try_match_item(_item_to_value(item), out match); } + class SelectorVM { + readonly IEqualityComparer _eq_comparer; + readonly IEqualityComparer _key_comparer; + readonly Func _key_fn; + readonly ObservableCollection _items = new ObservableCollection(); + readonly Dictionary _map; + /// not as noisy as , + /// this event is only raised after a batch collection change has been completed. + /// . + IComparer _comparer; + T _selected_item; + TKey _pending_selected_item_key; + bool _is_pending_selection; + + public SelectorVM(Func key_fn) + : this(key_fn, EqualityComparer.Default) { } + + public SelectorVM(Func key_fn, IEqualityComparer key_comparer) { + _key_fn = key_fn ?? throw new ArgumentNullException(nameof(key_fn)); + _key_comparer = key_comparer ?? throw new ArgumentNullException(nameof(key_comparer)); + _eq_comparer = EqualityComparer.Create(_key_fn, _key_comparer); + _map = new Dictionary(key_comparer); + items = ObservableAutoWrapper.create_read_only_wrapper(_items); + } + + public event Action selected_item_changed; + + public IReadOnlyObservableCollection items { get; } + public T selected_item { + get => _selected_item; + set { + if (!_eq_comparer.Equals(_selected_item, value)) { + _selected_item = value; + selected_item_changed?.Invoke(); + } + } + } + public IComparer comparer { + get => _comparer; + set { + _comparer = value; + if (value != null) + _update_items(items, false); + } + } + + public bool try_select(T item, bool wait_for_item_to_arrive = false) => + try_select(_key_fn(item), wait_for_item_to_arrive); + + public bool try_select(TKey item_key, bool wait_for_item_to_arrive = false) { + if (_map.TryGetValue(item_key, out var item)) { + selected_item = item; + return true; + } else if (wait_for_item_to_arrive) { + _pending_selected_item_key = item_key; + _is_pending_selection = true; + } + return false; + } + + public void update_items(IEnumerable items) => + _update_items(items, true); + + void _update_items(IEnumerable items, bool map_changed) { + // The currently selected item may be removed during this collection change. + // After the items have been changed, attempt to re-select the last selected item; + // if there is already a particular 'pending selection,' attempt to select that instead. + var target_key = _is_pending_selection ? _pending_selected_item_key : _key_fn(_selected_item); + if (map_changed) + _map.Clear(); + if (items is null) { + _items.Clear(); + } else { + if (_comparer != null) { + items = items.Ordered(_comparer); + } + _items.sync_with(items); + } + if (map_changed) { + foreach (var i in this.items) + _map[_key_fn(i)] = i; + } + if (_map.Count != this.items.Count) + throw new InvalidOperationException("Map and items are out of sync (different item counts)."); + try_select(target_key, _is_pending_selection || _items.Count == 0); + } + } + static class SelectionModelExtensions { public static IObservable observe_selected_item(this SelectionModel selection_model, bool notify_on_subscribe = false) => Observable.Create(o => { @@ -166,4 +259,4 @@ namespace mbts.ui.utility { return Disposable.Create(() => selection_model.Changed -= callback); } } -} \ No newline at end of file +}