PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/BE2012/Helpers/GroupCollection.cs

https://bitbucket.org/damirarh/bleedingedge2012
C# | 186 lines | 133 code | 36 blank | 17 comment | 8 complexity | 9999d439138848ce443870a7470db0f2 MD5 | raw file
  1. /*
  2. * The MIT License (MIT)
  3. * Copyright (c) 2012 Richard Garside - www.nogginbox.co.uk
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  6. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  7. */
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.ComponentModel;
  12. using System.Linq;
  13. namespace BE2012.Helpers
  14. {
  15. /// <summary>
  16. /// A collection of Groups of T that gives you easy access to all items and also selected items
  17. /// </summary>
  18. public class GroupCollection<TItem, TTitle>
  19. {
  20. #region Constructors
  21. public GroupCollection(IEnumerable<TTitle> groupTitles, Func<TItem, TTitle> groupTitleFunc)
  22. {
  23. Groups = new ObservableCollection<Group<TItem, TTitle>>();
  24. Items = new ObservableCollection<TItem>();
  25. ItemsWithHeaders = new ObservableCollection<object>();
  26. _groupTitleFunc = groupTitleFunc;
  27. InitGroups(groupTitles);
  28. }
  29. #endregion
  30. #region Properties
  31. public ObservableCollection<Group<TItem, TTitle>> Groups { get; private set; }
  32. public ObservableCollection<TItem> Items { get; private set; }
  33. public ObservableCollection<object> ItemsWithHeaders { get; private set; }
  34. private readonly Func<TItem, TTitle> _groupTitleFunc;
  35. #endregion
  36. #region Calculated read only properties
  37. /// <summary>
  38. /// Number of items in all groups
  39. /// </summary>
  40. public int ItemCount
  41. {
  42. get
  43. {
  44. return Groups.Select(fg => fg.Items.Count).Sum();
  45. }
  46. }
  47. #endregion
  48. #region Public methods
  49. public void AddItems(IEnumerable<TItem> items)
  50. {
  51. AddItemsToGroups(items);
  52. AddItemsGenerateItemsList();
  53. RaisePropertyChanged("ItemCount");
  54. }
  55. private void AddItemsGenerateItemsList()
  56. {
  57. Items.Clear();
  58. ItemsWithHeaders.Clear();
  59. foreach (var group in Groups)
  60. {
  61. if (!group.Items.Any()) continue;
  62. ItemsWithHeaders.Add(group);
  63. foreach (var item in group.Items)
  64. {
  65. Items.Add(item);
  66. ItemsWithHeaders.Add(item);
  67. }
  68. }
  69. }
  70. private void AddItemsToGroups(IEnumerable<TItem> items)
  71. {
  72. var itemGroups = items.GroupBy(_groupTitleFunc);
  73. foreach (var itemGroup in itemGroups)
  74. {
  75. var title = itemGroup.Key;
  76. var group = Groups.FirstOrDefault(g => g.Title.Equals(title));
  77. if (group == null)
  78. {
  79. group = new Group<TItem, TTitle>(title);
  80. InsertGroup(group);
  81. }
  82. foreach (var item in itemGroup)
  83. {
  84. group.Items.Add(item);
  85. }
  86. }
  87. }
  88. public Group<TItem, TTitle> GetGroupFor(TItem item)
  89. {
  90. return Groups.FirstOrDefault(fg => fg.Items.Contains(item));
  91. }
  92. /// <summary>
  93. /// Inserts a group in the correct place in the collection of groups
  94. /// </summary>
  95. /// <param name="group"></param>
  96. public void InsertGroup(Group<TItem, TTitle> group)
  97. {
  98. var orderedTitles = Groups.Select(g => g.Title).Union(new List<TTitle> { group.Title }).OrderBy(t => t).ToList();
  99. var place = orderedTitles.IndexOf(group.Title);
  100. Groups.Insert(place, group);
  101. }
  102. public void RemoveItem(TItem item)
  103. {
  104. Items.Remove(item);
  105. ItemsWithHeaders.Remove(item);
  106. var groupToRemoveFrom = GetGroupFor(item);
  107. groupToRemoveFrom.Items.Remove(item);
  108. RemoveGroupIfEmpty(groupToRemoveFrom);
  109. RaisePropertyChanged("ItemCount");
  110. }
  111. private void RemoveGroupIfEmpty(Group<TItem, TTitle> group)
  112. {
  113. if (!group.Items.Any())
  114. {
  115. ItemsWithHeaders.Remove(group);
  116. }
  117. }
  118. public void ClearItems()
  119. {
  120. foreach (var group in Groups)
  121. {
  122. group.Items.Clear();
  123. }
  124. Items.Clear();
  125. ItemsWithHeaders.Clear();
  126. RaisePropertyChanged("ItemCount");
  127. }
  128. #endregion
  129. #region Init methods
  130. private void InitGroups(IEnumerable<TTitle> groupTitles)
  131. {
  132. if (groupTitles == null) return;
  133. foreach (var groupTitle in groupTitles)
  134. {
  135. var group = new Group<TItem, TTitle>(groupTitle);
  136. Groups.Add(group);
  137. }
  138. }
  139. #endregion
  140. public event PropertyChangedEventHandler PropertyChanged;
  141. protected void RaisePropertyChanged(string propertyName)
  142. {
  143. var handler = PropertyChanged;
  144. if (handler != null)
  145. {
  146. handler(this, new PropertyChangedEventArgs(propertyName));
  147. }
  148. }
  149. }
  150. }