From b0101bfbcbfbe4288d0f6dc89e2f4cd4446b03c8 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 27 Mar 2018 19:07:23 -0400 Subject: [PATCH] ViewModelBase Property automatic validation, observe_current --- .../spark/core/Utility/ViewModelBase.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/client/spark/core/Utility/ViewModelBase.cs b/src/client/spark/core/Utility/ViewModelBase.cs index 326d5b3c1..9cf0bd21a 100644 --- a/src/client/spark/core/Utility/ViewModelBase.cs +++ b/src/client/spark/core/Utility/ViewModelBase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.ComponentModel; +using System.Reactive.Linq; using System.Runtime.CompilerServices; using System.Reflection; using System.Windows.Input; @@ -131,6 +132,8 @@ namespace mbts.ui.shell public event PropertyChange Changing; public event PropertyChange Changed; + public string name => _propname; + public void set(T value) { if (!Equals(value, _value)) { Changing?.Invoke(_value, value); @@ -149,6 +152,13 @@ namespace mbts.ui.shell }); } + public IObservable observe_current(bool start_immediately = false) { + var source = this.Select(vals => vals.newval); + if (start_immediately) + source = source.StartWith(_value); + return source; + } + public T Value { get { return _value; } set { set(value); } @@ -204,6 +214,18 @@ namespace mbts.ui.shell { Dictionary _errors = new Dictionary(); + protected void assert(bool condition, [CallerMemberName]string property = "", string message = null) { + if (condition) { + clear_error(property); + } else if (!_errors.TryGetValue(property, out string result) || !string.Equals(message, result)) { + _errors[property] = message; + FireErrorsChanged(property); + } + } + + protected bool has_errors(string property) => + _errors.ContainsKey(property); + protected void set_error(string property, string message, bool ignore_if_already_error = false) { if (!_errors.ContainsKey(property) || !ignore_if_already_error) { _errors[property] = message; @@ -289,6 +311,31 @@ namespace mbts.ui.shell public IEnumerable AllErrors { get { return _errors.Values; } } #endregion + + protected Property backing(string name, T default_value, params Func[] validation_steps) => + backing(name, default_value, validation_steps.Select(v => new PropertyValidation(v)).ToLArray()); + + protected Property backing(string name, T default_value, IEnumerable> validation_steps) { + var prop = new Property(this, name, default_value); + prop.Changed += (old_val, new_val) => { + foreach (var pv in validation_steps) { + (bool valid, string msg) = pv.validate(new_val); + assert(valid, name, msg); + } + }; + return prop; + } + + // TODO: maybe include a revert_on_error flag? + protected class PropertyValidation { + public readonly Func validate; + + public PropertyValidation(Func validate) => + this.validate = val => (validate(val), null); + + public PropertyValidation(Func validate) => + this.validate = validate; + } } public abstract class DisposableViewModelBase : ViewModelBaseWithValidation, IDisposable @@ -320,4 +367,13 @@ namespace mbts.ui.shell Dispose(true); } } + + [Browsable(false)] + [EditorBrowsable(EditorBrowsableState.Never)] + static class NotifyDataErrorInfoExtensions { + public static IDisposable subscribe_errors_changed(this INotifyDataErrorInfo self, EventHandler callback) { + self.ErrorsChanged += callback; + return Disposable.Create(() => self.ErrorsChanged -= callback); + } + } } \ No newline at end of file