refactor: Split analytics module/viewmodel into sep classes
This commit is contained in:
1 parent
8c9d8e7089
commit
e255e054d4
9 files changed
+198
-64
No files matched your search
@@ -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<IShellWindowFactory> _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<SparkCommand> 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<AlgoMenuItemVM> 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<SparkCommand> actions => Enumerable.Empty<SparkCommand>();
|
||||
|
||||
public UIElement create_view() =>
|
||||
new AnalyticsShellModuleView { DataContext = this };
|
||||
new AnalyticsShellModuleView { DataContext = vm };
|
||||
|
||||
public Task<IShellWindow> 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<string> _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<int, string> 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<VolSurfaceWindow>().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<VolSurfaceWindow>().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<int, string> id_names) {
|
||||
vm.vol_surface_launcher_vm.items.sync_with(id_names.Select(
|
||||
kv => new KeyValueItemVM<int, string>(kv.Key, kv.Value)));
|
||||
// reset titles for all open vol surface windows
|
||||
foreach (var window in _wm.list_windows().OfType<VolSurfaceWindow>()) {
|
||||
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) ?? "(...)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<int, string> vol_surface_launcher_vm { get; } = new();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
<UserControl
|
||||
<UserControl
|
||||
x:Class="mbts.ui.shell.AnalyticsShellModuleView"
|
||||
x:ClassModifier="internal"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
@@ -137,7 +137,7 @@
|
||||
MinWidth="60"
|
||||
Margin="4"
|
||||
IsDefault="True"
|
||||
Command="{Binding create_new_vol_surface}">Create</Button>
|
||||
Command="{Binding create_vol_surface_cmd}">Create</Button>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#nullable enable
|
||||
namespace mbts.ui {
|
||||
using System;
|
||||
|
||||
class CompositeEventCommand<T1, T2> : IEventCommand<(T1, T2)>, IRaiseCanExecuteChangedCommand, IDisposable {
|
||||
IDisposable _child_can_exe_hooks;
|
||||
|
||||
public CompositeEventCommand(EventCommand<T1> cmd1, EventCommand<T2> 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<T1> cmd1 { get; }
|
||||
public EventCommand<T2> 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#nullable enable
|
||||
namespace mbts.ui {
|
||||
using System;
|
||||
|
||||
class EventCommand : EventCommand<object> {
|
||||
public static EventCommand<T> create<T>(Func<T?, bool>? can_execute = null) =>
|
||||
new(can_execute);
|
||||
}
|
||||
|
||||
class EventCommand<T> : IEventCommand<T> {
|
||||
readonly Func<T?, bool>? _can_execute;
|
||||
|
||||
public EventCommand(Func<T?, bool>? can_execute = null) =>
|
||||
_can_execute = can_execute;
|
||||
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
public event Action<T?>? executed;
|
||||
|
||||
public bool CanExecute(object? parameter) =>
|
||||
_can_execute?.Invoke(ConvertUtils.try_convert<T>(parameter)) ?? true;
|
||||
|
||||
public void Execute(object? parameter) =>
|
||||
executed?.Invoke(ConvertUtils.try_convert<T>(parameter));
|
||||
|
||||
public void RaiseCanExecuteChanged() =>
|
||||
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
static class EventCommandExtensions {
|
||||
public static IDisposable subscribe_executed<T>(this IEventCommand<T> cmd, Action<T?> callback) {
|
||||
cmd.executed += callback;
|
||||
return Disposable.Create(() => cmd.executed -= callback);
|
||||
}
|
||||
|
||||
public static IDisposable subscribe_executed<T>(this IEventCommand<T> cmd, Action callback) {
|
||||
return cmd.subscribe_executed(on_executed);
|
||||
void on_executed(T? _) =>
|
||||
callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#nullable enable
|
||||
namespace mbts.ui {
|
||||
using System;
|
||||
|
||||
interface IEventCommand<T> : IRaiseCanExecuteChangedCommand {
|
||||
event Action<T?>? executed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Spark.ViewModel {
|
||||
class KeyValueItemVM<TKey, TValue> {
|
||||
public KeyValueItemVM(TKey key, TValue value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public TKey key { get; }
|
||||
public TValue value { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#nullable enable
|
||||
namespace Spark.ViewModel {
|
||||
using mbts.ui;
|
||||
using mbts.ui.shell;
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
class KeyValueLauncherVM<TKey, TValue> : ViewModelBase where TKey : notnull {
|
||||
TValue? _new_value;
|
||||
|
||||
public TValue? new_value {
|
||||
get => _new_value;
|
||||
set => Set(ref _new_value, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<KeyValueItemVM<TKey, TValue>> items { get; } = new();
|
||||
|
||||
public EventCommand<TValue> create_cmd { get; } = new();
|
||||
public EventCommand<TKey> launch_cmd { get; } = new();
|
||||
public EventCommand<TKey> delete_cmd { get; } = new();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace bts.api {
|
||||
using System;
|
||||
|
||||
public static class BusExtensions {
|
||||
public static Action<T> post_callback<T>(this IBus bus, ITag<T> tag) =>
|
||||
param => bus.post(tag, param);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user