Sunday, March 18, 2012

File searching and filtering


In my last post, I discussed accessing and displaying file data from your Metro app, and grouping files using the pivot API. Let me follow up on that with a discussion of basic file filtering using the search API. So in our photo browser app, suppose we’d like the user filter photos by their own rating. In the Windows OS, the user can right click on a picture and set a personal rating using the Rating property :
image
This is the property that we’re going to use for our filtering example. So we’ll assume the user has at least some pictures rated using the above property settings in her file system.
Here’s what we want to achieve :
image
The buttons in the top row match each distinct rating value the user has assigned to her pics. So we need to generate these buttons. Then, when the user clicks one of them, the list will refresh to only show pics matching the selected rating value.
Let’s start with the buttons :
   1: private async void CreateRatingButtons()
   2: {
   3:     var pivot = CommonFolderQuery.GroupByRating;
   4:     var folders = await library.GetFoldersAsync(pivot);
   5:  
   6:     var ratingValues = folders.Cast<StorageFolder>().Select(
   7:         f => new Rating { Name = f.Name }).OrderBy(r => r.Name);
   8:     Ratings = new ObservableCollection<Rating>(ratingValues);
   9:     DefaultViewModel["Ratings"] = Ratings;
  10: }

You can see our old friend the pivot, which we first use to group the files by rating.  As in the previous post, we generate a list of virtual folders, each representing a group of pictures matching a specific rating value. Then we just retrieve the vfolder name which is the rating value itself. At this point, our goal is just to get the list of rating values assigned by the user in her file system. We store them in a simple data model class in an observable collection to make data binding easier. Then we bind an items control to the collection to generate the list of buttons :


   1: <ItemsControl x:Name="icRatings"
   2:               ItemsSource="{Binding Ratings}">
   3:     <ItemsControl.ItemsPanel>
   4:         <ItemsPanelTemplate>
   5:             <StackPanel Orientation="Horizontal" />
   6:         </ItemsPanelTemplate>
   7:     </ItemsControl.ItemsPanel>
   8:     <ItemsControl.ItemTemplate>
   9:         <DataTemplate>
  10:             <Button Name="btnFilter" 
  11:                     Content="{Binding Name}"
  12:                     Click="btnFilter_Click" />
  13:         </DataTemplate>
  14:     </ItemsControl.ItemTemplate>
  15: </ItemsControl>

So the ItemsControl is bound to the Ratings list (here through the DefaultViewModel property for convenience, but you could just set icRatings’s ItemsSource to Ratings in code if you so prefer). That takes care of creating our filter buttons.

Next, what happens when a button is clicked ? Well, here’s the workhorse method :


   1: private async Task GetFilesAsync(StorageFolder rootFolder, string search = "")
   2: {
   3:     var options = new QueryOptions { FolderDepth = FolderDepth.Deep };
   4:     options.ApplicationSearchFilter = search;
   5:  
   6:  
   7:     var result = rootFolder.CreateFileQueryWithOptions(options);
   8:     var files = await result.GetFilesAsync();
   9:  
  10:     foreach (var file in files)
  11:     {
  12:         var myFile = new MyFile { FileName = file.Name };
  13:  
  14:         myFile.Thumbnail = await file.GetThumbnailAsync(ThumbnailMode.PicturesView, 200);
  15:  
  16:         var props = await file.Properties.RetrievePropertiesAsync(new List<string> { "System.RatingText", "System.Title" });
  17:         var title = props["System.Title"];
  18:         var ratingText = props["System.RatingText"];
  19:         myFile.Title = title != null ? title.ToString() : string.Empty;
  20:         myFile.RatingText = ratingText != null ? ratingText.ToString() : string.Empty;
  21:  
  22:         myFiles.Add(myFile);
  23:     }
  24: }

I start by creating a QueryOptions instance, and set its ApplicationSearchFilter to the filter criterion we’re interested in. In this case, search should be set in the button click handler :


   1: private async void btnFilter_Click(object sender, RoutedEventArgs e)
   2: {
   3:     myFiles.Clear();
   4:     
   5:     var rating = (sender as Button).DataContext as Rating;
   6:     if (rating != null)
   7:     {
   8:         ...
   9:         await GetFilesAsync(library, "System.Rating:" + ratingText);
  10:     }
  11: }

Note that I also set FolderDepth to Deep so as to specify a deep search. As we’ve seen before, I then create the query and start the async operation to retrieve the files matching the passed in search criterion. Then I loop through the result files, and seek to retrieve the actual rating value for the file (although I already know it since it should match the search criterion) as well as the image title. This is to illustrate accessing file properties using the new set of interfaces in WinRT Consumer Prev. Notice I first access the file’s Properties value and call RetrievePropertiesAsync() on that object, passing to it a list of property names I need to retrieve. The property names to pass are very badly documented at this point, but after some digging I found they must conform to Windows’ Advanced Query Syntax (documented here).  Here’s a list of Windows properties : http://msdn.microsoft.com/en-us/library/bb760685(VS.85).aspx

So anyway to make things short, the property we’re interested in is not System.Rating, which returns a number, but System.RatingText. So we retrieve that as well as Title, and we store the retrieved values in our MyFile data model object so we can bind to it in the UI. Here’s the updated version of MyFile :


   1: public class MyFile : BindableBase
   2: {
   3:     public string FileName { get; set; }
   4:  
   5:     string title;
   6:     public string Title 
   7:     {
   8:         get { return title; }
   9:         set { SetProperty<string>(ref title, value); }
  10:     }
  11:  
  12:     public StorageItemThumbnail Thumbnail { get; set; }
  13:     public string RatingText { get; set; }
  14: }

So hopefully, this has given you an idea of how you can implement searching/filtering on file data in your apps. I’ve used a simple example but you can obviously implement much more complex scenarios using these techniques.

Happy filtering…

Using files as your app data


In Windows 8, you can use your files as data for your app. Suppose you have a bunch of pics stored on your machine or on your network, for example. You can easily display them in your Metro app :
image
Here’s the basic method behind this UI :
   1: private async Task GetFilesAsync(IStorageFolder rootFolder)
   2: {
   3:     myFiles.Clear();
   4:  
   5:     var files = await rootFolder.GetFilesAsync();
   6:  
   7:     foreach (var f in files)
   8:     {
   9:         var myFile = new MyFile { FileName = f.Name };
  10:  
  11:         myFile.Thumbnail = await (f as StorageFile).GetThumbnailAsync(
  12:             ThumbnailMode.ListView, 100, ThumbnailOptions.ResizeThumbnail);
  13:  
  14:         myFiles.Add(myFile);
  15:     }
  16: }


It uses a data model file that looks something like this :


   1: public class MyFile : BindableBase
   2: {
   3:     public string FileName { get; set; }
   4:  
   5:     string title;
   6:     public string Title 
   7:     {
   8:         get { return title; }
   9:         set { SetProperty<string>(ref title, value); }
  10:     }
  11:  
  12:     public StorageItemThumbnail Thumbnail { get; set; }
  13: }

A custom file class that stores the file name, title (set by the user on her pictures) and thumbnail to display. Note it extends BindableBase for convenience to easily implement INotifyPropertyChanged and raise UI notifications but simply calling SetMethod(), defined in BindableBase.

Also note the GetFilesAsync method asynchronously retrieves the thumbnail of each pic by calling GetThumbnailAsync. How easy is that ? Thanks winRT.

Now suppose you want to group the pics in some way. You can do it easily through the pivot API, which creates virtual folders that represent groups. Here’s another method :


   1: private async Task GetVirtualFoldersAsync(StorageFolder rootFolder)
   2: {
   3:     myVirtualFolders.Clear();
   4:  
   5:     var pivot = CommonFolderQuery.GroupByMonth;
   6:     var queryOptions = new QueryOptions(pivot);
   7:  
   8:     var queryResult = rootFolder.CreateFolderQueryWithOptions(queryOptions);
   9:     var vFolders = await queryResult.GetFoldersAsync();
  10:  
  11:     foreach (StorageFolder vfolder in vFolders)
  12:     { 
  13:         var myFolder = new MyFolder { FolderName = vfolder.Name };
  14:  
  15:         var files = await vfolder.GetFilesAsync();
  16:         myFolder.ImageCount = files.Count();
  17:  
  18:         myFolder.Thumbnail = await (vfolder as StorageFolder).GetThumbnailAsync(
  19:             ThumbnailMode.ListView, 200, ThumbnailOptions.ResizeThumbnail);
  20:  
  21:         myVirtualFolders.Add(myFolder);
  22:     }
  23: }


What this method does is generate a list of virtual folders each matching a group of pics taken during a given month. The pivot is defined using the GroupByMonth query, which will result in the files being pivoted on using this criterion. We could have picked another pivot, such as CommonFolderQuery.GroupByAuthor for example.

Next we call CreateFolderQueryWithOptions(options) to actually create the pivoted query. This method is available on the StorageFolder passed into the method, for example Windows.Storage.KnownFolders.PicturesLibrary.  Then we call GetFoldersAsync to actually dig and retrieve the files grouped by month.

Once the method completes, you can access the list of virtual folders representing the groups created. Here this is done in the foreach loop which loops over the virtual folders. My code then goes on to do app-specific stuff, such as retrieving the list of files in the group (vfolder) so as to display the count in the UI, and retrieve the vfolder’s thumbnail. Windows does that for you, it will generate a thumbnail for your folder or virtual folder, usually the thumbnail of the first pic in the folder. So you can call GetThumbnailAsync on a StorageFile OR a StorageFolder. Awesome.

Like before, I instantiate a custom class to store the vfolder data and store it in an observable collection so my UI can bind to it and get change notified. The UI then lists groups (vfolders), and I can add code so that when a Vfolder thumbnail is tapped, the list of actual pics in that vfolder gets displayed.

image

So you can see how easy it is to use files as app data and then implement simple pivots on that data. The beauty of this is the way we perform asynchronous operations, which would have previously made a hellish mess in our code, so smoothly and easily using async / await. Thanks to this new pattern the work we need to do to define and implement pivots on top of file resources in our data apps is almost fun!

Happy pivoting

Saturday, March 10, 2012

Win8 Metro controls and data grouping


In Windows 8, the primary collection controls are ListView and GridView, which are items controls that derive from a common ListViewBase class. There’s also the FlipView, which binds to data collections as well but displays one item at a time. ListView and GridView have built-in support for data grouping, which makes a lot of very common scenarios a breeze to implement. In this post I’ll zoom in on the grouping feature in Metro list controls.
image
Suppose you have an app that displays a list of courses. You can use the Grid Application project template in Visual Studio to generate the skeleton for you. Then you need to create your own data source in place of the SampleDataSource provided in the project. In my example, I read course data from an XML file that I add to the project:
   1: <courses>
   2:   <course>
   3:     <title>Windows Phone 7 Hands-On</title>
   4:     <description>This course covers many real-world issues developers face when building Windows Phone applications</description>
   5:     <author>Yacine Khammal</author>
   6:     <level>Intermediate</level>
   7:     <duration>03:04:56</duration>
   8:     <published>04/18/2011</published>
   9:     <track>WP7</track>
  10:     <image>Data/Images/wphandson.png</image>
  11:     <modules>
  12:       <module>
  13:         <title>WP application architecture</title>
  14:         <description>A look at building an MVVM application for Windows Phone 7 application. We examine phone-specific choices and constraints faced when structuring a data backed business app for the phone.</description>
  15:         <image>Data/Images/wcfriaservices.png</image>
  16:         <clips>
  17:           <clip title="Intro" videouri="section1.wmv" />
  18:           <clip title="Overview of MVVM" videouri="section3.wmv" />
  19:           <clip title="Windows Phone 7 considerations" videouri="section4.wmv" />
  20:           <clip title="Sample requirements" videouri="section2.wmv" />
  21:           <clip title="Hands-on : application structure" videouri="section6.wmv" />
  22:           <clip title="Hands-on : viewmodel classes" videouri="section7.wmv" />
  23:           <clip title="Hands-on : the main view" videouri="section8.wmv" />
  24:           <clip title="Hands-on : view hierarchy" videouri="section9.wmv" />
  25:           <clip title="Navigation topics" videouri="section10-0.wmv" />
  26:           <clip title="Hands-on : navigation" videouri="section10.wmv" />
  27:           <clip title="Commanding" videouri="section11-0.wmv" />
  28:           <clip title="Hands-on : adding commands - 1" videouri="section11.wmv" />
  29:           <clip title="Hands-on : adding commands - 2" videouri="section12.wmv" />
  30:           <clip title="Takeaways" videouri="section13.wmv" />
  31:         </clips>
  32:       </module>
  33:       <module> ... </module>
  34:     </course>
  35:     <course> ... </course>
  36: </courses>

Oh and by the way, in case you’re interested, for lack of better ideas, here’s the approach I’ve used to read the data from the xml :


   1: private async Task GetCoursesAsync()
   2: {
   3:     try
   4:     {
   5:         var storageFolder = await Package.Current.InstalledLocation.GetFolderAsync("Data");
   6:         var xmlFile = await storageFolder.GetFileAsync("CourseData.xml");
   7:         var stream = await xmlFile.OpenAsync(FileAccessMode.Read);
   8:  
   9:         XmlDocument xmlDoc = await XmlDocument.LoadFromFileAsync(xmlFile);
  10:         XDocument doc = XDocument.Parse(xmlDoc.GetXml());
  11:  
  12:         courseList = (from course in doc.Descendants("course")
  13:                       select new Course
  14:                       {
  15:                           Title = course.Element("title").Value,
  16:                           Description = course.Element("description").Value,
  17:                           Author = course.Element("author").Value,
  18:                           Level = course.Element("level").Value,
  19:                           Duration = TimeSpan.Parse(course.Element("duration").Value),
  20:                           Published = DateTime.Parse(course.Element("published").Value),
  21:                           Track = course.Element("track").Value,
  22:                           Image = new BitmapImage(new Uri(projectBaseUri, course.Element("image").Value)),
  23:                           Modules = (from module in course.Element("modules").Descendants("module")
  24:                                      select new Module
  25:                                      {
  26:                                          Title = module.Element("title").Value,
  27:                                          Description = module.Element("description").Value,
  28:                                          Image = new BitmapImage(new Uri(projectBaseUri, module.Element("image").Value)),
  29:                                          Clips = (from clip in module.Element("clips").Descendants("clip")
  30:                                                   select new Clip
  31:                                                   {
  32:                                                       Title = clip.Attribute("title").Value,
  33:                                                       VideoUri = new Uri(projectBaseUri, "Data/Video/" + clip.Attribute("videouri").Value),
  34:                                                       Transcript = clip.HasElements ? clip.Element("transcript").Value : ""
  35:                                                   }).ToList()
  36:                                      }).ToList()
  37:                       }).ToList();
  38:  
  39: ...
  40: }

So now I have a data source. Of course I’ve previously defined a Course class, a Module class, a Clip class, each with appropriate data properties. If your app also needs to update data as well as display it, then you should have your data classes extend the BindableBase class included in the Common directory of the VS project templates, and have your property setters call SetProperty() to raise UI-bound change notifications.

We could just go ahead and bind a list control to our data source, just like we’ve done in my previous post. However we want to first group the data, so the first level of listing is groups. So first, we define a grouping construct, which IS a list of items or EXPOSES a list of items. For example :


   1: public class CourseTrack
   2: {
   3:     public CourseTrack()
   4:     {
   5:         Courses = new ObservableCollection<Course>();
   6:     }
   7:  
   8:     public string TrackName { get; set; }
   9:     public ObservableCollection<Course> Courses { get; set; }
  10: }

There we have it, a CourseTrack grouped concept that exposes a list of courses as well as a group name called TrackName. Here’s an alternative approach :


   1: public class CourseTrack : List<Course>
   2: {
   3:     public string TrackName { get; set; }
   4:     public new IEnumerator<object> GetEnumerator()
   5:     {
   6:         return (IEnumerator<object>)base.GetEnumerator();
   7:     }
   8:  
   9:     public CourseTrack(string trackName, IEnumerable<Course> courses)
  10:     {
  11:         TrackName = trackName;
  12:         base.AddRange(courses);
  13:     }
  14: }

Here the CourseTrack IS a list of courses since it extends a collection. This approach is actually required if you want to use semantic zoom.  And here’s an example of how you actually group the data :


   1: internal void GroupCoursesByTrack()
   2: {
   3:     CoursesByTrack = new List<CourseTrack>();
   4:  
   5:     var query = from course in courseList
   6:                 orderby course.Track
   7:                 group course by course.Track into g
   8:                 select new CourseTrack(g.Key, g);
   9:  
  10:     CoursesByTrack = query.ToList();
  11:  
  12: }

You got it, CoursesByTrack is a list of groups, that is a list of CourseTracks each representing a list of groups.

Then you bind a CollectionViewSource to the list  :


   1: <CollectionViewSource
   2:     x:Name="tracksViewSource"
   3:     Source="{Binding CourseTracks}"
   4:     IsSourceGrouped="true" />

Your sharp eye probably didn’t miss the IsSourceGroup property, which as the name suggests indicates the data source is grouped, i.e. the elements in the list are groups. As a result, any ListViewBase control that binds to the CollectionViewSource will display the root elements as groups using the group-related templates you specify :


   1: <GridView ItemsSource="{Binding Source={StaticResource tracksViewSource}}"
   2:           ItemTemplate="{StaticResource TrackListItemTemplate}">
   3:     ...
   4:     <GridView.GroupStyle>
   5:         <GroupStyle>
   6:             <GroupStyle.HeaderTemplate>
   7:                 <DataTemplate>
   8:                     ...
   9:                 </DataTemplate>
  10:             </GroupStyle.HeaderTemplate>
  11:             <GroupStyle.Panel>
  12:                 ...
  13:             </GroupStyle.Panel>
  14:         </GroupStyle>
  15:     </GridView.GroupStyle>
  16: </GridView>

Within the GroupStyle property you can specify the group header through the HeaderTemplate, and the panel to use for displaying the list of groups using the Panel template.

Note that if you used approach 1 above, that is you define your group class, CourseTrack, as EXPOSING a collection of courses as a property as opposed to deriving from a collection type, then in your CollectionViewSource, in addition to setting IsGroupedSource to true, you need to indicate the path to the actual groups within the data source using the ItemsPath property :


   1: <CollectionViewSource
   2:     x:Name="tracksViewSource"
   3:     Source="{Binding CourseTracks}"
   4:     IsSourceGrouped="true"
   5:     ItemsPath="Courses"/>

If you’ve worked with the Developer Preview, you probably remember you had to have your group classes implement an IGroupInfo interface for this to work. Now all you need to do is extend or expose a collection property. So much simpler and more intuitive! Grouping your data source has really become a breeze.

Happy grouping !