/// <summary> /// Removes strategies matching <typeparamref name="TStrategy"/> from the builder. /// </summary> /// <typeparam name="TStrategy">Any type implementing <see cref="IDynamicCommandStrategy"/>.</typeparam> /// <param name="builder">The builder.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder WithoutStrategy <TStrategy>(this IDynamicCommandBuilder builder) where TStrategy : IDynamicCommandStrategy { var itemsToRevome = builder.Strategies.Where(s => s is TStrategy).ToList(); foreach (var item in itemsToRevome) { builder.Strategies.Remove(item); } return(builder); }
/// <summary> /// Adds a <paramref name="strategy"/> to the builder. /// </summary> /// <param name="builder">The builder.</param> /// <param name="strategy">The strategy to add.</param> /// <param name="wrapExisting">When true, the <paramref name="strategy"/> is added at the start of the list, so that it wraps all existing strategies already present in the list.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder WithStrategy(this IDynamicCommandBuilder builder, DecoratorCommandStrategy strategy, bool wrapExisting = false) { if (wrapExisting) { builder.Strategies.Insert(0, strategy); } else { builder.Strategies.Add(strategy); } return(builder); }
/// <summary> /// Will execute the command on a background thread. /// </summary> /// <param name="builder">The builder.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder OnBackgroundThread(this IDynamicCommandBuilder builder) => builder.WithStrategy(new BackgroundCommandStrategy());
/// <summary> /// Will cancel the previous command execution when executing the command. /// </summary> /// <param name="builder">The builder.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder CancelPrevious(this IDynamicCommandBuilder builder) => builder.WithStrategy(new CancelPreviousCommandStrategy());
/// <summary> /// Removes all strategies from the builder. /// </summary> /// <remarks> /// This can be usefull if you want to completely discard the default configuration. /// Note that the <see cref="IDynamicCommandBuilder.BaseStrategy"/> is not modified, only <see cref="IDynamicCommandBuilder.Strategies"/> is cleared. /// </remarks> /// <param name="builder">The builder.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder ClearStrategies(this IDynamicCommandBuilder builder) { builder.Strategies.Clear(); return(builder); }
/// <summary> /// Will attach the <see cref="ICommand.CanExecute(object)"/> to the specified <see cref="IDynamicProperty"/>. /// </summary> /// <param name="builder">The builder.</param> /// <param name="canExecute"><see cref="IDynamicProperty"/> that affects the CanExecute</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder WithCanExecute(this IDynamicCommandBuilder builder, IDynamicProperty <bool> canExecute) => builder.WithStrategy(new CanExecuteCommandStrategy(canExecute));
/// <summary> /// Will disable the command while it's executing. /// </summary> /// <param name="builder">The builder.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder DisableWhileExecuting(this IDynamicCommandBuilder builder) => builder.WithStrategy(new DisableWhileExecutingCommandStrategy());
/// <summary> /// Will catch any exception thrown by the execution of the command and delegate it to the specified error handler. /// </summary> /// <param name="builder">The builder.</param> /// <param name="errorHandler">Error handler</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder CatchErrors(this IDynamicCommandBuilder builder, Func <CancellationToken, IDynamicCommand, Exception, Task> errorHandler) => builder.WithStrategy(new ErrorHandlerCommandStrategy(new DynamicCommandErrorHandler(errorHandler)));
/// <summary> /// Will catch any exception thrown by the execution of the command and delegate it to the specified error handler. /// </summary> /// <param name="builder">The builder.</param> /// <param name="errorHandler">Error handler</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder CatchErrors(this IDynamicCommandBuilder builder, IDynamicCommandErrorHandler errorHandler) => builder.WithStrategy(new ErrorHandlerCommandStrategy(errorHandler));
/// <summary> /// Will add logs to the command execution. /// </summary> /// <param name="builder">The builder.</param> /// <param name="logger">Optional; the desired logger. If null is passed, a new one will be created using <see cref="DynamicMvvmConfiguration.LoggerFactory"/>.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder WithLogs(this IDynamicCommandBuilder builder, ILogger logger = null) => builder.WithStrategy(new LoggerCommandStrategy(logger ?? typeof(IDynamicCommand).Log()));
/// <summary> /// Will lock the command execution. /// </summary> /// <param name="builder">The builder.</param> /// <returns><see cref="IDynamicCommandBuilder"/></returns> public static IDynamicCommandBuilder Locked(this IDynamicCommandBuilder builder) => builder.WithStrategy(new LockCommandStrategy());