示例#1
0
        public async void Search(string query, User from, SearchMessagesFilter filter)
        {
            if (string.Equals(_query, query) && _from?.Id == from?.Id && _filter?.GetType() == filter?.GetType() && PreviousCanExecute())
            {
                PreviousExecute();
            }
            else
            {
                From   = from;
                Filter = filter;
                Query  = query;

                var chat = _dialog.Chat;
                if (chat == null)
                {
                    return;
                }

                Items        = null;
                SelectedItem = null;

                if (string.IsNullOrEmpty(query) && from == null && filter == null)
                {
                    return;
                }

                var fromMessageId = 0L;

                var field = _dialog.ListField;
                if (field != null)
                {
                    var panel = field.ItemsPanelRoot as ItemsStackPanel;
                    if (panel != null && panel.LastVisibleIndex >= 0 && panel.LastVisibleIndex < _dialog.Items.Count && _dialog.Items.Count > 0)
                    {
                        fromMessageId = _dialog.Items[panel.LastVisibleIndex].Id;
                    }
                }

                var collection = new SearchChatMessagesCollection(ProtoService, chat.Id, query, from?.Id ?? 0, fromMessageId, filter);
                var result     = await collection.LoadMoreItemsAsync(100);

                if (result.Count > 0)
                {
                    var target = collection.FirstOrDefault();

                    if (fromMessageId != 0)
                    {
                        var closest = collection.Aggregate((x, y) => Math.Abs(x.Id - fromMessageId) < Math.Abs(y.Id - fromMessageId) ? x : y);
                        if (closest != null)
                        {
                            target = closest;
                        }
                    }

                    Items        = collection;
                    SelectedItem = target;

                    await Dialog.LoadMessageSliceAsync(null, target.Id);
                }
                else
                {
                    Items        = collection;
                    SelectedItem = null;
                }
            }
        }