Ionite
1 year ago
9 changed files with 205 additions and 43 deletions
@ -0,0 +1,71 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Collections.Specialized; |
||||
using Avalonia.Collections; |
||||
using Avalonia.Threading; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models; |
||||
|
||||
/// <summary> |
||||
/// Observable AvaloniaList supporting child item deletion requests. |
||||
/// </summary> |
||||
public class AdvancedObservableList<T> : AvaloniaList<T> |
||||
{ |
||||
/// <inheritdoc /> |
||||
public AdvancedObservableList() |
||||
{ |
||||
CollectionChanged += CollectionChangedEventRegistrationHandler; |
||||
} |
||||
|
||||
/// <inheritdoc /> |
||||
public AdvancedObservableList(IEnumerable<T> items) : base(items) |
||||
{ |
||||
CollectionChanged += CollectionChangedEventRegistrationHandler; |
||||
} |
||||
|
||||
private void CollectionChangedEventRegistrationHandler(object? sender, NotifyCollectionChangedEventArgs e) |
||||
{ |
||||
if (e.OldItems != null) |
||||
{ |
||||
foreach (var item in e.OldItems) |
||||
{ |
||||
TryUnregisterRemovableListItem((T)item); |
||||
} |
||||
} |
||||
if (e.NewItems != null) |
||||
{ |
||||
foreach (var item in e.NewItems) |
||||
{ |
||||
TryRegisterRemovableListItem((T)item); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void OnItemRemoveRequested(object? sender, EventArgs e) |
||||
{ |
||||
if (sender is T item) |
||||
{ |
||||
Dispatcher.UIThread.Post(() => Remove(item)); |
||||
} |
||||
} |
||||
|
||||
private bool TryRegisterRemovableListItem(T item) |
||||
{ |
||||
if (item is IRemovableListItem removableListItem) |
||||
{ |
||||
removableListItem.ParentListRemoveRequested += OnItemRemoveRequested; |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
private bool TryUnregisterRemovableListItem(T item) |
||||
{ |
||||
if (item is IRemovableListItem removableListItem) |
||||
{ |
||||
removableListItem.ParentListRemoveRequested -= OnItemRemoveRequested; |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
} |
@ -0,0 +1,8 @@
|
||||
using System; |
||||
|
||||
namespace StabilityMatrix.Avalonia.Models; |
||||
|
||||
public interface IRemovableListItem |
||||
{ |
||||
public event EventHandler ParentListRemoveRequested; |
||||
} |
Loading…
Reference in new issue