Beispiel #1
0
        public override void Search(string text)
        {
            _lastText = text;
            Status    = string.Empty;
            Items.Clear();

            if (string.IsNullOrEmpty(text))
            {
                Status = AppResources.NoResults;
                return;
            }

            var messages = CacheService.GetMessages().OfType <TLMessage>();

            var cache = new Dictionary <int, TLMessage>();

            LazyItems.Clear();
            foreach (var message in messages)
            {
                if (message.Message.Value.IndexOf(text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    var dialog = CacheService.GetDialog(message);
                    LazyItems.Add(new TLDialog24 {
                        With = dialog.With, TopMessage = message
                    });
                    cache[message.Index] = message;
                }
            }

            IsWorking          = true;
            _offset            = 0;
            _isLastSliceLoaded = false;
            MTProtoService.SearchAsync(
                new TLInputPeerEmpty(),
                new TLString(text),
                null,
                new TLInputMessagesFilterEmpty(),
                new TLInt(0),
                new TLInt(0),
                new TLInt(_offset),
                new TLInt(0),
                new TLInt(Limit),
                new TLInt(0),
                result =>
            {
                CacheService.AddChats(result.Chats, results => { });
                CacheService.AddUsers(result.Users, results => { });

                _offset  += Limit;
                IsWorking = false;

                var newMessages = result as TLMessages;
                if (newMessages != null)
                {
                    foreach (var message in newMessages.Messages.OfType <TLMessageCommon>())
                    {
                        if (cache.ContainsKey(message.Index))
                        {
                            continue;
                        }

                        var dialog = new TLDialog {
                            TopMessage = message
                        };
                        var peer = TLUtils.GetPeerFromMessage(message);
                        if (peer is TLPeerUser)
                        {
                            var user = newMessages.Users.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (user == null)
                            {
                                continue;
                            }
                            dialog.With = user;
                        }
                        else if (peer is TLPeerChat)
                        {
                            var chat = newMessages.Chats.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (chat == null)
                            {
                                continue;
                            }

                            dialog.With = chat;
                        }
                        else if (peer is TLPeerChannel)
                        {
                            var channel = newMessages.Chats.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (channel == null)
                            {
                                continue;
                            }

                            dialog.With = channel;
                        }
                        LazyItems.Add(dialog);
                    }
                }

                _lastCache = cache;
                if (Items.Count == 0 && LazyItems.Count == 0)
                {
                    Status = AppResources.NoResults;
                }

                Deployment.Current.Dispatcher.BeginInvoke(PopulateItems);
            },
                error =>
            {
                IsWorking = false;
            });

            Deployment.Current.Dispatcher.BeginInvoke(PopulateItems);
        }
Beispiel #2
0
        public void LoadNextSlice()
        {
            if (IsWorking)
            {
                return;
            }
            //if (LazyItems.Count > 0) return;
            if (_isLastSliceLoaded)
            {
                return;
            }

            var cache = _lastCache;
            var text  = _lastText;

            IsWorking = true;
            MTProtoService.SearchAsync(
                new TLInputPeerEmpty(),
                new TLString(_lastText),
                null,
                new TLInputMessagesFilterEmpty(),
                new TLInt(0),
                new TLInt(0),
                new TLInt(_offset),
                new TLInt(0),
                new TLInt(Limit),
                new TLInt(0),
                result =>
            {
                CacheService.AddChats(result.Chats, results => { });
                CacheService.AddUsers(result.Users, results => { });

                IsWorking = false;

                if (text != _lastText)
                {
                    return;
                }

                _offset           += Limit;
                _isLastSliceLoaded = result.Messages.Count < Limit;

                var dialogs     = new List <TLDialog>(result.Messages.Count);
                var newMessages = result as TLMessages;
                if (newMessages != null)
                {
                    foreach (var message in newMessages.Messages.OfType <TLMessageCommon>())
                    {
                        if (cache != null &&
                            cache.ContainsKey(message.Index))
                        {
                            continue;
                        }

                        var dialog = new TLDialog {
                            TopMessage = message
                        };
                        var peer = TLUtils.GetPeerFromMessage(message);
                        if (peer is TLPeerUser)
                        {
                            var user = newMessages.Users.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (user == null)
                            {
                                continue;
                            }
                            dialog.With = user;
                        }
                        else if (peer is TLPeerChat)
                        {
                            var chat = newMessages.Chats.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (chat == null)
                            {
                                continue;
                            }

                            dialog.With = chat;
                        }
                        dialogs.Add(dialog);
                        //LazyItems.Add(dialog);
                    }
                }

                if (Items.Count == 0 && dialogs.Count == 0 && LazyItems.Count == 0)
                {
                    Status = AppResources.NoResults;
                }

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    foreach (var dialog in dialogs)
                    {
                        Items.Add(dialog);
                    }
                });
                //Deployment.Current.Dispatcher.BeginInvoke(PopulateItems);
            },
                error =>
            {
                IsWorking = false;
            });
        }
        public void ProcessRepliesAndAudio(IList <TLMessageBase> messages)
        {
            for (var i = 0; i < messages.Count; i++)
            {
                var message = messages[i] as TLMessage;
                if (message != null)
                {
                    var mediaAudio = message.Media as TLMessageMediaAudio;
                    if (mediaAudio != null)
                    {
                        var audio = mediaAudio.Audio as TLAudio;
                        if (audio == null)
                        {
                            return;
                        }

                        var store         = IsolatedStorageFile.GetUserStoreForApplication();
                        var audioFileName = audio.GetFileName();
                        if (TLString.Equals(audio.MimeType, new TLString("audio/mpeg"), StringComparison.OrdinalIgnoreCase))
                        {
                            if (!store.FileExists(audioFileName))
                            {
                                mediaAudio.IsCanceled          = false;
                                mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                                BeginOnThreadPool(() =>
                                {
                                    DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                                });
                                continue;
                            }
                        }

                        var wavFileName = Path.GetFileNameWithoutExtension(audioFileName) + ".wav";

                        if (!store.FileExists(wavFileName))
                        {
                            mediaAudio.IsCanceled          = false;
                            mediaAudio.DownloadingProgress = mediaAudio.LastProgress > 0.0 ? mediaAudio.LastProgress : 0.001;
                            BeginOnThreadPool(() =>
                            {
                                DownloadAudioFileManager.DownloadFile(audio.DCId, audio.ToInputFileLocation(), message, audio.Size);
                            });
                            continue;
                        }
                    }
                }
            }

            var replyToMsgIds = new TLVector <TLInt>();
            var replyToMsgs   = new List <TLMessage25>();

            for (var i = 0; i < messages.Count; i++)
            {
                var message25 = messages[i] as TLMessage25;
                if (message25 != null)
                {
                    if (message25.ReplyToMsgId != null)
                    {
                        var replyToMsgId = message25.ReplyToMsgId;
                        if (replyToMsgId != null &&
                            replyToMsgId.Value != 0)
                        {
                            TLInt channelId   = null;
                            var   peerChannel = message25.ToId as TLPeerChannel;
                            if (peerChannel != null)
                            {
                                channelId = peerChannel.Id;
                            }

                            var reply = CacheService.GetMessage(replyToMsgId, channelId);
                            if (reply != null)
                            {
                                messages[i].Reply = reply;
                            }
                            else
                            {
                                replyToMsgIds.Add(replyToMsgId);
                                replyToMsgs.Add(message25);
                            }
                        }
                    }

                    if (message25.NotListened)
                    {
                        if (message25.Media != null)
                        {
                            message25.Media.NotListened = true;
                        }
                    }
                }
            }

            if (replyToMsgIds.Count > 0)
            {
                var channel = With as TLChannel;
                if (channel != null)
                {
                    var firstReplyToMsg = replyToMsgs.FirstOrDefault();
                    var peerChat        = firstReplyToMsg != null ? firstReplyToMsg.ToId as TLPeerChat : null;
                    if (peerChat != null)
                    {
                        MTProtoService.GetMessagesAsync(
                            replyToMsgIds,
                            result =>
                        {
                            CacheService.AddChats(result.Chats, results => { });
                            CacheService.AddUsers(result.Users, results => { });

                            for (var i = 0; i < result.Messages.Count; i++)
                            {
                                for (var j = 0; j < replyToMsgs.Count; j++)
                                {
                                    var messageToReply = replyToMsgs[j];
                                    if (messageToReply != null &&
                                        messageToReply.ReplyToMsgId.Value == result.Messages[i].Index)
                                    {
                                        replyToMsgs[j].Reply = result.Messages[i];
                                    }
                                }
                            }

                            //Execute.BeginOnUIThread(() =>
                            //{
                            for (var i = 0; i < replyToMsgs.Count; i++)
                            {
                                replyToMsgs[i].NotifyOfPropertyChange(() => replyToMsgs[i].ReplyInfo);
                            }
                            //});
                        },
                            error =>
                        {
                            Execute.ShowDebugMessage("messages.getMessages error " + error);
                        });
                    }
                    else
                    {
                        MTProtoService.GetMessagesAsync(
                            channel.ToInputChannel(),
                            replyToMsgIds,
                            result =>
                        {
                            CacheService.AddChats(result.Chats, results => { });
                            CacheService.AddUsers(result.Users, results => { });

                            for (var i = 0; i < result.Messages.Count; i++)
                            {
                                for (var j = 0; j < replyToMsgs.Count; j++)
                                {
                                    var messageToReply = replyToMsgs[j];
                                    if (messageToReply != null &&
                                        messageToReply.ReplyToMsgId.Value == result.Messages[i].Index)
                                    {
                                        replyToMsgs[j].Reply = result.Messages[i];
                                    }
                                }
                            }

                            //Execute.BeginOnUIThread(() =>
                            //{
                            for (var i = 0; i < replyToMsgs.Count; i++)
                            {
                                replyToMsgs[i].NotifyOfPropertyChange(() => replyToMsgs[i].ReplyInfo);
                            }
                            //});
                        },
                            error =>
                        {
                            Execute.ShowDebugMessage("channels.getMessages error " + error);
                        });
                    }
                }
                else
                {
                    MTProtoService.GetMessagesAsync(
                        replyToMsgIds,
                        result =>
                    {
                        CacheService.AddChats(result.Chats, results => { });
                        CacheService.AddUsers(result.Users, results => { });

                        for (var i = 0; i < result.Messages.Count; i++)
                        {
                            for (var j = 0; j < replyToMsgs.Count; j++)
                            {
                                var messageToReply = replyToMsgs[j];
                                if (messageToReply != null &&
                                    messageToReply.ReplyToMsgId.Value == result.Messages[i].Index)
                                {
                                    replyToMsgs[j].Reply = result.Messages[i];
                                }
                            }
                        }

                        //Execute.BeginOnUIThread(() =>
                        //{
                        for (var i = 0; i < replyToMsgs.Count; i++)
                        {
                            replyToMsgs[i].NotifyOfPropertyChange(() => replyToMsgs[i].ReplyInfo);
                        }
                        //});
                    },
                        error =>
                    {
                        Execute.ShowDebugMessage("messages.getMessages error " + error);
                    });
                }
            }
        }
        public void ProcessMessagesSearch(SearchRequest request, bool nextSlice = false)
        {
            if (request.MessageResults != null && !nextSlice)
            {
                if (!string.Equals(Text, request.Text, StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
                if (request.IsCanceled)
                {
                    return;
                }

                if (request.MessageResults.Count > 0)
                {
                    Items.Add(new TLServiceText {
                        Text = AppResources.Messages
                    });
                    foreach (var result in request.MessageResults)
                    {
                        Items.Add(result);
                    }
                }

                Status = Items.Count == 0 ? AppResources.NoResults : string.Empty;
            }
            else
            {
                if (IsWorking)
                {
                    return;
                }

                IsWorking = true;
                MTProtoService.SearchAsync(
                    new TLInputPeerEmpty(),
                    new TLString(request.Text),
                    new TLInputMessagesFilterEmpty(),
                    new TLInt(0), new TLInt(0), new TLInt(request.Offset), new TLInt(0), new TLInt(request.Limit),
                    result =>
                {
                    CacheService.AddChats(result.Chats, results => { });
                    CacheService.AddUsers(result.Users, results => { });

                    var items       = new List <TLObject>();
                    var newMessages = result as TLMessages;
                    if (newMessages != null)
                    {
                        var usersCache = new Dictionary <int, TLUserBase>();
                        foreach (var user in newMessages.Users)
                        {
                            usersCache[user.Index] = user;
                        }

                        var chatsCache = new Dictionary <int, TLChatBase>();
                        foreach (var chat in newMessages.Chats)
                        {
                            chatsCache[chat.Index] = chat;
                        }

                        foreach (var message in newMessages.Messages.OfType <TLMessageCommon>())
                        {
                            var dialog = new TLDialog {
                                TopMessage = message
                            };
                            var peer = TLUtils.GetPeerFromMessage(message);
                            if (peer is TLPeerUser)
                            {
                                TLUserBase user;
                                if (!usersCache.TryGetValue(peer.Id.Value, out user))
                                {
                                    continue;
                                }
                                dialog.With = user;
                            }
                            else if (peer is TLPeerChat ||
                                     peer is TLPeerChannel)
                            {
                                TLChatBase chat;
                                if (!chatsCache.TryGetValue(peer.Id.Value, out chat))
                                {
                                    continue;
                                }

                                dialog.With = chat;
                            }
                            items.Add(dialog);
                        }
                    }

                    Execute.BeginOnUIThread(() =>
                    {
                        IsWorking = false;

                        if (request.MessageResults == null)
                        {
                            request.MessageResults = new List <TLObject>();
                        }
                        foreach (var item in items)
                        {
                            request.MessageResults.Add(item);
                        }
                        request.Offset += request.Limit;

                        if (!string.Equals(Text, request.Text, StringComparison.OrdinalIgnoreCase))
                        {
                            return;
                        }
                        if (request.IsCanceled)
                        {
                            return;
                        }

                        if (request.MessageResults.Count > 0)
                        {
                            if (request.Offset == request.Limit)
                            {
                                Items.Add(new TLServiceText {
                                    Text = AppResources.Messages
                                });
                            }
                            foreach (var item in request.MessageResults)
                            {
                                Items.Add(item);
                            }
                        }

                        Status = Items.Count == 0 ? AppResources.NoResults : string.Empty;
                    });
                },
                    error => Execute.BeginOnUIThread(() =>
                {
                    IsWorking = false;
                    Status    = Items.Count == 0 ? AppResources.NoResults : string.Empty;
                }));
            }
        }
Beispiel #5
0
        private void LoadCalls()
        {
            var timespan = Items.Count > 0 ? 1.0 : 0.0;

            BeginOnThreadPool(TimeSpan.FromSeconds(timespan), () =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                //if (!FirstRun)
                //{
                //    return;
                //}
                if (!isAuthorized)
                {
                    return;
                }

                FirstRun = false;

                Status    = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.Loading : string.Empty;
                var limit = 50;//Constants.DialogsSlice;
                MTProtoService.SearchAsync(
                    new TLInputPeerEmpty(),
                    TLString.Empty,
                    null,
                    new TLInputMessagesFilterPhoneCalls {
                    Flags = new TLInt(0)
                },
                    new TLInt(0),
                    new TLInt(0),
                    new TLInt(0),
                    new TLInt(0),
                    new TLInt(limit),
                    new TLInt(0),
                    result =>
                {
                    CacheService.AddChats(result.Chats, results => { });
                    CacheService.AddUsers(result.Users, results => { });

                    var items       = new List <TLDialogBase>();
                    var newMessages = result as TLMessages;
                    if (newMessages != null)
                    {
                        var usersCache = new Dictionary <int, TLUserBase>();
                        foreach (var user in newMessages.Users)
                        {
                            usersCache[user.Index] = user;
                        }

                        var chatsCache = new Dictionary <int, TLChatBase>();
                        foreach (var chat in newMessages.Chats)
                        {
                            chatsCache[chat.Index] = chat;
                        }

                        foreach (var message in newMessages.Messages.OfType <TLMessageCommon>())
                        {
                            var dialog = new TLDialog {
                                TopMessage = message
                            };
                            var peer = TLUtils.GetPeerFromMessage(message);
                            if (peer is TLPeerUser)
                            {
                                TLUserBase user;
                                if (!usersCache.TryGetValue(peer.Id.Value, out user))
                                {
                                    continue;
                                }
                                dialog.With = user;
                                items.Add(dialog);
                            }
                        }
                    }

                    Telegram.Api.Helpers.Execute.BeginOnUIThread(() =>
                    {
                        IsLastSliceLoaded = result.Messages.Count < limit;
                        _offset           = Constants.DialogsSlice;

                        _isUpdated = true;

                        const int maxMessagesSlice = 8;
                        Items.Clear();
                        for (var i = 0; i < items.Count; i++)
                        {
                            if (i < maxMessagesSlice)
                            {
                                Items.Add(items[i]);
                            }
                            else
                            {
                                LazyItems.Add(items[i]);
                            }
                        }

                        Status = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.NoCallsHere : string.Empty;

                        if (LazyItems.Count > 0)
                        {
                            BeginOnUIThread(() =>
                            {
                                for (var i = 0; i < LazyItems.Count; i++)
                                {
                                    Items.Add(LazyItems[i]);
                                }
                                LazyItems.Clear();
                            });
                        }
                    });
                },
                    error => Execute.BeginOnUIThread(() =>
                {
                    Telegram.Api.Helpers.Execute.ShowDebugMessage("messages.getDialogs error " + error);
                    //_isUpdated = true;
                    Status = string.Empty;
                }));
            });

            base.OnInitialize();
        }