From e255e054d4c3d3367b05b2c01818052425ab39cc Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 22 Mar 2022 11:47:57 -0400 Subject: [PATCH] refactor: Split analytics module/viewmodel into sep classes --- .../Modules/Analytics/AnalyticsShellModule.cs | 99 +++++++------------ .../Analytics/AnalyticsShellModuleVM.cs | 12 +++ .../Analytics/AnalyticsShellModuleView.xaml | 4 +- .../Commands/CompositeEventCommand.cs | 56 +++++++++++ .../spark/ViewModel/Commands/EventCommand.cs | 42 ++++++++ .../spark/ViewModel/Commands/IEventCommand.cs | 8 ++ src/client/spark/ViewModel/KeyValueItemVM.cs | 11 +++ .../spark/ViewModel/KeyValueLauncherVM.cs | 22 +++++ src/client/spark/core/Bus/BusExtensions.cs | 8 ++ 9 files changed, 198 insertions(+), 64 deletions(-) create mode 100644 src/client/spark/Modules/Analytics/AnalyticsShellModuleVM.cs create mode 100644 src/client/spark/ViewModel/Commands/CompositeEventCommand.cs create mode 100644 src/client/spark/ViewModel/Commands/EventCommand.cs create mode 100644 src/client/spark/ViewModel/Commands/IEventCommand.cs create mode 100644 src/client/spark/ViewModel/KeyValueItemVM.cs create mode 100644 src/client/spark/ViewModel/KeyValueLauncherVM.cs create mode 100644 src/client/spark/core/Bus/BusExtensions.cs diff --git a/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs b/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs index 0ee5d773f..66a3ac973 100644 --- a/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs +++ b/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs @@ -1,7 +1,5 @@ namespace mbts.spark { - using bts; using bts.api; - using bts.collections; using bts.shell; using bts.spark; @@ -11,24 +9,21 @@ namespace mbts.spark { using System; using System.Collections.Generic; - using System.Collections.ObjectModel; using System.Composition; using System.IO; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows; - using System.Windows.Input; using System.Windows.Media; [Export(typeof(IShellModule))] - class AnalyticsShellModule : ViewModelBase, IShellModule, IShellModuleViewFactory { + class AnalyticsShellModule : IShellModule, IShellModuleViewFactory { readonly IBus _bus; readonly IWindowManager _wm; readonly List _window_factories; - string _new_vol_surface_name; - IDisposable _subscriptions; + IDisposable _dispose; [ImportingConstructor] public AnalyticsShellModule(IBus bus, IWindowManager wm, persist.UserSettingsHub settings_hub, OrdersModel orders_model) { @@ -38,9 +33,13 @@ namespace mbts.spark { new VolSurfaceWindowFactory(this, bus), new AnalyticsExpirationParametersWindowFactory(this, bus), }; - _subscriptions = Disposable.Create( - _bus.subscribe(SparkTags.VolSurfaceListTag).OnDispatcher() - .Subscribe(async vs => await _set_current_vol_surfaces(vs))); + var vol_surfaces_src = _bus.subscribe(SparkTags.VolSurfaceListTag).OnDispatcher(); + _dispose = new DisposableCombo( + vol_surfaces_src.Subscribe(_on_vol_surfaces), + vm.launch_exp_params_view_cmd.subscribe_executed(_launch_exp_params_view), + vm.launch_vol_surface_cmd.subscribe_executed(_launch_vol_surface), + vm.create_vol_surface_cmd.subscribe_executed(_create_new_vol_surface), + vm.delete_vol_surface_cmd.subscribe_executed(_delete_vol_surface)); } public enum WindowType { Main, Heatmap } @@ -49,40 +48,20 @@ namespace mbts.spark { public ImageSource icon => Icons.analytics; public int priority => 20; - public IEnumerable actions { - get { - yield return _launch_exp_params_view_spark_cmd; - } - } + public AnalyticsShellModuleVM vm { get; } = new(); - public ICommand launch_exp_params_view_cmd => - _launch_exp_params_view_spark_cmd.ICommand(_bus); - - static SparkCommand _launch_exp_params_view_spark_cmd => - SparkCommands.show_analytics_exp_params().complete(AnalyticsExpirationParametersSettings.Default); - - public ObservableCollection current_vol_surfaces { get; } = new(); - - public string new_vol_surface_name { - get => _new_vol_surface_name; - set => Set(ref _new_vol_surface_name, value); - } - - public ICommand create_new_vol_surface => new DelegateCommand(_create_new_vol_surface); + public IEnumerable actions => Enumerable.Empty(); public UIElement create_view() => - new AnalyticsShellModuleView { DataContext = this }; + new AnalyticsShellModuleView { DataContext = vm }; public Task create(Stream load_stream) => ShellWindowFactory.create(load_stream, _window_factories); public void Dispose() => - Disposable.dispose(ref _subscriptions); + Disposable.dispose(ref _dispose); - void _create_new_vol_surface() { - if (string.IsNullOrWhiteSpace(_new_vol_surface_name)) { - return; - } + void _create_new_vol_surface(string new_vol_surface_name) { var vm = new VolSurfaceSettingsEditorVM(_bus, new VolSurfaceSettings { core_settings = new VolSurfaceCoreSettings { vol_surface_name = new_vol_surface_name, @@ -105,9 +84,7 @@ namespace mbts.spark { }, polynomial_model_settings = new PolynomialModelVolSurfaceSettings() }, - (settings) => _bus.post(SparkTags.VolSurfaceCreateTag, settings)); - _new_vol_surface_name = string.Empty; - RaisePropertyChanged(nameof(new_vol_surface_name)); + _bus.post_callback(SparkTags.VolSurfaceCreateTag)); var window = new VolSurfaceSettingsEditor { DataContext = vm, WindowStartupLocation = WindowStartupLocation.CenterScreen @@ -115,33 +92,31 @@ namespace mbts.spark { window.Show(); } - async Task _get_vs_name(int vs_id) { - var (vol_surfaces, success) = await _bus.subscribe(SparkTags.VolSurfaceListTag).FirstAsyncTask(TimeSpan.FromSeconds(2)); - return success && vol_surfaces != null && vol_surfaces.TryGetValue(vs_id, out var vs_name) ? - vs_name : "(...)"; + void _launch_exp_params_view() { + _bus.post(SparkTags.show_analytics_exp_params, AnalyticsExpirationParametersSettings.Default); } - async Task _set_current_vol_surfaces(IDictionary id_names) { - current_vol_surfaces.SetItems(id_names.Select(kvp => new AlgoMenuItemVM(kvp.Key, kvp.Value, - (id, name) => { - var windows = _wm.list_windows(); - if (windows.OfType().TryGetFirst(w => w.vs_id == id, out var w)) { - _wm.MakeVisible(w); - return; - } - _bus.post(SparkTags.ShowVolSurface, new() { id = id }); - }, - (id, name) => { - if (BTSMessageBox.Confirm($"Are you sure you want to delete {name}?", "Vol Surface")) { - _bus.post(SparkTags.VolSurfaceDeleteTag, id); - } - }))); - //reset titles for all open vol surface windows + void _launch_vol_surface(int id) { + var windows = _wm.list_windows(); + if (windows.OfType().TryGetFirst(w => w.vs_id == id, out var win)) { + _wm.MakeVisible(win); + return; + } + _bus.post(SparkTags.ShowVolSurface, new() { id = id }); + } + + void _delete_vol_surface(int id) { + if (BTSMessageBox.Confirm($"Are you sure you want to delete {name}?", "Vol Surface")) { + _bus.post(SparkTags.VolSurfaceDeleteTag, id); + } + } + + void _on_vol_surfaces(IDictionary id_names) { + vm.vol_surface_launcher_vm.items.sync_with(id_names.Select( + kv => new KeyValueItemVM(kv.Key, kv.Value))); + // reset titles for all open vol surface windows foreach (var window in _wm.list_windows().OfType()) { - var vs_name = await _get_vs_name(window.vs_id); - if (window.vs_name != vs_name) { - window.Title = vs_name; - } + window.Title = id_names.GetValueOrDefault(window.vs_id) ?? "(...)"; } } } diff --git a/src/client/spark/Modules/Analytics/AnalyticsShellModuleVM.cs b/src/client/spark/Modules/Analytics/AnalyticsShellModuleVM.cs new file mode 100644 index 000000000..da3d43245 --- /dev/null +++ b/src/client/spark/Modules/Analytics/AnalyticsShellModuleVM.cs @@ -0,0 +1,12 @@ +#nullable enable +namespace mbts.spark { + using mbts.ui; + using mbts.ui.shell; + + using Spark.ViewModel; + + class AnalyticsShellModuleVM : ViewModelBase { + public EventCommand launch_exp_params_view_cmd { get; } = new(); + public KeyValueLauncherVM vol_surface_launcher_vm { get; } = new(); + } +} diff --git a/src/client/spark/Modules/Analytics/AnalyticsShellModuleView.xaml b/src/client/spark/Modules/Analytics/AnalyticsShellModuleView.xaml index d3629b151..b6494fe62 100644 --- a/src/client/spark/Modules/Analytics/AnalyticsShellModuleView.xaml +++ b/src/client/spark/Modules/Analytics/AnalyticsShellModuleView.xaml @@ -1,4 +1,4 @@ -Create + Command="{Binding create_vol_surface_cmd}">Create diff --git a/src/client/spark/ViewModel/Commands/CompositeEventCommand.cs b/src/client/spark/ViewModel/Commands/CompositeEventCommand.cs new file mode 100644 index 000000000..271ef73dc --- /dev/null +++ b/src/client/spark/ViewModel/Commands/CompositeEventCommand.cs @@ -0,0 +1,56 @@ +#nullable enable +namespace mbts.ui { + using System; + + class CompositeEventCommand : IEventCommand<(T1, T2)>, IRaiseCanExecuteChangedCommand, IDisposable { + IDisposable _child_can_exe_hooks; + + public CompositeEventCommand(EventCommand cmd1, EventCommand cmd2) { + this.cmd1 = cmd1; + this.cmd2 = cmd2; + _child_can_exe_hooks = _attach_child_can_exe_changed(); + } + + public event Action<(T1, T2)>? executed; + public event EventHandler? CanExecuteChanged; + + public EventCommand cmd1 { get; } + public EventCommand cmd2 { get; } + + public bool CanExecute(object? parameter) => + cmd1.CanExecute(parameter) && cmd2.CanExecute(parameter); + + public void Execute(object? parameter) { + if (!ConvertUtils.try_convert(parameter, out (T1, T2) param_tup)) { + return; + } + cmd1.Execute(param_tup.Item1); + cmd2.Execute(param_tup.Item2); + executed?.Invoke(param_tup); + } + + public void RaiseCanExecuteChanged() { + Disposable.dispose(ref _child_can_exe_hooks); + cmd1.RaiseCanExecuteChanged(); + cmd2.RaiseCanExecuteChanged(); + CanExecuteChanged?.Invoke(this, EventArgs.Empty); + _child_can_exe_hooks = _attach_child_can_exe_changed(); + } + + public void Dispose() => + Disposable.dispose(ref _child_can_exe_hooks); + + IDisposable _attach_child_can_exe_changed() => + _attach_child_can_exe_changed((s, e) => + CanExecuteChanged?.Invoke(this, EventArgs.Empty)); + + IDisposable _attach_child_can_exe_changed(EventHandler callback) { + cmd1.CanExecuteChanged += callback; + cmd2.CanExecuteChanged += callback; + return Disposable.Create(() => { + cmd1.CanExecuteChanged -= callback; + cmd2.CanExecuteChanged -= callback; + }); + } + } +} diff --git a/src/client/spark/ViewModel/Commands/EventCommand.cs b/src/client/spark/ViewModel/Commands/EventCommand.cs new file mode 100644 index 000000000..6d825f165 --- /dev/null +++ b/src/client/spark/ViewModel/Commands/EventCommand.cs @@ -0,0 +1,42 @@ +#nullable enable +namespace mbts.ui { + using System; + + class EventCommand : EventCommand { + public static EventCommand create(Func? can_execute = null) => + new(can_execute); + } + + class EventCommand : IEventCommand { + readonly Func? _can_execute; + + public EventCommand(Func? can_execute = null) => + _can_execute = can_execute; + + public event EventHandler? CanExecuteChanged; + + public event Action? executed; + + public bool CanExecute(object? parameter) => + _can_execute?.Invoke(ConvertUtils.try_convert(parameter)) ?? true; + + public void Execute(object? parameter) => + executed?.Invoke(ConvertUtils.try_convert(parameter)); + + public void RaiseCanExecuteChanged() => + CanExecuteChanged?.Invoke(this, EventArgs.Empty); + } + + static class EventCommandExtensions { + public static IDisposable subscribe_executed(this IEventCommand cmd, Action callback) { + cmd.executed += callback; + return Disposable.Create(() => cmd.executed -= callback); + } + + public static IDisposable subscribe_executed(this IEventCommand cmd, Action callback) { + return cmd.subscribe_executed(on_executed); + void on_executed(T? _) => + callback(); + } + } +} diff --git a/src/client/spark/ViewModel/Commands/IEventCommand.cs b/src/client/spark/ViewModel/Commands/IEventCommand.cs new file mode 100644 index 000000000..24b857b51 --- /dev/null +++ b/src/client/spark/ViewModel/Commands/IEventCommand.cs @@ -0,0 +1,8 @@ +#nullable enable +namespace mbts.ui { + using System; + + interface IEventCommand : IRaiseCanExecuteChangedCommand { + event Action? executed; + } +} diff --git a/src/client/spark/ViewModel/KeyValueItemVM.cs b/src/client/spark/ViewModel/KeyValueItemVM.cs new file mode 100644 index 000000000..e42a448a1 --- /dev/null +++ b/src/client/spark/ViewModel/KeyValueItemVM.cs @@ -0,0 +1,11 @@ +namespace Spark.ViewModel { + class KeyValueItemVM { + public KeyValueItemVM(TKey key, TValue value) { + this.key = key; + this.value = value; + } + + public TKey key { get; } + public TValue value { get; } + } +} diff --git a/src/client/spark/ViewModel/KeyValueLauncherVM.cs b/src/client/spark/ViewModel/KeyValueLauncherVM.cs new file mode 100644 index 000000000..5702f5fbd --- /dev/null +++ b/src/client/spark/ViewModel/KeyValueLauncherVM.cs @@ -0,0 +1,22 @@ +#nullable enable +namespace Spark.ViewModel { + using mbts.ui; + using mbts.ui.shell; + + using System.Collections.ObjectModel; + + class KeyValueLauncherVM : ViewModelBase where TKey : notnull { + TValue? _new_value; + + public TValue? new_value { + get => _new_value; + set => Set(ref _new_value, value); + } + + public ObservableCollection> items { get; } = new(); + + public EventCommand create_cmd { get; } = new(); + public EventCommand launch_cmd { get; } = new(); + public EventCommand delete_cmd { get; } = new(); + } +} diff --git a/src/client/spark/core/Bus/BusExtensions.cs b/src/client/spark/core/Bus/BusExtensions.cs new file mode 100644 index 000000000..ba4c8c94c --- /dev/null +++ b/src/client/spark/core/Bus/BusExtensions.cs @@ -0,0 +1,8 @@ +namespace bts.api { + using System; + + public static class BusExtensions { + public static Action post_callback(this IBus bus, ITag tag) => + param => bus.post(tag, param); + } +}