Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
using System;
using System.Collections;
using System.Collections.Specialized;

namespace Microsoft.Maui.Controls.Compatibility.Platform.Android
{
internal class ObservableItemsSource : IItemsViewSource
{
readonly IEnumerable _itemsSource;
readonly BindableObject _container;
readonly ICollectionChangedNotifier _notifier;
bool _disposed;

public ObservableItemsSource(IEnumerable itemSource, BindableObject container, ICollectionChangedNotifier notifier)
{
_itemsSource = itemSource as IList ?? itemSource as IEnumerable;
_container = container;
_notifier = notifier;

((INotifyCollectionChanged)itemSource).CollectionChanged += CollectionChanged;
}


internal event NotifyCollectionChangedEventHandler CollectionItemsSourceChanged;

public int Count => ItemsCount() + (HasHeader ? 1 : 0) + (HasFooter ? 1 : 0);

public bool HasHeader { get; set; }
public bool HasFooter { get; set; }

public void Dispose()
{
Dispose(true);
}

public bool IsFooter(int index)
{
return HasFooter && index == Count - 1;
}

public bool IsHeader(int index)
{
return HasHeader && index == 0;
}

public int GetPosition(object item)
{
for (int n = 0; n < ItemsCount(); n++)
{
var elementByIndex = ElementAt(n);
var isEqual = elementByIndex == item || (elementByIndex != null && item != null && elementByIndex.Equals(item));

if (isEqual)
{
return AdjustPositionForHeader(n);
}
}

return -1;
}

public object GetItem(int position)
{
return ElementAt(AdjustIndexForHeader(position));
}

protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}

_disposed = true;

if (disposing)
{
((INotifyCollectionChanged)_itemsSource).CollectionChanged -= CollectionChanged;
}
}

int AdjustIndexForHeader(int index)
{
return index - (HasHeader ? 1 : 0);
}

int AdjustPositionForHeader(int position)
{
return position + (HasHeader ? 1 : 0);
}

void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
_container.Dispatcher.DispatchIfRequired(() => CollectionChanged(args));
}

void CollectionChanged(NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
Add(args);
break;
case NotifyCollectionChangedAction.Remove:
Remove(args);
break;
case NotifyCollectionChangedAction.Replace:
Replace(args);
break;
case NotifyCollectionChangedAction.Move:
Move(args);
break;
case NotifyCollectionChangedAction.Reset:
_notifier.NotifyDataSetChanged();
break;
default:
throw new ArgumentOutOfRangeException();
}
CollectionItemsSourceChanged?.Invoke(this, args);
}

void Move(NotifyCollectionChangedEventArgs args)
{
var count = args.NewItems.Count;

if (count == 1)
{
// For a single item, we can use NotifyItemMoved and get the animation
_notifier.NotifyItemMoved(this, AdjustPositionForHeader(args.OldStartingIndex), AdjustPositionForHeader(args.NewStartingIndex));
return;
}

var start = AdjustPositionForHeader(Math.Min(args.OldStartingIndex, args.NewStartingIndex));
var end = AdjustPositionForHeader(Math.Max(args.OldStartingIndex, args.NewStartingIndex) + count);
_notifier.NotifyItemRangeChanged(this, start, end);
}

void Add(NotifyCollectionChangedEventArgs args)
{
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : _itemsSource.IndexOf(args.NewItems[0]);
startIndex = AdjustPositionForHeader(startIndex);
var count = args.NewItems.Count;

if (count == 1)
{
_notifier.NotifyItemInserted(this, startIndex);
return;
}

_notifier.NotifyItemRangeInserted(this, startIndex, count);
}

void Remove(NotifyCollectionChangedEventArgs args)
{
var startIndex = args.OldStartingIndex;

if (startIndex < 0)
{
// INCC implementation isn't giving us enough information to know where the removed items were in the
// collection. So the best we can do is a NotifyDataSetChanged()
_notifier.NotifyDataSetChanged();
return;
}

startIndex = AdjustPositionForHeader(startIndex);

// If we have a start index, we can be more clever about removing the item(s) (and get the nifty animations)
var count = args.OldItems.Count;

if (count == 1)
{
_notifier.NotifyItemRemoved(this, startIndex);
return;
}

_notifier.NotifyItemRangeRemoved(this, startIndex, count);
}

void Replace(NotifyCollectionChangedEventArgs args)
{
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : _itemsSource.IndexOf(args.NewItems[0]);
startIndex = AdjustPositionForHeader(startIndex);
var newCount = args.NewItems.Count;

if (newCount == args.OldItems.Count)
{
// We are replacing one set of items with a set of equal size; we can do a simple item or range
// notification to the adapter
if (newCount == 1)
{
_notifier.NotifyItemChanged(this, startIndex);
}
else
{
_notifier.NotifyItemRangeChanged(this, startIndex, newCount);
}

return;
}

// The original and replacement sets are of unequal size; this means that everything currently in view will
// have to be updated. So we just have to use NotifyDataSetChanged and let the RecyclerView update everything
_notifier.NotifyDataSetChanged();
}

internal int ItemsCount()
{
if (_itemsSource is IList list)
return list.Count;

int count = 0;
foreach (var item in _itemsSource)
count++;
return count;
}

internal object ElementAt(int index)
{
if (_itemsSource is IList list)
return (index >= 0 && index < list.Count) ? list[index] : null;

int count = 0;
foreach (var item in _itemsSource)
{
if (count == index)
return item;
count++;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Microsoft.Maui.Controls.Handlers.Items
internal class DataChangeObserver : RecyclerView.AdapterDataObserver
{
IntPtr _adapter;
readonly Action _onDataChange;
readonly Action<bool> _onDataChange;

public bool Observing { get; private set; }

public DataChangeObserver(Action onDataChange) : base()
public DataChangeObserver(Action<bool> onDataChange) : base()
{
_onDataChange = onDataChange;
}
Expand Down Expand Up @@ -45,37 +45,37 @@ public void Stop(Adapter adapter)
public override void OnChanged()
{
base.OnChanged();
_onDataChange?.Invoke();
_onDataChange?.Invoke(false);
}

public override void OnItemRangeInserted(int positionStart, int itemCount)
{
base.OnItemRangeInserted(positionStart, itemCount);
_onDataChange?.Invoke();
_onDataChange?.Invoke(true);
}

public override void OnItemRangeChanged(int positionStart, int itemCount)
{
base.OnItemRangeChanged(positionStart, itemCount);
_onDataChange?.Invoke();
_onDataChange?.Invoke(false);
}

public override void OnItemRangeChanged(int positionStart, int itemCount, Object payload)
{
base.OnItemRangeChanged(positionStart, itemCount, payload);
_onDataChange?.Invoke();
_onDataChange?.Invoke(false);
}

public override void OnItemRangeRemoved(int positionStart, int itemCount)
{
base.OnItemRangeRemoved(positionStart, itemCount);
_onDataChange?.Invoke();
_onDataChange?.Invoke(false);
}

public override void OnItemRangeMoved(int fromPosition, int toPosition, int itemCount)
{
base.OnItemRangeMoved(fromPosition, toPosition, itemCount);
_onDataChange?.Invoke();
_onDataChange?.Invoke(false);
}

bool IsValidAdapter(Adapter adapter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ internal int ItemsCount()
internal object ElementAt(int index)
{
if (_itemsSource is IList list)
return list[index];
return (index >= 0 && index < list.Count) ? list[index] : null;

int count = 0;
foreach (var item in _itemsSource)
Expand All @@ -249,7 +249,7 @@ internal object ElementAt(int index)
count++;
}

return -1;
return null;
}
}
}
Loading
Loading