navule
navule

Reputation: 3654

Single ObservableAsPropertyHelper with multiple ReactiveCommand IsExecuting

From the sample here, I'm trying to change the _SpinnerVisibility to multiple reactivecommand objects. So below snippet shows what I did..

public ReactiveCommand<object> ExecuteSearch { get; protected set; }

public ReactiveCommand<List<string>> OtherOperation { get; protected set; }

public AppViewModel(ReactiveCommand<object> testExecuteSearchCommand = null, IObservable<List<FlickrPhoto>> testSearchResults = null)
{

_SpinnerVisibility = OtherOperation.IsExecuting.Select(x=> x? Visibility.Visible : Visibility.Collapsed)                
                .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);
_SpinnerVisibility = ExecuteSearch.IsExecuting.Select(x=> x? Visibility.Visible : Visibility.Collapsed)                
                .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);
}

Here I was setting the visibility based on multiple reactive commands. But always the last registered command will trigger the visibility. Is there a way to change the visibility based on both the reactive commands ExecuteSearch, OtherOperation?

Upvotes: 3

Views: 668

Answers (1)

Gluck
Gluck

Reputation: 2962

You need to merge both IsExecuting observables into one.

If your commands are mutually exclusive (that is they can't execute at the same time), this should work:

_SpinnerVisibility = new IReactiveCommand[] { ExecuteSearch, OtherOperation }
    .Select(cmd => cmd.IsExecuting)
    .Merge()
    .Select(x=> x? Visibility.Visible : Visibility.Collapsed)                
    .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);

If they can be, then it'll be more like:

_SpinnerVisibility = new IReactiveCommand[] { ExecuteSearch, OtherOperation }
    .Select(cmd => cmd.IsExecuting)
    .CombineLatest()
    .Select(x => x.Any() ? Visibility.Collapsed : Visibility.Visible)                
    .ToProperty(this, x => x.SpinnerVisibility, Visibility.Hidden);

Both will work with any number of commands.

Upvotes: 2

Related Questions