Ejemplo n.º 1
0
        private void CheckNewSearchString()
        {
            if (forceNewSearch || SearchString != localSearchString && !currentlySearching)
            {
                forceNewSearch     = false;
                currentlySearching = true;
                localSearchString  = SearchString;
                var sw = new Stopwatch();
                sw.Start();
                SelectedIndex = -1;
                List <RunItem> list = Indexing.Search(localSearchString);
                Application.Current.Dispatcher.Invoke(() =>
                {
                    SearchSuggestions.RaiseListChangedEvents = false;
                    SearchSuggestions.Clear();
                    foreach (var item in list)
                    {
                        SearchSuggestions.Add(item);
                    }

                    SearchSuggestions.RaiseListChangedEvents = true;
                    SearchSuggestions.ResetBindings();
                }, DispatcherPriority.Background);

                sw.Stop();
                RenderingTime      = (int)sw.ElapsedMilliseconds;
                currentlySearching = false;
            }
        }
        private async Task UpdateSearchSuggestionsInner(string partialSearchQuery, CancellationToken cancellationToken)
        {
            IList <SearchSuggestionViewModel> suggestions;
            bool reverseSearch;

            try
            {
                await querySemaphore.WaitAsync(cancellationToken);

                (suggestions, reverseSearch) = await GetSearchSuggestions(partialSearchQuery, cancellationToken);
            }
            catch (OperationCanceledException)
            {
                suggestions   = null;
                reverseSearch = false;
            }
            finally
            {
                querySemaphore.Release();
            }

            if (suggestions != null)
            {
                UpdateSearchSuggestions(suggestions, reverseSearch);
            }
            else
            {
                SearchSuggestions.Clear();
            }
        }
        public void SuggestedProductsMethodShouldReturnExpectedOutBelowIfGivenParameterIsHavana()
        {
            var products   = new string[] { "havana" };
            var searchWord = "havana";

            var response = SearchSuggestions.SuggestedProducts(products, searchWord);

            Assert.AreEqual(6, response.Count);
            Assert.AreEqual("havana", response[0][0]);
            Assert.AreEqual("havana", response[1][0]);
            Assert.AreEqual("havana", response[2][0]);
            Assert.AreEqual("havana", response[3][0]);
            Assert.AreEqual("havana", response[4][0]);
            Assert.AreEqual("havana", response[5][0]);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get a list of search query suggestions.
        /// </summary>
        public async Task GetSearchSuggestions()
        {
            var query = Query != null?Query.Trim() : null;

            if (String.IsNullOrWhiteSpace(query))
            {
                return;
            }
            var suggestions = await searchService.GetSearchSuggestionsAsync(Query);

            SearchSuggestions.Clear();
            foreach (var suggestion in suggestions)
            {
                SearchSuggestions.Add(suggestion.Value);
            }
        }
        public void SuggestedProductsMethodShouldReturnExpectedOutBelowIfGivenParameterIsBags()
        {
            var products   = new string[] { "bags", "baggage", "banner", "box", "cloths" };
            var searchWord = "bags";

            var response = SearchSuggestions.SuggestedProducts(products, searchWord);

            Assert.AreEqual(4, response.Count);
            Assert.AreEqual("baggage", response[0][0]);
            Assert.AreEqual("bags", response[0][1]);
            Assert.AreEqual("banner", response[0][2]);
            Assert.AreEqual("baggage", response[1][0]);
            Assert.AreEqual("bags", response[1][1]);
            Assert.AreEqual("banner", response[1][2]);
            Assert.AreEqual("baggage", response[2][0]);
            Assert.AreEqual("bags", response[2][1]);
            Assert.AreEqual("bags", response[3][0]);
        }
Ejemplo n.º 6
0
        private async Task PullSearchSuggestionAsync(bool userChangedQuery)
        {
            if (!userChangedQuery)
            {
                return;
            }

            var query = SearchQuery;

            if (string.IsNullOrEmpty(query))
            {
                SearchResultsAvailable = false;
                SearchSuggestions.Clear();
                SearchSuggestions.Add(new TraktShow {
                    Title = "No result"
                });
                return;
            }

            var searchResults = await _client.Search.GetTextQueryResultsAsync(TraktSearchResultType.Show, query);

            var tasks = searchResults
                        .Select(result => result.Show.Ids.Trakt.ToString())
                        .Select(showId => _client.Shows.GetShowAsync(showId, new TraktExtendedOption {
                Full = true, Images = true
            }))
                        .ToList();

            var fullShows = await Task.WhenAll(tasks);

            if (fullShows.Any())
            {
                SearchSuggestions.Clear();
                SearchSuggestions.AddRange(fullShows);
            }
            else
            {
                SearchSuggestions.Clear();
                SearchSuggestions.Add(new TraktShow {
                    Title = "No result"
                });
            }
        }
        public async Task PerformQuery(string searchQuery, bool dontSearchInBothDirections = false)
        {
            if (searchSuggestionCancellationTokenSource != null)
            {
                searchSuggestionCancellationTokenSource.Cancel();
            }

            try
            {
                await querySemaphore.WaitAsync();

                // it may happen that the AutoSuggestBox does not hide search suggestions automatically after
                // performing a query. For these cases, clear the suggestions manually
                SearchSuggestions.Clear();

                Task <List <DictionaryEntry> > searchTask = PerformQueryInner(searchQuery, dontSearchInBothDirections);

                Task animationDelayTask = Task.Delay(250);

                Task finishedTask = await Task.WhenAny(searchTask, animationDelayTask);

                if (finishedTask == animationDelayTask)
                {
                    IsSearchInProgress = true;
                    await searchTask;
                }

                SearchContext searchContext = new SearchContext(searchQuery, selectedDirection, dontSearchInBothDirections);

                List <DictionaryEntry> results = searchTask.Result;

                DictionaryEntries = new LazyCollection <DictionaryEntry, DictionaryEntryViewModel>(
                    results, entry => new DictionaryEntryViewModel(entry, searchContext));
            }
            finally
            {
                IsSearchInProgress = false;

                querySemaphore.Release();
            }
        }
        public void SuggestedProductsMethodShouldReturnExpectedOutBelowIfGivenParameterIsMouse()
        {
            var products   = new string[] { "mobile", "mouse", "moneypot", "monitor", "mousepad" };
            var searchWord = "mouse";

            var response = SearchSuggestions.SuggestedProducts(products, searchWord);

            Assert.AreEqual(5, response.Count);
            Assert.AreEqual("mobile", response[0][0]);
            Assert.AreEqual("moneypot", response[0][1]);
            Assert.AreEqual("monitor", response[0][2]);
            Assert.AreEqual("mobile", response[1][0]);
            Assert.AreEqual("moneypot", response[1][1]);
            Assert.AreEqual("monitor", response[1][2]);
            Assert.AreEqual("mouse", response[2][0]);
            Assert.AreEqual("mousepad", response[2][1]);
            Assert.AreEqual("mouse", response[3][0]);
            Assert.AreEqual("mousepad", response[3][1]);
            Assert.AreEqual("mouse", response[4][0]);
            Assert.AreEqual("mousepad", response[4][1]);
        }
        private void UpdateSearchSuggestions(IList <SearchSuggestionViewModel> suggestions, bool reverseSearch)
        {
            SearchSuggestionViewModelComparer comparer = new SearchSuggestionViewModelComparer(reverseSearch, Settings.Instance.ShowWordClasses);

            for (int i = 0; i < SearchSuggestions.Count; i++)
            {
                if (!suggestions.Contains(SearchSuggestions[i], comparer))
                {
                    SearchSuggestions.RemoveAt(i);
                    i--;
                }
            }

            for (int i = 0; i < suggestions.Count; i++)
            {
                if (!SearchSuggestions.Contains(suggestions[i], comparer))
                {
                    SearchSuggestions.Insert(i, suggestions[i]);
                }
            }
        }
Ejemplo n.º 10
0
 public async Task ClearStateAsync()
 {
     SearchSuggestions.Clear();
     Shows.Clear();
     await PerformSearchQueryAsync(null);
 }