added ObservantAsyncCommand factory methods

This commit is contained in:
John Colvin committed 2019-05-19 17:44:33 -05:00
1 parent a6fa5f9852
commit d20b30557f
2 files changed
+38 -1

No files matched your search

@@ -73,7 +73,7 @@ namespace mbts.ui {
public AsyncCommand(Func<Task> execute)
: base(_ => execute()) { }
public AsyncCommand(Func<Task> execute, Func<bool> can_execute)
public AsyncCommand(Func<Task> execute, Func<bool> can_execute, bool allow_concurrent_executions = false)
: base(_ => execute(), _ => can_execute()) { }
public AsyncCommand(Func<object, Task> execute)
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Reactive;
using System.Threading.Tasks;
using System.Windows.Input;
@@ -51,5 +52,41 @@ namespace mbts.ui {
public ObservantAsyncCommand(Func<object, Task> execute, bool allow_concurrent_executions = false)
: base(execute, allow_concurrent_executions) { }
public static ICommand create(Func<Task> execute, out IObserver<bool> observer) {
var cmd = new ObservantAsyncCommand(execute);
observer = cmd;
return cmd;
}
public static ICommand create<T>(Func<T, Task> execute, Func<T, bool> get_can_execute, out IObserver<T> observer, bool allow_concurrent_executions = false) {
T param = default;
bool can_execute = false;
var cmd = new AsyncCommand(() => execute(param), () => can_execute, allow_concurrent_executions);
observer = new AnonymousObserver<T>(p => {
param = p;
if (can_execute != get_can_execute(param))
cmd.RaiseCanExecuteChanged();
});
return cmd;
}
public static ICommand create<T>(Func<T, Task> execute, Func<T, IObservable<bool>> get_can_execute_source, out IObserver<T> observer, bool allow_concurrent_executions = false) {
T param = default;
bool can_execute = false;
var cmd = new AsyncCommand(() => execute(param), () => can_execute, allow_concurrent_executions);
IDisposable can_execute_sub = null;
observer = new AnonymousObserver<T>(p => {
param = p;
Disposable.dispose(ref can_execute_sub);
can_execute_sub = get_can_execute_source(p).Subscribe(ce => {
if (can_execute != ce) {
can_execute = ce;
cmd.RaiseCanExecuteChanged();
}
});
}, () => Disposable.dispose(ref can_execute_sub));
return cmd;
}
}
}