Skip to content

Commit

Permalink
Execute notify collection changed events as a batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Jan 26, 2024
1 parent cc0fb35 commit 6072686
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
@@ -1,13 +1,14 @@
#nullable disable
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using System.Linq;
using Microsoft.Maui.Controls.Internals;

namespace Microsoft.Maui.Controls.Platform
{
internal class ObservableItemTemplateCollection : ObservableCollection<ItemTemplateContext>
internal class ObservableItemTemplateCollection : ObservableRangeCollection<ItemTemplateContext>
{
readonly IList _itemsSource;
readonly DataTemplate _itemTemplate;
Expand Down Expand Up @@ -164,10 +165,24 @@ void Add(NotifyCollectionChangedEventArgs args)

var count = args.NewItems.Count;

List<ItemTemplateContext> itemsToAdd;

if (startIndex == 0)
{
itemsToAdd = [];
}
else
{
itemsToAdd = Items.ToList();
}

for (int n = 0; n < count; n++)
{
Insert(startIndex, new ItemTemplateContext(_itemTemplate, args.NewItems[n], _container, _itemHeight, _itemWidth, _itemSpacing, _mauiContext));
var itemToAdd = new ItemTemplateContext(_itemTemplate, args.NewItems[n], _container, _itemHeight, _itemWidth, _itemSpacing, _mauiContext);
itemsToAdd.Insert(startIndex, itemToAdd);
}

ReplaceRange(itemsToAdd);
}

void Move(NotifyCollectionChangedEventArgs args)
Expand Down Expand Up @@ -204,10 +219,15 @@ void Remove(NotifyCollectionChangedEventArgs args)

var count = args.OldItems.Count;

List<ItemTemplateContext> itemsToRemove = [];

for (int n = startIndex + count - 1; n >= startIndex; n--)
{
RemoveAt(n);
var itemToRemve = Items[n];
itemsToRemove.Add(itemToRemve);
}

RemoveRange(itemsToRemove);
}

void Replace(NotifyCollectionChangedEventArgs args)
Expand All @@ -216,15 +236,22 @@ void Replace(NotifyCollectionChangedEventArgs args)

if (newItemCount == args.OldItems.Count)
{
CheckReentrancy();

Clear();

List<ItemTemplateContext> newItems = [];

for (int n = 0; n < newItemCount; n++)
{
var index = args.OldStartingIndex + n;
var oldItem = this[index];
var newItem = new ItemTemplateContext(_itemTemplate, args.NewItems[n], _container, _itemHeight, _itemWidth, _itemSpacing, _mauiContext);
Items[index] = newItem;
var update = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, oldItem, index);
OnCollectionChanged(update);
newItems.Add(newItem);
}

AddRange(newItems);

var replace = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItems, Items.ToList(), -1);
OnCollectionChanged(replace);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/Properties/AssemblyInfo.cs
Expand Up @@ -5,6 +5,8 @@
using Compatibility = Microsoft.Maui.Controls.Compatibility;

[assembly: InternalsVisibleTo("iOSUnitTests")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Benchmarks.Droid")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Benchmarks.WinUI")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Compatibility.ControlGallery")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Compatibility")]
[assembly: InternalsVisibleTo("Microsoft.Maui.Controls.Compatibility.Android")]
Expand Down

0 comments on commit 6072686

Please sign in to comment.