Add ability to insert an ordered item at UI configuration time

This commit is contained in:
TheXamlGuy
2024-01-17 20:16:55 +00:00
parent 1f02f6970e
commit 92ee4a678b
5 changed files with 42 additions and 12 deletions
+14 -3
View File
@@ -1,5 +1,16 @@
namespace Hyperbar;
public record Created<TValue>(TValue Value) : INotification;
public record Inserted<TValue>(int Index, TValue Value) : INotification;
public record Created<TValue>(TValue Value, object Target) : INotification
{
public static Created<TValue> For<TTarget>(TValue value)
{
return new Created<TValue>(value, typeof(TTarget).Name);
}
}
public record Inserted<TValue>(int Index, TValue Value, object Target) : INotification
{
public static Inserted<TValue> For<TTarget>(int index, TValue value)
{
return new Inserted<TValue>(index, value, typeof(TTarget).Name);
}
}
@@ -11,6 +11,7 @@ public partial class ObservableCollectionViewModel<TItem> :
IObservableCollectionViewModel<TItem>,
INotificationHandler<Removed<TItem>>,
INotificationHandler<Created<TItem>>,
INotificationHandler<Inserted<TItem>>,
IDisposable
where TItem :
IDisposable
@@ -228,16 +229,33 @@ public partial class ObservableCollectionViewModel<TItem> :
public ValueTask Handle(Created<TItem> notification,
CancellationToken cancellationToken)
{
if (notification.Value is not null)
if (notification.Target.Equals(GetType().Name))
{
Add(notification.Value);
if (notification.Value is TItem item)
{
Add(item);
}
}
return ValueTask.CompletedTask;
}
public int IndexOf(TItem item) =>
collection.IndexOf(item);
public ValueTask Handle(Inserted<TItem> notification,
CancellationToken cancellationToken)
{
if (notification.Target.Equals(GetType().Name))
{
if (notification.Value is TItem item)
{
Insert(notification.Index, item);
}
}
return ValueTask.CompletedTask;
}
public int IndexOf(TItem item) =>
collection.IndexOf(item);
int IList.IndexOf(object? value) =>
IsCompatibleObject(value) ?