Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous command execution with user confirmation

I need to execute async delete operation with user confirmation. Something like this:

public ReactiveAsyncCommand DeleteCommand { get; protected set; }
...
DeleteCommand = new ReactiveAsyncCommand();
DeleteCommand.RegisterAsyncAction(DeleteEntity);

...

private void DeleteEntity(object obj)
{
    if (MessageBox.Show("Do you really want to delete this entity?", "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
    {
        //some delete operations
    }
}

The problem is the MessageBox would execute asynchronously too. Which is the best pattern in ReactiveUI to ask user synchronously and then execute method asynchronously?

like image 870
elshev Avatar asked Sep 06 '12 10:09

elshev


1 Answers

The most straightforward way to do this is to just use two commands:

public ReactiveCommand DeleteCommand { get; protected set; }
private ReactiveAsyncCommand ExecuteDelete { get; protected set; }

/*
 * In the Constructor
 */

ExecuteDelete = new ReactiveAsyncCommand();
ExecuteDelete.RegisterAsyncAction(() => /* Do the delete */);

DeleteCommand = new ReactiveCommand(ExecuteDelete.CanExecuteObservable);
DeleteCommand
    .Where(_ => MessageBox.Show("Delete?") == MessageBoxResult.Yes)
    .InvokeCommand(ExecuteDelete);
like image 62
Ana Betts Avatar answered Oct 19 '22 14:10

Ana Betts