diff --git a/src/client/spark/core/Commands/AsyncCommand.cs b/src/client/spark/core/Commands/AsyncCommand.cs index f70531eed..5b2471962 100644 --- a/src/client/spark/core/Commands/AsyncCommand.cs +++ b/src/client/spark/core/Commands/AsyncCommand.cs @@ -73,7 +73,7 @@ namespace mbts.ui { public AsyncCommand(Func execute) : base(_ => execute()) { } - public AsyncCommand(Func execute, Func can_execute) + public AsyncCommand(Func execute, Func can_execute, bool allow_concurrent_executions = false) : base(_ => execute(), _ => can_execute()) { } public AsyncCommand(Func execute) diff --git a/src/client/spark/core/Commands/ObservantAsyncCommand.cs b/src/client/spark/core/Commands/ObservantAsyncCommand.cs index bfbbfe6ca..ab40b55b6 100644 --- a/src/client/spark/core/Commands/ObservantAsyncCommand.cs +++ b/src/client/spark/core/Commands/ObservantAsyncCommand.cs @@ -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 execute, bool allow_concurrent_executions = false) : base(execute, allow_concurrent_executions) { } + + public static ICommand create(Func execute, out IObserver observer) { + var cmd = new ObservantAsyncCommand(execute); + observer = cmd; + return cmd; + } + + public static ICommand create(Func execute, Func get_can_execute, out IObserver 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(p => { + param = p; + if (can_execute != get_can_execute(param)) + cmd.RaiseCanExecuteChanged(); + }); + return cmd; + } + + public static ICommand create(Func execute, Func> get_can_execute_source, out IObserver 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(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; + } } }