Ejemplo n.º 1
0
 private void SearchSuggestionSourceChangedHandler(ISearchSuggestionsSource newSearchSuggestionSource)
 {
     if (m_searchSuggestionsController != null)
     {
         m_searchSuggestionsController.SearchSuggestionsSource = newSearchSuggestionSource;
     }
 }
        /// <summary>
        /// Starts a new task for the search and propagates the result via the <see cref="SearchSuggestionsChanged" /> event.
        /// This method does some technical stuff and delegates the actual search to the <see cref="ISearchSuggestionsSource" />.
        /// </summary>
        /// <param name="searchTerm">The term to search for</param>
        public void Search(string searchTerm)
        {
            string id = Guid.NewGuid().ToString();

            ISearchSuggestionsSource searchSuggestionsSource = null;

            lock (m_lockObject)
            {
                searchSuggestionsSource = m_searchSuggestionsSource;

                // no source, no search
                if (searchSuggestionsSource == null)
                {
                    return;
                }

                LastId = id;
            }

            Task.Delay(SearchDelay)
            .ContinueWith((prevTask) =>
            {
                // search only if there was no other request during the delay
                if (DoSearchWithId(id))
                {
                    IList <string> searchSuggestions = null;
                    bool isFromHistory = false;

                    if (!string.IsNullOrEmpty(searchTerm))
                    {
                        searchSuggestions = searchSuggestionsSource.GetAutoCompletion(searchTerm);
                        isFromHistory     = false;
                    }
                    else
                    {
                        searchSuggestions = searchSuggestionsSource.GetSearchSuggestions();
                        isFromHistory     = true;
                    }

                    // a final check if this result will not be replaced by another active search
                    if (DoSearchWithId(id))
                    {
                        SearchSuggestionsChanged?.Invoke(this, new SearchSuggestionsChangedEventArgs(searchSuggestions, isFromHistory));
                    }
                }
            });
        }
        /// <summary>
        /// Creates a new <see cref="SearchSuggestionsController" />.
        /// </summary>
        public SearchSuggestionsController()
        {
            m_searchSuggestionsSource = null;

            m_lastId = null;
        }