Beispiel #1
0
        private static async Task HandleCommandException(CancellationToken ct, IDynamicCommand command, Exception exception, IServiceProvider services)
        {
            if (exception?.IsOrContainsExceptionType <OperationCanceledException>() ?? false)
            {
                return;
            }

            var messageDialogService = services.GetRequiredService <IMessageDialogService>();
            var stringLocalizer      = services.GetRequiredService <IStringLocalizer>();

            var titleResourceKey = string.Empty;
            var bodyResourceKey  = string.Empty;

            if (exception.IsOrContainsExceptionType <NoNetworkException>())
            {
                titleResourceKey = $"NoNetwork_Error_DialogTitle";
                bodyResourceKey  = $"NoNetwork_Error_DialogBody";
            }
            else
            {
                titleResourceKey = $"{command.Name}_Error_DialogTitle";
                bodyResourceKey  = $"{command.Name}_Error_DialogBody";
            }

            var title = stringLocalizer[titleResourceKey];
            var body  = stringLocalizer[bodyResourceKey];

            title = title.ResourceNotFound ? stringLocalizer["Default_Error_DialogTitle"] : title;
            body  = body.ResourceNotFound ? stringLocalizer["Default_Error_DialogBody"] : body;

            await messageDialogService.ShowMessage(ct, m => m
                                                   .Title(title)
                                                   .Content(body)
                                                   );
        }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DynamicCommandDataLoaderTrigger"/> class.
 /// </summary>
 /// <param name="command">The dynamic command causing the triggers.</param>
 /// <param name="triggerOptions">The trigger options.</param>
 public DynamicCommandDataLoaderTrigger(IDynamicCommand command, CommandTriggerOptions triggerOptions)
     : base(command?.Name)
 {
     _command                     = command ?? throw new ArgumentNullException(nameof(command));
     _triggerOptions              = triggerOptions;
     _command.IsExecutingChanged += OnIsExecutingChanged;
 }
Beispiel #3
0
 /// <summary>
 /// Observes if the <see cref="IDynamicCommand"/> is currently executing.
 /// </summary>
 /// <param name="command"><see cref="IDynamicCommand"/></param>
 /// <returns>Observable of boolean</returns>
 public static IObservable <bool> ObserveIsExecuting(this IDynamicCommand command)
 {
     return(Observable.FromEventPattern <EventHandler, EventArgs>(
                h => command.IsExecutingChanged += h,
                h => command.IsExecutingChanged -= h
                )
            .Select(p => command.IsExecuting));
 }
Beispiel #4
0
 /// <inheritdoc/>
 public override async Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
 {
     try
     {
         await base.Execute(ct, parameter, command);
     }
     catch (Exception e)
     {
         await _errorHandler.HandleError(ct, command, e);
     }
 }
Beispiel #5
0
        /// <inheritdoc />
        public override async Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
        {
            TryCancelExecution();

            _cancellationTokenSource = new CancellationTokenSource();

            using (ct.Register(TryCancelExecution))
            {
                await base.Execute(_cancellationTokenSource.Token, parameter, command);
            }
        }
        /// <inheritdoc />
        public override async Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
        {
            try
            {
                await _semaphore.WaitAsync(ct);

                await base.Execute(ct, parameter, command);
            }
            finally
            {
                _semaphore.Release();
            }
        }
Beispiel #7
0
        /// <inheritdoc/>
        public IDynamicCommand Build()
        {
            if (_command != null)
            {
                throw new InvalidOperationException("This builder already built a command. A DynamicCommandBuilder can only build a single command because strategy instances cannot be shared.");
            }

            var strategy = GetStrategy(BaseStrategy, Strategies);

            _command = new DynamicCommand(Name, strategy);

            return(_command);
        }
Beispiel #8
0
 /// <inheritdoc />
 public override async Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
 {
     if (Interlocked.CompareExchange(ref _isExecuting, 1, 0) == 0)
     {
         try
         {
             await base.Execute(ct, parameter, command);
         }
         finally
         {
             _isExecuting = 0;
         }
     }
 }
Beispiel #9
0
        /// <inheritdoc />
        public override async Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
        {
            try
            {
                _logger.LogDebug($"Executing command '{command.Name}'.");

                await base.Execute(ct, parameter, command);

                _logger.LogInformation($"Executed command '{command.Name}'.");
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Failed to execute command '{command.Name}'.");

                throw;
            }
        }
Beispiel #10
0
 /// <summary>
 /// Executes the specified <see cref="IDynamicCommand"/> without a parameter.
 /// </summary>
 /// <param name="command"><see cref="IDynamicCommand"/></param>
 /// <returns><see cref="Task"/></returns>
 public static Task Execute(this IDynamicCommand command)
 {
     return(command.Execute(null));
 }
Beispiel #11
0
 /// <summary>
 /// Adds a <see cref="IDynamicCommand"/> trigger.
 /// </summary>
 /// <remarks>
 /// The name of the trigger is the name of the <paramref name="command"/>.
 /// </remarks>
 /// <typeparam name="TBuilder">The type of builder.</typeparam>
 /// <param name="dataLoaderBuilder">This builder.</param>
 /// <param name="command">The dynamic command causing the triggers.</param>
 /// <param name="triggerExecution">The trigger options.</param>
 /// <returns><see cref="IDataLoaderBuilder"/></returns>
 public static TBuilder TriggerOnCommandExecution <TBuilder>(
     this TBuilder dataLoaderBuilder,
     IDynamicCommand command,
     CommandTriggerOptions triggerExecution = CommandTriggerOptions.TriggerBeforeCommandExecution)
     where TBuilder : IDataLoaderBuilder
 => dataLoaderBuilder.WithTrigger(new DynamicCommandDataLoaderTrigger(command, triggerExecution));
Beispiel #12
0
 /// <inheritdoc />
 public override Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
 {
     return(Task.Run(() => base.Execute(ct, parameter, command)));
 }
 /// <inheritdoc />
 public Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
 => _execute(ct, parameter);
Beispiel #14
0
 /// <inheritdoc/>
 public Task HandleError(CancellationToken ct, IDynamicCommand command, Exception exception)
 => _handlerFunction.Invoke(ct, command, exception);
 /// <inheritdoc />
 public bool CanExecute(object parameter, IDynamicCommand command) => true;
 public bool CanExecute(object parameter, IDynamicCommand command) => _onCanExecute != null
                 ? _onCanExecute.Invoke(parameter, command)
                 : true;
            public override Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
            {
                _testAction();

                return(base.Execute(ct, parameter, command));
            }
 public Task Execute(CancellationToken ct, object parameter, IDynamicCommand command) => _onExecute != null
                 ? _onExecute.Invoke(ct, parameter, command)
                 : Task.CompletedTask;
Beispiel #19
0
 /// <summary>
 /// Gets and observes if the <see cref="IDynamicCommand"/> is currently executing.
 /// </summary>
 /// <param name="command"><see cref="IDynamicCommand"/></param>
 /// <returns>Observable of boolean</returns>
 public static IObservable <bool> GetAndObserveIsExecuting(this IDynamicCommand command)
 {
     return(ObserveIsExecuting(command).StartWith(command.IsExecuting));
 }
        /// <inheritdoc />
        public Task Execute(CancellationToken ct, object parameter, IDynamicCommand command)
        {
            _execute(parameter);

            return(Task.CompletedTask);
        }