/// <inheritdoc/>
        public SearchOperation Search(string term, Action <SearchItem> resultFoundCallback, Action <IList <SearchItem> > SearchCompleted)
        {
            var token     = new CancellationTokenSource();
            var operation = new SearchOperation(token);
            var result    = new List <SearchItem>();

            operation.Result = result;

            term = term.ToLower().Replace(" ", "");

            Task.WhenAll(_searchCategories.Select(category =>
                                                  Task.Factory.StartNew(() => Search(category), token.Token)))
            .ContinueWith(t =>
            {
                operation.IsCompleted = true;
                SearchCompleted?.Invoke(result);
            }, token.Token);

            void Search(CategorySearchItems category)
            {
                SearchItem foundItem = null;

                if (category.Items.ContainsKey(term))
                {
                    foundItem = category.Items[term];
                    ResultFound(category.Items[term]);
                }
                foreach (var categoryItem in category.Items)
                {
                    if (categoryItem.Key.Contains(term))
                    {
                        if (foundItem != categoryItem.Value)
                        {
                            ResultFound(categoryItem.Value);
                        }
                    }
                }
            }

            void ResultFound(SearchItem resultItem)
            {
                resultFoundCallback(resultItem);
                result.Add(resultItem);
            }

            return(operation);
        }
Esempio n. 2
0
 private void Search(string term)
 {
     if (string.IsNullOrEmpty(term))
     {
         return;
     }
     SelectedResult = null;
     ShowPopup      = true;
     ExecuteOnUIContext(() =>
     {
         SearchResults.Clear();
     });
     if (_lastOperation != null && !_lastOperation.IsCompleted)
     {
         _lastOperation?.Abort();
     }
     _lastOperation = _applicationSearchService.Search(term, ItemFound,
                                                       res =>
     {
         SearchResultsFound = res != null && res.Any();
     });
 }