private void UpdateMappings(ItemGroup group, IList items, ref int liveItemCount) { bool isLimitingGroups = InitialItemsLimit > 0; int ilg = InitialItemsLimit; int c = items.Count; #if DEBUG_GIC Debug.WriteLine("UpdateMappings is looking at " + group.Name + " working with " + c + " items and the incoming live count was " + liveItemCount); #endif Dictionary<Mapping, bool> previousItemsHash = new Dictionary<Mapping, bool>(group.Items.Count); foreach (var item in group.Items) { #if DEBUG_GIC Debug.WriteLine("A previous item is " + item.SpecializedString); #endif previousItemsHash[item] = true; } group.Items.Clear(); int insertionIndex = -1; for (int i = 0; i < c; ++i) { insertionIndex++; liveItemCount++; var icsc = items[i] as ISpecializedComparisonString; //Debug.Assert(icsc != null); string s = icsc != null ? icsc.SpecializedComparisonString : items[i].ToString(); Mapping existing; if (s == null) { continue; } bool needToCreate = true; // If it already mapped somewhere? if (_knownSpecializations.TryGetValue(s, out existing)) { //Debug.WriteLine("A known specialization exists already for " + s); if (existing.Group.Name == group.Name) { needToCreate = false; //Debug.WriteLine("+ and it stayed in the same group."); previousItemsHash.Remove(existing); // TODO: REORDERING! //int itemIndex = group.Panel.Children.IndexOf(existing.Presenter); existing.Content = items[i]; if (existing.HasBeenShownYet) { existing.Presenter.Content = existing.Content; } // Needs to be in the new ordered list. group.Items.Insert(insertionIndex, existing); // Could see if they even really changed or not. } else { //Debug.WriteLine("- but it moved to this group from another!"); // Now present in this group, remove it from the old. #if DEBUG_GIC Debug.WriteLine("A previous item moved to the new group! " + s + existing.Content); #endif existing.Group.Panel.Children.Remove(existing.Presenter); group.Items.Remove(existing); _knownSpecializations.Remove(s); // Decrement for a new insert! // i--; //insertionIndex--; // ? is this correct ? } } if (needToCreate) { // Create a new mapping. var map = new Mapping { Content = items[i], Group = group, }; map.SetToEmpty(MinimumItemHeight); _knownSpecializations[s] = map; map.OverallItemIndex = liveItemCount; // Index of 0, not a count actually. if (liveItemCount < _itemsInFirstScreen + 1) { map.IsInitialLoadSet = true; } if (isLimitingGroups && i > ilg) { map.IsHidden = true; // store insertion index? // TODO: SHOW GROUP FOOTER! } else { int count = group.Panel.Children.Count; #if DEBUG if (count < insertionIndex) { // TODO: DEBUG AND FIX!!!!! Debug.WriteLine("DANGER! This is not good math. {0} {1} {2} {3}", insertionIndex, count, IsFlatList, group != null ? group.Name : "null group name"); } #endif if (count < insertionIndex) { insertionIndex = count; } group.Panel.Children.Insert(insertionIndex, map.Presenter); group.Items.Insert(insertionIndex, map); } } } foreach (var emptyItem in previousItemsHash.Keys) { // Only if the item didn't move groups. So imagine a move up // to a higher group, this would still return for hte other. if (emptyItem.Group.Name == group.Name) { var pp = emptyItem.Presenter; #if DEBUG_GIC Debug.WriteLine("Removing previous item " + emptyItem.SpecializedString + emptyItem.ToString() + " " + emptyItem.Content); #endif emptyItem.Group.Panel.Children.Remove(pp); _knownSpecializations.Remove(emptyItem.SpecializedString); } } // Empty group. if (c == 0) { #if DEBUG_GIC Debug.WriteLine("--- empty group with c=0 so removing"); #endif group.RemoveGroup(this); } }
private void UpdateMappings(ItemGroup group, IList items, ref int liveItemCount) { bool isLimitingGroups = InitialItemsLimit > 0; int ilg = InitialItemsLimit; int c = items.Count; #if DEBUG_GIC Debug.WriteLine("UpdateMappings is looking at " + group.Name + " working with " + c + " items and the incoming live count was " + liveItemCount); #endif Dictionary <Mapping, bool> previousItemsHash = new Dictionary <Mapping, bool>(group.Items.Count); foreach (var item in group.Items) { #if DEBUG_GIC Debug.WriteLine("A previous item is " + item.SpecializedString); #endif previousItemsHash[item] = true; } group.Items.Clear(); int insertionIndex = -1; for (int i = 0; i < c; ++i) { insertionIndex++; liveItemCount++; var icsc = items[i] as ISpecializedComparisonString; //Debug.Assert(icsc != null); string s = icsc != null ? icsc.SpecializedComparisonString : items[i].ToString(); Mapping existing; if (s == null) { continue; } bool needToCreate = true; // If it already mapped somewhere? if (_knownSpecializations.TryGetValue(s, out existing)) { //Debug.WriteLine("A known specialization exists already for " + s); if (existing.Group.Name == group.Name) { needToCreate = false; //Debug.WriteLine("+ and it stayed in the same group."); previousItemsHash.Remove(existing); // TODO: REORDERING! //int itemIndex = group.Panel.Children.IndexOf(existing.Presenter); existing.Content = items[i]; if (existing.HasBeenShownYet) { existing.Presenter.Content = existing.Content; } // Needs to be in the new ordered list. group.Items.Insert(insertionIndex, existing); // Could see if they even really changed or not. } else { //Debug.WriteLine("- but it moved to this group from another!"); // Now present in this group, remove it from the old. #if DEBUG_GIC Debug.WriteLine("A previous item moved to the new group! " + s + existing.Content); #endif existing.Group.Panel.Children.Remove(existing.Presenter); group.Items.Remove(existing); _knownSpecializations.Remove(s); // Decrement for a new insert! // i--; //insertionIndex--; // ? is this correct ? } } if (needToCreate) { // Create a new mapping. var map = new Mapping { Content = items[i], Group = group, }; map.SetToEmpty(MinimumItemHeight); _knownSpecializations[s] = map; map.OverallItemIndex = liveItemCount; // Index of 0, not a count actually. if (liveItemCount < _itemsInFirstScreen + 1) { map.IsInitialLoadSet = true; } if (isLimitingGroups && i > ilg) { map.IsHidden = true; // store insertion index? // TODO: SHOW GROUP FOOTER! } else { int count = group.Panel.Children.Count; #if DEBUG if (count < insertionIndex) { // TODO: DEBUG AND FIX!!!!! Debug.WriteLine("DANGER! This is not good math. {0} {1} {2} {3}", insertionIndex, count, IsFlatList, group != null ? group.Name : "null group name"); } #endif if (count < insertionIndex) { insertionIndex = count; } group.Panel.Children.Insert(insertionIndex, map.Presenter); group.Items.Insert(insertionIndex, map); } } } foreach (var emptyItem in previousItemsHash.Keys) { // Only if the item didn't move groups. So imagine a move up // to a higher group, this would still return for hte other. if (emptyItem.Group.Name == group.Name) { var pp = emptyItem.Presenter; #if DEBUG_GIC Debug.WriteLine("Removing previous item " + emptyItem.SpecializedString + emptyItem.ToString() + " " + emptyItem.Content); #endif emptyItem.Group.Panel.Children.Remove(pp); _knownSpecializations.Remove(emptyItem.SpecializedString); } } // Empty group. if (c == 0) { #if DEBUG_GIC Debug.WriteLine("--- empty group with c=0 so removing"); #endif group.RemoveGroup(this); } }