//--------------------------------------------------------Events:---------------------------------------------------------------------\\ #region --Events-- private void OnImagePropertyChanged(object sender, PropertyChangedEventArgs e) { ChatMessageImageReceivedModel img = SpeechBubbleViewModel.MODEL.Message.Message.imageReceived; if (img is null) { return; } switch (e.PropertyName) { case nameof(ChatMessageImageReceivedModel.state): case nameof(ChatMessageImageReceivedModel.targetFileName): case nameof(ChatMessageImageReceivedModel.targetFolderPath): if (img.state == DownloadState.DONE) { MODEL.ImagePath = img.GetFullPath(); } break; case nameof(ChatMessageImageReceivedModel.error): MODEL.ErrorText = img.error.ToString(); break; default: break; } }
private void LoadImageProperties(ChatMessageImageReceivedModel image) { if (image is not null) { MODEL.ErrorText = image.error.ToString(); // Only set the image path in case the image was successfully downloaded to prevent exceptions: MODEL.ImagePath = image.state == DownloadState.DONE ? image.GetFullPath() : null; } }
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task StartDownloadAsync(ChatMessageImageReceivedModel image) { await DOWNLOAD_SEMA.WaitAsync(); if (image.state != DownloadState.DOWNLOADING && image.state != DownloadState.QUEUED) { await DOWNLOAD_HANDLER.EnqueueDownloadAsync(image); } DOWNLOAD_SEMA.Release(); }
/// <summary> /// Converts the path to an BitmapImage. /// This is a workaround to open also images that are stored on a separate drive. /// </summary> /// <param name="imgModel">The actual image the <paramref name="path"/> corresponds to.</param> /// <param name="path">The absolute path to the image.</param> /// <returns>The BitmapImage representation of the current path object.</returns> private async Task <BitmapImage> GetBitmapImageAsync(ChatMessageImageReceivedModel imgModel, string path) { if (path is null) { return(null); } try { StorageFile file = await StorageFile.GetFileFromPathAsync(path); if (file is null) { return(null); } BitmapImage img = null; // Bitmap stuff has to be done in the UI thread, // so make sure we execute it there: Exception ex = null; await SharedUtils.CallDispatcherAsync(async() => { try { img = new BitmapImage(); img.SetSource(await file.OpenReadAsync()); } catch (Exception e) { ex = e; } }); // If loading the image failed log the exception: if (ex is not null) { Logger.Error("Failed to load image: " + path, ex); MODEL.ErrorText = "Failed to load image. Try downloading it again."; imgModel.state = DownloadState.ERROR; imgModel.Update(); return(null); } return(img); } catch (Exception e) { Logger.Error("Failed to load image: " + path, e); MODEL.ErrorText = "Failed to load image. Try downloading it again."; imgModel.state = DownloadState.ERROR; imgModel.Update(); return(null); } }
private void TryLoadingImageFromPath() { ChatMessageImageReceivedModel img = SpeechBubbleViewModel.MODEL.Message.Message.imageReceived; if (img.state == DownloadState.DONE) { if (loadImageCancellationSource is not null) { loadImageCancellationSource.Cancel(); } loadImageCancellationSource = new CancellationTokenSource(); Task.Run(async() => { MODEL.IsLoadingImage = true; MODEL.ImageBitmap = await GetBitmapImageAsync(img, MODEL.ImagePath); if (MODEL.ImageBitmap is null) { MODEL.IsLoadingImage = false; } }, loadImageCancellationSource.Token); } }
public void CancelDownload(ChatMessageImageReceivedModel image) { DOWNLOAD_HANDLER.CancelDownload(image); }
public Task RedownloadAsync(ChatMessageImageReceivedModel image) { return(DOWNLOAD_HANDLER.EnqueueDownloadAsync(image)); }