file360

Log | Files | Refs

AlphaKeyGroup.cs (3137B)


      1 using System.Collections.Generic;
      2 using System.Globalization;
      3 using Microsoft.Phone.Globalization;
      4 
      5 namespace File360
      6 {
      7     public class AlphaKeyGroup<T> : List<T>
      8     {
      9         /// <summary>
     10         /// The delegate that is used to get the key information.
     11         /// </summary>
     12         /// <param name="item">An object of type T</param>
     13         /// <returns>The key value to use for this object</returns>
     14         public delegate string GetKeyDelegate(T item);
     15 
     16         /// <summary>
     17         /// The Key of this group.
     18         /// </summary>
     19         public string Key { get; private set; }
     20 
     21         /// <summary>
     22         /// Public constructor.
     23         /// </summary>
     24         /// <param name="key">The key for this group.</param>
     25         public AlphaKeyGroup(string key)
     26         {
     27             Key = key;
     28         }
     29 
     30         /// <summary>
     31         /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
     32         /// </summary>
     33         /// <param name="slg">The </param>
     34         /// <returns>Theitems source for a LongListSelector</returns>
     35         private static List<AlphaKeyGroup<T>> CreateGroups(SortedLocaleGrouping slg)
     36         {
     37             List<AlphaKeyGroup<T>> list = new List<AlphaKeyGroup<T>>();
     38 
     39             foreach (string key in slg.GroupDisplayNames)
     40             {
     41                 list.Add(new AlphaKeyGroup<T>(key));
     42             }
     43 
     44             return list;
     45         }
     46 
     47         /// <summary>
     48         /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
     49         /// </summary>
     50         /// <param name="items">The items to place in the groups.</param>
     51         /// <param name="ci">The CultureInfo to group and sort by.</param>
     52         /// <param name="getKey">A delegate to get the key from an item.</param>
     53         /// <param name="sort">Will sort the data if true.</param>
     54         /// <returns>An items source for a LongListSelector</returns>
     55         public static List<AlphaKeyGroup<T>> CreateGroups(IEnumerable<T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
     56         {
     57             SortedLocaleGrouping slg = new SortedLocaleGrouping(ci);
     58             List<AlphaKeyGroup<T>> list = CreateGroups(slg);
     59 
     60             foreach (T item in items)
     61             {
     62                 int index = 0;
     63                 if (slg.SupportsPhonetics)
     64                 {
     65                     //check if your database has yomi string for item
     66                     //if it does not, then do you want to generate Yomi or ask the user for this item.
     67                     //index = slg.GetGroupIndex(getKey(Yomiof(item)));
     68                 }
     69                 else
     70                 {
     71                     index = slg.GetGroupIndex(getKey(item));
     72                 }
     73                 if (index >= 0 && index < list.Count)
     74                 {
     75                     list[index].Add(item);
     76                 }
     77             }
     78 
     79             if (sort)
     80             {
     81                 foreach (AlphaKeyGroup<T> group in list)
     82                 {
     83                     group.Sort((c0, c1) => { return ci.CompareInfo.Compare(getKey(c0), getKey(c1)); });
     84                 }
     85             }
     86 
     87             return list;
     88         }
     89 
     90     }
     91 }