private void EmojiSet_Click(object sender, RoutedEventArgs e)
        {
            var radio    = sender as RadioButton;
            var emojiSet = radio.Tag as EmojiSet;

            _selectedSet = emojiSet;

            var file = emojiSet.Document;

            if (file.Local.IsDownloadingCompleted)
            {
            }
            else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
            {
                _protoService.DownloadFile(file.Id, 32);
            }
        }
        private void EmojiSet_Click(object sender, RoutedEventArgs e)
        {
            var radio    = sender as RadioButton;
            var emojiSet = radio.Tag as EmojiSet;

            _selectedSet = emojiSet;

            var file = emojiSet.Document;

            if (file.Local.IsFileExisting())
            {
            }
            else
            {
                UpdateManager.Subscribe(radio, _protoService, file, UpdateFile);

                if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
                {
                    _protoService.DownloadFile(file.Id, 32);
                }
            }
        }
        private void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
        {
            if (args.InRecycleQueue)
            {
                return;
            }

            var radio     = args.ItemContainer.ContentTemplateRoot as RadioButton;
            var content   = radio.Content as Grid;
            var emojiPack = args.Item as EmojiSet;

            radio.IsChecked = SettingsService.Current.Appearance.EmojiSet.Id == emojiPack.Id;

            if (SettingsService.Current.Appearance.EmojiSet.Id == emojiPack.Id)
            {
                _selectedSet = emojiPack;
            }

            radio.Tag   = emojiPack;
            content.Tag = emojiPack;

            if (args.Phase == 0)
            {
                var title = content.Children[1] as TextBlock;
                title.Text = emojiPack.Title;
            }
            else if (args.Phase == 1)
            {
                var subtitle = content.Children[2] as TextBlock;
                var file     = emojiPack.Document;

                var size = Math.Max(file.Size, file.ExpectedSize);
                if (file.Local.IsDownloadingActive)
                {
                    subtitle.Text       = string.Format("{0} {1} / {2}", "Downloading", FileSizeConverter.Convert(file.Local.DownloadedSize, size), FileSizeConverter.Convert(size));
                    subtitle.Foreground = App.Current.Resources["SystemControlDisabledChromeDisabledLowBrush"] as Brush;
                }
                else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingCompleted)
                {
                    subtitle.Text       = string.Format("{0} {1}", Strings.Resources.AccActionDownload, FileSizeConverter.Convert(size));
                    subtitle.Foreground = App.Current.Resources["SystemControlDisabledChromeDisabledLowBrush"] as Brush;
                }
                else
                {
                    if (SettingsService.Current.Appearance.EmojiSet.Id == emojiPack.Id)
                    {
                        subtitle.Text       = "Current Set";
                        subtitle.Foreground = App.Current.Resources["SystemControlForegroundAccentBrush"] as Brush;
                    }
                    else
                    {
                        subtitle.Text       = emojiPack.IsDefault ? Strings.Resources.Default : "Downloaded";
                        subtitle.Foreground = App.Current.Resources["SystemControlDisabledChromeDisabledLowBrush"] as Brush;
                    }
                }
            }
            else if (args.Phase == 2)
            {
                var photo = content.Children[0] as Image;

                var file = emojiPack.Thumbnail;
                if (file != null && file.Local.IsDownloadingCompleted)
                {
                    photo.Source = new BitmapImage {
                        UriSource = new Uri("file:///" + file.Local.Path), DecodePixelWidth = 40, DecodePixelHeight = 40
                    };
                }
                else if (file != null && file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
                {
                    photo.Source = null;
                    _protoService.DownloadFile(file.Id, 1);
                }
            }

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

            args.Handled = true;
        }
        public async Task <IList <EmojiSet> > GetCloudSetsAsync()
        {
            var chat = await _protoService.SendAsync(new SearchPublicChat("cGFnbGlhY2Npb19kaV9naGlhY2Npbw")) as Chat;

            if (chat == null)
            {
                return(new EmojiSet[0]);
            }

            await _protoService.SendAsync(new OpenChat(chat.Id));

            var response = await _protoService.SendAsync(new SearchChatMessages(chat.Id, string.Empty, 0, 0, 0, 100, new SearchMessagesFilterDocument())) as Messages;

            if (response == null)
            {
                _protoService.Send(new CloseChat(chat.Id));
                return(new EmojiSet[0]);
            }

            _protoService.Send(new CloseChat(chat.Id));

            var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("emoji", CreationCollisionOption.OpenIfExists);

            var dict       = new Dictionary <string, List <EmojiSet> >();
            var thumbnails = new Dictionary <string, File>();

            var results = new List <EmojiSet>();

            foreach (var message in response.MessagesValue)
            {
                var document = message.Content as MessageDocument;
                if (document == null)
                {
                    continue;
                }

                var hashtags = new List <string>();
                var title    = string.Empty;

                foreach (var entity in document.Caption.Entities)
                {
                    if (entity.Type is TextEntityTypeHashtag)
                    {
                        hashtags.Add(document.Caption.Text.Substring(entity.Offset, entity.Length));
                    }
                    else if (entity.Type is TextEntityTypeCode)
                    {
                        title = document.Caption.Text.Substring(entity.Offset, entity.Length);
                    }
                }

                if (!hashtags.Contains("#emoji"))
                {
                    continue;
                }

                if (hashtags.Contains("#preview"))
                {
                    if (dict.TryGetValue(document.Document.FileName, out List <EmojiSet> sets))
                    {
                        foreach (var set in sets)
                        {
                            _mapping[document.Document.DocumentValue.Id].Add(set);
                            set.Thumbnail = document.Document.DocumentValue;
                        }
                    }
                    else
                    {
                        thumbnails[document.Document.FileName] = document.Document.DocumentValue;
                    }
                }
                else
                {
                    var versionKey  = hashtags.FirstOrDefault(x => x.StartsWith("#v"));
                    var positionKey = hashtags.FirstOrDefault(x => x.StartsWith("#p"));

                    var version  = 1;
                    var position = 1;

                    if (int.TryParse(versionKey.Substring(2), out int v))
                    {
                        version = v;
                    }
                    if (int.TryParse(positionKey.Substring(2), out int p))
                    {
                        position = p;
                    }

                    var set = new EmojiSet
                    {
                        Id       = document.Document.FileName,
                        Title    = title,
                        Version  = version,
                        Document = document.Document.DocumentValue
                    };

                    _mapping[document.Document.DocumentValue.Id].Add(set);

                    if (thumbnails.TryGetValue(document.Document.FileName, out File thumbnail))
                    {
                        set.Thumbnail = thumbnail;
                    }

                    if (dict.TryGetValue(document.Document.FileName, out List <EmojiSet> sets))
                    {
                        sets.Add(set);
                    }
                    else
                    {
                        dict[document.Document.FileName] = new List <EmojiSet> {
                            set
                        };
                    }
                }
            }

            foreach (var sets in dict.Values)
            {
                var latest = sets.Max(x => x.Version);

                foreach (var set in sets)
                {
                    if (set.Version == latest)
                    {
                        if (set.Thumbnail.Local.IsDownloadingCompleted)
                        {
                            await TryCopyPartLocally(folder, set.Thumbnail.Local.Path, set.Id, 0, false);
                        }

                        if (set.Document.Local.IsDownloadingCompleted)
                        {
                            await TryCopyPartLocally(folder, set.Document.Local.Path, set.Id, set.Version, true);
                        }

                        results.Add(set);
                    }
                    else
                    {
                        // Delete the file from chat cache as it isn't needed anymore
                        _protoService.Send(new DeleteFileW(set.Document.Id));
                    }
                }
            }

            return(results);
        }