private static ImageSource ReturnOrEnqueueSticker(TLDocument22 document, TLStickerItem sticker)
        {
            if (document == null)
            {
                return(null);
            }

            var documentLocalFileName = document.GetFileName();

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(documentLocalFileName))
                {
                    TLObject owner = document;
                    if (sticker != null)
                    {
                        owner = sticker;
                    }

                    // 1. download full size
                    IoC.Get <IDocumentFileManager>().DownloadFileAsync(document.FileName, document.DCId, document.ToInputFileLocation(), owner, document.Size, progress => { });

                    // 2. download preview
                    var thumbCachedSize = document.Thumb as TLPhotoCachedSize;
                    if (thumbCachedSize != null)
                    {
                        var fileName = "cached" + document.GetFileName();
                        var buffer   = thumbCachedSize.Bytes.Data;
                        if (buffer == null)
                        {
                            return(null);
                        }

                        return(DecodeWebPImage(fileName, buffer, () => { }));
                    }

                    var thumbPhotoSize = document.Thumb as TLPhotoSize;
                    if (thumbPhotoSize != null)
                    {
                        var location = thumbPhotoSize.Location as TLFileLocation;
                        if (location != null)
                        {
                            return(ReturnOrEnqueueStickerPreview(location, sticker, thumbPhotoSize.Size));
                        }
                    }
                }
                else
                {
                    if (document.DocumentSize > 0 &&
                        document.DocumentSize < Telegram.Api.Constants.StickerMaxSize)
                    {
                        byte[] buffer;
                        using (var file = store.OpenFile(documentLocalFileName, FileMode.Open))
                        {
                            buffer = new byte[file.Length];
                            file.Read(buffer, 0, buffer.Length);
                        }

                        return(DecodeWebPImage(documentLocalFileName, buffer,
                                               () =>
                        {
                            using (var localStore = IsolatedStorageFile.GetUserStoreForApplication())
                            {
                                localStore.DeleteFile(documentLocalFileName);
                            }
                        }));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        private void GetStickerHints(string text)
        {
            var stickersCache      = new Dictionary <long, long>();
            var stickers           = new List <TLStickerItem>();
            var currentStickerText = text.Trim();

            var allStickers43 = StateService.GetAllStickers() as TLAllStickers43;
            var key           = string.Format("{0}\ashowStickersByEmoji={1}", text, allStickers43 != null ? allStickers43.ShowStickersByEmoji : ShowStickersByEmoji.AllSets);

            if (allStickers43 != null)
            {
                if (allStickers43.ShowStickersByEmoji == ShowStickersByEmoji.AllSets ||
                    allStickers43.ShowStickersByEmoji == ShowStickersByEmoji.MySets)
                {
                    var favedDict     = new Dictionary <long, long>();
                    var favedStickers = allStickers43.FavedStickers;
                    if (favedStickers != null)
                    {
                        for (var i = 0; i < favedStickers.Documents.Count; i++)
                        {
                            favedDict[favedStickers.Documents[i].Index] = favedStickers.Documents[i].Index;
                        }
                    }

                    // 1. 5 most recently used (flag=[my sets, all sets])
                    var recentStickers = allStickers43.RecentStickers;
                    if (recentStickers != null)
                    {
                        const int maxRecentCount = 5;
                        var       recentCount    = 0;
                        for (var i = 0; i < recentStickers.Documents.Count && recentCount < maxRecentCount; i++)
                        {
                            var sticker = recentStickers.Documents[i] as TLDocument22;
                            if (sticker != null && sticker.Emoticon == text && !stickersCache.ContainsKey(sticker.Index) && !favedDict.ContainsKey(sticker.Index))
                            {
                                stickers.Add(new TLStickerItem {
                                    Document = sticker
                                });
                                stickersCache[sticker.Index] = sticker.Index;
                                recentCount++;
                            }
                        }
                    }

                    // 2. faved stickers (flag=[my sets, all sets])
                    if (favedStickers != null)
                    {
                        for (var i = 0; i < favedStickers.Documents.Count; i++)
                        {
                            var sticker = favedStickers.Documents[i] as TLDocument22;
                            if (sticker != null && sticker.Emoticon == text && !stickersCache.ContainsKey(sticker.Index))
                            {
                                stickers.Add(new TLStickerItem {
                                    Document = sticker
                                });
                                stickersCache[sticker.Index] = sticker.Index;
                            }
                        }
                    }

                    // 3. installed stickers (flag=[my sets, all sets])
                    var stickerPack = GetStickerPack(currentStickerText);
                    if (stickerPack != null)
                    {
                        for (var i = 0; i < stickerPack.Documents.Count; i++)
                        {
                            var sticker = Stickers.Documents.FirstOrDefault(x => x.Id.Value == stickerPack.Documents[i].Value);
                            if (sticker != null && !stickersCache.ContainsKey(sticker.Index))
                            {
                                stickers.Add(new TLStickerItem {
                                    Document = sticker
                                });
                                stickersCache[sticker.Index] = sticker.Index;
                            }
                        }
                    }
                }

                if (allStickers43.ShowStickersByEmoji == ShowStickersByEmoji.AllSets)
                {
                    // 4. featured stickers (flag=[all sets])

                    /*var featuredPack = GetFeaturedStickerPack(currentStickerText);
                     * if (featuredPack != null)
                     * {
                     *  var featuredStickers = StateService.GetFeaturedStickers();
                     *  for (var i = 0; i < featuredPack.Documents.Count; i++)
                     *  {
                     *      var sticker = featuredStickers.Documents.FirstOrDefault(x => x.Id.Value == featuredPack.Documents[i].Value);
                     *      if (sticker != null && !stickersCache.ContainsKey(sticker.Index))
                     *      {
                     *          stickers.Add(new TLStickerItem { Document = sticker });
                     *          stickersCache[sticker.Index] = sticker.Index;
                     *      }
                     *  }
                     * }*/

                    // 5. search on server side
                    MTProtoService.GetStickersAsync(new TLString(currentStickerText), new TLInt(0),
                                                    result => Execute.BeginOnUIThread(() =>
                    {
                        var stickersResult = result as TLStickers79;
                        if (stickersResult != null)
                        {
                            if (!string.IsNullOrEmpty(Text))
                            {
                                string searchText;
                                var searchByStickers = SearchStickerHints(Text, out searchText);
                                if (searchByStickers)
                                {
                                    if (string.Equals(currentStickerText, searchText))
                                    {
                                        if (StickerHints.Hints.Count == MaxResults)
                                        {
                                            IList <TLStickerItem> cachedResult;
                                            if (_cachedStickerResults.TryGetValue(key, out cachedResult))
                                            {
                                                for (var i = 0; i < stickersResult.Stickers.Count; i++)
                                                {
                                                    var item = new TLStickerItem {
                                                        Document = stickersResult.Stickers[i]
                                                    };

                                                    cachedResult.Add(item);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            IList <TLStickerItem> cachedResult;
                                            if (_cachedStickerResults.TryGetValue(key, out cachedResult))
                                            {
                                                for (var i = 0; i < stickersResult.Stickers.Count; i++)
                                                {
                                                    var item = new TLStickerItem {
                                                        Document = stickersResult.Stickers[i]
                                                    };

                                                    cachedResult.Add(item);
                                                    StickerHints.Hints.Add(item);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }),
                                                    error => Execute.BeginOnUIThread(() =>
                    {
                        Execute.ShowDebugMessage("messages.getStickers error " + error);
                    }));
                }
            }

            _cachedStickerResults[key] = stickers;

            ClearStickerHints();

            if (stickers.Count > 0)
            {
                CreateStickerHints();

                for (var i = 0; i < stickers.Count; i++)
                {
                    if (i == MaxResults)
                    {
                        break;
                    }

                    StickerHints.Hints.Add(stickers[i]);
                }
            }
        }