Repository navigation
Expand file tree
/
Copy pathAutoRefreshFilter.cs
More file actions
187 lines (146 loc) · 5.21 KB
/
Copy pathAutoRefreshFilter.cs
File metadata and controls
187 lines (146 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using DynamicData.Binding;
using FluentAssertions;
using Xunit;
namespace DynamicData.Tests;
public class AutoRefreshFilter
{
[Fact]
public void Bind_Transform_and_FilterOnObservable()
{
var count = 3;
var list = new SourceList<string>();
list.AddRange(Enumerable.Range(1, count).Select(c => $"item {c}"));
var bindedList = new ObservableCollectionExtended<string>();
list.Connect()
.FilterOnObservable(_ => Observable.Return(true))
.Transform(str => str)
.Bind(bindedList)
.Subscribe(
_ => { },
ex => {Assert.Fail("There should be no error");}
);
}
[Fact]
public void Test()
{
var a0 = new Item("A0");
var i1 = new Item("I1");
var i2 = new Item("I2");
var i3 = new Item("I3");
var obsList = new SourceList<Item>();
obsList.AddRange(new[] { a0, i1, i2, i3 });
var obsListDerived = obsList.Connect().AutoRefresh(x => x.Name).Filter(x => x.Name.Contains("I")).AsObservableList();
obsListDerived.Count.Should().Be(3);
obsListDerived.Items.Should().BeEquivalentTo(new []{ i1, i2, i3});
i1.Name = "X2";
obsListDerived.Count.Should().Be(2);
obsListDerived.Items.Should().BeEquivalentTo(new[] { i2, i3});
a0.Name = "I0";
obsListDerived.Count.Should().Be(3);
obsListDerived.Items.Should().BeEquivalentTo(new[] { a0, i2, i3});
}
[Fact]
public void AutoRefreshWithObservablePredicate1()
{
var item1 = new ActivableItem
{
Activated = false
};
var items = new SourceList<ActivableItem>();
items.Add(item1);
var filterSubject = new BehaviorSubject<Func<ActivableItem, bool>>(_ => false);
var obsListDerived = items
.Connect()
.AutoRefresh(i => i.Activated)
.Filter(filterSubject)
.Do(x => Console.WriteLine("Changes" + x.TotalChanges))
.AsObservableList();
// Default filter predicate denies all items
// The binding collection should stay empty, until the predicate changes
obsListDerived.Count.Should().Be(0);
item1.Activated = true;
obsListDerived.Count.Should().Be(0);
item1.Activated = false;
obsListDerived.Count.Should().Be(0);
item1.Activated = true;
obsListDerived.Count.Should().Be(0);
// Changing predicate, all "Activated" items should added to the binding collection
filterSubject.OnNext(i => i.Activated);
obsListDerived.Count.Should().Be(1);
obsListDerived.Items.Should().BeEquivalentTo(new[] { item1 });
// Changing property value
item1.Activated = false;
obsListDerived.Count.Should().Be(0);
}
[Fact]
public void AutoRefreshWithObservablePredicate2()
{
var item1 = new ActivableItem
{
Activated = false
};
var items = new ObservableCollection<ActivableItem>();
items.Add(item1);
var filterSubject = new BehaviorSubject<Func<ActivableItem, bool>>(_ => false);
var obsListDerived = items
.ToObservableChangeSet()
.AutoRefresh(i => i.Activated)
.Filter(filterSubject)
.AsObservableList();
// Default filter predicate denies all items
// The binding collection should stay empty, until the predicate changes
obsListDerived.Count.Should().Be(0);
item1.Activated = true;
obsListDerived.Count.Should().Be(0);
item1.Activated = false;
obsListDerived.Count.Should().Be(0);
item1.Activated = true;
obsListDerived.Count.Should().Be(0);
// Changing predicate, all "Activated" items should added to the binding collection
filterSubject.OnNext(i => i.Activated);
obsListDerived.Count.Should().Be(1);
obsListDerived.Items.Should().BeEquivalentTo(new[] { item1 });
// Changing property value multiple times
item1.Activated = false;
item1.Activated = true;
item1.Activated = false;
item1.Activated = true;
obsListDerived.Count.Should().Be(1);
obsListDerived.Items.Should().BeEquivalentTo(new[] { item1 });
}
}
public class Item(string name) : INotifyPropertyChanged
{
private string _name = name;
public event PropertyChangedEventHandler? PropertyChanged;
public Guid Id { get; } = Guid.NewGuid();
public string Name
{
get => _name;
set
{
_name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
}
}
}
public class ActivableItem : INotifyPropertyChanged
{
private bool _activated;
public bool Activated
{
get => _activated;
set
{
_activated = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Activated)));
}
}
public event PropertyChangedEventHandler? PropertyChanged;
}