From 3aedebd0abb40bb168ba7559bdb84fe01d5e96a1 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 20 Jan 2019 15:02:31 -0600 Subject: [PATCH] AlgoTemplateFavoriteButtonsView - cancel automatic selection of the previous item when SelectedItem is removed from ItemsSource --- .../spark/Orders/OrderPropertiesEditorVM.cs | 26 ++++---------- .../core/GlobalExtensions/GlobalExtensions.cs | 21 +++++++++++ .../AlgoTemplateFavoriteButtonsView.xaml.cs | 35 ++++++++++++++++++- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/src/client/spark/Orders/OrderPropertiesEditorVM.cs b/src/client/spark/Orders/OrderPropertiesEditorVM.cs index 89eb7238c..f2fd3afd1 100644 --- a/src/client/spark/Orders/OrderPropertiesEditorVM.cs +++ b/src/client/spark/Orders/OrderPropertiesEditorVM.cs @@ -5,6 +5,7 @@ using bts.collections; using bts.spark; using mbts.collections; using mbts.ui; +using mbts.ui.controls; using mbts.ui.shell; using mbts.ui.utility; using mbts.utils; @@ -92,9 +93,11 @@ namespace mbts.spark { _selected_favorite_algo_template = backing(nameof(selected_favorite_algo_template)); // items source views algo_templates_view = CollectionViewSource.GetDefaultView(algo_templates); - var favorite_algo_templates_view = new ListCollectionView(algo_templates); + var favorite_algo_templates_view = new ListCollectionView(ObservableAggregateCollection.create(_algo_definitions.AsCollection(), d => d.templates)); favorite_algo_templates_view.LiveGroupingProperties.Add(nameof(AlgoDefinitionTemplateVM.owner_vm_key)); + favorite_algo_templates_view.LiveFilteringProperties.Add(nameof(AlgoDefinitionTemplateVM.is_favorite)); favorite_algo_templates_view.Filter = item => (item as AlgoDefinitionTemplateVM)?.is_favorite ?? false; + favorite_algo_templates_view.IsLiveFiltering = true; this.favorite_algo_templates_view = favorite_algo_templates_view; // commands save_template_cmd = new AsyncCommand(save_template_async); @@ -121,6 +124,7 @@ namespace mbts.spark { _algo_definitions.connect_definitions(_definitions_buffer.OnDispatcher()), model.algo_model.algo_metadata.OnDispatcher().Subscribe(_on_template_metadata), CollectionHelper.dispose_on_removed(_algo_definitions)) { + () => Disposable.dispose(ref _template_collection_sync), _algo_definitions.Clear }; async Task save_template_async(AlgoDefinitionTemplateVM template) { @@ -133,13 +137,7 @@ namespace mbts.spark { void _selected_algo_definition_Changed(AlgoDefinitionVM old_value, AlgoDefinitionVM new_value, SelectionReason reason) { Disposable.dispose(ref _template_collection_sync); if (new_value != null) { - /// and are essentially TreeViewItem viewmodels. - /// The definitions are the root/parent nodes, and each one has a collection of templates (child-nodes). - /// The ladder uses a single popup window to serve as the view for each definition's child-nodes. - /// 1 popup window = 1 ItemsControl, and 1 ItemsControl = 1 ItemsSource. - /// In other words, the templates view must be bound to 1 ObservableCollection (selected_definition.algo_templates won't work, as each definition has a different collection of templates). - /// is that 1 collection. This call synchronizes it with selected_definition.algo_templates. - _template_collection_sync = ObservableAutoWrapper.connect_two_way(new_value.templates_with_custom, algo_templates); + _template_collection_sync = ObservableAutoWrapper.connect_two_way(new_value.templates, algo_templates); } else { algo_templates.Clear(); } @@ -165,11 +163,7 @@ namespace mbts.spark { public CustomOrderDefinitionImmutable? definition => selected_algo_template?.algo_root is IOrderTypeVM vm ? vm.createi() : (CustomOrderDefinitionImmutable?)null; // items sources public ObservableCollection algo_definitions => _algo_definitions; - /// The binding source of ItemsControls.ItemsSource should never change (i.e. the actual collection reference should be readonly). - /// Although the template-items in the dropdown should always contain the exact same elements as selected_algo_definition.templates, the view must be bound to a single collection instance. - /// is such an instance. It should always be in sync with selected_algo_definition.templates. public ObservableCollection algo_templates { get; } = new ObservableCollection(); - public ObservableCollection favorite_algo_templates => _favorite_templates; public ICollectionView algo_definitions_view { get; } public ICollectionView algo_templates_view { get; } public ICollectionView favorite_algo_templates_view { get; } @@ -253,13 +247,7 @@ namespace mbts.spark { var curr_selected_template = selected_algo_template; if (curr_selected_template?.is_favorite ?? false) selected_favorite_algo_template = curr_selected_template; - favorite_algo_templates_view.Refresh(); - /// If the was just removed from favorites, the favorites listbox will immediately select the previous item in its list. - /// This behavior is by microsoft design, and changing it would not be trivial. - /// It's a weird situation because there are 2 selector views (templates, and favorite templates), which use the same set of items but with different filters. - /// Selecting an item in 1 view causes the same item (if present) to be selected in the other view. - /// Therefore, curr_selected_template is reselected to negate any such incidental selection change. - selected_algo_template = curr_selected_template; + algo_definitions_view.Refresh(); } // This preserves the selected items through a save op. diff --git a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs index f41ca1e77..94e0257ac 100644 --- a/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs +++ b/src/client/spark/core/GlobalExtensions/GlobalExtensions.cs @@ -374,5 +374,26 @@ namespace System.Reactive.Linq }); }); } + + /// + /// A less verbose way to call . + /// + public static IObservable FromAction(Action event_source) => + Observable.FromEvent, T>(cb => event_source += cb, cb => event_source -= cb); + + /// + /// An inner subscription is made for each value in the source sequence using the specified delegate (). + /// When a new value arrives, the subscription for the previous value is disposed before is invoked with the new value. + /// + /// + /// + /// + /// + public static IDisposable SubscribeEach(this IObservable source, Func subscribe_value) { + IDisposable value_sub = null; + return new Disposables.CompositeDisposable(source.Subscribe(val => Disposable.replace(ref value_sub, subscribe_value(val)))) { + () => Disposable.dispose(ref value_sub) + }; + } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml.cs b/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml.cs index 43628447e..3c6f1c557 100644 --- a/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml.cs +++ b/src/client/spark/view/Controls/Order/AlgoTemplateFavoriteButtonsView.xaml.cs @@ -1,4 +1,8 @@ -using System.Windows; +using System; +using System.Collections; +using System.Collections.Specialized; +using System.Reactive.Linq; +using System.Windows; using System.Windows.Controls; namespace mbts.ui.spark.algo { @@ -6,7 +10,36 @@ namespace mbts.ui.spark.algo { /// Interaction logic for AlgoTemplateFavoriteButtonsView.xaml /// class AlgoTemplateFavoriteButtonsView : ListBox { + /// When the SelectedItem is removed from the ItemsSource, ListBox automatically reverts the selection to the previous item. + /// This boolean flag () is used in a workaround to avoid this behavior, and instead set SelectedItem to null. + /// When the SelectedItem is removed, the flag is set to true, and in the imminent subsequent call to , the event is canceled, and the flag reset to false. + /// For implementation details: , and + bool _cancel_revert_selection; + static AlgoTemplateFavoriteButtonsView() => DefaultStyleKeyProperty.OverrideMetadata(typeof(AlgoTemplateFavoriteButtonsView), new FrameworkPropertyMetadata(typeof(AlgoTemplateFavoriteButtonsView))); + + public AlgoTemplateFavoriteButtonsView() => + System.Reactive.Linq.ObservableExtensions.FromAction(_items_source_changed) + .SubscribeEach(items_source => items_source == null ? Disposable.empty : items_source.Subscribe(_on_items_source_CollectionChanged)); + + event Action _items_source_changed; + + protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) => + _items_source_changed?.Invoke(newValue as INotifyCollectionChanged); + + protected override void OnSelectionChanged(SelectionChangedEventArgs e) { + e.Handled = _cancel_revert_selection; + _cancel_revert_selection = false; + } + + void _on_items_source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { + switch (e.Action) { + case NotifyCollectionChangedAction.Remove: + var si = SelectedItem; + _cancel_revert_selection = si != null && e.OldItems.Contains(si); + break; + } + } } } \ No newline at end of file