Ensure item is updated when modified

This commit is contained in:
TheXamlGuy
2024-05-21 21:57:26 +01:00
parent 83fef5e399
commit fda5e7db6c
5 changed files with 31 additions and 23 deletions
+7
View File
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public record MoveTo
{
public static MoveToEventArgs<TValue> As<TValue>(int oldIndex, int newIndex) =>
new(oldIndex, newIndex);
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record MoveToEventArgs<TValue>(int OldIndex, int NewIndex);
+21 -13
View File
@@ -25,10 +25,10 @@ public partial class ObservableCollection<TItem> :
IDisposerRequired,
INotificationHandler<RemoveEventArgs<TItem>>,
INotificationHandler<RemoveAtEventArgs<TItem>>,
INotificationHandler<RemoveAndInsertAtEventArgs<TItem>>,
INotificationHandler<CreateEventArgs<TItem>>,
INotificationHandler<InsertEventArgs<TItem>>,
INotificationHandler<MoveEventArgs<TItem>>,
INotificationHandler<MoveToEventArgs<TItem>>,
INotificationHandler<ReplaceEventArgs<TItem>>
where TItem :
IDisposable
@@ -284,6 +284,12 @@ public partial class ObservableCollection<TItem> :
return Task.CompletedTask;
}
public Task Handle(MoveToEventArgs<TItem> args)
{
Move(args.OldIndex, args.NewIndex);
return Task.CompletedTask;
}
public Task Handle(MoveEventArgs<TItem> args)
{
if (args.Value is TItem item)
@@ -293,7 +299,6 @@ public partial class ObservableCollection<TItem> :
return Task.CompletedTask;
}
public Task Handle(ReplaceEventArgs<TItem> args)
{
if (args.Value is TItem item)
@@ -314,17 +319,6 @@ public partial class ObservableCollection<TItem> :
return Task.CompletedTask;
}
public Task Handle(RemoveAndInsertAtEventArgs<TItem> 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<TItem> :
}
}
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);
-7
View File
@@ -1,7 +0,0 @@
namespace Toolkit.Foundation;
public record RemoveAndInsertAt
{
public static RemoveAndInsertAtEventArgs<TValue> As<TValue>(int oldIndex, int newIndex, TValue value) =>
new(oldIndex, newIndex, value);
}
@@ -1,3 +0,0 @@
namespace Toolkit.Foundation;
public record RemoveAndInsertAtEventArgs<TValue>(int OldIndex, int NewIndex, TValue Value);