Ensure item is updated when modified
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public record MoveTo
|
||||||
|
{
|
||||||
|
public static MoveToEventArgs<TValue> As<TValue>(int oldIndex, int newIndex) =>
|
||||||
|
new(oldIndex, newIndex);
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public record MoveToEventArgs<TValue>(int OldIndex, int NewIndex);
|
||||||
@@ -25,10 +25,10 @@ public partial class ObservableCollection<TItem> :
|
|||||||
IDisposerRequired,
|
IDisposerRequired,
|
||||||
INotificationHandler<RemoveEventArgs<TItem>>,
|
INotificationHandler<RemoveEventArgs<TItem>>,
|
||||||
INotificationHandler<RemoveAtEventArgs<TItem>>,
|
INotificationHandler<RemoveAtEventArgs<TItem>>,
|
||||||
INotificationHandler<RemoveAndInsertAtEventArgs<TItem>>,
|
|
||||||
INotificationHandler<CreateEventArgs<TItem>>,
|
INotificationHandler<CreateEventArgs<TItem>>,
|
||||||
INotificationHandler<InsertEventArgs<TItem>>,
|
INotificationHandler<InsertEventArgs<TItem>>,
|
||||||
INotificationHandler<MoveEventArgs<TItem>>,
|
INotificationHandler<MoveEventArgs<TItem>>,
|
||||||
|
INotificationHandler<MoveToEventArgs<TItem>>,
|
||||||
INotificationHandler<ReplaceEventArgs<TItem>>
|
INotificationHandler<ReplaceEventArgs<TItem>>
|
||||||
where TItem :
|
where TItem :
|
||||||
IDisposable
|
IDisposable
|
||||||
@@ -284,6 +284,12 @@ public partial class ObservableCollection<TItem> :
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task Handle(MoveToEventArgs<TItem> args)
|
||||||
|
{
|
||||||
|
Move(args.OldIndex, args.NewIndex);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
public Task Handle(MoveEventArgs<TItem> args)
|
public Task Handle(MoveEventArgs<TItem> args)
|
||||||
{
|
{
|
||||||
if (args.Value is TItem item)
|
if (args.Value is TItem item)
|
||||||
@@ -293,7 +299,6 @@ public partial class ObservableCollection<TItem> :
|
|||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task Handle(ReplaceEventArgs<TItem> args)
|
public Task Handle(ReplaceEventArgs<TItem> args)
|
||||||
{
|
{
|
||||||
if (args.Value is TItem item)
|
if (args.Value is TItem item)
|
||||||
@@ -314,17 +319,6 @@ public partial class ObservableCollection<TItem> :
|
|||||||
return Task.CompletedTask;
|
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) =>
|
public int IndexOf(TItem item) =>
|
||||||
collection.IndexOf(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)
|
public bool Move(int index, TItem item)
|
||||||
{
|
{
|
||||||
int oldIndex = collection.IndexOf(item);
|
int oldIndex = collection.IndexOf(item);
|
||||||
|
|||||||
@@ -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);
|
|
||||||
Reference in New Issue
Block a user