/// <summary> /// Index, group, sort and insert an entity to the collection. /// If group doesn't exist, it will be created and sorted. /// </summary> /// <param name="item">The item to insert into.</param> /// <param name="ignoreBackendFlatList">Whether ignore backend list or not.</param> private void AddInternal(CommonViewItemModel item, bool ignoreBackendFlatList = false) { if (item != null) { var index = Indexer.GetIndexForGroup(item); // If group exists if (_itemsIndex.ContainsKey(index)) { _itemsIndex[index].Add(item); } // Or create group else { var newCreatedGroup = new GroupedItems(index, Indexer, ItemComparer); _itemsIndex.Add(index, newCreatedGroup); var processed = false; if (Items.Count != 0) { for (int i = 0; i < Items.Count; i++) { if (GroupComparer.Compare(Items[i].Title, index) >= 0) { Items.Insert(i, newCreatedGroup); processed = true; break; } } } if (!processed) { Items.Add(newCreatedGroup); } // Add item newCreatedGroup.Add(item); } // Add backend item (optional) if (!ignoreBackendFlatList) { _entityCopies.Add(item); } // Notify change if required (precondition: IsEmpty == true) if (IsEmpty) { IsEmpty = false; } } }
private int RenderGroupIfNeeded(object item, object previous, int index) { bool isAlternateRow = index % 2 != 0 && (AlternatingGroupFooterTemplate != null || AlternatingGroupHeaderTemplate != null); if (GroupComparer.Compare(item, previous) == 1) { RenderTemplate(isAlternateRow ? AlternatingGroupFooterTemplate : GroupFooterTemplate, previous, index); } if (GroupComparer.Compare(previous, item) == -1) { RenderTemplate(isAlternateRow ? AlternatingGroupHeaderTemplate : GroupHeaderTemplate, item, index++); } return(index); }