Added ability to move items by order

This commit is contained in:
TheXamlGuy
2024-01-18 23:07:08 +00:00
parent 19e83d5043
commit a0042a5299
4 changed files with 58 additions and 3 deletions
@@ -28,6 +28,29 @@ public class PrimaryWidgetConfigurationHandler(IMediator mediator,
}
}
foreach (KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> moved in
items.ExceptBy(cache.Select(x => new { x.Value.Order, x.Value.Id }), x => new { x.Value.Order, x.Value.Id }))
{
if (moved.Value is PrimaryCommandConfiguration configuration)
{
if (provider.Get(configuration) is IWidgetComponentViewModel viewModel)
{
await mediator.PublishAsync(new Moved<IWidgetComponentViewModel>(configuration.Order, viewModel),
moved.Key.ParentId == Guid.Empty ? nameof(PrimaryWidgetViewModel) : moved.Key.ParentId,
cancellationToken);
cache.Remove(moved.Key);
cache.Add(moved.Key, moved.Value);
}
}
// // cache.Add(added.Key, added.Value);
//}
}
foreach (KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> added in
items.ExceptBy(cache.Select(x => x.Key.Id), x => x.Key.Id))
{
+3
View File
@@ -0,0 +1,3 @@
namespace Hyperbar;
public record Moved<TValue>(int Index, TValue Value) : INotification;
@@ -12,6 +12,7 @@ public partial class ObservableCollectionViewModel<TItem> :
INotificationHandler<Removed<TItem>>,
INotificationHandler<Created<TItem>>,
INotificationHandler<Inserted<TItem>>,
INotificationHandler<Moved<TItem>>,
IDisposable
where TItem :
IDisposable
@@ -248,6 +249,17 @@ public partial class ObservableCollectionViewModel<TItem> :
return Task.CompletedTask;
}
public Task Handle(Moved<TItem> notification,
CancellationToken cancellationToken)
{
if (notification.Value is TItem item)
{
Move(notification.Index, item);
}
return Task.CompletedTask;
}
public int IndexOf(TItem item) =>
collection.IndexOf(item);
@@ -267,10 +279,27 @@ public partial class ObservableCollectionViewModel<TItem> :
}
}
public bool Move(int index, TItem item)
{
int oldIndex = collection.IndexOf(item);
if (oldIndex < 0)
{
return false;
}
RemoveItem(oldIndex);
Insert(index, item);
return true;
}
public bool Remove(TItem item)
{
int index = collection.IndexOf(item);
if (index < 0) return false;
if (index < 0)
{
return false;
}
Disposer.Remove(this, item);
RemoveItem(index);