Pages

venerdì 18 gennaio 2013

Add historical search suggestions on your Windows 8 app

Today I will talk to you about how to add search suggestions inside a Windows 8 store app: I will NOT talk about how to integrate searching feature because there are loads and loads of article about that (msdn is the first link that comes into my mind)

In this case I will fill the suggestions with the previous search by the user, but this example can be easily adapted to use different sources like REST services,telepathy or extra-terrestrial data teletransport.

To get this done we'll use "ApplicationData.Current.LocalSettings" to store locally every search query made by the user. The class above will help us to encapsulate the logic:

    public class SuggestionsHelper
    {
        internal const int SearchPaneMaxSuggestions = 5;
        public static string[] SuggestionsList
        {
            get
            {
                if (ApplicationData.Current.LocalSettings.Values.ContainsKey("Suggestions"))
                {
                    return ApplicationData.Current.LocalSettings.Values["Suggestions"] as string[];
                }
                else
                {
                    string[] result = new string[] { "" };
                    ApplicationData.Current.LocalSettings.Values["Suggestions"] = result;
                    return result;
                }
            }
            set
            {
                ApplicationData.Current.LocalSettings.Values["Suggestions"] = value;
            }
        }

        public static void OnSearchPaneSuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs e)
        {
            var queryText = e.QueryText;
            if (!string.IsNullOrEmpty(queryText))
            {
                var request = e.Request;
                foreach (string suggestion in SuggestionsList)
                {
                    // if we find a perfect match add the suggestions and break the cycle
                    if (suggestion.CompareTo(queryText) == 0)
                    {
                        if (request != null && request.SearchSuggestionCollection != null)
                        {
                            request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);
                            break;
                        }
                    }
                    else  // else if we find a partial match we'll add to the suggestions
                        if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
                        {                    request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);
                        }

                    // break the cycle if we have reached the maximum size
                    if (request.SearchSuggestionCollection.Size >= SearchPaneMaxSuggestions)
                    {
                        break;
                    }
                }
            }
        }

        public static void addItemToSuggestionsList(string item)
        {
            if (!SuggestionsList.Contains(item))
            {
                string[] result = SuggestionsList;
                Array.Resize(ref result, result.Length + 1);
                result.SetValue(item, result.Length - 1);
                SuggestionsList = result;
            }
        }

    }

As we can see the class contains a method called "addItemToSuggestionsList" that adds the suggestion inside an array of Strings stored through the LocalSettings of the application (encapsulated by the property "SuggestionsList") and a method "OnSearchPaneSuggestionsRequested" that creates the actual list of suggestions inside the SearchPanel when is requested Using the instance of the class "SearchPaneSuggestionsRequestedEventArgs" provided by the "SearchPaneSuggestionsRequested" Event Handler. This method have to be binded to the related event inside the method "OnLaunched" available inside the App.xaml.cs file using this instruction:

            SearchPane.GetForCurrentView().SuggestionsRequested +=
                new TypedEventHandler<SearchPane, SearchPaneSuggestionsRequestedEventArgs>
                    (SuggestionsHelper.OnSearchPaneSuggestionsRequested);

The last step is to fill the array everytime the user makes a search with the query string. To do this all we need to do is call the "addItemToSuggestionsList" function inside the "LoadState" method of the Search Page of your application:

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
        var queryText = navigationParameter as String;
        SuggestionsHelper.addItemToSuggestionsList(queryText);
}

That's pretty much it!

This is a basic example but many things can be added, for example by adding a function to flush the history or by adding a maximum limit of stored suggestions but for a default usage this version works pretty good.

Nessun commento:

Posta un commento