Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IList behavior in ListNotifyCollectionChangedSynchronizedView #61

Closed
wants to merge 4 commits into from
Closed
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
@@ -1,8 +1,9 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;

namespace ObservableCollections.Internal
{
Expand All @@ -25,7 +26,7 @@ public NotifyCollectionChangedSynchronizedView(ISynchronizedView<T, TView> paren
parent.AttachFilter(this);
}

public int Count => parent.Count;
public virtual int Count => parent.Count;

public event NotifyCollectionChangedEventHandler? CollectionChanged;
public event PropertyChangedEventHandler? PropertyChanged;
Expand Down Expand Up @@ -152,7 +153,19 @@ public TView this[int index]
{
lock (view.SyncRoot)
{
return view.list[index].Item2;
int currentIndex = 0;
foreach (var (value, itemView) in view.list)
{
if (view.CurrentFilter.IsMatch(value, itemView))
{
if (currentIndex == index)
{
return itemView;
}
currentIndex++;
}
}
throw new ArgumentOutOfRangeException(nameof(index));
}
}
set => throw new NotSupportedException();
Expand All @@ -162,14 +175,40 @@ public TView this[int index]
{
get
{
return this[index];
lock (view.SyncRoot)
{
int currentIndex = 0;
foreach (var (value, itemView) in view.list)
{
if (view.CurrentFilter.IsMatch(value, itemView))
{
if (currentIndex == index)
{
return itemView;
}
currentIndex++;
}
}
throw new ArgumentOutOfRangeException(nameof(index));
}
}
set => throw new NotSupportedException();
}

public override int Count
{
get
{
lock (view.SyncRoot)
{
return view.list.Count(item => view.CurrentFilter.IsMatch(item.Item1, item.Item2));
}
}
}

static bool IsCompatibleObject(object? value)
{
return (value is T) || (value == null && default(T) == null);
return (value is T) || (value is TView) || (value == null && default(T) == null);
}

public bool IsReadOnly => true;
Expand Down Expand Up @@ -199,11 +238,14 @@ public bool Contains(TView item)
{
lock (view.SyncRoot)
{
foreach (var listItem in view.list)
foreach (var (value, itemView) in view.list)
{
if (EqualityComparer<TView>.Default.Equals(listItem.Item2, item))
if (view.CurrentFilter.IsMatch(value, itemView))
{
return true;
if (EqualityComparer<TView>.Default.Equals(itemView, item))
{
return true;
}
}
}
}
Expand Down Expand Up @@ -234,13 +276,16 @@ public int IndexOf(TView item)
lock (view.SyncRoot)
{
var index = 0;
foreach (var listItem in view.list)
foreach (var (value, itemView) in view.list)
{
if (EqualityComparer<TView>.Default.Equals(listItem.Item2, item))
if (view.CurrentFilter.IsMatch(value, itemView))
{
return index;
if (EqualityComparer<TView>.Default.Equals(itemView, item))
{
return index;
}
index++;
}
index++;
}
}
return -1;
Expand Down Expand Up @@ -280,4 +325,4 @@ public void RemoveAt(int index)
throw new NotSupportedException();
}
}
}
}