Beispiel #1
0
 private void LoadImageProperties(ChatMessageImageModel image)
 {
     if (!(image is 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(ChatMessageImageModel image)
        {
            await DOWNLOAD_SEMA.WaitAsync();

            if (image.state != DownloadState.DOWNLOADING && image.state != DownloadState.QUEUED)
            {
                await DOWNLOAD_HANDLER.EnqueueDownloadAsync(image);
            }
            DOWNLOAD_SEMA.Release();
        }
Beispiel #3
0
        /// <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(ChatMessageImageModel 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 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);
            }
        }
Beispiel #4
0
        //--------------------------------------------------------Events:---------------------------------------------------------------------\\
        #region --Events--
        private void OnImagePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ChatMessageImageModel img = SpeechBubbleViewModel.MODEL.Message.Message.image;

            switch (e.PropertyName)
            {
            case nameof(ChatMessageImageModel.state):
            case nameof(ChatMessageImageModel.targetFileName):
            case nameof(ChatMessageImageModel.targetFolderPath):
                if (img.state == DownloadState.DONE)
                {
                    MODEL.ImagePath = img.GetFullPath();
                }
                break;

            case nameof(ChatMessageImageModel.error):
                MODEL.ErrorText = img.error.ToString();
                break;

            default:
                break;
            }
        }
Beispiel #5
0
        private void TryLoadingImageFromPath()
        {
            ChatMessageImageModel img = SpeechBubbleViewModel.MODEL.Message.Message.image;

            if (img.state == DownloadState.DONE)
            {
                if (!(loadImageCancellationSource is 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(ChatMessageImageModel image)
 {
     DOWNLOAD_HANDLER.CancelDownload(image);
 }
 public async Task RedownloadAsync(ChatMessageImageModel image)
 {
     await DOWNLOAD_HANDLER.EnqueueDownloadAsync(image);
 }