Example #1
0
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, Func <T, string> keySelector, bool sort)
        {
            CharacterGroupings slg = new CharacterGroupings();
            //List<AlphaKeyGroup<T>> list = CreateDefaultGroups(slg);
            List <AlphaKeyGroup <T> > list = CreateAZGroups();

            foreach (T item in items)
            {
                int      index = 0;
                string[] label = ChineseHelper.GetFirstWord(keySelector(item));
                for (int i = 0; i < label.Length; i++)
                {
                    string lableKey = label[i];
                    index = list.FindIndex(alphaKeyGroup => (alphaKeyGroup.Key.Equals(lableKey, StringComparison.CurrentCulture)));
                    list[index].InternalList.Add(item);
                }
            }

            if (sort)
            {
                foreach (AlphaKeyGroup <T> group in list)
                {
                    group.InternalList.Sort((c0, c1) => { return(keySelector(c0).CompareTo(keySelector(c1))); });
                    //group.InternalList = group.InternalList.OrderBy(r => {  return r.WebSite; });
                }
            }

            return(list);
        }
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, Func <T, string> keySelector, bool sort)
        {
            CharacterGroupings        slg  = new CharacterGroupings("EN");
            List <AlphaKeyGroup <T> > list = CreateDefaultGroups(slg);

            foreach (T item in items)
            {
                int index = 0;
                {
                    string label = slg.Lookup(keySelector(item));
                    index = list.FindIndex(alphakeygroup => (alphakeygroup.Key.Equals(label, StringComparison.CurrentCulture)));
                }

                if (index >= 0 && index < list.Count)
                {
                    list[index].InternalList.Add(item);
                }
            }

            if (sort)
            {
                foreach (AlphaKeyGroup <T> group in list)
                {
                    group.InternalList.Sort((c0, c1) => { return(keySelector(c0).CompareTo(keySelector(c0))); });
                }
            }

            return(list);
        }
Example #3
0
        public DataManager()
        {
            AlphaGroups = new ObservableCollection <AlphaKeyContent>();
            CharacterGroupings cg = new CharacterGroupings();

            foreach (var key in cg)
            {
                if (!string.IsNullOrEmpty(key.Label))
                {
                    AlphaGroups.Add(new AlphaKeyContent {
                        Key = key.Label
                    });
                }
            }
            var list = from f in DataList
                       group f by cg.Lookup(f.Name) into g
                       orderby g.Key
                       select g;

            foreach (var group in list)
            {
                var collection = AlphaGroups.FirstOrDefault(f => group.Key.Equals(f.Key));
                if (collection == null)
                {
                    collection = AlphaGroups.Last();
                }
                foreach (var item in group)
                {
                    collection.Add(item);
                }
            }
        }
        public void GetGroups_Click(object sender, RoutedEventArgs e)
        {
            rootPage.NotifyUser("", NotifyType.StatusMessage);
            if (LanguageText.Text == "Default")
            {
                ActiveGroupings = new CharacterGroupings();
            }
            else if (Windows.Globalization.Language.IsWellFormed(LanguageText.Text))
            {
                ActiveGroupings = new CharacterGroupings(LanguageText.Text);
            }
            else
            {
                rootPage.NotifyUser("Invalid language tag.", NotifyType.ErrorMessage);
                return;
            }

            GroupingsGrid.Items.Clear();
            groups.Clear();
            foreach (CharacterGrouping grouping in ActiveGroupings)
            {
                // Save the "other" group for the end.
                if (!string.IsNullOrEmpty(grouping.Label))
                {
                    AddGroup(grouping.Label, new TextBlock {
                        Text = grouping.Label
                    });
                }
            }
            AddGroup(GlobeString, new SymbolIcon {
                Symbol = Symbol.Globe
            });
        }
Example #5
0
        /// <summary>
        /// Groups and sorts into a list of alpha groups based on a string selector.
        /// </summary>
        /// <typeparam name="TSource">Type of the items in the list.</typeparam>
        /// <param name="source">List to be grouped and sorted.</param>
        /// <param name="selector">A selector that will provide a value that items to be sorted and grouped by.</param>
        /// <returns>A list of JumpListGroups.</returns>
        public static List <JumpListGroup <TSource> > ToAlphaGroups <TSource>(
            this IEnumerable <TSource> source, Func <TSource, string> selector)
        {
            // Get the letters representing each group for current language using CharacterGroupings class
            var characterGroupings = new CharacterGroupings();

            // Create dictionary for the letters and replace '...' with proper globe icon
            var keys = characterGroupings.Where(x => x.Label.Count() >= 1)
                       .Select(x => x.Label)
                       .ToDictionary(x => x);

            keys["..."] = "\uD83C\uDF10";

            // Create groups for each letters
            var groupDictionary = keys.Select(x => new JumpListGroup <TSource>()
            {
                Key = x.Value
            })
                                  .ToDictionary(x => (string)x.Key);

            // Sort and group items into the groups based on the value returned by the selector
            var query = from item in source
                        orderby selector(item)
                        select item;

            foreach (var item in query)
            {
                var sortValue = selector(item);
                groupDictionary[keys[characterGroupings.Lookup(sortValue)]].Add(item);
            }

            return(groupDictionary.Select(x => x.Value).ToList());
        }
Example #6
0
        private static void LookupWorldCharacter <T>(Dictionary <string, AlphaKeyGroup <T> > table, IEnumerable <T> items, Func <T, string> getKey)
        {
            var chars = new CharacterGroupings();

            foreach (var c in chars.Where(ch => ch.Label.Length > 0).Select(ch => ch.Label[0].ToString().ToUpper()).Where(c => !table.ContainsKey(c) && c != "."))
            {
                table.Add(c, new AlphaKeyGroup <T>(c));
            }

            foreach (var item in items)
            {
                var key = getKey(item);

                var lookupChar = chars.Lookup(key).ToUpper();

                var index = lookupChar.Length > 1 ? (lookupChar.Any(CharEx.IsAlphabet) ? lookupChar.First(CharEx.IsAlphabet).ToString() : lookupChar) : lookupChar;

                if (table.ContainsKey(index))
                {
                    table[index].Add(item);
                }
                else
                {
                    table.Add(index, new AlphaKeyGroup <T>(index)
                    {
                        item
                    });
                }
            }
        }
Example #7
0
        /// <summary>
        /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, CultureInfo ci, GetKeyDelegate getKey, bool sort)
        {
            CharacterGroupings        slg  = new CharacterGroupings();
            List <AlphaKeyGroup <T> > list = CreateGroups(slg);

            foreach (T item in items)
            {
                try
                {
                    string index = "";
                    index = slg.Lookup(getKey(item));
                    if (string.IsNullOrEmpty(index) == false)
                    {
                        list.Find(a => a.Key == index).Add(item);
                    }
                }
                catch
                {
                }
            }

            if (sort)
            {
                foreach (AlphaKeyGroup <T> group in list)
                {
                    group.Sort((c0, c1) => { return(ci.CompareInfo.Compare(getKey(c0), getKey(c1))); });
                }
            }

            return(list);
        }
 /// <summary>
 ///     Create a list of AlphaGroup with keys set by a SortedLocaleGrouping.
 /// </summary>
 /// <param name="slg">The </param>
 /// <returns>Theitems source for a LongListSelector</returns>
 public static List<AlphaKeyGroup> CreateGroups(CharacterGroupings slg)
 {
     return
         (from key in slg
             where string.IsNullOrWhiteSpace(key.Label) == false
             select new AlphaKeyGroup(key.Label)).ToList();
 }
Example #9
0
    public static List <JumpListGroup <TSource> > ToAlphaGroups <TSource>(
        this IEnumerable <TSource> source, Func <TSource, string> selector)
    {
        var characterGroupings = new CharacterGroupings();
        var keys = characterGroupings.Where(x => x.Label.Count() >= 1)
                   .Select(x => x.Label)
                   .ToDictionary(x => x);

        keys["..."] = "\uD83C\uDF10";
        var groupDictionary = keys.Select(x => new JumpListGroup <TSource>()
        {
            Key = x.Value
        })
                              .ToDictionary(x => (string)x.Key);
        var query = from item in source
                    orderby selector(item)
                    select item;

        foreach (var item in query)
        {
            var sortValue = selector(item);
            groupDictionary[keys[characterGroupings.Lookup(sortValue)]].Add(item);
        }

        return(groupDictionary.Select(x => x.Value).ToList());
    }
Example #10
0
        public static List<ListGroup<T>> GroupList<T>(IEnumerable<T> listToGroup, GetNameDelegate<T> getName, bool sort)
        {
            string numberKey = "0 – 9";
            string displayNumberKey = "#";
            List<ListGroup<T>> groupedList = new List<ListGroup<T>>();
            CharacterGroupings slg = new CharacterGroupings();
            foreach (CharacterGrouping key in slg)
            {
                if (string.IsNullOrWhiteSpace(key.Label) == false){
                    string displayKey = key.Label;
                    if (displayKey.Equals(numberKey))
                    {
                        displayKey = displayNumberKey;
                    }
                    groupedList.Add(new ListGroup<T>(displayKey));
                }
            }

#if WINDOWS_APP //Duct tape for Windows 8
            if (groupedList.Find(t => t.Key == "...") == null)
            {
                groupedList.Add(new ListGroup<T>("..."));
            }
#endif //Duct tape ends

            foreach (T item in listToGroup)
            {
                string itemName = GetTrimmedName(getName(item));

                string groupLabel = slg.Lookup(itemName);
                if (groupLabel.Equals(numberKey))
                {
                    groupLabel = displayNumberKey;
                }
                if (!string.IsNullOrEmpty(groupLabel))
                {
                    var groupToAddTo = groupedList.Find(t => t.Key == groupLabel);

#if WINDOWS_APP //Duct tape for Windows 8
                    if (groupToAddTo == null)
                    {
                        groupToAddTo = groupedList.Find(t => t.Key == "...");
                    }
#endif //Duct tape ends

                    groupToAddTo.Add(item);                    
                }
            }

            if (sort)
            {
                foreach (ListGroup<T> group in groupedList)
                {
                    group.Sort((c0, c1) => { return CultureInfo.InvariantCulture.CompareInfo.Compare(GetTrimmedName(getName(c0)), GetTrimmedName(getName(c1))); });
                }
            }

            return groupedList;
        }
Example #11
0
        public static void UpdateSortedCollection <T>(T item, bool removed, string key,
                                                      Func <ObservableCollection <AlphaKeyGroup <T> > > getSorted)
        {
            if (string.IsNullOrEmpty(key))
            {
                return;
            }

            var    sortedGroups = getSorted();
            var    charKey      = new CharacterGroupings().Lookup(key);
            string charkey      = charKey.ToUpper();

            AlphaKeyGroup <T> group;

            try
            {
                group = sortedGroups.First(a => a.Key.Equals(charkey, StringComparison.CurrentCultureIgnoreCase));
            }
            catch
            {
                group = sortedGroups.FirstOrDefault();
            }

            bool zero;

            if (removed)
            {
                group.Remove(item);
                zero = group.Count == 0;
            }

            else
            {
                zero = group.Count == 0;
                var index = 0;

                //if the group is not empty, then insert acording to sort
                if (!zero)
                {
                    var list = group.ToList();
                    list.Add(item);
                    list.Sort(
                        (x, y) => String.Compare(group.OrderKey(x), group.OrderKey(y), StringComparison.Ordinal));
                    index = list.IndexOf(item);
                }
                group.Insert(index, item);
            }

            if (!zero)
            {
                return;
            }

            //removing and readding to update the groups collection in the listview
            var groupIndex = sortedGroups.IndexOf(group);

            sortedGroups.Remove(group);
            sortedGroups.Insert(groupIndex, group);
        }
Example #12
0
        /// <summary>
        /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static IEnumerable <GroupedItem <T> > CreateGroupsByAlpha(IEnumerable <T> items)
        {
            var g      = new CharacterGroupings("zh-CN");
            var groups = from i in items group i by RemovePinYin(g.Lookup(i.Key));

            var ordered = groups.OrderBy(z => z.Key, new StringHelper());

            return(from p in ordered where !p.IsNullorEmpty() select new GroupedItem <T>(p));
        }
 public static List<string> CreateDefaultGroups(CharacterGroupings slg)
 {
     List<string> list = new List<string>();
     foreach (CharacterGrouping cg in slg)
     {
         if (cg.Label == "") continue;
         else
             list.Add(cg.Label);
     }
     return list;
 }
Example #14
0
        protected void CreateCharacterGroupings()
        {
            // Get the letters representing each group for current language using CharacterGroupings class
            _CharacterGroupings = new CharacterGroupings();
            // Create dictionary for the letters and replace '...' with proper globe icon
            var keys = _CharacterGroupings.Where(x => x.Label.Count() >= 1)
                       .Select(x => x.Label)
                       .ToDictionary(x => x);

            keys["..."] = "\uD83C\uDF10";
        }
        public List <MusicGroup> GroupMusicByFirstLetter(IEnumerable <MusicUi> source)
        {
            CharacterGroupings cgs = new CharacterGroupings();

            var musicGroupList = source.GroupBy(mu => cgs.Lookup(mu.Title).Replace("拼音", "")).Select(g => new MusicGroup(g.Key, g)).ToList();
            var cgsGroupList   = cgs.Where(c => !c.Label.Contains("拼音") && musicGroupList.All(mg => mg.Name != c.Label))
                                 .Select(c => new MusicGroup(c.Label)).ToList();

            musicGroupList.AddRange(cgsGroupList);
            return(musicGroupList.Where(mg => mg.Items.Any() || !string.IsNullOrWhiteSpace(mg.Name)).ToList());
        }
Example #16
0
        /// <summary>
        /// Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="slg">The </param>
        /// <returns>Theitems source for a LongListSelector</returns>
        private static List<AlphaKeyGroup<T>> CreateGroups(CharacterGroupings slg)
        {
            List<AlphaKeyGroup<T>> list = new List<AlphaKeyGroup<T>>();

            foreach (CharacterGrouping key in slg)
            {
                if (string.IsNullOrWhiteSpace(key.Label) == false)
                    list.Add(new AlphaKeyGroup<T>(key.Label));
            }

            return list;
        }
Example #17
0
        private static Dictionary <string, JumpListGroup <TSource> > ToAlphaGroupsDictionary <TSource>(
            IEnumerable <TSource> source, Func <TSource, string> selector)
        {
            // Get the letters representing each group for current language using CharacterGroupings class
            var characterGroupings = new CharacterGroupings();

            // Create dictionary for the letters and replace '...' with proper globe icon
            var keys = characterGroupings.Where(x => x.Label.Count() >= 1)
                       .Select(x => x.Label)
                       .ToDictionary(x => x);

            keys["..."] = "\uD83C\uDF10";

            // Create groups for each letters
            var groupDictionary = keys.Select(x => new JumpListGroup <TSource>()
            {
                Key = x.Value
            })
                                  .ToDictionary(x => (string)x.Key);

            // Sort and group items into the groups based on the value returned by the selector
            var query = from item in source
                        orderby selector(item)
                        select item;

            foreach (var item in query)
            {
                try
                {
                    var    sortValue = selector(item);
                    string lookup    = characterGroupings.Lookup(sortValue);

                    string key;
                    if (!keys.TryGetValue(lookup, out key))
                    {
                        key = "\uD83C\uDF10";
                    }

                    groupDictionary[key].Add(item);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }

            return(groupDictionary);
        }
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, Func <T, string> keySelector)
        {
            CharacterGroupings        slg  = new CharacterGroupings();
            List <AlphaKeyGroup <T> > list = CreateAZGroups(); //CreateDefaultGroups(slg);

            foreach (T item in items)
            {
                int    index = 0;
                string label = slg.Lookup(keySelector(item));
                index = list.FindIndex(alphagroupkey => (alphagroupkey.Key.Equals(label, StringComparison.CurrentCulture)));
                if (index > -1 && index < list.Count)
                {
                    list[index].Add(item);
                }
            }
            return(list);
        }
Example #19
0
        private void Load()
        {
            if (records == null)
            {
                records = TmpData.PasswordKeeper.Records;
            }
            CharacterGroupings groupings = new CharacterGroupings();

            observablerecords = new RangedObservableCollection <PasswordRecordViewModelGroup>(records.Select(a => PasswordRecordViewModel.CreateFromRecord(a))
                                                                                              .OrderBy(a => a.DisplayName).GroupBy((a) => groupings.Lookup(a.DisplayName))
                                                                                              .Select(a => new PasswordRecordViewModelGroup(a.Key, ct(a))));

            UpdateForVisualState(AdaptiveStates.CurrentState);

            // Don't play a content transition for first item load.
            // Sometimes, this content will be animated as part of the page transition.
            DisableContentTransitions();
        }
Example #20
0
        /// <summary>
        ///     Create a list of AlphaGroup<T> with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static OptimizedObservableCollection <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, CultureInfo ci,
                                                                                      GetKeyDelegate getKey,
                                                                                      bool sort)
        {
            var slg  = new CharacterGroupings();
            var list = CreateGroups(slg);

            foreach (T item in items)
            {
                var lookUp = getKey(item);

                if (string.IsNullOrEmpty(lookUp))
                {
                    continue;
                }

                var index  = slg.Lookup(lookUp);
                var indexs = index.ToUpper();

                try
                {
                    list.Find(a => a.Key.Equals(indexs, StringComparison.CurrentCultureIgnoreCase)).Items.Add(item);
                }
                catch
                {
                    list.FirstOrDefault().Items.Add(item);
                }


                if (sort)
                {
                    foreach (AlphaKeyGroup <T> group in list)
                    {
                        group.OrderKey = getKey;
                        var asList = group.ToList();
                        asList.Sort((x, y) => string.Compare(getKey(x), getKey(y), StringComparison.Ordinal));
                        group.SwitchTo(asList);
                    }
                }
            }

            return(new OptimizedObservableCollection <AlphaKeyGroup <T> >(list));
        }
Example #21
0
        /// <summary>
        /// 原始分组创建
        /// </summary>
        /// <param name="groupings"></param>
        /// <returns></returns>
        private static List <AlphaKeyGroup <T> > CreateDefaultGroups(CharacterGroupings groupings)
        {
            List <AlphaKeyGroup <T> > list = new List <AlphaKeyGroup <T> >();

            foreach (CharacterGrouping one in groupings)
            {
                if (one.Label == "")
                {
                    continue;
                }
                if (one.Label == "...")
                {
                    list.Add(new AlphaKeyGroup <T>(GlobeGroupKey));
                }
                else
                {
                    list.Add(new AlphaKeyGroup <T>(one.Label));
                }
            }
            return(list);
        }
Example #22
0
        public static ObservableCollection <GroupedOC <T> > CreateGrouped <T>(IEnumerable <T> InitialItemsList, Func <T, string> selector)
        {
            ObservableCollection <GroupedOC <T> > GroupedItems = new ObservableCollection <GroupedOC <T> >();
            //ObservableCollection<IGrouping<string, T>> GroupedItems2 = new ObservableCollection<IGrouping<string, T>>();
            //ObservableCollection<GroupedOC<T>> GroupedItems2 = new ObservableCollection<GroupedOC<T>>;

            var characterGroupings = new CharacterGroupings();

            //System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
            //st.Start();
            //foreach (var q in InitialItemsList.GroupBy(s => characterGroupings.Lookup(selector(s))).Select(s => s).OrderBy(t => t.Key))
            //{
            //    GroupedItems2.Add(q);
            //}
            //foreach (var c in characterGroupings)
            //{
            //    bool found = false;
            //    foreach(var g in GroupedItems2)
            //    {
            //        if (g.Key == c.Label) found = true;
            //    }
            //    if (!found)
            //    {
            //        GroupedItems2.Add(new GroupedOC<T>(c.Label));
            //    }
            //}
            //st.Stop();
            //st.Restart();
            foreach (var c in characterGroupings)
            {
                GroupedItems.Add(new GroupedOC <T>(c.Label));
            }
            foreach (var item in InitialItemsList)
            {
                string a = characterGroupings.Lookup(selector(item));
                GroupedItems.FirstOrDefault(e => e.Key.Equals(a)).Add(item);
            }
            //st.Stop();
            return(GroupedItems);
        }
        private static List <AlphaKeyGroup <T> > CreateDefaultGroups(CharacterGroupings slg)
        {
            List <AlphaKeyGroup <T> > list = new List <AlphaKeyGroup <T> >();

            foreach (CharacterGrouping cg in slg)
            {
                if (cg.Label == "")
                {
                    continue;
                }
                if (cg.Label == "...")
                {
                    continue;
                }
                else
                {
                    list.Add(new AlphaKeyGroup <T>(cg.Label));
                }
            }

            return(list);
        }
Example #24
0
        public static List <AlphaKeyGroup <T> > CreateGroups(IEnumerable <T> items, Func <T, string> keySelector, bool sort)
        {
            CharacterGroupings slg = new CharacterGroupings();
            //List<AlphaKeyGroup<T>> list = CreateDefaultGroups(slg);
            List <AlphaKeyGroup <T> > list = CreateAZGroups();

            foreach (T item in items)
            {
                int index = 0;
                {
                    string label = ChineseHelper.GetFirstWord(keySelector(item));
                    //string label = slg.Lookup(keySelector(item));
                    index = list.FindIndex(alphaKeyGroup => (alphaKeyGroup.Key.Equals(label, StringComparison.CurrentCulture)));
                }

                if (index >= 0 && index < list.Count)
                {
                    list[index].InternalList.Add(item);
                }
                else
                {
                    //临时解决方案,中文加入??中
                    list[list.Count - 1].InternalList.Add(item);
                }
            }

            if (sort)
            {
                foreach (AlphaKeyGroup <T> group in list)
                {
                    group.InternalList.Sort((c0, c1) => { return(keySelector(c0).CompareTo(keySelector(c1))); });
                    //group.InternalList = group.InternalList.OrderBy(r => {  return r.WebSite; });
                }
            }

            return(list);
        }
        /// <summary>
        /// Groups and sorts into a list of alpha groups based on a string selector.
        /// </summary>
        /// <typeparam name="TSource">Type of the items in the list.</typeparam>
        /// <param name="source">List to be grouped and sorted.</param>
        /// <param name="selector">A selector that will provide a value that items to be sorted and grouped by.</param>
        /// <returns>A list of JumpListGroups.</returns>
        public static Dictionary <string, List <TSource> > ToAlphaGroups <TSource>(
            this IEnumerable <TSource> source, Func <TSource, string> selector)
        {
            // Get the letters representing each group for current language using CharacterGroupings class
            var characterGroupings = new CharacterGroupings();

            // Create dictionary for the letters and replace '...' with proper globe icon
            var keys = characterGroupings.Where(x => x.Label.Any())
                       .Select(x => x.Label)
                       .ToDictionary(x => x);

            keys["..."] = "\uD83C\uDF10";

            // Create groups for each letters
            var groupDictionary = keys.ToDictionary(x => x.Key, v => new List <TSource>());

            // Sort and group items into the groups based on the value returned by the selector
            var query = from item in source
                        orderby selector(item)
                        select item;

            foreach (var item in query)
            {
                var sortValue = selector(item);
                var key       = characterGroupings.Lookup(sortValue);
                if (keys.ContainsKey(key))
                {
                    groupDictionary[key].Add(item);
                }
                else
                {
                    groupDictionary["..."].Add(item);
                }
            }

            return(groupDictionary);
        }
        private void UpdateSortedCollection <T>(T item, bool removed, string key,
                                                Func <OptimizedObservableCollection <AlphaKeyGroup> > getSorted)
        {
            if (IsNullOrEmpty(key))
            {
                return;
            }

            var sortedGroups = getSorted();

            try
            {
                var charKey = new CharacterGroupings().Lookup(key);
                var group   = sortedGroups.FirstOrDefault(a => a.Key.EqualsIgnoreCase(charKey));

                if (!removed && group.Contains(item))
                {
                    return;
                }
                if (removed && !group.Contains(item))
                {
                    return;
                }

                bool zero;
                if (removed)
                {
                    group.Remove(item);
                    zero = group.Count == 0;
                }

                else
                {
                    zero = group.Count == 0;
                    var index = 0;

                    //if the group is not empty, then insert acording to sort
                    if (!zero)
                    {
                        var list = group.ToList();
                        list.Add(item);
                        list.Sort((x, y) =>
                                  Compare(group.OrderKey(x), group.OrderKey(y), StringComparison.Ordinal));
                        index = list.IndexOf(item);
                    }
                    group.Insert(index, item);
                }

                if (!zero)
                {
                    return;
                }

                //removing and readding to update the groups collection in the listview
                var groupIndex = sortedGroups.IndexOf(group);
                sortedGroups.Remove(group);
                sortedGroups.Insert(groupIndex, group);
            }
            catch
            {
                // ignored
            }
        }
Example #27
0
        /// <summary>
        ///     Create a list of AlphaGroup with keys set by a SortedLocaleGrouping.
        /// </summary>
        /// <param name="items">The items to place in the groups.</param>
        /// <param name="ci">The CultureInfo to group and sort by.</param>
        /// <param name="getKey">A delegate to get the key from an item.</param>
        /// <param name="sort">Will sort the data if true.</param>
        /// <returns>An items source for a LongListSelector</returns>
        public static OptimizedObservableCollection<AlphaKeyGroup> CreateGroups(IEnumerable<object> items, CultureInfo ci,
            GetKeyDelegate getKey,
            bool sort = true)
        {
            var slg = new CharacterGroupings();
            var list = CreateGroups(slg);

            foreach (var item in items)
            {
                var lookUp = getKey(item);

                if (string.IsNullOrEmpty(lookUp))
                    continue;

                var index = slg.Lookup(lookUp);
                if (string.IsNullOrEmpty(index) == false)
                {
                    list.Find(a => a.Key.EqualsIgnoreCase(index)).Items.Add(item);
                }
            }

            var max = list.Select(p => p.Count).Max();

            if (sort)
            {
                foreach (var group in list)
                {
                    group.OrderKey = getKey;
                    var percent = group.Count > 1 ? Math.Log(group.Count, max) : Math.Log(2, max) / 2;
                    percent = Math.Max(.1, percent);
                    if (double.IsNaN(percent))
                        percent = 0.1;
                    if (group.Count == 0)
                        percent = 0;

                    group.GridLeftLength = new GridLength(percent, GridUnitType.Star);
                    group.GridRightLength = new GridLength(1 - percent, GridUnitType.Star);

                    var asList = group.ToList();
                    asList.Sort((x, y) => string.Compare(getKey(x), getKey(y), StringComparison.Ordinal));
                    group.SwitchTo(asList);

                    group.CollectionChanged += (sender, args) =>
                    {
                        max = list.Select(p => p.Count).Max();
                        foreach (var other in list)
                        {
                            percent = other.Count > 1 ? Math.Log(other.Count, max) : Math.Log(2, max) / 2;
                            percent = Math.Max(.1, percent);
                            if (double.IsNaN(percent))
                                percent = 0.1;
                            if (other.Count == 0)
                                percent = 0;

                            other.GridLeftLength = new GridLength(percent, GridUnitType.Star);
                            other.GridRightLength = new GridLength(1 - percent, GridUnitType.Star);
                        }
                    };
                }
            }

            return new OptimizedObservableCollection<AlphaKeyGroup>(list);
        }
Example #28
0
 /// <summary>
 /// Class constructor.
 /// </summary>
 public CharacterGroupingIndexer()
 {
     _slg = new CharacterGroupings();
 }
Example #29
0
 private static IEnumerable <string> GetDefaultGroup(CharacterGroupings g)
 {
     return(from i in g where !i.Label.IsNullorEmpty() select i.Label);
 }