/// <summary> /// Begin a download for the requested <typeparamref name="TModel"/>. /// </summary> /// <param name="model">The <typeparamref name="TModel"/> to be downloaded.</param> /// <param name="minimiseDownloadSize">Whether this download should be optimised for slow connections. Generally means extras are not included in the download bundle.</param> /// <returns>Whether the download was started.</returns> public bool Download(TModel model, bool minimiseDownloadSize = false) { if (!canDownload(model)) { return(false); } var request = CreateDownloadRequest(model, minimiseDownloadSize); DownloadNotification notification = new DownloadNotification { Text = $"Downloading {request.Model}", }; request.DownloadProgressed += progress => { notification.State = ProgressNotificationState.Active; notification.Progress = progress; }; request.Success += filename => { Task.Factory.StartNew(async() => { // This gets scheduled back to the update thread, but we want the import to run in the background. var imported = await Import(notification, filename); // for now a failed import will be marked as a failed download for simplicity. if (!imported.Any()) { downloadFailed.Value = new WeakReference <ArchiveDownloadRequest <TModel> >(request); } currentDownloads.Remove(request); }, TaskCreationOptions.LongRunning); }; request.Failure += triggerFailure; notification.CancelRequested += () => { request.Cancel(); return(true); }; currentDownloads.Add(request); PostNotification?.Invoke(notification); api.PerformAsync(request); downloadBegan.Value = new WeakReference <ArchiveDownloadRequest <TModel> >(request); return(true); void triggerFailure(Exception error) { currentDownloads.Remove(request); downloadFailed.Value = new WeakReference <ArchiveDownloadRequest <TModel> >(request); notification.State = ProgressNotificationState.Cancelled; if (!(error is OperationCanceledException)) { Logger.Error(error, $"{HumanisedModelName.Titleize()} download failed!"); } } }
/// <summary> /// Begin a download for the requested <see cref="TModel"/>. /// </summary> /// <param name="model">The <see cref="TModel"/> to be downloaded.</param> /// <param name="minimiseDownloadSize">Whether this download should be optimised for slow connections. Generally means extras are not included in the download bundle.</param> /// <returns>Whether the download was started.</returns> public bool Download(TModel model, bool minimiseDownloadSize = false) { if (!canDownload(model)) { return(false); } var request = CreateDownloadRequest(model, minimiseDownloadSize); DownloadNotification notification = new DownloadNotification { Text = $"Downloading {request.Model}", }; request.DownloadProgressed += progress => { notification.State = ProgressNotificationState.Active; notification.Progress = progress; }; request.Success += filename => { Task.Factory.StartNew(async() => { // This gets scheduled back to the update thread, but we want the import to run in the background. await Import(notification, filename); currentDownloads.Remove(request); }, TaskCreationOptions.LongRunning); }; request.Failure += error => { DownloadFailed?.Invoke(request); if (error is OperationCanceledException) { return; } notification.State = ProgressNotificationState.Cancelled; Logger.Error(error, $"{HumanisedModelName.Titleize()} download failed!"); currentDownloads.Remove(request); }; notification.CancelRequested += () => { request.Cancel(); currentDownloads.Remove(request); notification.State = ProgressNotificationState.Cancelled; return(true); }; currentDownloads.Add(request); PostNotification?.Invoke(notification); Task.Factory.StartNew(() => request.Perform(api), TaskCreationOptions.LongRunning); DownloadBegan?.Invoke(request); return(true); }