diff --git a/Hyperbar.Windows.Primary/PrimaryWidgetConfigurationHandler.cs b/Hyperbar.Windows.Primary/PrimaryWidgetConfigurationHandler.cs index d2584b2..7d534b7 100644 --- a/Hyperbar.Windows.Primary/PrimaryWidgetConfigurationHandler.cs +++ b/Hyperbar.Windows.Primary/PrimaryWidgetConfigurationHandler.cs @@ -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(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)) { diff --git a/Hyperbar/Views/Inserted.cs b/Hyperbar/Views/Inserted.cs index eebb699..f084dfa 100644 --- a/Hyperbar/Views/Inserted.cs +++ b/Hyperbar/Views/Inserted.cs @@ -1,3 +1,3 @@ namespace Hyperbar; -public record Inserted(int Index, TValue Value) : INotification; \ No newline at end of file +public record Inserted(int Index, TValue Value) : INotification; diff --git a/Hyperbar/Views/Moved.cs b/Hyperbar/Views/Moved.cs new file mode 100644 index 0000000..6909d35 --- /dev/null +++ b/Hyperbar/Views/Moved.cs @@ -0,0 +1,3 @@ +namespace Hyperbar; + +public record Moved(int Index, TValue Value) : INotification; \ No newline at end of file diff --git a/Hyperbar/Views/ObservableCollectionViewModel.cs b/Hyperbar/Views/ObservableCollectionViewModel.cs index 9e5a483..5487f16 100644 --- a/Hyperbar/Views/ObservableCollectionViewModel.cs +++ b/Hyperbar/Views/ObservableCollectionViewModel.cs @@ -12,6 +12,7 @@ public partial class ObservableCollectionViewModel : INotificationHandler>, INotificationHandler>, INotificationHandler>, + INotificationHandler>, IDisposable where TItem : IDisposable @@ -248,8 +249,19 @@ public partial class ObservableCollectionViewModel : return Task.CompletedTask; } + public Task Handle(Moved notification, + CancellationToken cancellationToken) + { + if (notification.Value is TItem item) + { + Move(notification.Index, item); + } + + return Task.CompletedTask; + } + public int IndexOf(TItem item) => - collection.IndexOf(item); + collection.IndexOf(item); int IList.IndexOf(object? value) => IsCompatibleObject(value) ? @@ -267,10 +279,27 @@ public partial class ObservableCollectionViewModel : } } + 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);