SelectorProperty - add 'SelectionReason' (enum) param to SelectionChanged event
This commit is contained in:
1 parent
ddc297d413
commit
009cd88df1
2 files changed
+34
-25
No files matched your search
@@ -162,7 +162,7 @@ namespace mbts.spark {
|
||||
return result;
|
||||
return order_types.FirstOrDefault();
|
||||
}
|
||||
void on_selected_order_type_changed(IOrderTypeVM old_val, IOrderTypeVM new_val) {
|
||||
void on_selected_order_type_changed(IOrderTypeVM old_val, IOrderTypeVM new_val, SelectionReason reason) {
|
||||
if (new_val == null) {
|
||||
assert(false, nameof(selected_order_type));
|
||||
} else {
|
||||
@@ -173,11 +173,7 @@ namespace mbts.spark {
|
||||
_order_type_param_errors_sub = ndei.subscribe_errors_changed((s, e) => assert_no_param_errors());
|
||||
void assert_no_param_errors() => assert(!ndei.HasErrors, "order type pararmeters invalid.");
|
||||
}
|
||||
/// This is a bit of a hack; the goal is to open the algo popup menu when users select a new algo.
|
||||
/// It isn't as straightforward as it sounds bc there are a couple non-user-invoked scenarios which ellicit calls to the SelectorProperty.selected_item's public setter:
|
||||
/// 1) a selected-order-type default value is observed from an IObservable settings source, which may overwrite the current selection, esp if the current value is null.
|
||||
/// 2) the order-type itemssource is observed from an IObservable settings source as well, which may cause the current selection to be changed by the view, if the itemssource no longer contains the currently selected value.
|
||||
if (new_val != null && new_val == selected_order_type && _order_type.items.Contains(new_val) && selected_order_type.has_editable_properties && _order_type.is_user_selected_item && old_val != null && _order_type.items.Contains(old_val))
|
||||
if (new_val != null && new_val.has_editable_properties && reason == SelectionReason.UserSelectedNewValue)
|
||||
selected_order_type.editable_properties_open = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -591,6 +591,16 @@ namespace mbts.ui.shell
|
||||
: base(owner, prop_name, i => i, select_default, comparer, validation_steps) { }
|
||||
}
|
||||
|
||||
protected enum SelectionReason {
|
||||
Other,
|
||||
UserSelectedNewValue,
|
||||
CurrentValueWasRemoved,
|
||||
CurrentValueWasRestored,
|
||||
SettingsMandatedSelection
|
||||
}
|
||||
|
||||
protected delegate void SelectionChanged<T>(T old_value, T new_value, SelectionReason reason);
|
||||
|
||||
/// <summary>
|
||||
/// Designed to observe an items source, and a [settings-driven] desired item. When a new desired item is observed, it will evaluate it's current state and determine if it should be assigned to the selected_item.
|
||||
/// <para/>Notes: If the user has selected an item (ie the selected_item has been assigned through the public setter), the desired item will be ignored unless force_override is true.
|
||||
@@ -606,7 +616,7 @@ namespace mbts.ui.shell
|
||||
readonly Func<TItem, TValue> _item_to_value;
|
||||
TValue _selected_item;
|
||||
InlineUserProperty<TItem> _desired_item = new InlineUserProperty<TItem>();
|
||||
bool _is_user_selected_item;
|
||||
bool _is_user_selected_item, _is_items_source_changing;
|
||||
|
||||
public SelectorProperty(DisposableViewModelBase owner, string prop_name, Func<TItem, TValue> item_to_value, Func<TValue> select_default, IEqualityComparer<TValue> value_comparer, IReadOnlyList<PropertyValidation<TValue>> validation_steps) {
|
||||
_owner = owner;
|
||||
@@ -618,24 +628,21 @@ namespace mbts.ui.shell
|
||||
items = new ReadOnlyObservableCollection<TItem>(_items);
|
||||
}
|
||||
|
||||
public event PropertyChange<TValue> Changing;
|
||||
public event PropertyChange<TValue> Changed;
|
||||
public event SelectionChanged<TValue> Changing;
|
||||
public event SelectionChanged<TValue> Changed;
|
||||
|
||||
public ReadOnlyObservableCollection<TItem> items { get; }
|
||||
|
||||
public TValue selected_item {
|
||||
get => _selected_item;
|
||||
set {
|
||||
_is_user_selected_item = true;
|
||||
_set_selected_item(value);
|
||||
}
|
||||
set => _set_selected_item(value, (_is_user_selected_item = !_is_items_source_changing) ? SelectionReason.UserSelectedNewValue : SelectionReason.CurrentValueWasRemoved);
|
||||
}
|
||||
public bool is_user_selected_item => _is_user_selected_item;
|
||||
|
||||
public IDisposable Subscribe(IObserver<(TValue oldval, TValue newval)> observer) {
|
||||
Changed += pch;
|
||||
return Disposable.Create(() => Changed -= pch);
|
||||
void pch(TValue o, TValue n) => observer.OnNext((o, n));
|
||||
void pch(TValue o, TValue n, SelectionReason r) => observer.OnNext((o, n));
|
||||
}
|
||||
|
||||
public IObservable<TValue> observe_current(bool start_now = false) {
|
||||
@@ -653,12 +660,14 @@ namespace mbts.ui.shell
|
||||
void IObserver<InlineUserProperty<TItem>>.OnCompleted() { }
|
||||
|
||||
void IObserver<IEnumerable<TItem>>.OnNext(IEnumerable<TItem> value) {
|
||||
_is_items_source_changing = true;
|
||||
if (_is_user_selected_item) {
|
||||
var curr_selected_item = selected_item;
|
||||
_items.sync_with(value);
|
||||
if (_try_match_item(curr_selected_item, out TValue match))
|
||||
selected_item = match;
|
||||
else {
|
||||
if (_try_match_item(curr_selected_item, out TValue match)) {
|
||||
_is_user_selected_item = true;
|
||||
_set_selected_item(match, SelectionReason.CurrentValueWasRestored);
|
||||
} else {
|
||||
_is_user_selected_item = false;
|
||||
_try_auto_select();
|
||||
}
|
||||
@@ -666,6 +675,7 @@ namespace mbts.ui.shell
|
||||
_items.sync_with(value);
|
||||
_try_auto_select();
|
||||
}
|
||||
_is_items_source_changing = false;
|
||||
}
|
||||
void IObserver<IEnumerable<TItem>>.OnError(Exception error) { }
|
||||
void IObserver<IEnumerable<TItem>>.OnCompleted() { }
|
||||
@@ -681,16 +691,16 @@ namespace mbts.ui.shell
|
||||
void _try_auto_select() {
|
||||
if (_is_user_selected_item) {
|
||||
if (desired_item_valid() && _desired_item.force_override && _try_match_item(_desired_item.value, out TValue match)) {
|
||||
_set_selected_item(match);
|
||||
_set_selected_item(match, SelectionReason.SettingsMandatedSelection);
|
||||
return;
|
||||
}
|
||||
} else if (desired_item_valid() && _try_match_item(_desired_item.value, out TValue match)) {
|
||||
_set_selected_item(match);
|
||||
_set_selected_item(match, SelectionReason.SettingsMandatedSelection);
|
||||
return;
|
||||
}
|
||||
if (_try_match_item(_select_default(), out TValue default_match))
|
||||
_set_selected_item(default_match);
|
||||
else {
|
||||
if (_try_match_item(_select_default(), out TValue default_match)) {
|
||||
_set_selected_item(default_match, SelectionReason.Other);
|
||||
} else {
|
||||
// the _set_selected_item has never been entered, so validation has never ran, force it here
|
||||
// so that if some value is required, the valdiation logic will catch it
|
||||
_run_validation(selected_item);
|
||||
@@ -698,12 +708,15 @@ namespace mbts.ui.shell
|
||||
bool desired_item_valid() => (_desired_item?.has_value).GetValueOrDefault();
|
||||
}
|
||||
|
||||
void _set_selected_item(TValue value) {
|
||||
void _set_selected_item(TValue value, SelectionReason reason) {
|
||||
if (!_value_comparer.Equals(value, _selected_item)) {
|
||||
Changing?.Invoke(_selected_item, value);
|
||||
//SelectionReason reason = _is_items_source_changing && !items.Select(_item_to_value).Contains(selected_item, _value_comparer) ?
|
||||
// SelectionReason.CurrentValueWasRemoved :
|
||||
// _is_user_selected_item ? SelectionReason.UserSelectedNewValue : SelectionReason.Other;
|
||||
Changing?.Invoke(_selected_item, value, reason);
|
||||
var old = _selected_item;
|
||||
_selected_item = value;
|
||||
Changed?.Invoke(old, value);
|
||||
Changed?.Invoke(_selected_item, value, reason);
|
||||
_owner.RaisePropertyChanged(_prop_name);
|
||||
_run_validation(value);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user