fix: ContractPicker syncs selected sec/md w/ selected key

This commit is contained in:
John Colvin committed 2021-09-06 18:27:32 -04:00
1 parent 0356221066
commit 6ea217b674
1 file changed
+9 -1
@@ -23,11 +23,13 @@ namespace mbts.ui.controls {
readonly PropertyUpdateGate _selection_sync_gate = new PropertyUpdateGate();
IReadOnlyDictionary<uint, SecurityMD> _cached_security_map = DictionaryHelper.empty<uint, SecurityMD>();
// The security key of the most recently changed property among
// SelectedSecurity, SelectedSecurityMD, and SelectedSecurityKey
uint? _last_selected_sec_key = null;
ContractPickerPopupController _controller;
bool _allow_empty, _is_lazy;
public ContractPickerPopup() {
SetCurrentValue(SecurityMapProperty, new Dictionary<uint, SecurityMD>(0));
var sec_map_source = Observable.Create<IReadOnlyDictionary<uint, SecurityMD>>(o => {
try_notify();
_security_map_changed += on_security_map_changed;
@@ -50,6 +52,9 @@ namespace mbts.ui.controls {
}
});
_controller = new ContractPickerPopupController(this, new ContractPickerModel(sec_map_source, true, null));
// This has to be set after _controller is initialized (because it causes a call to the IsEditMode getter,
// which assumes _controller is non-null)
SetCurrentValue(SecurityMapProperty, new Dictionary<uint, SecurityMD>(0));
InitializeComponent();
DataContextChanged += (s, e) => {
// - unhooking from oldval must co-incide with hooking to newval.
@@ -91,6 +96,8 @@ namespace mbts.ui.controls {
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
(o, v) => {
o._cached_security_map = v;
o._selection_sync_gate.try_update(() => o._sync_selection_props(
o.SelectedSecurityMD, o.SelectedSecurity, o.SelectedSecurityKey, o._last_selected_sec_key));
o._security_map_changed?.Invoke(v);
},
(o, v) => v ?? new Dictionary<uint, SecurityMD>());
@@ -125,6 +132,7 @@ namespace mbts.ui.controls {
}
void _sync_selection_props(SecurityMD? curr_sec_md, SecurityImmutable? curr_sec, uint? curr_sec_key, uint? target_sec_key) {
_last_selected_sec_key = target_sec_key;
if (curr_sec_key != target_sec_key) {
SetCurrentValue(SelectedSecurityKeyProperty, target_sec_key);
}