Fix ItemsControl not handling collection changed

This commit is contained in:
2023-09-05 11:11:15 +02:00
parent b85c19407e
commit 0050439932
2 changed files with 30 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using ObservableComputations;
using PropertyChanged.SourceGenerator;
using TerminalUI.Models;
@@ -27,6 +28,10 @@ public sealed partial class ItemsControl<TDataContext, TItem>
set
{
if (_itemsSource == value) return;
if(_itemsSource is INotifyCollectionChanged notifyCollectionChanged)
notifyCollectionChanged.CollectionChanged -= SourceCollectionChanged;
_itemsSource = value;
foreach (var disposable in _itemsDisposables)
@@ -39,17 +44,25 @@ public sealed partial class ItemsControl<TDataContext, TItem>
if (_itemsSource is ObservableCollection<TItem> observableDeclarative)
{
var consumer = new OcConsumer();
_children = observableDeclarative
var children = observableDeclarative
.Selecting(i => CreateItem(i))
.For(consumer);
children.CollectionChanged += SourceCollectionChanged;
_children = children;
_itemsDisposables.Add(consumer);
}
else if (_itemsSource is ReadOnlyObservableCollection<TItem> readOnlyObservableDeclarative)
{
var consumer = new OcConsumer();
_children = readOnlyObservableDeclarative
var children = readOnlyObservableDeclarative
.Selecting(i => CreateItem(i))
.For(consumer);
children.CollectionChanged += SourceCollectionChanged;
_children = children;
_itemsDisposables.Add(consumer);
}
else if (_itemsSource is ICollection<TItem> collection)
@@ -78,6 +91,10 @@ public sealed partial class ItemsControl<TDataContext, TItem>
RerenderProperties.Add(nameof(Orientation));
}
private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) => RequestRerenderForThis();
private void RequestRerenderForThis()
=> ApplicationContext?.RenderEngine.RequestRerender(this);
protected override Size CalculateSize()
{

View File

@@ -76,6 +76,10 @@ public sealed partial class ListView<TDataContext, TItem> : View<ListView<TDataC
set
{
if (_itemsSource == value) return;
if (_itemsSource is INotifyCollectionChanged notifyCollectionChanged)
notifyCollectionChanged.CollectionChanged -= SourceCollectionChanged;
_itemsSource = value;
foreach (var disposable in _itemsDisposables)
@@ -87,15 +91,13 @@ public sealed partial class ListView<TDataContext, TItem> : View<ListView<TDataC
if (_itemsSource is ObservableCollection<TItem> observableDeclarative)
{
((INotifyCollectionChanged) observableDeclarative).CollectionChanged +=
(_, _) => ApplicationContext?.RenderEngine.RequestRerender(this);
((INotifyCollectionChanged) observableDeclarative).CollectionChanged += SourceCollectionChanged;
_getItems = () => observableDeclarative;
}
else if (_itemsSource is ReadOnlyObservableCollection<TItem> readOnlyObservableDeclarative)
{
((INotifyCollectionChanged) readOnlyObservableDeclarative).CollectionChanged +=
(_, _) => ApplicationContext?.RenderEngine.RequestRerender(this);
((INotifyCollectionChanged) readOnlyObservableDeclarative).CollectionChanged += SourceCollectionChanged;
_getItems = () => readOnlyObservableDeclarative;
}
@@ -135,6 +137,11 @@ public sealed partial class ListView<TDataContext, TItem> : View<ListView<TDataC
RerenderProperties.Add(nameof(Orientation));
}
private void SourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) => RequestRerenderForThis();
private void RequestRerenderForThis()
=> ApplicationContext?.RenderEngine.RequestRerender(this);
protected override Size CalculateSize()
{
InstantiateItemViews();