Skip to content

Commit

Permalink
Dictionary FilteredView add operation adds last
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Sep 9, 2024
1 parent b0bc7c2 commit 9c4f7b2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
33 changes: 7 additions & 26 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,17 @@
using System.Collections.Generic;


var dict = new ObservableDictionary<int, string>();
var view = dict.CreateView(x => x).ToNotifyCollectionChanged();
dict.Add(key: 1, value: "foo");
dict.Add(key: 2, value: "bar");


// Queue <-> List Synchronization
var queue = new ObservableQueue<int>();

queue.Enqueue(1);
queue.Enqueue(10);
queue.Enqueue(100);
queue.Enqueue(1000);
queue.Enqueue(10000);

using var view = queue.CreateView(x => x.ToString() + "$");

using var viewList = view.ToViewList();

Console.WriteLine(viewList[2]); // 100$


view.ViewChanged += View_ViewChanged;

void View_ViewChanged(in SynchronizedViewChangedEventArgs<int, string> eventArgs)
foreach (var item in view)
{
if (eventArgs.Action == NotifyCollectionChangedAction.Add)
{
// eventArgs.OldItem.View.
}

throw new NotImplementedException();
Console.WriteLine(item);
}


class ViewModel
{
public int Id { get; set; }
Expand Down
17 changes: 14 additions & 3 deletions src/ObservableCollections/SynchronizedViewList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,20 @@ private void Parent_ViewChanged(in SynchronizedViewChangedEventArgs<T, TView> e)
case NotifyCollectionChangedAction.Add: // Add or Insert
if (e.IsSingleItem)
{
var index = listView.Insert(e.NewStartingIndex, e.NewItem.View);
OnCollectionChanged(e.WithNewStartingIndex(index));
return;
if (e.OldStartingIndex == -1)
{
// add operation
var index = listView.Count;
listView.Insert(index, e.NewItem.View);
OnCollectionChanged(e.WithNewStartingIndex(index));
return;
}
else
{
var index = listView.Insert(e.NewStartingIndex, e.NewItem.View);
OnCollectionChanged(e.WithNewStartingIndex(index));
return;
}
}
else
{
Expand Down

0 comments on commit 9c4f7b2

Please sign in to comment.