-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Dynamic Filter with SourceList #164
Comments
It looks to be working for me. |
I missed the filter part of the question. Use a behavior subject and pass an initial filter in. It should work then |
@RolandPheasant, Thank you, for your fast answer! In my project I use ReactiveUI's using DynamicData;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
namespace DynamicDataTests
{
class UserPermission
{
public int Id;
public int UserId;
public int RoleId;
}
class FilterContainer : ReactiveObject
{
private HashSet<int> _filter;
public HashSet<int> Filter
{
get => _filter;
set => this.RaiseAndSetIfChanged(ref _filter, value);
}
}
class Program
{
static void Main(string[] args)
{
var filterContainer = new FilterContainer();
filterContainer.Filter = new HashSet<int>();
var dynamicFilter = filterContainer
.WhenAnyValue(x => x.Filter)
.Select(ids => (Func<UserPermission, bool>) (x => ids.Contains(x.UserId)));
var list = new SourceList<UserPermission>();
list.Connect()
.Filter(dynamicFilter)
.Subscribe(x =>
{
Console.WriteLine($"Changes: {x.TotalChanges}");
});
list.Connect()
.Filter(dynamicFilter)
.ToCollection()
.Subscribe(x =>
{
Console.WriteLine($"Items: {string.Join(",", x.Select(up => up.Id))}");
});
Console.WriteLine("Add items to list");
list.Edit(innerList =>
{
innerList.Add(new UserPermission {Id = 0, UserId = 0, RoleId = 0});
innerList.Add(new UserPermission {Id = 1, UserId = 0, RoleId = 0});
innerList.Add(new UserPermission {Id = 2, UserId = 0, RoleId = 1});
innerList.Add(new UserPermission {Id = 3, UserId = 0, RoleId = 1});
Console.WriteLine($"Items in collection: {innerList.Count}");
});
Console.WriteLine("\nShow User 0");
filterContainer.Filter = new HashSet<int>(new[] {0});
Console.WriteLine("\nRemove items from list");
list.Edit(innerList =>
{
var toRemove = innerList.Where(x => x.RoleId == 0);
Console.WriteLine($"Remove: {string.Join(",", toRemove.Select(up => up.Id))}");
innerList.RemoveMany(toRemove);
Console.WriteLine($"Items in collection: {innerList.Count}");
});
Console.WriteLine("\nShow User 1");
filterContainer.Filter = new HashSet<int>(new[] {1});
Console.ReadKey();
}
}
} Output is
There are no updates when items was removed. I think that there are problems with threads/synchronization. |
I am not sure. Is the changed event always firing, and does |
|
Changed event is always firing for the not filtered list.
DynamicData.Snippets/Filter/FilterFixture.cs | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/DynamicData.Snippets/Filter/FilterFixture.cs b/DynamicData.Snippets/Filter/FilterFixture.cs
index d388511..c6c387e 100644
--- a/DynamicData.Snippets/Filter/FilterFixture.cs
+++ b/DynamicData.Snippets/Filter/FilterFixture.cs
@@ -78,6 +78,13 @@ namespace DynamicData.Snippets.Filter
schedulerProvider.TestScheduler.Start();
sut.Filtered.Items.ShouldAllBeEquivalentTo(_items.Where(a => a.Type == "Frog"));
sut.Filtered.Count.Should().Be(1);
+
+ //Remove
+ sourceList.RemoveMany(_items.Where(x => x.Type == "Frog"));
+ sut.AnimalFilter = "Whale";
+ schedulerProvider.TestScheduler.Start();
+ sut.Filtered.Items.ShouldAllBeEquivalentTo(_items.Where(a => a.Type == "Whale"));
+ sut.Filtered.Count.Should().Be(1);
}
} And it works... DynamicData.Snippets/Filter/FilterFixture.cs | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/DynamicData.Snippets/Filter/FilterFixture.cs b/DynamicData.Snippets/Filter/FilterFixture.cs
index c6c387e..9e20a2c 100644
--- a/DynamicData.Snippets/Filter/FilterFixture.cs
+++ b/DynamicData.Snippets/Filter/FilterFixture.cs
@@ -49,12 +49,17 @@ namespace DynamicData.Snippets.Filter
using (var sourceList = new SourceList<Animal>())
using (var sut = new DynamicFilter(sourceList, schedulerProvider))
{
- //start the scheduler
+ //set initial filter
+ sut.AnimalFilter = "Kitty";
schedulerProvider.TestScheduler.Start();
//add items
sourceList.AddRange(_items);
+ //clear filter
+ sut.AnimalFilter = null;
+ schedulerProvider.TestScheduler.Start();
+
sut.Filtered.Items.ShouldAllBeEquivalentTo(_items);
sut.Filtered.Count.Should().Be(_items.Length); The test fails, because in the filtered list there is the removed Frog |
diff.txt [Fact]
public void RemoveFiltered()
{
var person = new Person("P1", 1);
_source.Add(person);
_results.Data.Count.Should().Be(0, "Should be 0 people in the cache");
_filter.OnNext(p => p.Age >= 1);
_results.Data.Count.Should().Be(1, "Should be 1 people in the cache");
_source.Remove(person);
_results.Data.Count.Should().Be(0, "Should be 0 people in the cache");
} In the beginning there is a filter I think there is an error in the internal state of |
Hello!
I am new to DynamicData and cannot understand why there is no update after removing the items from filtered SourceList.
What I got:
What I expected to see:
Why there is no update after removing items?
The text was updated successfully, but these errors were encountered: