diff --git a/Toolkit.Foundation/MoveTo.cs b/Toolkit.Foundation/MoveTo.cs new file mode 100644 index 0000000..7816e07 --- /dev/null +++ b/Toolkit.Foundation/MoveTo.cs @@ -0,0 +1,7 @@ +namespace Toolkit.Foundation; + +public record MoveTo +{ + public static MoveToEventArgs As(int oldIndex, int newIndex) => + new(oldIndex, newIndex); +} \ No newline at end of file diff --git a/Toolkit.Foundation/MoveToEventArgs.cs b/Toolkit.Foundation/MoveToEventArgs.cs new file mode 100644 index 0000000..7073b17 --- /dev/null +++ b/Toolkit.Foundation/MoveToEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Foundation; + +public record MoveToEventArgs(int OldIndex, int NewIndex); diff --git a/Toolkit.Foundation/ObservableCollection.cs b/Toolkit.Foundation/ObservableCollection.cs index 5ec43a8..7448802 100644 --- a/Toolkit.Foundation/ObservableCollection.cs +++ b/Toolkit.Foundation/ObservableCollection.cs @@ -25,10 +25,10 @@ public partial class ObservableCollection : IDisposerRequired, INotificationHandler>, INotificationHandler>, - INotificationHandler>, INotificationHandler>, INotificationHandler>, INotificationHandler>, + INotificationHandler>, INotificationHandler> where TItem : IDisposable @@ -284,6 +284,12 @@ public partial class ObservableCollection : return Task.CompletedTask; } + public Task Handle(MoveToEventArgs args) + { + Move(args.OldIndex, args.NewIndex); + return Task.CompletedTask; + } + public Task Handle(MoveEventArgs args) { if (args.Value is TItem item) @@ -293,7 +299,6 @@ public partial class ObservableCollection : return Task.CompletedTask; } - public Task Handle(ReplaceEventArgs args) { if (args.Value is TItem item) @@ -314,17 +319,6 @@ public partial class ObservableCollection : return Task.CompletedTask; } - public Task Handle(RemoveAndInsertAtEventArgs args) - { - if (args.OldIndex >= 0 && args.OldIndex <= Count - 1 && args.Value is TItem item) - { - RemoveAt(args.OldIndex); - Insert(args.NewIndex, item); - } - - return Task.CompletedTask; - } - public int IndexOf(TItem item) => collection.IndexOf(item); @@ -357,6 +351,20 @@ public partial class ObservableCollection : } } + public bool Move(int oldIndex, int newIndex) + { + if (oldIndex < 0) + { + return false; + } + + TItem item = this[oldIndex]; + + RemoveItem(oldIndex); + Insert(newIndex, item); + + return true; + } public bool Move(int index, TItem item) { int oldIndex = collection.IndexOf(item); diff --git a/Toolkit.Foundation/RemoveAndInsertAt.cs b/Toolkit.Foundation/RemoveAndInsertAt.cs deleted file mode 100644 index 0b44d06..0000000 --- a/Toolkit.Foundation/RemoveAndInsertAt.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Toolkit.Foundation; - -public record RemoveAndInsertAt -{ - public static RemoveAndInsertAtEventArgs As(int oldIndex, int newIndex, TValue value) => - new(oldIndex, newIndex, value); -} \ No newline at end of file diff --git a/Toolkit.Foundation/RemoveAndInsertAtEventArgs.cs b/Toolkit.Foundation/RemoveAndInsertAtEventArgs.cs deleted file mode 100644 index 362bb1c..0000000 --- a/Toolkit.Foundation/RemoveAndInsertAtEventArgs.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace Toolkit.Foundation; - -public record RemoveAndInsertAtEventArgs(int OldIndex, int NewIndex, TValue Value);