From 85a48e71d1f46ab59e72b7103cfb4868dfb03037 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 23 Apr 2022 01:55:08 -0400 Subject: [PATCH] refactor: Move vs row context menu vm to own file & unnest class --- .../ViewModel/Grid/VolSurfaceRowContext.cs | 192 +---------------- .../Grid/VolSurfaceRowContextMenuVM.cs | 204 ++++++++++++++++++ 2 files changed, 206 insertions(+), 190 deletions(-) create mode 100644 src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs diff --git a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContext.cs b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContext.cs index f7a9a6c72..9fc750d6c 100644 --- a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContext.cs +++ b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContext.cs @@ -2,14 +2,11 @@ namespace mbts.spark.VolSurface { using mbts.ui; using mbts.ui.controls; using mbts.ui.shell; - using mbts.ui.utility; using System; using System.Collections.Generic; - using System.Collections.ObjectModel; using System.Linq; using System.Reactive.Linq; - using System.Windows.Input; using System.Windows.Media; public struct VolSurfaceCellState { @@ -27,7 +24,7 @@ namespace mbts.spark.VolSurface { /// (). readonly Dictionary _cells = new(); - MenuContext _context_menu; + VolSurfaceRowContextMenuVM _context_menu; bool _is_selected; public VolSurfaceRowContext(VolSurfaceGridVM parent, VolSurfaceDisplayGridExpiration exp) { @@ -55,7 +52,7 @@ namespace mbts.spark.VolSurface { } } - public MenuContext context_menu => + public VolSurfaceRowContextMenuVM context_menu => _context_menu ??= new(_parent, this); public VolGridCellVM get_cell(VolSurfaceDisplayGridColumn column) { @@ -130,191 +127,6 @@ namespace mbts.spark.VolSurface { cell.candidate_value = _parent.current_grid.read(_exp, cell.column_id); } - public class MenuContext : ViewModelBase { - readonly VolSurfaceRowContext _row; - readonly VolSurfaceGridVM _parent; - - VolSurfaceColumn _col; - DIYGridSuperHeaderVM _supercol; - - ICommand _revert_cmd; - ICommand _shift_selection_cmd; - ICommand _shift_masters_cmd; - ICommand _shift_all_cmd; - ICommand _clear_cmd; - ICommand _copy_to_selection_cmd; - ICommand _copy_to_masters_cmd; - ICommand _copy_to_all_cmd; - ICommand _copy_all_to_clipboard_cmd; - ICommand _smart_copy_rolls_rates_cmd; - ICommand _import_csv_cmd; - - public MenuContext( - VolSurfaceGridVM parent, - VolSurfaceRowContext row) { - _parent = parent; - _row = row; - } - - public string column_name => _col.model.display_name; - public string group_name => _supercol.id; - public string expiration_name => _row.ToString(); - public VolSurfaceColumn column { - get => _col; - set => Set(ref _col, value); - } - public DIYGridSuperHeaderVM super_header { - get => _supercol; - set => Set(ref _supercol, value); - } - public VolSurfaceDisplayGridExpiration expiration => _row.expiration; - - public class SymbolFilter { - readonly MenuContext _context; - bool? _visible; - - public SymbolFilter(MenuContext context, string symbol, int count, int visible_count) { - _context = context; - this.symbol = symbol; - if (count == visible_count) { - _visible = true; - } else if (visible_count == 0) { - _visible = false; - } else { - _visible = null; - } - } - - public string symbol { get; private set; } - public bool? visible { - get => _visible; - set { - _visible = value; - if (value == true) { - _context._show_symbol(symbol); - } else if (value == false) { - _context._hide_symbol(symbol); - } - } - } - } - - public IEnumerable symbols { - get { - var visible_exps = _parent.visible_rows - .Cast() - .GroupBy(r => r._exp.symbol) - .ToDictionary(g => g.Key); - var result = _parent.rows - .GroupBy(r => r._exp.symbol) - .OrderBy(g => g.Key) - .Select(g => new SymbolFilter(this, g.Key, g.Count(), visible_exps.ContainsKey(g.Key) ? visible_exps[g.Key].Count() : 0)); - return result; - } - } - - public bool is_input_cell => _col.type == VolSurfaceColumnType.ReadWriteTick; - - public ICommand main_settings_cmd => _parent.main_settings_cmd; - public ICommand advanced_settings_cmd => _parent.advanced_settings_cmd; - public ICommand grid_settings_cmd => _parent.grid_settings_cmd; - public ICommand graph_settings_cmd => _parent.graph_settings_cmd; - - public ICommand revert_cmd => - _revert_cmd ??= new DelegateCommand(_ => _row.get_cell(_col.model)?.revert()); - - public ICommand shift_selection_cmd => - _shift_selection_cmd ??= new DelegateCommand(() => { - if (_col.model.type == VolSurfaceColumnType.ReadWriteTick) { - _parent.shift_selection(_col.model); - } - }, () => _col.model.type == VolSurfaceColumnType.ReadWriteTick); - - public ICommand shift_masters_cmd => - _shift_masters_cmd ??= new DelegateCommand(() => { - if (_col.model.type == VolSurfaceColumnType.ReadWriteTick) { - _parent.shift_masters(_col.model); - } - }, () => _col.model.type == VolSurfaceColumnType.ReadWriteTick); - - public ICommand shift_all_cmd => - _shift_all_cmd ??= new DelegateCommand(() => { - if (_col.model.type == VolSurfaceColumnType.ReadWriteTick) { - _parent.shift_all(_col.model); - } - }, () => _col.model.type == VolSurfaceColumnType.ReadWriteTick); - - public ICommand clear_cmd => - _clear_cmd ??= new DelegateCommand(where => { - if (Enum.TryParse(where, out GridColumnSet gc)) { - _parent.clear(_col.model, gc); - } - }, - _has_editable_cells); - - public ICommand copy_to_selection_cmd => - _copy_to_selection_cmd ??= new DelegateCommand(where => { - if (Enum.TryParse(where, out GridColumnSet gc)) { - _parent.copy_to_selection(expiration, _col.model, gc); - } - }, - _has_editable_cells); - - public ICommand copy_to_masters_cmd => - _copy_to_masters_cmd ??= new DelegateCommand(where => { - if (Enum.TryParse(where, out GridColumnSet gc)) { - _parent.copy_to_masters(expiration, _col.model, gc); - } - }, - _has_editable_cells); - - public ICommand copy_to_all_cmd => - _copy_to_all_cmd ??= new DelegateCommand(where => { - if (Enum.TryParse(where, out GridColumnSet gc)) { - _parent.copy_to_all(expiration, _col.model, gc); - } - }, - _has_editable_cells); - - public ICommand copy_all_to_clipboard_cmd => - _copy_all_to_clipboard_cmd ??= new DelegateCommand(() => { - string clipboard_text = ClipboardHelper.GetExcelFormattedText( - _parent.visible_rows - .OfType() - .Select(r => r.get_cell_clipboard_strings()) - .Where(cells => cells != null)); - if (!clipboard_text.is_null_or_empty()) { - ClipboardHelper.CopyTextToClipboard(clipboard_text); - } - }); - - public ICommand smart_copy_rolls_rates_cmd => - _smart_copy_rolls_rates_cmd ??= new DelegateCommand(_ => _parent.model.sync_slave_ref_forwards(expiration.ToVSExp())); - - public ICommand import_csv_cmd => - _import_csv_cmd ??= FileDialogCommand.open_file( - _parent.model.import_csv_file, - () => _parent.model.csv_importing_enabled(), - null); - - bool _has_editable_cells(string where) => - !Enum.TryParse(where, out GridColumnSet gc) || - gc == GridColumnSet.Column || _col.model.type != VolSurfaceColumnType.ReadOnly; - - void _hide_symbol(string symbol) { - _parent.visible_rows.RemoveAll(s => ((VolSurfaceRowContext)s).expiration.symbol == symbol); - _parent.signal_selection_changed(); - } - - void _show_symbol(string symbol) { - // Insert items from rows to visible_Rows that have symbol=true in sorted order - // do this by making the desired collection and then leveraging sync_with - var visible = new HashSet(_parent.visible_rows.Cast().Select(row => row.expiration)); - var desired = _parent.rows.Where(row => row.expiration.symbol == symbol || visible.Contains(row.expiration)); - _parent.visible_rows.sync_with(desired); - } - } - class CellData : IDisposable { IDisposable _dispose; VolGridCellVM _vm; diff --git a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs new file mode 100644 index 000000000..3460fb992 --- /dev/null +++ b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs @@ -0,0 +1,204 @@ +namespace mbts.spark { + using bts.collections; + + using mbts.spark.VolSurface; + using mbts.ui; + using mbts.ui.controls; + using mbts.ui.shell; + using mbts.ui.utility; + + using System; + using System.Collections.Generic; + using System.Collections.ObjectModel; + using System.Linq; + using System.Windows.Input; + + class VolSurfaceRowContextMenuVM : ViewModelBase { + readonly VolSurfaceRowContext _row; + readonly VolSurfaceGridVM _parent; + + VolSurfaceColumn _col; + DIYGridSuperHeaderVM _supercol; + + ICommand _revert_cmd; + ICommand _shift_selection_cmd; + ICommand _shift_masters_cmd; + ICommand _shift_all_cmd; + ICommand _clear_cmd; + ICommand _copy_to_selection_cmd; + ICommand _copy_to_masters_cmd; + ICommand _copy_to_all_cmd; + ICommand _copy_all_to_clipboard_cmd; + ICommand _smart_copy_rolls_rates_cmd; + ICommand _import_csv_cmd; + + public VolSurfaceRowContextMenuVM(VolSurfaceGridVM parent, VolSurfaceRowContext row) { + _parent = parent; + _row = row; + } + + public string column_name => _col.model.display_name; + public string group_name => _supercol.id; + public string expiration_name => _row.ToString(); + public VolSurfaceColumn column { + get => _col; + set => Set(ref _col, value); + } + public DIYGridSuperHeaderVM super_header { + get => _supercol; + set => Set(ref _supercol, value); + } + public VolSurfaceDisplayGridExpiration expiration => _row.expiration; + + public IEnumerable symbols { + get { + var visible_exps = _parent.visible_rows + .GroupBy(r => r.expiration.symbol) + .ToDictionary(g => g.Key); + var result = _parent.rows + .GroupBy(r => r.expiration.symbol) + .OrderBy(g => g.Key) + .Select(g => new SymbolFilter( + this, g.Key, g.Count(), visible_exps.ContainsKey(g.Key) ? visible_exps[g.Key].Count() : 0)); + return result; + } + } + + + public bool is_input_cell => _col.type == VolSurfaceColumnType.ReadWriteTick; + + public ICommand main_settings_cmd => _parent.main_settings_cmd; + public ICommand advanced_settings_cmd => _parent.advanced_settings_cmd; + public ICommand grid_settings_cmd => _parent.grid_settings_cmd; + public ICommand graph_settings_cmd => _parent.graph_settings_cmd; + + public ICommand revert_cmd => + _revert_cmd ??= new Command(_ => _row.get_cell(_col.model)?.revert()); + + public ICommand shift_selection_cmd => + _shift_selection_cmd ??= new Command(() => { + if (_col.model.type == VolSurfaceColumnType.ReadWriteTick) { + _parent.shift_selection(_col.model); + } + }, () => _col.model.type == VolSurfaceColumnType.ReadWriteTick); + + public ICommand shift_masters_cmd => + _shift_masters_cmd ??= new Command(() => { + if (_col.model.type == VolSurfaceColumnType.ReadWriteTick) { + _parent.shift_masters(_col.model); + } + }, () => _col.model.type == VolSurfaceColumnType.ReadWriteTick); + + public ICommand shift_all_cmd => + _shift_all_cmd ??= new Command(() => { + if (_col.model.type == VolSurfaceColumnType.ReadWriteTick) { + _parent.shift_all(_col.model); + } + }, () => _col.model.type == VolSurfaceColumnType.ReadWriteTick); + + public ICommand clear_cmd => + _clear_cmd ??= new Command(where => { + if (Enum.TryParse(where, out GridColumnSet gc)) { + _parent.clear(_col.model, gc); + } + }, + _has_editable_cells); + + public ICommand copy_to_selection_cmd => + _copy_to_selection_cmd ??= new Command(where => { + if (Enum.TryParse(where, out GridColumnSet gc)) { + _parent.copy_to_selection(expiration, _col.model, gc); + } + }, + _has_editable_cells); + + public ICommand copy_to_masters_cmd => + _copy_to_masters_cmd ??= new Command(where => { + if (Enum.TryParse(where, out GridColumnSet gc)) { + _parent.copy_to_masters(expiration, _col.model, gc); + } + }, + _has_editable_cells); + + public ICommand copy_to_all_cmd => + _copy_to_all_cmd ??= new Command(where => { + if (Enum.TryParse(where, out GridColumnSet gc)) { + _parent.copy_to_all(expiration, _col.model, gc); + } + }, + _has_editable_cells); + + public ICommand copy_all_to_clipboard_cmd => + _copy_all_to_clipboard_cmd ??= new Command(() => { + string clipboard_text = ClipboardHelper.GetExcelFormattedText( + _parent.visible_rows + .OfType() + .Select(r => r.get_cell_clipboard_strings()) + .Where(cells => cells != null)); + if (!clipboard_text.is_null_or_empty()) { + ClipboardHelper.CopyTextToClipboard(clipboard_text); + } + }); + + public ICommand smart_copy_rolls_rates_cmd => + _smart_copy_rolls_rates_cmd ??= new Command( + () => _parent.model.sync_slave_ref_forwards(expiration.ToVSExp())); + + public ICommand import_csv_cmd => + _import_csv_cmd ??= FileDialogCommand.open_file( + _parent.model.import_csv_file, + _parent.model.csv_importing_enabled, + null); + + bool _has_editable_cells(string where) => + !Enum.TryParse(where, out GridColumnSet gc) || + gc == GridColumnSet.Column || _col.model.type != VolSurfaceColumnType.ReadOnly; + + void _hide_symbol(string symbol) { + _parent.visible_rows.RemoveAll(s => s.expiration.symbol == symbol); + _parent.signal_selection_changed(); + } + + void _show_symbol(string symbol) { + // Insert items from rows to visible_Rows that have symbol=true in sorted order + // do this by making the desired collection and then leveraging sync_with + var visible = _parent.visible_rows + .Select(row => row.expiration) + .ToHashSet(); + var desired = _parent.rows.Where( + row => row.expiration.symbol == symbol || visible.Contains(row.expiration)); + _parent.visible_rows.sync_with(desired); + } + + public class SymbolFilter { + readonly VolSurfaceRowContextMenuVM _context; + bool? _visible; + + public SymbolFilter(VolSurfaceRowContextMenuVM context, string symbol, int count, int visible_count) { + _context = context; + this.symbol = symbol; + if (count == visible_count) { + _visible = true; + } else if (visible_count == 0) { + _visible = false; + } else { + _visible = null; + } + } + + public string symbol { get; private set; } + public bool? visible { + get => _visible; + set { + _visible = value; + if (value == true) { + _context._show_symbol(symbol); + } else if (value == false) { + _context._hide_symbol(symbol); + } + } + } + } + + } +}