예제 #1
0
        private string UpdateGlyph(TLDocument document)
        {
            Visibility = Visibility.Visible;

            var fileName = document.GetFileName();

            if (File.Exists(FileUtils.GetTempFileName(fileName)))
            {
                if (TLMessage.IsVideo(document) || TLMessage.IsRoundVideo(document) || TLMessage.IsMusic(document))
                {
                    return("\uE102");
                }
                else if (TLMessage.IsGif(document))
                {
                    Visibility = Visibility.Collapsed;
                    return("\uE102");
                }

                return("\uE160");
            }
            else if (document.IsTransferring)
            {
                return("\uE10A");
            }
            else if (document.DownloadingProgress > 0 && document.DownloadingProgress < 1)
            {
                return("\uE10A");
            }
            else if (document.UploadingProgress > 0 && document.DownloadingProgress < 1)
            {
                return("\uE10A");
            }

            return("\uE118");
        }
예제 #2
0
        public void Play(IEnumerable <TLBotInlineResultBase> items, bool auto)
        {
            var news = new Dictionary <string, MediaPlayerItem>();

            foreach (var message in items)
            {
                var container = Items.ContainerFromItem(message) as GridViewItem;
                if (container == null)
                {
                    continue;
                }

                if (message is TLBotInlineMediaResult mediaResult && mediaResult.Type.Equals("gif", StringComparison.OrdinalIgnoreCase))
                {
                    var document = mediaResult.Document as TLDocument;
                    if (document == null || !TLMessage.IsGif(document))
                    {
                        continue;
                    }

                    var fileName = document.GetFileName();
                    if (File.Exists(FileUtils.GetTempFileName(fileName)))
                    {
                        var root = container.ContentTemplateRoot as FrameworkElement;
                        if (root is Grid final)
                        {
                            news[FileUtils.GetTempFileUrl(fileName)] = new MediaPlayerItem {
                                Container = final, Watermark = false
                            };
                        }
                    }
                }
예제 #3
0
        protected static bool IsInlineMedia(TLMessageMediaBase media)
        {
            if (media == null)
            {
                return(false);
            }

            if (media.TypeId == TLType.MessageMediaContact)
            {
                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaGeoLive)
            {
                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaVenue)
            {
                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaPhoto)
            {
                var photoMedia = media as TLMessageMediaPhoto;
                if (string.IsNullOrWhiteSpace(photoMedia.Caption))
                {
                    return(false);
                }

                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaDocument)
            {
                var documentMedia = media as TLMessageMediaDocument;
                if (TLMessage.IsMusic(documentMedia.Document))
                {
                    return(true);
                }
                else if (TLMessage.IsVoice(documentMedia.Document))
                {
                    return(true);
                }
                else if (TLMessage.IsVideo(documentMedia.Document))
                {
                    if (string.IsNullOrWhiteSpace(documentMedia.Caption))
                    {
                        return(false);
                    }
                }
                else if (TLMessage.IsGif(documentMedia.Document))
                {
                    if (string.IsNullOrWhiteSpace(documentMedia.Caption))
                    {
                        return(false);
                    }
                }

                return(true);
            }

            return(false);
        }
예제 #4
0
        protected static bool IsFullMedia(TLMessageMediaBase media, bool width = false)
        {
            if (media == null)
            {
                return(false);
            }

            if (media.TypeId == TLType.MessageMediaGeo)
            {
                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaGeoLive)
            {
                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaVenue)
            {
                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaPhoto)
            {
                return(true);
            }
            else if (media.TypeId == TLType.MessageMediaDocument)
            {
                var documentMedia = media as TLMessageMediaDocument;
                if (TLMessage.IsGif(documentMedia.Document))
                {
                    return(true);
                }
                else if (TLMessage.IsVideo(documentMedia.Document))
                {
                    return(true);
                }
            }
            else if (media.TypeId == TLType.MessageMediaInvoice && width)
            {
                var invoiceMedia = media as TLMessageMediaInvoice;
                if (invoiceMedia.HasPhoto && invoiceMedia.Photo != null)
                {
                    return(true);
                }
            }
            //else if (media.TypeId == TLType.MessageMediaWebPage && width)
            //{
            //    var webPageMedia = media as TLMessageMediaWebPage;
            //    var webPage = webPageMedia.WebPage as TLWebPage;
            //    if (webPage != null && MediaTemplateSelector.IsWebPagePhotoTemplate(webPage))
            //    {
            //        return true;
            //    }
            //}

            return(false);
        }
예제 #5
0
 private void OnClick(object sender, RoutedEventArgs e)
 {
     if (Transferable is TLPhoto photo)
     {
         if (photo.Full is TLPhotoSize photoSize)
         {
             var fileName = string.Format("{0}_{1}_{2}.jpg", photoSize.Location.VolumeId, photoSize.Location.LocalId, photoSize.Location.Secret);
             if (File.Exists(FileUtils.GetTempFileName(fileName)))
             {
                 Update();
                 Completed?.Invoke(this, new TransferCompletedEventArgs(fileName));
             }
             else
             {
                 if (photo.IsTransferring)
                 {
                     photo.Cancel(UnigramContainer.Current.ResolveType <IDownloadFileManager>(), UnigramContainer.Current.ResolveType <IUploadFileManager>());
                 }
                 else
                 {
                     var context = DefaultPhotoConverter.BitmapContext[photo, false];
                 }
             }
         }
     }
     else if (Transferable is TLDocument document)
     {
         var fileName = document.GetFileName();
         if (File.Exists(FileUtils.GetTempFileName(fileName)))
         {
             Update();
             Completed?.Invoke(this, new TransferCompletedEventArgs(fileName));
         }
         else
         {
             if (document.IsTransferring)
             {
                 document.Cancel(ChooseDownloadManager(document), ChooseUploadManager(document));
             }
             else
             {
                 if (TLMessage.IsGif(document))
                 {
                     var context = DefaultPhotoConverter.BitmapContext[document, null];
                 }
                 else
                 {
                     var manager = ChooseDownloadManager(document);
                     document.DownloadAsync(manager);
                 }
             }
         }
     }
 }
예제 #6
0
        public TLBitmapSource(TLDocument document, bool thumbnail)
        {
            _source = document;

            if (TLMessage.IsSticker(document))
            {
                if (thumbnail)
                {
                    SetWebPSource(null, document.Thumb, PHASE_THUMBNAIL);
                    return;
                }

                if (TrySetWebPSource(document, PHASE_FULL))
                {
                    return;
                }

                SetWebPSource(null, document.Thumb, PHASE_THUMBNAIL);
                SetWebPSource(document, document, document.Size, PHASE_FULL);
            }
            else if (TLMessage.IsGif(document))
            {
                SetSource(null, document.Thumb, PHASE_THUMBNAIL);

                if (ApplicationSettings.Current.AutoDownload[_protoService.NetworkType].HasFlag(AutoDownloadType.GIF))
                {
                    SetDownloadSource(document, document, document.Size, PHASE_FULL);
                }
            }
            else if (TLMessage.IsVideo(document))
            {
                SetSource(null, document.Thumb, PHASE_THUMBNAIL);

                if (ApplicationSettings.Current.AutoDownload[_protoService.NetworkType].HasFlag(AutoDownloadType.Video))
                {
                    SetDownloadSource(document, document, document.Size, PHASE_FULL);
                }
            }
            else if (TLMessage.IsRoundVideo(document))
            {
                SetSource(null, document.Thumb, PHASE_THUMBNAIL);

                if (ApplicationSettings.Current.AutoDownload[_protoService.NetworkType].HasFlag(AutoDownloadType.Round))
                {
                    SetDownloadSource(document, document, document.Size, PHASE_FULL);
                }
            }
            else
            {
                SetSource(null, document.Thumb, PHASE_THUMBNAIL);
            }
        }
예제 #7
0
        public TLBitmapSource(TLDocument document, bool thumbnail)
        {
            _source = document;

            if (TLMessage.IsSticker(document))
            {
                if (thumbnail)
                {
                    SetWebPSource(null, document.Thumb, PHASE_THUMBNAIL);
                    return;
                }

                if (TrySetWebPSource(document, PHASE_FULL) == false)
                {
                    SetWebPSource(null, document.Thumb, PHASE_THUMBNAIL);
                    SetWebPSource(document, document, document.Size, PHASE_FULL);
                }
            }
            else if (TLMessage.IsGif(document))
            {
                if (thumbnail)
                {
                    SetSource(null, document.Thumb, PHASE_THUMBNAIL);
                    return;
                }

                _renderer = _animatedFactory.CreateRenderer(320, 320);
                Image     = _renderer.ImageSource;

                if (TrySetAnimatedSource(document, PHASE_FULL) == false && ApplicationSettings.Current.AutoDownload[_protoService.NetworkType].HasFlag(AutoDownloadType.GIF))
                {
                    SetAnimatedSource(document, document, document.Size, PHASE_FULL);
                }
            }
            else if (TLMessage.IsVideo(document))
            {
                SetSource(null, document.Thumb, PHASE_THUMBNAIL);

                if (ApplicationSettings.Current.AutoDownload[_protoService.NetworkType].HasFlag(AutoDownloadType.Video))
                {
                    //SetSource(photo, photo.Full, PHASE_FULL);
                }
            }
            else
            {
                SetSource(null, document.Thumb, PHASE_THUMBNAIL);
            }
        }
예제 #8
0
 public void Download()
 {
     if (PHASE_FULL > Phase && _source is TLPhoto photo)
     {
         SetSource(photo, photo.Full, PHASE_FULL);
     }
     else if (PHASE_FULL > Phase && _source is TLDocument document)
     {
         if (TLMessage.IsSticker(document))
         {
             SetWebPSource(document, document, document.Size, PHASE_FULL);
         }
         else if (TLMessage.IsGif(document))
         {
             SetAnimatedSource(document, document, document.Size, PHASE_FULL);
         }
     }
 }
        public void Play(TLMessage message)
        {
            var document = message.GetDocument();

            if (document == null || !TLMessage.IsGif(document))
            {
                return;
            }

            var fileName = FileUtils.GetTempFileUrl(document.GetFileName());

            if (_old.ContainsKey(fileName))
            {
                Play(new TLMessage[0], false);
            }
            else
            {
                Play(new[] { message }, true);
            }
        }
예제 #10
0
        protected static bool IsFullMedia(TLMessageMediaBase media, bool width = false)
        {
            if (media == null)
            {
                return(false);
            }

            if (media is TLMessageMediaGeo)
            {
                return(true);
            }
            else if (media is TLMessageMediaGeoLive)
            {
                return(true);
            }
            else if (media is TLMessageMediaVenue)
            {
                return(true);
            }
            else if (media is TLMessageMediaPhoto)
            {
                return(true);
            }
            else if (media is TLMessageMediaDocument documentMedia)
            {
                if (TLMessage.IsGif(documentMedia.Document))
                {
                    return(true);
                }
                else if (TLMessage.IsVideo(documentMedia.Document))
                {
                    return(true);
                }
            }
            else if (media is TLMessageMediaInvoice invoiceMedia && width)
            {
                if (invoiceMedia.HasPhoto && invoiceMedia.Photo != null)
                {
                    return(true);
                }
            }
예제 #11
0
        private string UpdateGlyph(TLDocument document)
        {
            Visibility = Visibility.Visible;

            var fileName = document.GetFileName();

            if (File.Exists(FileUtils.GetTempFileName(fileName)))
            {
                var message = DataContext as TLMessage;
                if (message != null && message.Media is TLMessageMediaDocument documentMedia && documentMedia.HasTTLSeconds)
                {
                    return("\uE60D");
                }

                if (TLMessage.IsVideo(document) || TLMessage.IsRoundVideo(document) || TLMessage.IsMusic(document))
                {
                    return("\uE102");
                }
                else if (TLMessage.IsGif(document))
                {
                    Visibility = Visibility.Collapsed;
                    return("\uE102");
                }

                return("\uE160");
            }
            else if (document.IsTransferring)
            {
                return("\uE10A");
            }
            else if (document.DownloadingProgress > 0 && document.DownloadingProgress < 1)
            {
                return("\uE10A");
            }
            else if (document.UploadingProgress > 0 && document.DownloadingProgress < 1)
            {
                return("\uE10A");
            }

            return("\uE118");
        }
        public static void OpenDocumentCommon(TLMessage message, IStateService stateService, IDocumentFileManager documentFileManager, System.Action openGifCallback)
#endif
        {
            var mediaDocument = message.Media as TLMessageMediaDocument;

            if (mediaDocument != null)
            {
                if (!string.IsNullOrEmpty(mediaDocument.IsoFileName))
                {
                }

                var document = mediaDocument.Document as TLDocument;
                if (document == null)
                {
                    return;
                }

                var documentLocalFileName = document.GetFileName();
                var store = IsolatedStorageFile.GetUserStoreForApplication();
#if WP81
                var documentFile = mediaDocument.File ?? await GetStorageFile(mediaDocument);
#endif

                if (!store.FileExists(documentLocalFileName)
#if WP81
                    && documentFile == null
#endif
                    )
                {
                    if (document.Size.Value == 0)
                    {
                        return;
                    }

                    mediaDocument.IsCanceled          = false;
                    mediaDocument.DownloadingProgress = mediaDocument.LastProgress > 0.0 ? mediaDocument.LastProgress : 0.001;
                    //_downloadVideoStopwatch = Stopwatch.StartNew();
                    //return;
                    documentFileManager.DownloadFileAsync(
                        document.FileName, document.DCId, document.ToInputFileLocation(), message, document.Size,
                        progress =>
                    {
                        if (progress > 0.0)
                        {
                            mediaDocument.DownloadingProgress = progress;
                        }
                    });
                }
                else
                {
                    if (message.IsGif())
                    {
                        if (documentFile != null && File.Exists(documentFile.Path))
                        {
                            mediaDocument.DownloadingProgress = 0.001;
                            await documentFile.CopyAsync(ApplicationData.Current.LocalFolder, documentLocalFileName, NameCollisionOption.ReplaceExisting);

                            mediaDocument.DownloadingProgress = 0.0;
                        }

                        return;
                    }

                    if (documentLocalFileName.EndsWith(".gif") ||
                        string.Equals(document.MimeType.ToString(), "image/gif", StringComparison.OrdinalIgnoreCase))
                    {
                        openGifCallback.SafeInvoke();

                        return;
                    }

                    if (documentLocalFileName.EndsWith(".mp3") ||
                        string.Equals(document.MimeType.ToString(), "audio/mpeg", StringComparison.OrdinalIgnoreCase))
                    {
                        var url         = new Uri(documentLocalFileName, UriKind.Relative);
                        var title       = document.FileName.ToString();
                        var performer   = "Unknown Artist";
                        var readId3Tags = true;
#if WP81
                        try
                        {
                            var storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(documentLocalFileName);

                            var audioProperties = await storageFile.Properties.GetMusicPropertiesAsync();

                            title       = audioProperties.Title;
                            performer   = audioProperties.Artist;
                            readId3Tags = false;
                        }
                        catch (Exception ex) { }
#endif
#if WP81
                        if (documentFile == null)
                        {
                            try
                            {
                                documentFile = await ApplicationData.Current.LocalFolder.GetFileAsync(documentLocalFileName);
                            }
                            catch (Exception ex)
                            {
                                Execute.ShowDebugMessage("LocalFolder.GetFileAsync docLocal exception \n" + ex);
                            }
                        }
                        Launcher.LaunchFileAsync(documentFile);
                        return;
#elif WP8
                        var file = await ApplicationData.Current.LocalFolder.GetFileAsync(documentLocalFileName);

                        Launcher.LaunchFileAsync(file);
                        return;
#endif

                        //if (readId3Tags)
                        //{
                        //    if (store.FileExists(documentLocalFileName))
                        //    {
                        //        using (var file = store.OpenFile(documentLocalFileName, FileMode.Open, FileAccess.Read))
                        //        {
                        //            var mp3Stream = new Mp3Stream(file);
                        //            if (mp3Stream.HasTags)
                        //            {
                        //                var tag = mp3Stream.GetTag(Id3TagFamily.FileStartTag);
                        //                title = tag.Title;
                        //                performer = tag.Artists;
                        //            }
                        //        }
                        //    }
                        //}

                        //var track = BackgroundAudioPlayer.Instance.Track;
                        //if (track == null || track.Source != url)
                        //{
                        //    BackgroundAudioPlayer.Instance.Track = new AudioTrack(url, title, performer, null, null);
                        //}
                        //BackgroundAudioPlayer.Instance.Play();

                        return;
                    }
                    else
                    {
#if WP81
                        if (documentFile == null)
                        {
                            try
                            {
                                documentFile = await ApplicationData.Current.LocalFolder.GetFileAsync(documentLocalFileName);
                            }
                            catch (Exception ex)
                            {
                                Execute.ShowDebugMessage("LocalFolder.GetFileAsync docLocal exception \n" + ex);
                            }
                        }
                        Launcher.LaunchFileAsync(documentFile);
                        return;
#elif WP8
                        var file = await ApplicationData.Current.LocalFolder.GetFileAsync(documentLocalFileName);

                        Launcher.LaunchFileAsync(file);
                        return;
#endif
                    }
                }
                return;
            }
        }
예제 #13
0
 public DialogGalleryViewModel(TLInputPeerBase peer, TLMessage selected, IMTProtoService protoService)
     : base(protoService, null, null)
 {
     if (selected.Media is TLMessageMediaPhoto photoMedia || selected.IsVideo() || selected.IsRoundVideo() || selected.IsGif())
     {
         Items = new MvxObservableCollection <GalleryItem> {
             new GalleryMessageItem(selected)
         };
         SelectedItem = Items[0];
         FirstItem    = Items[0];
     }
        private void SendCompressedVideoInternal(TLMessage message, object o)
#endif
        {
            var documentMedia = message.Media as TLMessageMediaDocument45;

            if (documentMedia != null)
            {
                var fileName = documentMedia.IsoFileName;
                if (string.IsNullOrEmpty(fileName))
                {
                    return;
                }

                var video = documentMedia.Document as TLDocument22;
                if (video == null)
                {
                    return;
                }


                byte[] thumbBytes = null;
                var    photoThumb = video.Thumb as TLPhotoSize;
                if (photoThumb != null)
                {
                    var location = photoThumb.Location as TLFileLocation;
                    if (location == null)
                    {
                        return;
                    }

                    var thumbFileName = String.Format("{0}_{1}_{2}.jpg",
                                                      location.VolumeId,
                                                      location.LocalId,
                                                      location.Secret);

                    using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (var thumbFile = storage.OpenFile(thumbFileName, FileMode.Open, FileAccess.Read))
                        {
                            thumbBytes = new byte[thumbFile.Length];
                            thumbFile.Read(thumbBytes, 0, thumbBytes.Length);
                        }
                    }
                }

                var fileId = message.Media.FileId ?? TLLong.Random();
                message.Media.FileId            = fileId;
                message.Media.UploadingProgress = 0.001;
                //return;
#if WP81
                if (file != null)
                {
                    UploadVideoFileManager.UploadFile(fileId, message.IsGif(), message, file);
                }
                else if (!string.IsNullOrEmpty(fileName))
                {
                    UploadVideoFileManager.UploadFile(fileId, message, fileName);
                }
                else
                {
                    return;
                }
#else
                UploadVideoFileManager.UploadFile(fileId, message, fileName);
#endif

                if (thumbBytes != null)
                {
                    var fileId2 = TLLong.Random();
                    UploadFileManager.UploadFile(fileId2, message.Media, thumbBytes);
                }

                return;
            }

            var videoMedia = message.Media as TLMessageMediaVideo;
            if (videoMedia != null)
            {
                var fileName = videoMedia.IsoFileName;
                if (string.IsNullOrEmpty(fileName))
                {
                    return;
                }

                var video = videoMedia.Video as TLVideo;
                if (video == null)
                {
                    return;
                }


                byte[] thumbBytes = null;
                var    photoThumb = video.Thumb as TLPhotoSize;
                if (photoThumb != null)
                {
                    var location = photoThumb.Location as TLFileLocation;
                    if (location == null)
                    {
                        return;
                    }

                    var thumbFileName = String.Format("{0}_{1}_{2}.jpg",
                                                      location.VolumeId,
                                                      location.LocalId,
                                                      location.Secret);

                    using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (var thumbFile = storage.OpenFile(thumbFileName, FileMode.Open, FileAccess.Read))
                        {
                            thumbBytes = new byte[thumbFile.Length];
                            thumbFile.Read(thumbBytes, 0, thumbBytes.Length);
                        }
                    }
                }

                var fileId = message.Media.FileId ?? TLLong.Random();
                message.Media.FileId            = fileId;
                message.Media.UploadingProgress = 0.001;

#if WP81
                if (file != null)
                {
                    UploadVideoFileManager.UploadFile(fileId, message.IsGif(), message, file);
                }
                else if (!string.IsNullOrEmpty(fileName))
                {
                    UploadVideoFileManager.UploadFile(fileId, message, fileName);
                }
                else
                {
                    return;
                }
#else
                UploadVideoFileManager.UploadFile(fileId, message, fileName);
#endif

                if (thumbBytes != null)
                {
                    var fileId2 = TLLong.Random();
                    UploadFileManager.UploadFile(fileId2, message.Media, thumbBytes);
                }

                return;
            }
        }
예제 #15
0
        public static async void Download_Click(FrameworkElement sender, TransferCompletedEventArgs e)
        {
            var element = sender as FrameworkElement;

            var bubble = element.Ancestors <MessageBubbleBase>().FirstOrDefault() as MessageBubbleBase;

            if (bubble == null)
            {
                return;
            }

            if (element.DataContext is TLMessageService serviceMessage && serviceMessage.Action is TLMessageActionChatEditPhoto editPhotoAction)
            {
                var media = element.Parent as FrameworkElement;
                if (media == null)
                {
                    media = element;
                }

                var chat = serviceMessage.Parent as TLChatBase;
                if (chat == null)
                {
                    return;
                }

                var chatFull = InMemoryCacheService.Current.GetFullChat(chat.Id);
                if (chatFull != null && chatFull.ChatPhoto is TLPhoto && chat != null)
                {
                    var viewModel = new ChatPhotosViewModel(bubble.ContextBase.ProtoService, bubble.ContextBase.CacheService, chatFull, chat, serviceMessage);
                    await GalleryView.Current.ShowAsync(viewModel, () => media);
                }

                return;
            }

            var message = element.DataContext as TLMessage;

            if (message == null)
            {
                return;
            }

            var document = message.GetDocument();

            if (TLMessage.IsGif(document) && !ApplicationSettings.Current.IsAutoPlayEnabled)
            {
                var page = bubble.Ancestors <IGifPlayback>().FirstOrDefault() as IGifPlayback;
                if (page == null)
                {
                    return;
                }

                if (bubble.ViewModel is TLMessage inner)
                {
                    page.Play(inner);
                }
            }
            else if (TLMessage.IsVideo(document) || TLMessage.IsRoundVideo(document) || TLMessage.IsGif(document) || message.IsPhoto())
            {
                var media = element.Ancestors().FirstOrDefault(x => x is FrameworkElement && ((FrameworkElement)x).Name.Equals("Media")) as FrameworkElement;
                if (media == null)
                {
                    media = element;
                }

                //ConnectedAnimationService.GetForCurrentView().PrepareToAnimate("FullScreenPicture", media);

                GalleryViewModelBase viewModel;
                if (message.Parent == null || TLMessage.IsRoundVideo(document) || TLMessage.IsGif(document))
                {
                    viewModel = new SingleGalleryViewModel(new GalleryMessageItem(message));
                }
                else
                {
                    viewModel = new DialogGalleryViewModel(bubble.ContextBase.ProtoService, bubble.ContextBase.CacheService, message.Parent.ToInputPeer(), message);
                }

                await GalleryView.Current.ShowAsync(viewModel, () => media);
            }
            else if (e != null)
            {
                var file = await StorageFile.GetFileFromApplicationUriAsync(FileUtils.GetTempFileUri(e.FileName));

                await Launcher.LaunchFileAsync(file);
            }
        }
예제 #16
0
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            var boh = DataTemplate.GetExtensionInstance(container as FrameworkElement);

            var presenter = container as ContentControl;

            if (presenter != null && item is TLDocument doc)
            {
                presenter.Content = new TLMessage {
                    Media = new TLMessageMediaDocument {
                        Document = doc
                    }
                };
            }

            if (item is TLMessage message)
            {
                item = message.Media;
            }

            if (item is TLMessageMediaEmpty)
            {
                return(EmptyTemplate);
            }
            else if (item is TLMessageMediaContact)
            {
                return(ContactTemplate);
            }
            else if (item is TLMessageMediaPhoto)
            {
                return(PhotoTemplate);
            }
            else if (item is TLMessageMediaGame)
            {
                return(GameTemplate);
            }
            else if (item is TLMessageMediaVenue)
            {
                return(VenueTemplate);
            }
            else if (item is TLMessageMediaGeo)
            {
                return(GeoPointTemplate);
            }
            else if (item is TLMessageMediaInvoice invoiceMedia)
            {
                if (invoiceMedia.HasPhoto && invoiceMedia.Photo != null)
                {
                    return(InvoicePhotoTemplate);
                }

                return(InvoiceTemplate);
            }
            else if (item is TLMessageMediaDocument || item is TLDocument)
            {
                if (item is TLMessageMediaDocument documentMedia)
                {
                    item = documentMedia.Document;
                }

                if (item is TLDocument document)
                {
                    if (TLMessage.IsVoice(document))
                    {
                        return(AudioTemplate);
                    }
                    else if (TLMessage.IsVideo(document))
                    {
                        return(VideoTemplate);
                    }
                    else if (TLMessage.IsRoundVideo(document))
                    {
                        return(RoundVideoTemplate);
                    }
                    else if (TLMessage.IsGif(document))
                    {
                        return(GifTemplate);
                    }
                    else if (TLMessage.IsSticker(document))
                    {
                        return(StickerTemplate);
                    }
                    else if (TLMessage.IsMusic(document))
                    {
                        return(MusicTemplate);
                    }

                    // TODO: ???
                    //var externalDocument = documentMedia.Document as TLDocumentExternal;
                    //if (externalDocument != null && TLMessage.IsGif(externalDocument))
                    //{
                    //    return GifTemplate;
                    //}

                    if (document.Thumb != null && !(document.Thumb is TLPhotoSizeEmpty))
                    {
                        return(DocumentThumbTemplate);
                    }
                }

                return(DocumentTemplate);
            }
            else if (item is TLMessageMediaWebPage webpageMedia)
            {
                if (webpageMedia.WebPage is TLWebPageEmpty)
                {
                    return(EmptyTemplate);
                }
                else if (webpageMedia.WebPage is TLWebPagePending)
                {
                    return(EmptyTemplate);
                }
                else if (webpageMedia.WebPage is TLWebPage webpage)
                {
                    /*if (TLMessage.IsGif(webpage.Document))
                     * {
                     *  return WebPageGifTemplate;
                     * }
                     * else
                     * if (webpage.Document != null && webpage.Type.Equals("document", StringComparison.OrdinalIgnoreCase))
                     * {
                     *  return WebPageDocumentTemplate;
                     * }*/

                    if (webpage.Document != null)
                    {
                        return(WebPageDocumentTemplate);
                    }

                    if (webpage.Photo != null && webpage.Type != null)
                    {
                        if (IsWebPagePhotoTemplate(webpage))
                        {
                            return(WebPagePhotoTemplate);
                        }

                        return(WebPageSmallPhotoTemplate);
                    }
                }
                return(WebPageTemplate);
            }
            else if (item is TLMessageMediaUnsupported)
            {
                return(UnsupportedTemplate);
            }

            return(null);
        }
        public void Play(IEnumerable <TLMessage> items, bool auto)
        {
            var news = new Dictionary <string, MediaPlayerItem>();

            foreach (var message in items)
            {
                var container = Messages.ContainerFromItem(message) as ListViewItem;
                if (container == null)
                {
                    continue;
                }

                var document = message.GetDocument();
                if (document == null || !TLMessage.IsGif(document))
                {
                    continue;
                }

                var fileName = document.GetFileName();
                if (File.Exists(FileUtils.GetTempFileName(fileName)))
                {
                    var root = container.ContentTemplateRoot as FrameworkElement;
                    if (root is Grid grid)
                    {
                        root = grid.FindName("Bubble") as FrameworkElement;
                    }

                    var media = root.FindName("Media") as ContentControl;
                    var panel = media.ContentTemplateRoot as FrameworkElement;

                    if (message.Media is TLMessageMediaWebPage)
                    {
                        media = panel.FindName("Media") as ContentControl;
                        panel = media.ContentTemplateRoot as FrameworkElement;
                    }
                    else if (message.Media is TLMessageMediaGame)
                    {
                        panel = panel.FindName("Media") as FrameworkElement;
                    }

                    if (panel is Grid final)
                    {
                        news[FileUtils.GetTempFileUrl(fileName)] = new MediaPlayerItem {
                            Container = final, Watermark = message.Media is TLMessageMediaGame
                        };
                    }
                }
            }

            foreach (var item in _old.Keys.Except(news.Keys).ToList())
            {
                var presenter = _old[item].Presenter;
                if (presenter != null && presenter.MediaPlayer != null)
                {
                    presenter.MediaPlayer.Source = null;
                    presenter.MediaPlayer.Dispose();
                    presenter.MediaPlayer = null;
                }

                var container = _old[item].Container;
                if (container != null && presenter != null)
                {
                    container.Children.Remove(presenter);
                }

                _old.Remove(item);
            }

            if (!auto)
            {
                return;
            }

            foreach (var item in news.Keys.Except(_old.Keys).ToList())
            {
                var container = news[item].Container;
                if (container != null && container.Children.Count < 5)
                {
                    var player = new MediaPlayer();
                    player.AutoPlay                 = true;
                    player.IsMuted                  = true;
                    player.IsLoopingEnabled         = true;
                    player.CommandManager.IsEnabled = false;
                    player.Source = MediaSource.CreateFromUri(new Uri(item));

                    var presenter = new MediaPlayerView();
                    presenter.MediaPlayer      = player;
                    presenter.IsHitTestVisible = false;
                    presenter.Constraint       = container.DataContext;

                    news[item].Presenter = presenter;
                    container.Children.Insert(news[item].Watermark ? 2 : 2, presenter);
                }

                _old.Add(item, news[item]);
            }
        }