diff --git a/Toolkit.Foundation/ObservableCollection.cs b/Toolkit.Foundation/ObservableCollection.cs index e3daf3f..0acdd43 100644 --- a/Toolkit.Foundation/ObservableCollection.cs +++ b/Toolkit.Foundation/ObservableCollection.cs @@ -1,4 +1,5 @@ using CommunityToolkit.Mvvm.ComponentModel; +using Microsoft.Extensions.DependencyInjection; using System.Collections; using System.Collections.Specialized; using System.Reactive.Disposables; @@ -46,7 +47,7 @@ public partial class ObservableCollection : [ObservableProperty] private TItem? selectedItem; - private bool supressSelection; + private IDispatcher dispatcher; public ObservableCollection(IServiceProvider provider, IServiceFactory factory, IMediator mediator, @@ -62,6 +63,7 @@ public partial class ObservableCollection : subscriber.Add(this); + dispatcher = Provider.GetRequiredService(); collection.CollectionChanged += OnCollectionChanged; } @@ -81,7 +83,9 @@ public partial class ObservableCollection : subscriber.Add(this); + dispatcher = Provider.GetRequiredService(); collection.CollectionChanged += OnCollectionChanged; + AddRange(items); } @@ -278,6 +282,22 @@ public partial class ObservableCollection : return Task.CompletedTask; } + private void UpdateSelection(TItem item) + { + if (item is ISelectable newSelection) + { + if (newSelection.Selected) + { + if (SelectedItem is ISelectable oldSelection) + { + oldSelection.Selected = false; + } + + dispatcher.Invoke(() => SelectedItem = item); + } + } + } + public Task Handle(InsertEventArgs args) { if (Activated) @@ -285,19 +305,6 @@ public partial class ObservableCollection : if (args.Value is TItem item) { Insert(args.Index, item); - if (item is ISelectable newSelection) - { - if (newSelection.Selected) - { - foreach (ISelectable oldSelection in this.OfType().Where(x => x is - ISelectable oldSelection && oldSelection != newSelection)) - { - oldSelection.Selected = false; - } - - SelectedItem = item; - } - } } } else @@ -360,9 +367,10 @@ public partial class ObservableCollection : { if (Activated) { - if (args.Index >= 0 && args.Index <= Count - 1) + int index = args.Index; + if (index >= 0 && index <= Count - 1) { - RemoveAt(args.Index); + RemoveAt(index); } } else @@ -407,6 +415,7 @@ public partial class ObservableCollection : return item; } + public void Insert(int index, TItem item) => InsertItem(index, item); @@ -498,6 +507,20 @@ public partial class ObservableCollection : RemoveItem(index); + if (item.Equals(SelectedItem)) + { + if (index <= Count - 1) + { + TItem selectedItem = this[index]; + dispatcher.Invoke(() => SelectedItem = selectedItem); + } + else if (index - 1 >= 0) + { + TItem selectedItem = this[index - 1]; + dispatcher.Invoke(() => SelectedItem = selectedItem); + } + } + return true; } @@ -528,12 +551,6 @@ public partial class ObservableCollection : return true; } - public void ResetAndAddRange(Action> args) - { - Clear(); - args.Invoke(this); - } - protected virtual void ClearItems() => collection.Clear(); @@ -554,6 +571,7 @@ public partial class ObservableCollection : } })); + UpdateSelection(item); collection.Insert(index > Count ? Count : index, item); } @@ -571,18 +589,6 @@ public partial class ObservableCollection : private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) => CollectionChanged?.Invoke(this, args); - - partial void OnSelectedItemChanged(TItem? oldValue, - TItem? newValue) - { - if (SelectedItem is not null && !SelectedItem.Equals(oldValue)) - { - if (oldValue is ISelectable selectable) - { - selectable.Selected = false; - } - } - } } public partial class ObservableCollection(IServiceProvider provider,