/
ShowExpanderObservable.cs
89 lines (78 loc) · 3.12 KB
/
ShowExpanderObservable.cs
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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Avalonia.Data;
using Avalonia.Experimental.Data;
using Avalonia.Reactive;
namespace Avalonia.Controls.Models.TreeDataGrid
{
internal class ShowExpanderObservable<TModel> : SingleSubscriberObservableBase<bool>,
IObserver<BindingValue<bool>>,
IObserver<BindingValue<IEnumerable<TModel>?>>
where TModel : class
{
private readonly Func<TModel, IEnumerable<TModel>?> _childSelector;
private readonly TypedBinding<TModel, bool>? _hasChildrenSelector;
private TModel? _model;
private IDisposable? _subscription;
private INotifyCollectionChanged? _incc;
public ShowExpanderObservable(
Func<TModel, IEnumerable<TModel>?> childSelector,
TypedBinding<TModel, bool>? hasChildrenSelector,
TModel model)
{
_childSelector = childSelector;
_hasChildrenSelector = hasChildrenSelector;
_model = model;
}
protected override void Subscribed()
{
if (_model is null)
throw new ObjectDisposedException(nameof(ShowExpanderObservable<TModel>));
if (_hasChildrenSelector is not null)
_subscription = _hasChildrenSelector?.Instance(_model).Subscribe(this);
else
// TODO: _childSelector needs to be made into a binding; leaving the observable
// machinery in place for this to be turned into a subscription later.
((IObserver<BindingValue<IEnumerable<TModel>?>>)this).OnNext(new(_childSelector(_model)));
}
protected override void Unsubscribed()
{
_subscription?.Dispose();
_subscription = null;
_model = null;
}
void IObserver<BindingValue<bool>>.OnNext(BindingValue<bool> value)
{
if (value.HasValue)
PublishNext(value.Value);
}
void IObserver<BindingValue<IEnumerable<TModel>?>>.OnNext(BindingValue<IEnumerable<TModel>?> value)
{
if (_incc is not null)
_incc.CollectionChanged -= OnCollectionChanged;
if (value.HasValue && value.Value is not null)
{
if (value.Value is INotifyCollectionChanged incc)
{
_incc = incc;
_incc.CollectionChanged += OnCollectionChanged;
}
PublishNext(value.Value.Any());
}
else
{
PublishNext(false);
}
}
void IObserver<BindingValue<bool>>.OnCompleted() { }
void IObserver<BindingValue<IEnumerable<TModel>?>>.OnCompleted() { }
void IObserver<BindingValue<bool>>.OnError(Exception error) { }
void IObserver<BindingValue<IEnumerable<TModel>?>>.OnError(Exception error) { }
private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
PublishNext((sender as IEnumerable<TModel>)?.Any() ?? false);
}
}
}