From dce0bb618914376483391f252ca0486e3e9b9faa Mon Sep 17 00:00:00 2001 From: Yoshifumi Kawai <46207+neuecc@users.noreply.github.com> Date: Wed, 4 Sep 2024 08:47:47 +0900 Subject: [PATCH 1/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8170ac4..761ccca 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Observable> IObservableCollection.ObserveReset() Observable> IObservableCollection.ObserveReset() Observable IObservableCollection.ObserveClear() Observable<(int Index, int Count)> IObservableCollection.ObserveReverse() -Observable<(int Index, int Count, IComparer Comparer)> IObservableCollection.ObserveSort() +Observable<(int Index, int Count, IComparer? Comparer)> IObservableCollection.ObserveSort() Observable IObservableCollection.ObserveCountChanged() ``` From 7a573289ea1deb20f3350c851471dae0ed6f1d67 Mon Sep 17 00:00:00 2001 From: neuecc Date: Wed, 4 Sep 2024 08:53:19 +0900 Subject: [PATCH 2/8] fix ObserveSort comparer nullable annotation --- .../ObservableCollectionR3Extensions.cs | 10 +++++----- .../NotifyCollectionChangedEventArgs.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ObservableCollections.R3/ObservableCollectionR3Extensions.cs b/src/ObservableCollections.R3/ObservableCollectionR3Extensions.cs index 67d5da7..c62a02e 100644 --- a/src/ObservableCollections.R3/ObservableCollectionR3Extensions.cs +++ b/src/ObservableCollections.R3/ObservableCollectionR3Extensions.cs @@ -71,7 +71,7 @@ public static class ObservableCollectionR3Extensions return new ObservableCollectionReverse(source, cancellationToken); } - public static Observable<(int Index, int Count, IComparer Comparer)> ObserveSort(this IObservableCollection source, CancellationToken cancellationToken = default) + public static Observable<(int Index, int Count, IComparer? Comparer)> ObserveSort(this IObservableCollection source, CancellationToken cancellationToken = default) { return new ObservableCollectionSort(source, cancellationToken); } @@ -290,18 +290,18 @@ sealed class ObservableCollectionReverse(IObservableCollection collection, } } -sealed class ObservableCollectionSort(IObservableCollection collection, CancellationToken cancellationToken) : Observable<(int Index, int Count, IComparer Comparer)> +sealed class ObservableCollectionSort(IObservableCollection collection, CancellationToken cancellationToken) : Observable<(int Index, int Count, IComparer? Comparer)> { - protected override IDisposable SubscribeCore(Observer<(int Index, int Count, IComparer Comparer)> observer) + protected override IDisposable SubscribeCore(Observer<(int Index, int Count, IComparer? Comparer)> observer) { return new _ObservableCollectionSort(collection, observer, cancellationToken); } sealed class _ObservableCollectionSort( IObservableCollection collection, - Observer<(int Index, int Count, IComparer Comparer)> observer, + Observer<(int Index, int Count, IComparer? Comparer)> observer, CancellationToken cancellationToken) - : ObservableCollectionObserverBase Comparer)>(collection, observer, cancellationToken) + : ObservableCollectionObserverBase? Comparer)>(collection, observer, cancellationToken) { protected override void Handler(in NotifyCollectionChangedEventArgs eventArgs) { diff --git a/src/ObservableCollections/NotifyCollectionChangedEventArgs.cs b/src/ObservableCollections/NotifyCollectionChangedEventArgs.cs index 790c606..22c149a 100644 --- a/src/ObservableCollections/NotifyCollectionChangedEventArgs.cs +++ b/src/ObservableCollections/NotifyCollectionChangedEventArgs.cs @@ -25,9 +25,9 @@ namespace ObservableCollections Comparer = comparer ?? NullComparerSentinel.Instance; } - public (int Index, int Count, IComparer Comparer) AsTuple() + public (int Index, int Count, IComparer? Comparer) AsTuple() { - return (Index, Count, Comparer!); + return (Index, Count, Comparer); } public static SortOperation CreateReverse(int index, int count) From 7ad977ffca03b5608d1c05beaff4453d6e485c96 Mon Sep 17 00:00:00 2001 From: neuecc Date: Wed, 4 Sep 2024 10:40:04 +0900 Subject: [PATCH 3/8] support Range for ToNotifyCollectionChanged #67 --- README.md | 4 + sandbox/AvaloniaApp/MainWindow.axaml | 11 ++- sandbox/AvaloniaApp/MainWindow.axaml.cs | 58 ++++++++++-- sandbox/WpfApp/MainWindow.xaml | 2 + sandbox/WpfApp/MainWindow.xaml.cs | 22 ++++- .../AlternateIndexList.cs | 4 +- .../ObservableList.OptimizeView.cs | 10 ++- .../SynchronizedViewList.cs | 88 ++++++++++++++++--- 8 files changed, 168 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 761ccca..21cf6b9 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,10 @@ public class WpfDispatcherCollection(Dispatcher dispatcher) : ICollectionEventDi } ``` +`ToNotifyCollectionChanged()` can also be called without going through a View. In this case, it's guaranteed that no filters will be applied, making it faster. If you want to apply filters, please generate a View before calling it. Additionally, `ObservableList` has a variation called `ToNotifyCollectionChangedSlim()`. This option doesn't generate a list for the View and shares the actual data, making it the fastest and most memory-efficient option. However, range operations such as `AddRange`, `InsertRange` and `RemoveRange` are not supported by WPF (or Avalonia), so they will throw runtime exceptions. + +Views and ToNotifyCollectionChanged are internally connected by events, so they need to be `Dispose` to release those connections. + Unity --- In Unity projects, you can installing `ObservableCollections` with [NugetForUnity](https://github.com/GlitchEnzo/NuGetForUnity). If R3 integration is required, similarly install `ObservableCollections.R3` via NuGetForUnity. diff --git a/sandbox/AvaloniaApp/MainWindow.axaml b/sandbox/AvaloniaApp/MainWindow.axaml index cfd3b92..9530d9b 100644 --- a/sandbox/AvaloniaApp/MainWindow.axaml +++ b/sandbox/AvaloniaApp/MainWindow.axaml @@ -7,7 +7,14 @@ Title="AvaloniaApp"> -