From 4c2bc8e6281e1e621954aa0bfd24d950e7608dae Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Tue, 21 May 2024 22:38:11 +0100 Subject: [PATCH] More selection edge cases fixed --- Toolkit.Foundation/ObservableCollection.cs | 20 +++++++++++++++++++- Toolkit.Foundation/Selected.cs | 10 ---------- Toolkit.Foundation/SelectedEventArgs.cs | 3 --- Toolkit.Foundation/Selection.cs | 10 ++++++++++ Toolkit.Foundation/SelectionEventArgs.cs | 3 +++ 5 files changed, 32 insertions(+), 14 deletions(-) delete mode 100644 Toolkit.Foundation/Selected.cs delete mode 100644 Toolkit.Foundation/SelectedEventArgs.cs create mode 100644 Toolkit.Foundation/Selection.cs create mode 100644 Toolkit.Foundation/SelectionEventArgs.cs diff --git a/Toolkit.Foundation/ObservableCollection.cs b/Toolkit.Foundation/ObservableCollection.cs index 7448802..57709cb 100644 --- a/Toolkit.Foundation/ObservableCollection.cs +++ b/Toolkit.Foundation/ObservableCollection.cs @@ -29,7 +29,8 @@ public partial class ObservableCollection : INotificationHandler>, INotificationHandler>, INotificationHandler>, - INotificationHandler> + INotificationHandler>, + INotificationHandler> where TItem : IDisposable { @@ -43,6 +44,9 @@ public partial class ObservableCollection : [ObservableProperty] private int selectedIndex = 0; + [ObservableProperty] + private TItem? selectedItem; + public ObservableCollection(IServiceProvider provider, IServiceFactory factory, IMediator mediator, @@ -279,6 +283,15 @@ public partial class ObservableCollection : if (args.Value is TItem item) { Insert(args.Index, item); + + if (item is ISelectable selectable) + { + if (selectable.Selected) + { + SelectedItem = item; + SelectedIndex = this.IndexOf(item); + } + } } return Task.CompletedTask; @@ -487,6 +500,11 @@ public partial class ObservableCollection : added.Selected = true; } } + + public Task Handle(SelectionEventArgs args) + { + return Task.CompletedTask; + } } public partial class ObservableCollection(IServiceProvider provider, diff --git a/Toolkit.Foundation/Selected.cs b/Toolkit.Foundation/Selected.cs deleted file mode 100644 index 9b4182f..0000000 --- a/Toolkit.Foundation/Selected.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Toolkit.Foundation; - -public record Selected -{ - public static SelectedEventArgs As(TValue value) => - new(value); - - public static SelectedEventArgs As() where TValue : new() => - new(new TValue()); -} \ No newline at end of file diff --git a/Toolkit.Foundation/SelectedEventArgs.cs b/Toolkit.Foundation/SelectedEventArgs.cs deleted file mode 100644 index 8340338..0000000 --- a/Toolkit.Foundation/SelectedEventArgs.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Toolkit.Foundation; - -public record SelectedEventArgs(TValue? Value); diff --git a/Toolkit.Foundation/Selection.cs b/Toolkit.Foundation/Selection.cs new file mode 100644 index 0000000..d0936f0 --- /dev/null +++ b/Toolkit.Foundation/Selection.cs @@ -0,0 +1,10 @@ +namespace Toolkit.Foundation; + +public record Selection +{ + public static SelectionEventArgs As(TValue value) => + new(value); + + public static SelectionEventArgs As() where TValue : new() => + new(new TValue()); +} \ No newline at end of file diff --git a/Toolkit.Foundation/SelectionEventArgs.cs b/Toolkit.Foundation/SelectionEventArgs.cs new file mode 100644 index 0000000..9fb09f1 --- /dev/null +++ b/Toolkit.Foundation/SelectionEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Foundation; + +public record SelectionEventArgs(TValue? Value);