ViewModelBase Property automatic validation, observe_current

This commit is contained in:
John Colvin committed 2018-03-27 20:05:11 -04:00
1 parent 5ff62baf85
commit b0101bfbcb
1 file changed
+56
@@ -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<T> 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<string, string> _errors = new Dictionary<string, string>();
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<string> AllErrors { get { return _errors.Values; } }
#endregion
protected Property<T> backing<T>(string name, T default_value, params Func<T, bool>[] validation_steps) =>
backing(name, default_value, validation_steps.Select(v => new PropertyValidation<T>(v)).ToLArray());
protected Property<T> backing<T>(string name, T default_value, IEnumerable<PropertyValidation<T>> validation_steps) {
var prop = new Property<T>(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<T> {
public readonly Func<T, (bool valid, string msg)> validate;
public PropertyValidation(Func<T, bool> validate) =>
this.validate = val => (validate(val), null);
public PropertyValidation(Func<T, (bool valid, string msg)> 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<DataErrorsChangedEventArgs> callback) {
self.ErrorsChanged += callback;
return Disposable.Create(() => self.ErrorsChanged -= callback);
}
}
}