Пример #1
0
        public void Search()
        {
            if (_lastUsersRequest != null)
            {
                _lastUsersRequest.Cancel();
            }

            var text = Text.Trim();

            if (string.IsNullOrEmpty(text))
            {
                LazyItems.Clear();
                Items.Clear();
                Status = string.IsNullOrEmpty(Text)? string.Empty : AppResources.NoResults;
                return;
            }

            SearchUsersRequest nextUsersRequest;

            if (!_searchResultsCache.TryGetValue(text, out nextUsersRequest))
            {
                IList <TLUserBase> source;

                if (_lastUsersRequest != null &&
                    text.IndexOf(_lastUsersRequest.Text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    source = _lastUsersRequest.Source;
                }
                else
                {
                    _source = _source ??
                              CacheService.GetContacts()
                              .Where(x => !(x is TLUserEmpty) && x.Index != StateService.CurrentUserId)
                              .OrderBy(x => x.FullName)
                              .ToList();

                    source = _source;
                }

                nextUsersRequest = new SearchUsersRequest(text, source);
            }

            IsWorking = true;
            nextUsersRequest.ProcessAsync(results =>
                                          Execute.BeginOnUIThread(() =>
            {
                if (nextUsersRequest.IsCanceled)
                {
                    return;
                }

                Status = string.Empty;
                Items.Clear();
                LazyItems.Clear();
                if (results.Count > 0)
                {
                    Items.Add(new TLServiceText {
                        Text = AppResources.Contacts
                    });
                }
                for (var i = 0; i < results.Count; i++)
                {
                    if (i < 6)
                    {
                        Items.Add(results[i]);
                    }
                    else
                    {
                        LazyItems.Add(results[i]);
                    }
                }

                IsWorking = false;
                //Status = Items.Count == 0 ? AppResources.NoResults : string.Empty;

                //return;

                if (LazyItems.Count > 0)
                {
                    PopulateItems(() => ProcessGlobalSearch(nextUsersRequest));
                }
                else
                {
                    ProcessGlobalSearch(nextUsersRequest);
                }
            }));
            _searchResultsCache[nextUsersRequest.Text] = nextUsersRequest;
            _lastUsersRequest = nextUsersRequest;
        }
Пример #2
0
        private void ProcessGlobalSearch(SearchUsersRequest nextUsersRequest)
        {
            if (nextUsersRequest.GlobalResults != null)
            {
                if (nextUsersRequest.GlobalResults.Count > 0)
                {
                    BeginOnUIThread(() =>
                    {
                        if (nextUsersRequest.IsCanceled)
                        {
                            return;
                        }

                        Items.Add(new TLServiceText {
                            Text = AppResources.GlobalSearch
                        });
                        foreach (var user in nextUsersRequest.GlobalResults)
                        {
                            Items.Add(user);
                        }
                        //Status = Items.Count == 0 ? AppResources.NoResults : string.Empty;
                    });
                }
            }
            else
            {
                //if (nextUsersRequest.Text.Length < 5)
                //{
                //    nextUsersRequest.GlobalResults = new List<TLObject>();
                //    return;
                //}


                MTProtoService.SearchAsync(new TLString(nextUsersRequest.Text), new TLInt(100),
                                           result => Execute.BeginOnUIThread(() =>
                {
                    nextUsersRequest.GlobalResults = new List <TLObject>();
                    foreach (var user in result.Users)
                    {
                        nextUsersRequest.GlobalResults.Add(user);
                    }
                    var contactsFound40 = result as TLContactsFound40;
                    if (contactsFound40 != null)
                    {
                        foreach (var chat in contactsFound40.Chats)
                        {
                            nextUsersRequest.GlobalResults.Add(chat);
                        }
                    }

                    if (nextUsersRequest.IsCanceled)
                    {
                        return;
                    }

                    var items = new List <TLUserBase>();
                    foreach (var user in result.Users)
                    {
                        if (!nextUsersRequest.ResultsIndex.ContainsKey(user.Index))
                        {
                            items.Add(user);
                        }
                    }

                    if (items.Count > 0)
                    {
                        Items.Add(new TLServiceText {
                            Text = AppResources.GlobalSearch
                        });
                        foreach (var user in items)
                        {
                            Items.Add(user);
                        }
                    }
                }),
                                           error =>
                {
                    if (TLRPCError.CodeEquals(error, ErrorCode.BAD_REQUEST) &&
                        TLRPCError.TypeEquals(error, ErrorType.QUERY_TOO_SHORT))
                    {
                        nextUsersRequest.GlobalResults = new List <TLObject>();
                    }
                    else if (TLRPCError.CodeEquals(error, ErrorCode.FLOOD))
                    {
                        nextUsersRequest.GlobalResults = new List <TLObject>();
                        BeginOnUIThread(() => MessageBox.Show(AppResources.FloodWaitString + Environment.NewLine + "(" + error.Message + ")", AppResources.Error, MessageBoxButton.OK));
                    }

                    Execute.ShowDebugMessage("contacts.search error " + error);
                });
            }
        }
Пример #3
0
        public void Search()
        {
            if (_lastUsersRequest != null)
            {
                _lastUsersRequest.Cancel();
            }

            var text = Text.Trim();

            if (string.IsNullOrEmpty(text))
            {
                System.Diagnostics.Debug.WriteLine("EmptyText text={0} canceled={1} items_count={2} current_text={3}", text, _lastUsersRequest != null ? _lastUsersRequest.IsCanceled.ToString() : "null", LazyItems.Count, Text);

                LazyItems.Clear();
                Items.Clear();
                Status = string.Empty;
                return;
            }

            SearchUsersRequest nextUsersRequest;

            if (!_searchResultsCache.TryGetValue(text, out nextUsersRequest))
            {
                IList <TLUserBase> source;

                if (_lastUsersRequest != null &&
                    text.IndexOf(_lastUsersRequest.Text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    source = _lastUsersRequest.Source;
                }
                else
                {
                    source = Source;
                }

                nextUsersRequest = new SearchUsersRequest(text, source);
            }

            IsWorking = true;
            nextUsersRequest.ProcessAsync(results =>
                                          Execute.BeginOnUIThread(() =>
            {
                if (nextUsersRequest.IsCanceled)
                {
                    return;
                }

                Items.Clear();
                LazyItems.Clear();
                for (var i = 0; i < results.Count; i++)
                {
                    if (i < 6)
                    {
                        Items.Add(results[i]);
                    }
                    else
                    {
                        LazyItems.Add(results[i]);
                    }
                }
                Status = Items.Count > 0 ? string.Empty : AppResources.NoResults;

                IsWorking = false;
                if (LazyItems.Count > 0)
                {
                    Execute.BeginOnUIThread(() =>
                    {
                        if (nextUsersRequest.IsCanceled)
                        {
                            return;
                        }

                        System.Diagnostics.Debug.WriteLine("ContinueResults text={0} canceled={1} items_count={2} current_text={3}", nextUsersRequest.Text, nextUsersRequest.IsCanceled, LazyItems.Count, Text);

                        foreach (var item in LazyItems)
                        {
                            Items.Add(item);
                        }
                        LazyItems.Clear();
                        Status = Items.Count > 0 ? string.Empty : AppResources.NoResults;
                    });
                }
            }));
            _searchResultsCache[nextUsersRequest.Text] = nextUsersRequest;
            _lastUsersRequest = nextUsersRequest;
        }