0

If CleanCommand is executing then SearchCommand will be inactive. If SearchCommand is executing then CleanCommand will be inactive.

Barcode is

public long Barcode
{
    get => _barcode;
    set => this.RaiseAndSetIfChanged(ref _barcode, value);
}

private readonly ObservableAsPropertyHelper<bool> _isSearching;
private readonly ObservableAsPropertyHelper<bool> _isCleaning;    
public bool IsSearching => _isSearching.Value;
public bool IsCleaning => _isCleaning.Value;
public ReactiveCommand<Unit, Unit> SearchCommand { get; }
public ReactiveCommand<Unit, Unit> CleanCommand { get; }

In constructor

SearchCommand = ReactiveCommand.CreateFromObservable(Search, SearchCanExecute());
SearchCommand.IsExecuting.ToProperty(this, x => x.IsSearching, out _isSearching);

CleanCommand = ReactiveCommand.CreateFromObservable(Clean, CleanCanExecute());
CleanCommand.IsExecuting.ToProperty(this, x => x.IsCleaning, out _isCleaning);

In class

    IObservable<bool> SearchCanExecute()
    {
        bool isCleaningSuited = !IsCleaning;
        bool isBarcodeSuited = Barcode > 0;

        return Observable.Return(isBarcodeSuited);
    }

    IObservable<bool> CleanCanExecute()
    {
        bool isSearchingSuited = !IsSearching;

        return Observable.Return(isSearchingSuited);
    }

I get the process status with *IsExecuting.ToProperty()

I get values with properties like Barcode.

Should i use WhenAny* method or can i do it this way?

echoman
  • 1
  • 3

2 Answers2

1

I'd probably do this.WhenAnyObservable on the command execution and pipe that into the respective command canExecute. That way you don't really need the functions that return observables and it's a bit more fluid.

Something along the lines of the following

            var commandOneCanExecute = this.WhenAnyObservable(x => x.CommandTwo.IsExecuting).StartWith(false).Select(x => !x);
            var commandTwoCanExecute = this.WhenAnyObservable(x => x.CommandOne.IsExecuting).StartWith(false).Select(x => !x);
            CommandOne = ReactiveCommand.CreateFromObservable(ExecuteCommand, commandOneCanExecute);
            CommandTwo = ReactiveCommand.CreateFromObservable(ExecuteCommand, commandTwoCanExecute);
Rodney Littles
  • 544
  • 2
  • 11
0

I think i solved the problem.

IObservable<bool> searchCommandObservable = this.WhenAnyObservable(x => x.SearchCommand.IsExecuting).StartWith(false).Select(x => x);
IObservable<bool> cleanCommandObservable = this.WhenAnyObservable(x => x.CleanCommand.IsExecuting).StartWith(false).Select(x => x);
IObservable<bool> barcodeObservable = this.WhenAnyValue(x => x.Barcode).StartWith(0).Select(x => x > 0);

IObservable<bool> searchCanExecute = Observable.Merge(
    searchCommandObservable.Select(x => !x),
    cleanCommandObservable.Select(x => !x),
    barcodeObservable
);

IObservable<bool> cleanCanExecute = Observable.Merge(
    searchCommandObservable.Select(x => !x),
    cleanCommandObservable.Select(x => !x)
);
echoman
  • 1
  • 3