예제 #1
0
        public async void UpdateFile(File file)
        {
            foreach (Sticker sticker in List.Items)
            {
                if (sticker.UpdateFile(file) && file.Id == sticker.Thumbnail?.Photo.Id)
                {
                    var container = List.ContainerFromItem(sticker) as SelectorItem;
                    if (container == null)
                    {
                        continue;
                    }

                    var content = container.ContentTemplateRoot as Grid;
                    if (content == null)
                    {
                        continue;
                    }

                    var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                    var buffer = await FileIO.ReadBufferAsync(temp);

                    var photo = content.Children[0] as Image;
                    photo.Source = WebPImage.DecodeFromBuffer(buffer);
                }
            }
        }
예제 #2
0
        public async void UpdateFile(MessageViewModel message, File file)
        {
            var sticker = GetContent(message.Content);

            if (sticker == null)
            {
                return;
            }

            if (sticker.Thumbnail != null && sticker.Thumbnail.Photo.Id == file.Id)
            {
                UpdateThumbnail(message, file);
                return;
            }
            else if (sticker.StickerValue.Id != file.Id)
            {
                return;
            }

            if (file.Local.IsDownloadingCompleted)
            {
                var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                var buffer = await FileIO.ReadBufferAsync(temp);

                Texture.Source = WebPImage.DecodeFromBuffer(buffer);
            }
            else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
            {
                message.ProtoService.Send(new DownloadFile(file.Id, 1));
            }
        }
예제 #3
0
        public async Task <InputMessageFactory> CreateDocumentAsync(StorageFile file, bool asFile)
        {
            var generated = await file.ToGeneratedAsync();

            var thumbnail = new InputThumbnail(await file.ToGeneratedAsync(ConversionType.DocumentThumbnail), 0, 0);

            if (!asFile && file.FileType.Equals(".webp", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    var buffer = await FileIO.ReadBufferAsync(file);

                    var webp = WebPImage.DecodeFromBuffer(buffer);

                    var width  = webp.PixelWidth;
                    var height = webp.PixelHeight;

                    return(new InputMessageFactory
                    {
                        InputFile = generated,
                        Type = new FileTypeSticker(),
                        Delegate = (inputFile, caption) => new InputMessageSticker(inputFile, null, width, height, string.Empty)
                    });
                }
                catch
                {
                    // Not really a sticker, go on sending as a file
                }
            }
            else if (!asFile && file.FileType.Equals(".tgs", StringComparison.OrdinalIgnoreCase))
            {
                // TODO
            }
            else if (!asFile && file.ContentType.StartsWith("audio/", StringComparison.OrdinalIgnoreCase))
            {
                var props = await file.Properties.GetMusicPropertiesAsync();

                var duration = (int)props.Duration.TotalSeconds;

                var title     = props.Title;
                var performer = string.IsNullOrEmpty(props.AlbumArtist) ? props.Artist : props.AlbumArtist;

                return(new InputMessageFactory
                {
                    InputFile = generated,
                    Type = new FileTypeAudio(),
                    Delegate = (inputFile, caption) => new InputMessageAudio(inputFile, thumbnail, duration, title, performer, caption)
                });
            }

            return(new InputMessageFactory
            {
                InputFile = generated,
                Type = new FileTypeDocument(),
                Delegate = (inputFile, caption) => new InputMessageDocument(inputFile, thumbnail, true, caption)
            });
        }
예제 #4
0
        private async void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
        {
            if (args.InRecycleQueue)
            {
                return;
            }

            var content    = args.ItemContainer.ContentTemplateRoot as Grid;
            var stickerSet = args.Item as StickerSetInfo;

            if (args.Phase == 0)
            {
                var title = content.Children[1] as TextBlock;
                title.Text = stickerSet.Title;
            }
            else if (args.Phase == 1)
            {
                var subtitle = content.Children[2] as TextBlock;
                subtitle.Text = Locale.Declension("Stickers", stickerSet.Size);
            }
            else if (args.Phase == 2)
            {
                var photo = content.Children[0] as Image;

                var cover = stickerSet.Covers.FirstOrDefault();
                if (cover == null || cover.Thumbnail == null)
                {
                    return;
                }

                var file = cover.Thumbnail.Photo;
                if (file.Local.IsDownloadingCompleted)
                {
                    var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                    var buffer = await FileIO.ReadBufferAsync(temp);

                    photo.Source = WebPImage.DecodeFromBuffer(buffer);
                }
                else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
                {
                    photo.Source = null;
                    ViewModel.ProtoService.Send(new DownloadFile(file.Id, 1));
                }
            }

            if (args.Phase < 2)
            {
                args.RegisterUpdateCallback(OnContainerContentChanging);
            }

            args.Handled = true;
        }
예제 #5
0
        private async void Stickers_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
        {
            var content = args.ItemContainer.ContentTemplateRoot as Image;

            if (args.InRecycleQueue)
            {
                content.Source = null;
                return;
            }

            var sticker = args.Item as ViewModels.Dialogs.StickerViewModel;

            if (sticker == null || sticker.Thumbnail == null)
            {
                content.Source = null;
                return;
            }

            //if (args.Phase < 2)
            //{
            //    content.Source = null;
            //    args.RegisterUpdateCallback(Stickers_ContainerContentChanging);
            //}
            //else
            if (args.Phase == 0)
            {
                Debug.WriteLine("Loading sticker " + sticker.StickerData.Id + " for sticker set id " + sticker.SetId);

                var file = sticker.Thumbnail.Photo;
                if (file.Local.IsDownloadingCompleted)
                {
                    var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                    var buffer = await FileIO.ReadBufferAsync(temp);

                    content.Source = WebPImage.DecodeFromBuffer(buffer);
                }
                else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
                {
                    DownloadFile(file.Id, sticker);
                }
            }
            else
            {
                throw new System.Exception("We should be in phase 0, but we are not.");
            }

            args.Handled = true;
        }
예제 #6
0
        public static async Task <ImageSource> GetWebpAsync(string path)
        {
            if (ApiInfo.CanDecodeWebp)
            {
                return(new BitmapImage(new Uri("file:///" + path)));
            }
            else
            {
                var temp = await StorageFile.GetFileFromPathAsync(path);

                var buffer = await FileIO.ReadBufferAsync(temp);

                return(WebPImage.DecodeFromBuffer(buffer));
            }
        }
예제 #7
0
        private async void UpdateThumbnail(MessageViewModel message, File file)
        {
            if (file.Local.IsDownloadingCompleted)
            {
                var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                var buffer = await FileIO.ReadBufferAsync(temp);

                Background = new ImageBrush {
                    ImageSource = WebPImage.DecodeFromBuffer(buffer)
                };
            }
            else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
            {
                message.ProtoService.Send(new DownloadFile(file.Id, 1));
            }
        }
예제 #8
0
        private async void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
        {
            if (args.InRecycleQueue)
            {
                return;
            }

            var content = args.ItemContainer.ContentTemplateRoot as Image;
            var sticker = args.Item as Sticker;

            if (sticker == null || sticker.Thumbnail == null)
            {
                content.Source = null;
                return;
            }

            if (args.Phase < 2)
            {
                content.Source = null;
                args.RegisterUpdateCallback(OnContainerContentChanging);
            }
            else
            {
                var file = sticker.Thumbnail.Photo;
                if (file.Local.IsDownloadingCompleted)
                {
                    var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                    var buffer = await FileIO.ReadBufferAsync(temp);

                    content.Source = WebPImage.DecodeFromBuffer(buffer);
                }
                else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
                {
                    ViewModel.ProtoService.Send(new DownloadFile(1, file.Id));
                }
            }

            args.Handled = true;
        }
예제 #9
0
        private async void Toolbar_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
        {
            if (args.InRecycleQueue)
            {
                return;
            }

            var content = args.ItemContainer.ContentTemplateRoot as Image;
            var sticker = args.Item as ViewModels.Dialogs.StickerSetViewModel;

            if (content == null || sticker == null || sticker.Covers == null)
            {
                return;
            }

            var cover = sticker.Covers.FirstOrDefault();

            if (cover == null || cover.Thumbnail == null)
            {
                content.Source = null;
                return;
            }

            var file = cover.Thumbnail.Photo;

            if (file.Local.IsDownloadingCompleted)
            {
                var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                var buffer = await FileIO.ReadBufferAsync(temp);

                content.Source = WebPImage.DecodeFromBuffer(buffer);
            }
            else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
            {
                //DownloadFile(file.Id, cover);
                ViewModel.ProtoService.Send(new DownloadFile(file.Id, 1));
            }
        }
예제 #10
0
        public async Task <InputMessageFactory> CreateDocumentAsync(StorageFile file)
        {
            var generated = await file.ToGeneratedAsync();

            var thumbnail = new InputThumbnail(await file.ToGeneratedAsync(ConversionType.DocumentThumbnail), 0, 0);

            if (file.FileType.Equals(".webp", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    var buffer = await FileIO.ReadBufferAsync(file);

                    var webp = WebPImage.DecodeFromBuffer(buffer);

                    var width  = webp.PixelWidth;
                    var height = webp.PixelHeight;

                    return(new InputMessageFactory {
                        InputFile = generated, Type = new FileTypeSticker(), Delegate = (inputFile, caption) => new InputMessageSticker(inputFile, null, width, height)
                    });
                }
                catch
                {
                    // Not really a sticker, go on sending as a file
                }
            }
            else if (file.FileType.Equals(".mp3", StringComparison.OrdinalIgnoreCase))
            {
                var props = await file.Properties.GetMusicPropertiesAsync();

                return(new InputMessageFactory {
                    InputFile = generated, Type = new FileTypeAudio(), Delegate = (inputFile, caption) => new InputMessageAudio(inputFile, thumbnail, (int)props.Duration.TotalSeconds, props.Title, props.Artist, caption)
                });
            }

            return(new InputMessageFactory {
                InputFile = generated, Type = new FileTypeDocument(), Delegate = (inputFile, caption) => new InputMessageDocument(inputFile, thumbnail, caption)
            });
        }
예제 #11
0
        private async void LoadVisibleStickers()
        {
            var scrollingHost = Stickers.ItemsPanelRoot as ItemsWrapGrid;

            if (scrollingHost == null)
            {
                return;
            }

            if (scrollingHost.FirstVisibleIndex < 0)
            {
                return;
            }

            var lastSet = 0L;

            for (int i = scrollingHost.FirstVisibleIndex; i <= scrollingHost.LastVisibleIndex; i++)
            {
                if (i >= Stickers.Items.Count)
                {
                    return;
                }

                var first = Stickers.Items[i] as ViewModels.Dialogs.StickerViewModel;
                if (first == null || first.SetId == lastSet)
                {
                    continue;
                }

                lastSet = first.SetId;

                var fromItem = Stickers.ContainerFromItem(first);
                if (fromItem == null)
                {
                    continue;
                }

                var header = Stickers.GroupHeaderContainerFromItemContainer(fromItem) as GridViewHeaderItem;
                if (header == null)
                {
                    continue;
                }

                var group = header.Content as ViewModels.Dialogs.StickerSetViewModel;
                if (group == null || group.IsLoaded)
                {
                    continue;
                }

                group.IsLoaded = true;

                Debug.WriteLine("Loading sticker set " + group.Id);

                var response = await ViewModel.ProtoService.SendAsync(new GetStickerSet(group.Id));

                if (response is StickerSet full)
                {
                    group.Update(full);
                    //group.Stickers.RaiseCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));

                    int j = 0;
                    foreach (var sticker in group.Stickers)
                    {
                        if (sticker.Thumbnail == null)
                        {
                            continue;
                        }

                        //group.Stickers.RaiseCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Move, sticker, j, j));
                        //j++;

                        var container = Stickers.ContainerFromItem(sticker) as SelectorItem;
                        if (container == null)
                        {
                            continue;
                        }

                        var content = container.ContentTemplateRoot as Image;

                        var file = sticker.Thumbnail.Photo;
                        if (file.Local.IsDownloadingCompleted)
                        {
                            var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                            var buffer = await FileIO.ReadBufferAsync(temp);

                            content.Source = WebPImage.DecodeFromBuffer(buffer);
                        }
                        else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
                        {
                            DownloadFile(file.Id, sticker);
                        }
                    }
                }
            }
        }
예제 #12
0
        public async void UpdateFile(File file)
        {
            if (!file.Local.IsDownloadingCompleted)
            {
                return;
            }

            if (_stickers.TryGetValue(file.Id, out List <ViewModels.Dialogs.StickerViewModel> items) && items.Count > 0)
            {
                var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                var buffer = await FileIO.ReadBufferAsync(temp);

                foreach (var item in items)
                {
                    item.UpdateFile(file);

                    var container = Stickers.ContainerFromItem(item) as SelectorItem;
                    if (container == null)
                    {
                        return;
                    }

                    var content = container.ContentTemplateRoot as Image;
                    content.Source = WebPImage.DecodeFromBuffer(buffer);
                }
            }

            foreach (MosaicMediaRow line in GifsView.Items)
            {
                var any = false;
                foreach (var item in line)
                {
                    if (item.Item is Animation animation && animation.UpdateFile(file))
                    {
                        any = true;
                    }
                }

                if (!any)
                {
                    continue;
                }

                var container = GifsView.ContainerFromItem(line) as SelectorItem;
                if (container == null)
                {
                    continue;
                }

                var content = container.ContentTemplateRoot as MosaicRow;
                if (content == null)
                {
                    continue;
                }

                content.UpdateFile(line, file);
            }

            foreach (ViewModels.Dialogs.StickerSetViewModel stickerSet in Toolbar.Items)
            {
                if (stickerSet.Covers == null)
                {
                    continue;
                }

                var cover = stickerSet.Covers.FirstOrDefault();
                if (cover == null || cover.Thumbnail == null)
                {
                    continue;
                }

                var container = Toolbar.ContainerFromItem(stickerSet) as SelectorItem;
                if (container == null)
                {
                    continue;
                }

                var content = container.ContentTemplateRoot as Image;
                if (content == null)
                {
                    continue;
                }

                if (cover.UpdateFile(file))
                {
                    var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);

                    var buffer = await FileIO.ReadBufferAsync(temp);

                    content.Source = WebPImage.DecodeFromBuffer(buffer);
                }
            }

            //foreach (ViewModels.Dialogs.StickerViewModel item in Stickers.Items)
            //{
            //    if (item.UpdateFile(file) && file.Local.IsDownloadingCompleted)
            //    {
            //        var container = Stickers.ContainerFromItem(item) as SelectorItem;
            //        if (container == null)
            //        {
            //            continue;
            //        }

            //        var content = container.ContentTemplateRoot as Image;
            //        if (item.Thumbnail.Photo.Id == file.Id)
            //        {
            //            var temp = await StorageFile.GetFileFromPathAsync(file.Local.Path);
            //            var buffer = await FileIO.ReadBufferAsync(temp);

            //            content.Source = WebPImage.DecodeFromBuffer(buffer);
            //        }
            //    }
            //}
        }