AlgoStudio - factor out some common code, remove unused

This commit is contained in:
John Colvin committed 2019-01-02 17:35:09 -06:00
1 parent 280ea56245
commit 233b6ef1e6
2 files changed
+70 -128

No files matched your search

@@ -337,7 +337,7 @@
<c:DialogSkirt Grid.Row="2" Grid.ColumnSpan="2" Margin="{StaticResource Margin.TP2}" Background="Transparent">
<c:DialogSkirt.RightItems>
<x:ArrayExtension Type="{x:Type Button}">
<Button ToolTipService.ShowOnDisabled="True" Command="{Binding commit_cmd}" CommandParameter="{Binding content_source}" Width="100" Style="{StaticResource {x:Static c:DialogSkirt.DialogSkirtButtonStyleKey}}" ToolTip="{Binding commit_cmd_tooltip}" IsDefault="True">
<Button ToolTipService.ShowOnDisabled="True" Command="{Binding save_cmd}" CommandParameter="{Binding content_source}" Width="100" Style="{StaticResource {x:Static c:DialogSkirt.DialogSkirtButtonStyleKey}}" ToolTip="{Binding save_cmd_tooltip}" IsDefault="True">
<TextBlock Text="{Binding content_source.name, StringFormat='{}Save {0}'}"/>
</Button>
<Button ToolTipService.ShowOnDisabled="True" Command="{Binding revert_cmd}" CommandParameter="{Binding content_source}" Width="100" Style="{StaticResource {x:Static c:DialogSkirt.DialogSkirtButtonStyleKey}}" ToolTip="{Binding revert_cmd_tooltip}">
@@ -1,5 +1,4 @@
using bts;
using bts.api.core;
using bts.api.orderman;
using bts.collections;
using mbts.spark.utilities;
@@ -11,7 +10,6 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
@@ -32,7 +30,7 @@ namespace mbts.spark {
readonly Property<AlgoDefinitionVM> _selected_definition;
readonly Property<AlgoDefinitionTemplateVM> _selected_template;
readonly Property<IOrderTypeVM> _editable_content;
readonly DelegateProperty<string> _commit_cmd_tooltip, _revert_cmd_tooltip;
readonly DelegateProperty<string> _save_cmd_tooltip, _revert_cmd_tooltip;
readonly AlgoDefinitionCollection _definitions;
readonly TableUpdateBuffer<CompositeAlgoRecordImmutable, uint> _definitions_update_buffer;
readonly ReactiveThresholdMonitor<AlgoDefinitionVM> _any_dirty_monitor = new ReactiveThresholdMonitor<AlgoDefinitionVM>(d => d.subscribe_is_definition_or_any_templates_dirty());
@@ -47,10 +45,66 @@ namespace mbts.spark {
_definitions_update_buffer = new TableUpdateBuffer<CompositeAlgoRecordImmutable, uint>(r => r.Key);
definitions_view = new ListCollectionView<AlgoDefinitionVM>(definitions);
definitions_view.Filter = d => !(d.vm_key.mode == AlgoVMKey.Mode.Persisted && _persisted_meta.TryGetValue(d.vm_key.persisted_id, out AlgoDefinitionMetadata meta) && meta.is_hidden);
/// Commit (<see cref="commit_cmd"/>, <see cref="commit_cmd_tooltip"/>)
var commit_cmd = new ObservantAsyncCommand<IAlgoStudioContentVM>(commit_async);
this.commit_cmd = commit_cmd;
_commit_cmd_tooltip = backing(nameof(commit_cmd_tooltip), () => {
// save
var save_cmd = new ObservantAsyncCommand<IAlgoStudioContentVM>(save_async);
this.save_cmd = save_cmd;
_save_cmd_tooltip = backing(nameof(save_cmd_tooltip), get_save_tooltip);
// revert
var revert_cmd = new ObservantCommand<IAlgoStudioContentVM>(revert);
this.revert_cmd = revert_cmd;
_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.algo_metadata.OnDispatcher().Subscribe(meta => {
_persisted_meta = meta;
definitions_view.Refresh();
}),
observe_selected().SelectManyInSeries(t => t.template?.observe_is_dirty() ?? t.definition.subscribe_is_definition_or_any_templates_dirty() ?? Observable.Return(false)).RefCount().OnDispatcher()
.Subscribe(new CompositeObserver<bool>(save_cmd, revert_cmd) { val => {
_save_cmd_tooltip.refresh();
_revert_cmd_tooltip.refresh();
} }),
ObservableAutoWrapper.connect_item_hooks(definitions, d => new CompositeDisposable(_any_dirty_monitor.join(d), d)));
// local methods
void revert(IAlgoStudioContentVM content) {
if (content is AlgoDefinitionTemplateVM tmp) {
revert_template(tmp);
} else if (content is AlgoDefinitionVM def) {
switch (def.vm_key.mode) {
case AlgoVMKey.Mode.Persisted:
foreach (var t in def.templates)
revert_template(t);
break;
}
}
}
void revert_template(AlgoDefinitionTemplateVM tmp) {
switch (tmp.vm_key.mode) {
case AlgoVMKey.Mode.Persisted: tmp.load_committed(); break;
}
}
string get_revert_tooltip() {
if (content_source is AlgoDefinitionTemplateVM tmp) {
switch (tmp.vm_key.mode) {
case AlgoVMKey.Mode.Persisted: return tmp.is_dirty ? $"Revert changes to '{tmp.name}'" : "No changes to revert";
}
} else if (content_source is AlgoDefinitionVM def) {
switch (def.vm_key.mode) {
case AlgoVMKey.Mode.Persisted: return def.is_dirty || def.templates.Any(t => t.is_dirty) ? $"Revert changes to '{def.name}' templates" : "No changes to revert";
}
}
return null;
}
async Task save_async(IAlgoStudioContentVM content) {
if (content is AlgoDefinitionTemplateVM tmp) {
await save_template(tmp);
} else if (content is AlgoDefinitionVM def) {
await save_definition(def);
}
}
string get_save_tooltip() {
if (content_source is AlgoDefinitionTemplateVM tmp) {
switch (tmp.vm_key.mode) {
case AlgoVMKey.Mode.CustomPlacholder: return tmp.is_dirty ? $"Save '{tmp.name}'" : $"Cannot Save '{tmp.name}'";
@@ -63,61 +117,6 @@ namespace mbts.spark {
}
}
return null;
});
/// Revert (<see cref="revert_cmd"/>, <see cref="revert_cmd_tooltip"/>)
var revert_cmd = new ObservantCommand<IAlgoStudioContentVM>(content => {
if (content is AlgoDefinitionTemplateVM tmp) {
revert(tmp);
} else if (content is AlgoDefinitionVM def) {
switch (def.vm_key.mode) {
case AlgoVMKey.Mode.Persisted:
foreach (var t in def.templates)
revert(t);
break;
}
}
});
this.revert_cmd = revert_cmd;
_revert_cmd_tooltip = backing(nameof(revert_cmd_tooltip), () => {
if (content_source is AlgoDefinitionTemplateVM tmp) {
switch (tmp.vm_key.mode) {
case AlgoVMKey.Mode.Persisted: return tmp.is_dirty ? $"Revert changes to '{tmp.name}'" : "No changes to revert";
}
} else if (content_source is AlgoDefinitionVM def) {
switch (def.vm_key.mode) {
case AlgoVMKey.Mode.Persisted: return def.is_dirty || def.templates.Any(t => t.is_dirty) ? $"Revert changes to '{def.name}' templates" : "No changes to revert";
}
}
return null;
});
//
_dispose = new CompositeDisposable(
_model.algo_model.algos.OnDispatcher().Subscribe(_definitions_update_buffer),
_definitions_update_buffer.Subscribe(_definitions.update),
_model.algo_model.algo_metadata.OnDispatcher().Subscribe(meta => {
_persisted_meta = meta;
definitions_view.Refresh();
}),
observe_selected().SelectManyInSeries(t => t.template?.observe_is_dirty() ?? t.definition.subscribe_is_definition_or_any_templates_dirty() ?? Observable.Return(false)).RefCount().OnDispatcher()
.Subscribe(new CompositeObserver<bool>(commit_cmd, revert_cmd) { val => {
_commit_cmd_tooltip.refresh();
_revert_cmd_tooltip.refresh();
} }),
ObservableAutoWrapper.connect_item_hooks(definitions, d => new CompositeDisposable(_any_dirty_monitor.join(d), d)));
async Task commit_async(IAlgoStudioContentVM content) {
if (content is AlgoDefinitionTemplateVM tmp) {
int def_index = definitions.IndexOf(tmp.owner);
int tmp_index = tmp.owner.templates.IndexOf(tmp);
await _save(definitions.WrapSubRange(def_index, 1).WrapSelector(definition => (definition, definition.templates.WrapSubRange(tmp_index, 1)), t => t.definition));
} else if (content is AlgoDefinitionVM def) {
int def_index = definitions.IndexOf(def);
await _save(definitions.WrapSubRange(def_index, 1).WrapSelector(definition => (definition, definition.templates.AsList()), t => t.definition));
}
}
void revert(AlgoDefinitionTemplateVM tmp) {
switch (tmp.vm_key.mode) {
case AlgoVMKey.Mode.Persisted: tmp.load_committed(); break;
}
}
}
@@ -131,9 +130,9 @@ namespace mbts.spark {
public IAlgoStudioContentVM content_source => (IAlgoStudioContentVM)selected_template ?? selected_definition;
public IOrderTypeVM editable_content => _editable_content.Value;
public ICollectionView<AlgoDefinitionVM> definitions_view { get; }
public ICommand commit_cmd { get; }
public ICommand save_cmd { get; }
public ICommand revert_cmd { get; }
public string commit_cmd_tooltip => _commit_cmd_tooltip.Value;
public string save_cmd_tooltip => _save_cmd_tooltip.Value;
public string revert_cmd_tooltip => _revert_cmd_tooltip.Value;
public AlgoDefinitionVM add_new_definition() {
@@ -175,60 +174,18 @@ namespace mbts.spark {
}
public async Task save_definition(AlgoDefinitionVM definition) {
if (definition.vm_key.mode == AlgoVMKey.Mode.CustomPlacholder) {
// 'New' definitions are treated differently from 'updated' ones.
// Before a definition is persisted, a temporary placeholder AlgoDefinitionVM is used to manipulate values edited by the user in the view.
// Once it is created, the placeholder vm is removed and replaced with a persisted one.
uint? new_def;
if (definition == selected_definition) {
/// The placeholder was selected (this is the tricky part) - replace the placeholder vm with its persisted counterpart, without unselecting it.
using (_definitions_update_buffer.buffer_updates()) {
/// Buffer_updates will prevent the definition collection from changing while we await the new persisted id.
/// Without such a buffer, the view will be refreshed before the placeholder is removed, and a visible 'flicker' occurs where both the old placeholder and the new persisted vm are visible.
new_def = await _save_definition(definition);
}
/// After disposing the buffer (upon exiting the using block above), <see cref="_definitions"/> is updated and now contains both the placeholder and persisted vm.
/// However, because the view has not had a chance to refresh yet, the 'flicker' can be avoided by removing the placeholder.
if (new_def.HasValue) {
if (_definitions.TryGetValue(new AlgoVMKey(new_def.Value), out AlgoDefinitionVM persisted)) {
if (definition == selected_definition && definition.vm_key.mode == AlgoVMKey.Mode.CustomPlacholder) {
/// Select the persisted vm.
request_view_select_definition?.Invoke(persisted);
}
}
/// Remove the placeholder to avoid the 'flicker.'
_definitions.Remove(definition.vm_key);
}
} else {
// The placeholder vm was not selected - remove it if the save succeeds, no further action neccessary.
new_def = await _save_definition(definition);
if (new_def.HasValue)
_definitions.Remove(definition.vm_key);
}
} else if (definition == selected_definition) {
var old_selected_tmp = definition.selected_template;
await _save_definition(definition);
if (selected_template != old_selected_tmp) {
request_view_select_template(old_selected_tmp);
}
}
int def_index = definitions.IndexOf(definition);
await _save(definitions.WrapSubRange(def_index, 1).WrapSelector(def => (def, definition.templates.AsList()), t => t.def));
}
public async Task save_template(AlgoDefinitionTemplateVM template) {
using (template.owner.templates_view.DeferRefresh()) {
var result = await _model.algo_model.save_template(template.create());
if (template.vm_key.mode == AlgoVMKey.Mode.CustomPlacholder && result.HasValue) {
uint new_id = result.Value;
if (request_view_select_template != null && template.owner.templates.TryGetFirst(t => t.vm_key.persisted_id == new_id, out AlgoDefinitionTemplateVM persisted_tmp)) {
request_view_select_template(persisted_tmp);
}
template.owner.templates.Remove(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));
}
public async Task save_all() =>
await _save(definitions.WrapSelector(definition => (definition, definition.templates.AsList()), t => t.definition));
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.
@@ -311,21 +268,6 @@ namespace mbts.spark {
}
}
async Task<uint?> _save_definition(AlgoDefinitionVM definition) {
Result<CompositeAlgoCreateResult>? result = null;
if (definition.vm_key.mode == AlgoVMKey.Mode.CustomPlacholder) {
var create_result = await _model.algo_model.create_definition_templates(definition.create_algo_def(), definition.templates.Select(t => t.create().AlgoTemplate).ToList());
if (create_result.HasValue)
result = create_result.Value.definition;
} else {
// TODO: implement update-definition when the backend is ready (there are a lot of dependencies on definitions, so modifying them after creation is not trivial).
}
return result.HasValue && result.Value.success ? result.Value.value.Record.Key : (uint?)null;
}
async Task<uint?> _save_template(AlgoDefinitionTemplateVM template) =>
await _model.algo_model.save_template(template.create());
void IHandleSelectedItemChanged.on_selected_item_changed(object old_value, object new_value) {
if (old_value is AlgoDefinitionVM old_def)
old_def.algo_root_changed -= _on_selected_definition_root_changed;