AlgoStudio - AlgoStudioViewController is responsible for reselecting newly persisted items
This commit is contained in:
1 parent
6b1e7bfee6
commit
76ffa8a61d
6 files changed
+63
-113
No files matched your search
@@ -20,18 +20,19 @@ namespace mbts.spark {
|
||||
|
||||
event Action<SecurityMD?> _security_md_changed;
|
||||
|
||||
public void update(TableUpdate<CompositeAlgoRecordImmutable, uint> templates) {
|
||||
_templates = templates;
|
||||
foreach (var record in templates.removed)
|
||||
Remove(new AlgoVMKey(record.Key));
|
||||
foreach (var record in templates.changed)
|
||||
this[new AlgoVMKey(record.Key)].load_record(record);
|
||||
foreach (var record in templates.added) {
|
||||
/// When creating new definitions, AlgoStudioVM adds a placeholder template before the user commits it. When the user commits the new definition, AlgoStudioVM adds the persisted record
|
||||
if (!ContainsKey(new AlgoVMKey(record.Key)))
|
||||
Add(AlgoDefinitionVM.create_persisted(_model, record, _security_md_source, true));
|
||||
}
|
||||
}
|
||||
public IDisposable connect_templates(IObservable<TableUpdate<CompositeAlgoRecordImmutable, uint>> source) =>
|
||||
source.Subscribe(templates => {
|
||||
_templates = templates;
|
||||
foreach (var record in templates.removed)
|
||||
Remove(new AlgoVMKey(record.Key));
|
||||
foreach (var record in templates.changed)
|
||||
this[new AlgoVMKey(record.Key)].load_record(record);
|
||||
foreach (var record in templates.added) {
|
||||
// When creating new definitions, AlgoStudioVM adds a placeholder template before the user commits it. When the user commits the new definition, AlgoStudioVM adds the persisted record
|
||||
if (!ContainsKey(new AlgoVMKey(record.Key)))
|
||||
Add(AlgoDefinitionVM.create_persisted(_model, record, _security_md_source, true));
|
||||
}
|
||||
});
|
||||
|
||||
public void on_security(SecurityMD security_md) {
|
||||
var new_factory_defs = _model.factory.default_order_type_templates(security_md.security);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using bts.collections;
|
||||
using mbts.spark;
|
||||
using mbts.ui.controls;
|
||||
using System;
|
||||
@@ -42,10 +43,33 @@ namespace mbts.ui.spark {
|
||||
class Controller : IAlgoStudioViewController {
|
||||
readonly AlgoStudioVM _vm;
|
||||
readonly TreeView _tree_view;
|
||||
/// During a save op, unpersisted items may be replaced with persisted ones.
|
||||
/// Consequently, the the selection state will be lost if an unpersisted item was selected.
|
||||
/// When a save op begins the selected indices are stored in the field below, so that when it ends, the new persisted items can be reselected
|
||||
/// Note: <see cref="_vm"/> is reponsible for inserting the new items in the proper index.
|
||||
(int? selected_def_index, int? selected_tmp_index)? _active_save_op;
|
||||
|
||||
public Controller(AlgoStudioVM vm, TreeView tree_view) {
|
||||
_vm = vm;
|
||||
_tree_view = tree_view;
|
||||
_vm.save_op_started += _vm_save_op_started;
|
||||
_vm.save_op_ended += _vm_save_op_ended;
|
||||
}
|
||||
|
||||
void _vm_save_op_started() =>
|
||||
_active_save_op = AlgoPersistenceHelper.selection_snapshot(_vm.definitions, _vm.selected_definition, _vm.selected_template);
|
||||
|
||||
void _vm_save_op_ended() {
|
||||
if (_active_save_op.HasValue) {
|
||||
(int? def_index, int? tmp_index) = _active_save_op.Value;
|
||||
if (def_index is int di && _vm.definitions.check_index(di)) {
|
||||
var new_def = _vm.definitions[di];
|
||||
if (tmp_index is int ti && new_def.templates.check_index(ti)) {
|
||||
select_template(new_def.templates[ti]);
|
||||
} else select_definition(new_def);
|
||||
}
|
||||
_active_save_op = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void add_definition() {
|
||||
|
||||
@@ -139,16 +139,6 @@ namespace mbts.spark {
|
||||
});
|
||||
_commands.Add(remove_spark_cmd);
|
||||
_resources.Add(content_src.OnDispatcher().Subscribe(remove_spark_cmd));
|
||||
// selection event hooks
|
||||
_vm.request_view_select_definition += select_definition;
|
||||
_vm.request_view_select_template += select_template;
|
||||
_resources.Add(() => {
|
||||
_vm.request_view_select_definition -= select_definition;
|
||||
_vm.request_view_select_template -= select_template;
|
||||
});
|
||||
// local methods
|
||||
void select_definition(AlgoDefinitionVM d) => _view.Dispatcher.CheckInvokeAsync(() => _view.select_definition(d)).ignore();
|
||||
void select_template(AlgoDefinitionTemplateVM t) => _view.Dispatcher.CheckInvokeAsync(() => _view.select_template(t)).ignore();
|
||||
}
|
||||
|
||||
void _dispose_core() {
|
||||
|
||||
@@ -55,8 +55,8 @@ namespace mbts.spark {
|
||||
_revert_cmd_tooltip = backing(nameof(revert_cmd_tooltip), get_revert_tooltip);
|
||||
//
|
||||
_dispose = new CompositeDisposable(
|
||||
_model.algo_model.algos.OnDispatcher().Subscribe(_definitions_update_buffer),
|
||||
_definitions_update_buffer.Subscribe(_definitions.update),
|
||||
_model.algo_model.algos.Subscribe(_definitions_update_buffer),
|
||||
_definitions.connect_templates(_definitions_update_buffer.OnDispatcher()),
|
||||
_model.algo_model.algo_metadata.OnDispatcher().Subscribe(meta => {
|
||||
_persisted_meta = meta;
|
||||
definitions_view.Refresh();
|
||||
@@ -120,8 +120,7 @@ namespace mbts.spark {
|
||||
}
|
||||
}
|
||||
|
||||
public event Action<AlgoDefinitionVM> request_view_select_definition;
|
||||
public event Action<AlgoDefinitionTemplateVM> request_view_select_template;
|
||||
public event Action save_op_started, save_op_ended;
|
||||
event Action<(AlgoDefinitionVM definition, AlgoDefinitionTemplateVM template)> _selected_changed;
|
||||
|
||||
public ObservableCollection<AlgoDefinitionVM> definitions => _definitions;
|
||||
@@ -174,98 +173,28 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
public async Task save_definition(AlgoDefinitionVM definition) {
|
||||
int def_index = definitions.IndexOf(definition);
|
||||
await _save(definitions.WrapSubRange(def_index, 1).WrapSelector(def => (def, definition.templates.AsList()), t => t.def));
|
||||
using (_save_op())
|
||||
await AlgoPersistenceHelper.persist_definition(_model, _definitions, definition);
|
||||
}
|
||||
|
||||
public async Task save_template(AlgoDefinitionTemplateVM template) {
|
||||
int def_index = definitions.IndexOf(template.owner);
|
||||
int tmp_index = template.owner.templates.IndexOf(template);
|
||||
await _save(definitions.WrapSubRange(def_index, 1).WrapSelector(def => (def, def.templates.WrapSubRange(tmp_index, 1)), t => t.def));
|
||||
using (_save_op())
|
||||
await AlgoPersistenceHelper.persist_template(_model, _definitions, template);
|
||||
}
|
||||
|
||||
public async Task save_all() =>
|
||||
await _save(definitions.WrapSelector(def => (def, def.templates.AsList()), t => t.def));
|
||||
|
||||
/// The persistence code here is far more complicated than one would expect.
|
||||
/// The trickey part is handling unpersisted placeholder items, namely converting them into persisted items when they are saved for the first time.
|
||||
/// Ideally, an unpersisted item could simply be modified to hold the new persisted id.
|
||||
/// Unfortunately this is a flawed approach, because the persisted id is used by the equality/hashing logic, so it has to be readonly.
|
||||
/// The solution employed here is to remove the unpersisted item, and replace it with a new persisted one, without breaking the item order, or the current item selection.
|
||||
/// The problem is made more complicated by the fact that the view is a TreeView, which deals with nested collections instead of a single flat collection like other ItemsControls.
|
||||
/// And if all that isn't enough to call this a pain in the ass, TreeView doesn't have a bindable SelectedItemProperty.
|
||||
/// For further implementation details items:
|
||||
/// <see cref="AlgoDefinitionVM.vm_key"/> <see cref="AlgoDefinitionVM.GetHashCode"/>, <see cref="AlgoDefinitionVM.Equals(AlgoDefinitionVM)"/>
|
||||
/// <see cref="AlgoDefinitionTemplateVM.vm_key"/> <see cref="AlgoDefinitionTemplateVM.GetHashCode"/>, <see cref="AlgoDefinitionTemplateVM.Equals(AlgoDefinitionTemplateVM)"/>.
|
||||
/// <see cref="AlgoVMKey"/>
|
||||
async Task _save(IList<(AlgoDefinitionVM definition, IList<AlgoDefinitionTemplateVM> templates)> definition_templates) {
|
||||
var old_selected_def = selected_definition;
|
||||
var old_selected_tmp = selected_template;
|
||||
/// If the selected definition has not been persisted yet, it will have a 'placeholder vm_key' (<see cref="AlgoDefinitionVM.vm_key"/>, <see cref="AlgoVMKey"/>).
|
||||
/// Each 'placeholder' <see cref="AlgoDefinitionVM"/> must be replaced with a new one with a 'persisted vm_key,' since the vm_key is readonly and used by <see cref="AlgoDefinitionVM.GetHashCode"/>.
|
||||
/// The same 'placeholder vm_key' logic applies to templates as well (<see cref="AlgoDefinitionTemplateVM.vm_key"/>).
|
||||
AlgoDefinitionVM reselect_def = null;
|
||||
AlgoDefinitionTemplateVM reselect_tmp = null;
|
||||
using (_definitions_update_buffer.buffer_updates()) {
|
||||
for (int i = 0; i < definition_templates.Count; i++) {
|
||||
(var def, var templates) = definition_templates[i];
|
||||
var persisted_def = def;
|
||||
if (def.is_dirty) {
|
||||
var def_record = await _model.algo_model.add_or_update(def.create_record());
|
||||
if (def_record.HasValue) {
|
||||
switch (def.vm_key.mode) {
|
||||
case AlgoVMKey.Mode.CustomPlacholder:
|
||||
persisted_def = def.to_persisted(def_record.Value);
|
||||
if (def == old_selected_def)
|
||||
reselect_def = persisted_def;
|
||||
foreach (var tmp in templates) {
|
||||
AlgoDefinitionTemplateVM persisted_tmp;
|
||||
var tmp_record = await _model.algo_model.add_or_update(tmp.create(def_record.Value));
|
||||
if (tmp_record.HasValue) {
|
||||
persisted_def.templates.Add(persisted_tmp = tmp.to_persisted(persisted_def, tmp_record.Value));
|
||||
} else {
|
||||
// if the save-op fails, the unpersisted template is cloned and added to persisted def's template items, so the user may try to save it again without having to re-enter all the templated-values.
|
||||
persisted_def.templates.Add(persisted_tmp = new AlgoDefinitionTemplateVM(persisted_def, _model, tmp.create()));
|
||||
}
|
||||
if (tmp == old_selected_tmp)
|
||||
reselect_tmp = persisted_tmp;
|
||||
}
|
||||
persisted_def.connect();
|
||||
definition_templates[i] = (persisted_def, persisted_def.templates);
|
||||
break;
|
||||
case AlgoVMKey.Mode.Persisted:
|
||||
// TODO: implement modification logic when backend is ready
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// so the def isn't dirty, but it still might have dirty templates
|
||||
using (persisted_def.buffer_updates()) {
|
||||
for (int j = 0; j < templates.Count; j++) {
|
||||
var tmp = templates[j];
|
||||
if (!tmp.is_dirty)
|
||||
continue;
|
||||
var tmp_record = await _model.algo_model.add_or_update(tmp.create());
|
||||
if (tmp_record.HasValue) {
|
||||
switch (tmp.vm_key.mode) {
|
||||
case AlgoVMKey.Mode.CustomPlacholder:
|
||||
var persisted_tmp = tmp.to_persisted(persisted_def, tmp_record.Value);
|
||||
if (tmp == old_selected_tmp)
|
||||
reselect_tmp = persisted_tmp;
|
||||
templates[j] = persisted_tmp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (reselect_tmp != null) {
|
||||
request_view_select_template?.Invoke(reselect_tmp);
|
||||
} else if (reselect_def != null) {
|
||||
request_view_select_definition?.Invoke(reselect_def);
|
||||
}
|
||||
using (_save_op())
|
||||
await AlgoPersistenceHelper.persist(_model, definition_templates);
|
||||
}
|
||||
|
||||
IDisposable _save_op() {
|
||||
save_op_started?.Invoke();
|
||||
return new CompositeDisposable(_definitions_update_buffer.buffer_updates()) {
|
||||
() => save_op_ended?.Invoke()
|
||||
};
|
||||
}
|
||||
|
||||
/// Despite the existence of these properties: <see cref="AlgoDefinitionVM.is_selected"/>, and <see cref="AlgoDefinitionTemplateVM.is_selected"/>,
|
||||
|
||||
@@ -128,10 +128,7 @@ namespace mbts.spark {
|
||||
using (_selected_algo_definition.begin_update())
|
||||
_algo_definitions.on_security(s);
|
||||
}),
|
||||
model.algo_model.algos.OnDispatcher().Subscribe(d => {
|
||||
using (_selected_algo_definition.begin_update())
|
||||
_algo_definitions.update(d);
|
||||
}),
|
||||
_algo_definitions.connect_templates(model.algo_model.algos.OnDispatcher()),
|
||||
model.algo_model.algo_metadata.OnDispatcher().Subscribe(_on_template_metadata),
|
||||
CollectionHelper.dispose_on_removed(_algo_definitions)) {
|
||||
_algo_definitions.Clear
|
||||
|
||||
@@ -85,4 +85,13 @@ namespace System.Collections.Generic {
|
||||
public static bool TryPopValueIf<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue, bool> should_pop) =>
|
||||
dictionary.TryGetValue(key, out TValue result) && should_pop(result) && dictionary.Remove(key);
|
||||
}
|
||||
|
||||
public static class DictionaryHelper {
|
||||
public static Dictionary<TKey, TValue> empty<TKey, TValue>() =>
|
||||
Empty<TKey, TValue>.value;
|
||||
|
||||
static class Empty<TKey, TValue> {
|
||||
public static readonly Dictionary<TKey, TValue> value = new Dictionary<TKey, TValue>(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user