Ejemplo n.º 1
0
    /// <summary>
    /// Applies a retrieval method to the implementation. Sets missing properties in the process.
    /// </summary>
    /// <param name="builder">The builder.</param>
    /// <param name="retrievalMethod">The retrieval method.</param>
    /// <param name="executor">Used to modify properties in an undoable fashion.</param>
    /// <param name="handler">A callback object used when the the user needs to be informed about IO tasks.</param>
    /// <param name="localPath">An optional local file path where the <paramref name="retrievalMethod"/> has already been downloaded.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="WebException">A file could not be downloaded from the internet.</exception>
    /// <exception cref="IOException">There is a problem accessing <paramref name="localPath"/>.</exception>
    /// <exception cref="UnauthorizedAccessException">Read access to <paramref name="localPath"/> is not permitted.</exception>
    public static void Add(this IBuilder builder, DownloadRetrievalMethod retrievalMethod, ICommandExecutor executor, ITaskHandler handler, string?localPath = null)
    {
        #region Sanity checks
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }
        if (retrievalMethod == null)
        {
            throw new ArgumentNullException(nameof(retrievalMethod));
        }
        if (executor == null)
        {
            throw new ArgumentNullException(nameof(executor));
        }
        if (handler == null)
        {
            throw new ArgumentNullException(nameof(handler));
        }
        #endregion

        void Process(Stream stream)
        {
            retrievalMethod.SetMissing(executor, localPath);

            builder.Add(retrievalMethod, stream, handler);

            long size = stream.Length;

            if (retrievalMethod is Archive archive)
            {
                size -= archive.StartOffset;
            }
            if (retrievalMethod.Size != size)
            {
                executor.Execute(SetValueCommand.For(() => retrievalMethod.Size, newValue: size));
            }
        }

        if (localPath == null)
        {
            try
            {
                handler.RunTask(new DownloadFile(ModelUtils.GetAbsoluteHref(retrievalMethod.Href, executor.Path), Process));
            }
            #region Error handling
            catch (UriFormatException ex)
            {
                // Wrap exception since only certain exception types are allowed
                throw new WebException(ex.Message, ex);
            }
            #endregion
        }
        else
        {
            handler.RunTask(new ReadFile(localPath, Process));
        }
    }
Ejemplo n.º 2
0
#pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
        /// <summary>
        /// Add a image to builder.
        /// </summary>
        /// <remarks>
        /// Image routed to <see cref="Plugin.Toast.Droid.IPlatformSpecificExtension.SetLargeIcon(Android.Graphics.Bitmap)"/>
        /// or <see cref="Plugin.Toast.IOS.IPlatformSpecificExtension.AddAttachment(UserNotifications.UNNotificationAttachment)"/>
        /// or <see cref="IUwpExtension.AddAppLogoOverride(Uri, ToastGenericAppLogoCrop?, string?, bool?)"/>. If there a
        /// <see cref="ISnackbarExtension"/> or <see cref="IIosLocalNotificationExtension"/> then no action will performed.
        /// <seealso cref="IToastImageSourceFactory"/>
        /// </remarks>
        public static IBuilder AddImage(this IBuilder builder, ToastImageSource imageSource)
        => builder.Add(imageSource, Router.Route.Default);
Ejemplo n.º 3
0
    /// <summary>
    /// Applies a retrieval method to the implementation. Sets missing properties in the process.
    /// </summary>
    /// <param name="builder">The builder.</param>
    /// <param name="retrievalMethod">The retrieval method.</param>
    /// <param name="executor">Used to modify properties in an undoable fashion.</param>
    /// <param name="handler">A callback object used when the the user needs to be informed about IO tasks.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="WebException">A file could not be downloaded from the internet.</exception>
    public static void Add(this IBuilder builder, RetrievalMethod retrievalMethod, ICommandExecutor executor, ITaskHandler handler)
    {
        #region Sanity checks
        if (builder == null)
        {
            throw new ArgumentNullException(nameof(builder));
        }
        if (retrievalMethod == null)
        {
            throw new ArgumentNullException(nameof(retrievalMethod));
        }
        if (executor == null)
        {
            throw new ArgumentNullException(nameof(executor));
        }
        if (handler == null)
        {
            throw new ArgumentNullException(nameof(handler));
        }
        #endregion

        void Apply(IRecipeStep step)
        {
            switch (step)
            {
            case DownloadRetrievalMethod download:
                builder.Add(download, executor, handler);
                break;

            case RemoveStep remove:
                builder.Remove(remove);
                break;

            case RenameStep rename:
                builder.Rename(rename);
                break;

            case CopyFromStep copyFrom:
                builder.CopyFrom(copyFrom, handler);
                break;

            default:
                throw new NotSupportedException($"Unknown recipe step: ${step}");
            }
        }

        switch (retrievalMethod)
        {
        case DownloadRetrievalMethod download:
            Apply(download);
            break;

        case Recipe recipe:
            foreach (var step in recipe.Steps)
            {
                Apply(step);
            }
            break;

        default:
            throw new NotSupportedException($"Unknown retrieval method: ${retrievalMethod}");
        }
    }