Files
spark/src/client/spark/Predicates/IPredicateVM.cs
T

197 lines
11 KiB
C#

using mbts.collections.extensions;
using mbts.config;
using mbts.spark;
using mbts.ui.shell;
using mbts.ui.spark;
using mbts.ui.utilities;
using mbts.ui.utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Data;
namespace mbts.ui.settings {
interface IPredicateVM<TRowData, TPredicateData, TPredicateDataOneof> : ITemplatedPropertyWrapperVM<TPredicateData> where TPredicateDataOneof : struct {
IReadOnlyCollection<TPredicateDataOneof> predicate_types { get; }
ICollectionView predicate_types_view { get; }
TPredicateDataOneof selected_predicate_type { get; set; }
ITemplatedPropertyWrapperVM<TPredicateData> selected_predicate { get; }
bool can_enable_toast_notifications { get; }
bool toast_notifications_enabled { get; }
}
interface IChildPredicateVM<TSource, TChildPredicate, TChildPredicateOneof> : ITemplatedPropertyWrapperVM<TSource> where TChildPredicateOneof : struct {
IReadOnlyCollection<TChildPredicateOneof> predicate_types { get; }
ICollectionView predicate_types_view { get; }
TChildPredicateOneof selected_predicate_type { get; set; }
ITemplatedPropertyWrapperVM<TChildPredicate> selected_predicate { get; }
}
static class PredicateVMWrapper {
static readonly bool _toast_notifications_enabled_global = Configuration.get<bts.config.toggles.ToastNotifications>().enabled;
public static ITemplatedPropertyWrapperVM<TSource> wrap_child_oneof<TSource, TChild, TChildOneof>(
Func<TSource, TChild> get, Action<TSource, TChild> set, Func<TChild, TChildOneof> get_child_oneof, Dictionary<TChildOneof, ITemplatedPropertyWrapperVM<TChild>> oneof_vms)
where TChildOneof : struct
where TChild : new() =>
//
new ChildImpl<TSource, TChild, TChildOneof>(get, set, get_child_oneof, oneof_vms);
public static IPredicateVM<TRowData, TPredicate, TPredicateOneof> create<TRowData, TPredicate, TPredicateOneof>(
Dictionary<TPredicateOneof, ITemplatedPropertyWrapperVM<TPredicate>> oneofs, Func<TPredicate, TPredicateOneof> key) where TPredicateOneof : struct =>
//
new Impl<TRowData, TPredicate, TPredicateOneof>(oneofs, key, _ => false, (p, b) => { });
public static IPredicateVM<TRowData, TPredicate, TPredicateOneof> create<TRowData, TPredicate, TPredicateOneof>(
Dictionary<TPredicateOneof, ITemplatedPropertyWrapperVM<TPredicate>> oneofs, Func<TPredicate, TPredicateOneof> key, Func<TPredicate, bool> get_is_toast_enabled, Action<TPredicate, bool> set_is_toast_enabled) where TPredicateOneof : struct =>
//
new Impl<TRowData, TPredicate, TPredicateOneof>(oneofs, key, get_is_toast_enabled, set_is_toast_enabled);
public static ITemplatedPropertyWrapperVM<TSource> create<TSource>(Action<TSource> set = null) =>
new PredicatePlaceholderVM<TSource>(set);
static ICollectionView _create_predicate_types_view<TPredicateOneof>(IReadOnlyCollection<TPredicateOneof> predicate_types) where TPredicateOneof : struct {
var cv = CollectionViewSource.GetDefaultView(predicate_types.Select(t => {
var pca = EnumHelper.get_attribute<TPredicateOneof, PredicateCaseAttribute>(t);
string desc = pca?.Description;
if (string.IsNullOrEmpty(desc))
desc = EnumHelper.get_display_name(t, TextCapitalizationFormat.FirstLetterFirstWord);
return new KeyGroupHeader<TPredicateOneof, PredicateCategory>(t, desc, pca?.category ?? PredicateCategory.Normal);
}).ToList());
cv.GroupDescriptions.Add(new PropertyGroupDescription(nameof(KeyGroupHeader<TPredicateOneof, PredicateCategory>.group)));
return cv;
}
class Impl<TRowData, TPredicate, TPredicateOneof> : DisposableViewModelBase, IPredicateVM<TRowData, TPredicate, TPredicateOneof>, ITemplatedPropertyWrapperVM<TPredicate>
where TPredicateOneof : struct {
readonly Dictionary<TPredicateOneof, ITemplatedPropertyWrapperVM<TPredicate>> _oneofs;
readonly Property<TPredicateOneof> _selected_predicate_type;
readonly Property<ITemplatedPropertyWrapperVM<TPredicate>> _selected_predicate;
readonly DelegateProperty<bool> _can_enable_toast_notifications;
readonly Property<bool> _toast_notifications_enabled;
readonly Func<TPredicate, TPredicateOneof> _key_selector;
readonly Func<TPredicate, bool> _get_is_toast_enabled;
readonly Action<TPredicate, bool> _set_is_toast_enabeled;
public Impl(Dictionary<TPredicateOneof, ITemplatedPropertyWrapperVM<TPredicate>> oneofs, Func<TPredicate, TPredicateOneof> key_selector, Func<TPredicate, bool> get_is_toast_enabled, Action<TPredicate, bool> set_is_toast_enabled) {
_oneofs = oneofs;
_key_selector = key_selector;
_get_is_toast_enabled = get_is_toast_enabled;
_set_is_toast_enabeled = set_is_toast_enabled;
_selected_predicate = backing<ITemplatedPropertyWrapperVM<TPredicate>>(nameof(selected_predicate));
_selected_predicate_type = backing<TPredicateOneof>(nameof(selected_predicate_type));
predicate_types_view = _create_predicate_types_view(predicate_types);
var toastable_predicates = new HashSet<TPredicateOneof>(oneofs.Keys.Where(k => {
var pca = EnumHelper.get_attribute<TPredicateOneof, PredicateCaseAttribute>(k);
return pca != null && pca.is_toastable;
}));
_can_enable_toast_notifications = backing(nameof(can_enable_toast_notifications), () => toastable_predicates.Contains(selected_predicate_type), nameof(selected_predicate_type));
_toast_notifications_enabled = backing<bool>(nameof(toast_notifications_enabled));
selected_predicate_type = predicate_types.FirstOrDefault();
}
public IReadOnlyCollection<TPredicateOneof> predicate_types => _oneofs.Keys;
public ICollectionView predicate_types_view { get; }
public TPredicateOneof selected_predicate_type {
get => _selected_predicate_type.Value;
set {
_selected_predicate_type.set(value);
_selected_predicate.set(_oneofs.GetValueOrDefault(value));
_can_enable_toast_notifications.refresh();
}
}
public ITemplatedPropertyWrapperVM<TPredicate> selected_predicate => _selected_predicate.Value;
public bool can_enable_toast_notifications => _toast_notifications_enabled_global && _can_enable_toast_notifications.Value;
public bool toast_notifications_enabled { get => _toast_notifications_enabled.Value; set => _toast_notifications_enabled.set(value); }
public bool is_root => true;
string ITemplateSelectorKey.key => PredicateTemplateKeys.Recursive;
protected override void Dispose(bool disposing) =>
_oneofs.Values.OfType<IDisposable>().DisposeAll();
public void load_from(TPredicate value) {
selected_predicate_type = _key_selector(value);
selected_predicate?.load_from(value);
if (can_enable_toast_notifications)
_toast_notifications_enabled.set(_get_is_toast_enabled(value));
}
public void assign_to(TPredicate value) {
selected_predicate?.assign_to(value);
if (can_enable_toast_notifications)
_set_is_toast_enabeled(value, toast_notifications_enabled);
}
}
class ChildImpl<TPredicate, TChild, TChildKey> : DisposableViewModelBase, IChildPredicateVM<TPredicate, TChild, TChildKey> where TChildKey : struct where TChild : new() {
readonly Property<ITemplatedPropertyWrapperVM<TChild>> _selected_predicate;
readonly Property<TChildKey> _selected_predicate_type;
readonly Dictionary<TChildKey, ITemplatedPropertyWrapperVM<TChild>> _children;
readonly Func<TPredicate, TChild> _get;
readonly Action<TPredicate, TChild> _set;
readonly Func<TChild, TChildKey> _key;
readonly string _template_key;
public ChildImpl(Func<TPredicate, TChild> get, Action<TPredicate, TChild> set, Func<TChild, TChildKey> key,
Dictionary<TChildKey, ITemplatedPropertyWrapperVM<TChild>> oneofs, string template = PredicateTemplateKeys.Recursive) {
_get = get;
_set = set;
_key = key;
_children = oneofs;
_selected_predicate = backing<ITemplatedPropertyWrapperVM<TChild>>(nameof(selected_predicate));
_selected_predicate_type = backing(nameof(selected_predicate_type), oneofs.Keys.FirstOrDefault());
// initialize 'predicate_type' by setting 'selected_predicate_type'
selected_predicate_type = predicate_types.FirstOrDefault();
predicate_types_view = _create_predicate_types_view(predicate_types);
_template_key = template;
}
public IReadOnlyCollection<TChildKey> predicate_types => _children.Keys;
public ICollectionView predicate_types_view { get; }
public TChildKey selected_predicate_type {
get => _selected_predicate_type.Value;
set {
_selected_predicate_type.set(value);
if (_children.TryGetValue(value, out ITemplatedPropertyWrapperVM<TChild> c)) {
_selected_predicate.set(c);
}
}
}
public ITemplatedPropertyWrapperVM<TChild> selected_predicate => _selected_predicate.Value;
public bool is_root => false;
string ITemplateSelectorKey.key => _template_key;
protected override void Dispose(bool disposing) {
_children.Values.OfType<IDisposable>().DisposeAll();
}
public void assign_to(TPredicate value) {
var cp = _get(value);
if (cp == null) {
cp = new TChild();
}
selected_predicate?.assign_to(cp);
_set(value, cp);
}
public void load_from(TPredicate value) {
var cp = _get(value);
selected_predicate_type = _key(cp);
selected_predicate?.load_from(cp);
}
}
class PredicatePlaceholderVM<TPredicateBuilder> : ITemplatedPropertyWrapperVM<TPredicateBuilder> {
readonly Action<TPredicateBuilder> _set;
public PredicatePlaceholderVM(Action<TPredicateBuilder> set = null) =>
_set = set;
public string key => PredicateTemplateKeys.None;
public void assign_to(TPredicateBuilder value) => _set?.Invoke(value);
public void load_from(TPredicateBuilder value) { }
public override string ToString() => string.Empty;
}
}
}