Ejemplo n.º 1
0
        public void Handle(DownloadableItem item)
        {
            var document = item.Owner as TLDocument;

            if (document != null)
            {
                BeginOnUIThread(() =>
                {
                    var messages = Items;
                    foreach (var m in messages)
                    {
                        var media = m.Media as TLMessageMediaDocument;
                        if (media != null && TLDocumentBase.DocumentEquals(media.Document, document))
                        {
                            media.NotifyOfPropertyChange(() => media.Document);
                            break;
                        }
                    }
                });
            }

            var message = item.Owner as TLMessage;

            if (message != null)
            {
                var mediaDocument1 = message.Media as TLMessageMediaDocument;
                if (mediaDocument1 == null)
                {
                    return;
                }

                BeginOnUIThread(() =>
                {
                    foreach (var m in Items)
                    {
                        var mediaDocument2 = m.Media as TLMessageMediaDocument;
                        if (mediaDocument2 != null && TLDocumentBase.DocumentEquals(mediaDocument1.Document, mediaDocument2.Document))
                        {
                            m.Media.IsCanceled          = false;
                            m.Media.LastProgress        = 0.0;
                            m.Media.DownloadingProgress = 0.0;
                            m.Media.NotifyOfPropertyChange(() => m.Media.Self); // update download icon for documents
                            m.NotifyOfPropertyChange(() => m.Self);
                            m.Media.IsoFileName = item.IsoFileName;
                        }
                    }
                });
            }
        }
        public void Handle(DownloadableItem item)
        {
            Execute.BeginOnUIThread(() =>
            {
                var chatPhoto = item.Owner as TLChatPhoto;
                if (chatPhoto != null)
                {
                    var channel = With as TLChannel;
                    if (channel != null)
                    {
                        channel.NotifyOfPropertyChange(() => channel.Photo);
                        //channel.NotifyOfPropertyChange(() => channel.ChatPhoto);
                    }

                    var serviceMessages = Items.OfType <TLMessageService>();
                    foreach (var serviceMessage in serviceMessages)
                    {
                        var editPhoto = serviceMessage.Action as TLMessageActionChatEditPhoto;
                        if (editPhoto != null && editPhoto.Photo == chatPhoto)
                        {
                            editPhoto.NotifyOfPropertyChange(() => editPhoto.Photo);
                            break;
                        }
                    }
                }

                var message = item.Owner as TLMessage;
                if (message != null)
                {
                    var messages = Items.OfType <TLMessage>();
                    foreach (var m in messages)
                    {
                        var media = m.Media as TLMessageMediaVideo;
                        if (media != null && m == item.Owner)
                        {
                            m.Media.IsCanceled          = false;
                            m.Media.LastProgress        = 0.0;
                            m.Media.DownloadingProgress = 0.0;
                            m.NotifyOfPropertyChange(() => m.Self);
                            m.Media.IsoFileName = item.IsoFileName;
                            break;
                        }

                        var doc = m.Media as TLMessageMediaDocument;
                        if (doc != null && m == item.Owner)
                        {
                            m.Media.IsCanceled          = false;
                            m.Media.LastProgress        = 0.0;
                            m.Media.DownloadingProgress = 0.0;
                            m.Media.NotifyOfPropertyChange(() => m.Media.Self); // update download icon for documents
                            m.NotifyOfPropertyChange(() => m.Self);
                            m.Media.IsoFileName = item.IsoFileName;
                            break;
                        }

                        var audioMedia = m.Media as TLMessageMediaAudio;
                        if (audioMedia != null && m == item.Owner)
                        {
                            m.Media.IsCanceled          = false;
                            m.Media.LastProgress        = 0.0;
                            m.Media.DownloadingProgress = 0.0;
                            m.Media.IsoFileName         = item.IsoFileName;

                            var a = audioMedia.Audio as TLAudio;
                            if (a != null)
                            {
                                var fileName    = a.GetFileName();
                                var wavFileName = Path.GetFileNameWithoutExtension(fileName) + ".wav";
#if WP8
                                var component = new WindowsPhoneRuntimeComponent();
                                var result    = component.InitPlayer(ApplicationData.Current.LocalFolder.Path + "\\" + fileName);
                                if (result == 1)
                                {
                                    var buffer    = new byte[16 * 1024];
                                    var args      = new int[3];
                                    var pcmStream = new MemoryStream();
                                    while (true)
                                    {
                                        component.FillBuffer(buffer, buffer.Length, args);
                                        var count       = args[0];
                                        var offset      = args[1];
                                        var endOfStream = args[2] == 1;

                                        pcmStream.Write(buffer, 0, count);
                                        if (endOfStream)
                                        {
                                            break;
                                        }
                                    }

                                    var wavStream = Wav.GetWavAsMemoryStream(pcmStream, 48000, 1, 16);
                                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                                    {
                                        using (var file = new IsolatedStorageFileStream(wavFileName, FileMode.OpenOrCreate, store))
                                        {
                                            wavStream.Seek(0, SeekOrigin.Begin);
                                            wavStream.CopyTo(file);
                                            file.Flush();
                                        }
                                    }
                                }
#endif
                            }

                            break;
                        }
                    }
                    return;
                }

                var photo = item.Owner as TLPhoto;
                if (photo != null)
                {
                    var isUpdated = false;
                    var messages  = Items.OfType <TLMessage>();
                    foreach (var m in messages)
                    {
                        var mediaPhoto = m.Media as TLMessageMediaPhoto;
                        if (mediaPhoto != null && mediaPhoto.Photo == photo)
                        {
                            mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Photo);
                            mediaPhoto.NotifyOfPropertyChange(() => mediaPhoto.Self);
                            isUpdated = true;
                            break;
                        }

                        var mediaWebPage = m.Media as TLMessageMediaWebPage;
                        if (mediaWebPage != null && mediaWebPage.Photo == photo)
                        {
                            mediaWebPage.NotifyOfPropertyChange(() => mediaWebPage.Photo);
                            mediaWebPage.NotifyOfPropertyChange(() => mediaWebPage.Self);
                            isUpdated = false;
                            break;
                        }
                    }

                    if (isUpdated)
                    {
                        return;
                    }

                    var serviceMessages = Items.OfType <TLMessageService>();
                    foreach (var serviceMessage in serviceMessages)
                    {
                        var editPhoto = serviceMessage.Action as TLMessageActionChatEditPhoto;
                        if (editPhoto != null && editPhoto.Photo == photo)
                        {
                            editPhoto.NotifyOfPropertyChange(() => editPhoto.Photo);
                            isUpdated = true;
                            break;
                        }
                    }
                }

                var document = item.Owner as TLDocument;
                if (document != null)
                {
                    var messages = Items.OfType <TLMessage>();
                    foreach (var m in messages)
                    {
                        var media = m.Media as TLMessageMediaDocument;
                        if (media != null && TLDocumentBase.DocumentEquals(media.Document, document))
                        {
                            media.NotifyOfPropertyChange(() => media.Document);
                            break;
                        }
                    }
                }

                var video = item.Owner as TLVideo;
                if (video != null)
                {
                    var messages = Items.OfType <TLMessage>();
                    foreach (var m in messages)
                    {
                        var media = m.Media as TLMessageMediaVideo;
                        if (media != null && media.Video == video)
                        {
                            media.NotifyOfPropertyChange(() => media.Video);
                            break;
                        }
                    }
                }

                var audio = item.Owner as TLAudio;
                if (audio != null)
                {
                    var messages = Items.OfType <TLMessage>();
                    foreach (var m in messages)
                    {
                        var media = m.Media as TLMessageMediaAudio;
                        if (media != null && media.Audio == audio)
                        {
                            media.NotifyOfPropertyChange(() => media.Audio);
                            break;
                        }
                    }
                }

                var webPage = item.Owner as TLWebPage;
                if (webPage != null)
                {
                    var messages = Items.OfType <TLMessage>();
                    foreach (var m in messages)
                    {
                        var media = m.Media as TLMessageMediaWebPage;
                        if (media != null && media.WebPage == webPage)
                        {
                            media.NotifyOfPropertyChange(() => media.Photo);
                            media.NotifyOfPropertyChange(() => media.Self);
                            break;
                        }
                    }
                }
            });
        }