sausage_mouse
sausage_mouse

Reputation: 142

ReactiveUI. Make ReactiveCommand be able to execute when neither of the other ReactiveCommands are executing

I'm currently working on an AvaloniaUI app and faced this problem. I have 3 ReactiveCommands:

public ReactiveCommand<Unit, Unit> MyCommand1 {get; set;}
public ReactiveCommand<Unit, Unit> MyCommand2 {get; set;}
public ReactiveCommand<Unit, Unit> MyCommand3 {get; set;}

which are initialized in constructor like that:

public MyClass()
{
    MyCommand1 = ReactiveCommand.CreateFromTask(MyMethod1);
    MyCommand2 = ReactiveCommand.CreateFromTask(MyMethod2);
    MyCommand3= ReactiveCommand.CreateFromTask(MyMethod3);
}

What I want is to make every command unexecutable when any other command is executing. I tried something like this:

public MyClass()
{
    MyCommand1 = ReactiveCommand.CreateFromTask(MyMethod1, IsAnyCommandIsExecuting);
    MyCommand2 = ReactiveCommand.CreateFromTask(MyMethod2, IsAnyCommandIsExecuting);
    MyCommand3= ReactiveCommand.CreateFromTask(MyMethod3, IsAnyCommandIsExecuting);
}

private IObservable<bool> IsAnyCommandIsExecuting => this.WhenAnyObservable(
    x => x.MyCommand11.IsExecuting,
    x => x.MyCommand2.IsExecuting,
    x => x.MyCommand3.IsExecuting);

But it didn't worked out: I had NotSupportedException for some reason.

Question number 2 is how do I invert IsAnyCommandIsExecuting. It is problematic since it has a type of Observable<bool>. I didn't come up with something better that comparing with Observable.Return(false), but I feel like there has to be a better solution

Upvotes: 2

Views: 45

Answers (1)

ALEXXXANDRO
ALEXXXANDRO

Reputation: 26

Try to do it like this

private IObservable<bool> IsAnyCommandIsExecuting => this.WhenAnyObservable(
    x => x.MyCommand11.IsExecuting,
    x => x.MyCommand2.IsExecuting,
    x => x.MyCommand3.IsExecuting).Select(isExecuting => !isExecuting);

Upvotes: 1

Related Questions