Fix more edge cases
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class AggerateAttribute(Type type, object key,
|
||||
AggerateMode mode = AggerateMode.Reset) : NotificationAttribute(type, key)
|
||||
{
|
||||
public AggerateMode Mode => mode;
|
||||
}
|
||||
public class AggerateAttribute(Type type, object key) : NotificationAttribute(type, key);
|
||||
@@ -1,31 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Aggerate
|
||||
{
|
||||
public static AggerateEventArgs<TValue, TOptions> With<TValue, TOptions>(TOptions options) where TOptions : class
|
||||
{
|
||||
return new AggerateEventArgs<TValue, TOptions>(options);
|
||||
}
|
||||
|
||||
public static AggerateEventArgs<TValue> With<TValue>()
|
||||
{
|
||||
return new AggerateEventArgs<TValue>();
|
||||
}
|
||||
}
|
||||
|
||||
public record AggerateEventArgs<TValue, TOptions>(TOptions? Options = null) :
|
||||
IAggerate
|
||||
where TOptions : class
|
||||
{
|
||||
public object? Key { get; init; }
|
||||
|
||||
public AggerateMode Mode { get; init; }
|
||||
}
|
||||
|
||||
public record AggerateEventArgs<TValue> :
|
||||
IAggerate
|
||||
{
|
||||
public object? Key { get; init; }
|
||||
|
||||
public AggerateMode Mode { get; init; }
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public enum AggerateMode
|
||||
{
|
||||
Append,
|
||||
Reset
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Aggregate
|
||||
{
|
||||
public static AggregateEventArgs<TValue, TOptions> As<TValue, TOptions>(TOptions options)
|
||||
where TOptions : class => new(options);
|
||||
|
||||
public static AggerateEventArgs<TValue> As<TValue>() =>
|
||||
new AggerateEventArgs<TValue>();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record AggregateEventArgs<TValue, TOptions>(TOptions? Options = null) :
|
||||
IAggregate
|
||||
where TOptions : class;
|
||||
|
||||
public record AggerateEventArgs<TValue> :
|
||||
IAggregate;
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record AggregateExpression(IAggregate Value, object? Key = null);
|
||||
@@ -6,6 +6,7 @@ public class HandlerProvider(SubscriptionCollection subscriptions) :
|
||||
public IEnumerable<object?> Get(Type type,
|
||||
object? key = null)
|
||||
{
|
||||
var d = subscriptions;
|
||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}";
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
||||
{
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IAggerate
|
||||
{
|
||||
object? Key { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IAggregate;
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
public interface IPublisher
|
||||
{
|
||||
void Publish<TMessage>(object key)
|
||||
void Publish<TMessage>(object? key = null)
|
||||
where TMessage : new();
|
||||
|
||||
void Publish<TMessage>(TMessage message)
|
||||
where TMessage : notnull;
|
||||
|
||||
void Publish<TMessage>(TMessage message,
|
||||
object key)
|
||||
object? key = null)
|
||||
where TMessage : notnull;
|
||||
|
||||
void Publish(object message,
|
||||
@@ -22,9 +22,9 @@ public interface IPublisher
|
||||
void Publish(object message);
|
||||
|
||||
void PublishUI<TMessage>(TMessage message,
|
||||
object key) where TMessage : notnull;
|
||||
object? key = null) where TMessage : notnull;
|
||||
|
||||
void PublishUI<TMessage>(object key)
|
||||
void PublishUI<TMessage>(object? key = null)
|
||||
where TMessage : new();
|
||||
|
||||
void PublishUI<TMessage>(TMessage message)
|
||||
|
||||
@@ -192,20 +192,20 @@ public partial class ObservableCollection<TItem> :
|
||||
}
|
||||
}
|
||||
|
||||
public void BeginAggregation()
|
||||
public void Fetch(bool reset = false)
|
||||
{
|
||||
if (this.GetAttribute<AggerateAttribute>() is AggerateAttribute attribute)
|
||||
{
|
||||
if (attribute.Mode == AggerateMode.Reset)
|
||||
if (reset)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
object? key = this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key;
|
||||
Publisher.PublishUI(OnAggerate(key));
|
||||
}
|
||||
AggregateExpression expression = CreateAggregateExpression();
|
||||
Publisher.PublishUI(expression.Value, expression.Key);
|
||||
}
|
||||
|
||||
protected virtual IAggregate OnAggerate() =>
|
||||
new AggerateEventArgs<TItem>();
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
clearing = true;
|
||||
@@ -389,7 +389,7 @@ public partial class ObservableCollection<TItem> :
|
||||
}
|
||||
|
||||
Initialized = true;
|
||||
BeginAggregation();
|
||||
Fetch();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
@@ -565,8 +565,11 @@ public partial class ObservableCollection<TItem> :
|
||||
collection.Insert(index > Count ? Count : index, item);
|
||||
}
|
||||
|
||||
protected virtual IAggerate OnAggerate(object? key) =>
|
||||
new AggerateEventArgs<TItem>() with { Key = key };
|
||||
protected virtual AggregateExpression CreateAggregateExpression() =>
|
||||
new AggregateExpression(new AggerateEventArgs<TItem>());
|
||||
|
||||
protected virtual object? CreateAggregationKey() =>
|
||||
default;
|
||||
|
||||
protected virtual void RemoveItem(int index) =>
|
||||
collection.RemoveAt(index);
|
||||
|
||||
@@ -9,7 +9,7 @@ public class Publisher(IHandlerProvider handlerProvider,
|
||||
IDispatcher dispatcher) :
|
||||
IPublisher
|
||||
{
|
||||
public void Publish<TMessage>(object key)
|
||||
public void Publish<TMessage>(object? key = null)
|
||||
where TMessage : new() =>
|
||||
Publish(serviceFactory.Create<TMessage>() ?? new TMessage(), async args => await args(), key);
|
||||
|
||||
@@ -17,7 +17,8 @@ public class Publisher(IHandlerProvider handlerProvider,
|
||||
where TMessage : notnull =>
|
||||
Publish(message, async args => await args(), null);
|
||||
|
||||
public void Publish<TMessage>(TMessage message, object key)
|
||||
public void Publish<TMessage>(TMessage message,
|
||||
object? key = null)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, async args => await args(), key);
|
||||
|
||||
@@ -60,7 +61,7 @@ public class Publisher(IHandlerProvider handlerProvider,
|
||||
where TMessage : new() =>
|
||||
Publish(new TMessage(), async args => await args(), null);
|
||||
|
||||
public void PublishUI<TMessage>(object key)
|
||||
public void PublishUI<TMessage>(object? key = null)
|
||||
where TMessage : new() =>
|
||||
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), key);
|
||||
|
||||
@@ -69,7 +70,7 @@ public class Publisher(IHandlerProvider handlerProvider,
|
||||
Publish(message, args => dispatcher.Invoke(async () => await args()), null);
|
||||
|
||||
public void PublishUI<TMessage>(TMessage message,
|
||||
object key)
|
||||
object? key = null)
|
||||
where TMessage : notnull =>
|
||||
Publish(message, args => dispatcher.Invoke(async () => await args()), key);
|
||||
|
||||
|
||||
@@ -10,13 +10,16 @@ public class Subscription(SubscriptionCollection subscriptions,
|
||||
{
|
||||
Type handlerType = subscriber.GetType();
|
||||
|
||||
IDictionary<Type, object> keys = GetKeysFromHandler(subscriber);
|
||||
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
|
||||
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
|
||||
{
|
||||
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
||||
{
|
||||
keys.TryGetValue(argumentType, out object? key);
|
||||
|
||||
subscribers.TryGetValue(argumentType, out List<object>? keys);
|
||||
if (keys is not null)
|
||||
{
|
||||
foreach (object key in keys)
|
||||
{
|
||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
|
||||
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
||||
{
|
||||
@@ -27,26 +30,58 @@ public class Subscription(SubscriptionCollection subscriptions,
|
||||
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string subscriptionKey = $"{argumentType}";
|
||||
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
||||
{
|
||||
collection.Add(new WeakReference(subscriber));
|
||||
return collection;
|
||||
});
|
||||
|
||||
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove(object subscriber)
|
||||
{
|
||||
Type handlerType = subscriber.GetType();
|
||||
IDictionary<Type, object> keys = GetKeysFromHandler(subscriber);
|
||||
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
|
||||
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
|
||||
{
|
||||
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
||||
{
|
||||
keys.TryGetValue(argumentType, out object? key);
|
||||
|
||||
subscribers.TryGetValue(argumentType, out List<object>? keys);
|
||||
if (keys is not null)
|
||||
{
|
||||
foreach (object key in keys)
|
||||
{
|
||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing))
|
||||
{
|
||||
for (int i = subscribers.Count - 1; i >= 0; i--)
|
||||
for (int i = existing.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (!subscribers[i].IsAlive || subscribers[i].Target == subscriber)
|
||||
if (!existing[i].IsAlive || existing[i].Target == subscriber)
|
||||
{
|
||||
subscribers.RemoveAt(i);
|
||||
existing.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string subscriptionKey = $"{argumentType}";
|
||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing))
|
||||
{
|
||||
for (int i = existing.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (!existing[i].IsAlive || existing[i].Target == subscriber)
|
||||
{
|
||||
existing.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,23 +109,23 @@ public class Subscription(SubscriptionCollection subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
//private object? GetKeyFromHandler(object handler) =>
|
||||
// handler.GetAttribute<NotificationAttribute>() is NotificationAttribute attribute
|
||||
// ? handler.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key : null;
|
||||
|
||||
private IDictionary<Type, object> GetKeysFromHandler(object handler)
|
||||
private IDictionary<Type, List<object>> GetSubscriptionKeys(object subscriber)
|
||||
{
|
||||
Dictionary<Type, object> keys = [];
|
||||
|
||||
foreach (NotificationAttribute attribute in handler.GetAttributes<NotificationAttribute>())
|
||||
Dictionary<Type, List<object>> keys = [];
|
||||
foreach (NotificationAttribute attribute in subscriber.GetAttributes<NotificationAttribute>())
|
||||
{
|
||||
keys.Add(attribute.Type, attribute.Key);
|
||||
if (!keys.TryGetValue(attribute.Type, out List<object>? value))
|
||||
{
|
||||
value = ([]);
|
||||
keys[attribute.Type] = value;
|
||||
}
|
||||
|
||||
value.Add(attribute.Key);
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
||||
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
|
||||
handlerType.GetInterfaces().Where(interfaceType =>
|
||||
{
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class AttachedBehaviour : Trigger
|
||||
public class AttachedBehaviour :
|
||||
Trigger
|
||||
{
|
||||
protected override void OnAttachedToVisualTree()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.LogicalTree;
|
||||
using Toolkit.UI.Controls.Avalonia;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class ListBoxExtension
|
||||
{
|
||||
public static readonly AttachedProperty<bool> IsItemInvokedEnabledProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigationViewItem, bool>("IsItemInvokedEnabled",
|
||||
typeof(ListBoxExtension), false);
|
||||
|
||||
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemInvokedEvent =
|
||||
RoutedEvent.Register<ItemInvokedEventArgs>("ItemInvoked",
|
||||
RoutingStrategies.Bubble, typeof(ListBoxExtension));
|
||||
|
||||
static ListBoxExtension()
|
||||
{
|
||||
IsItemInvokedEnabledProperty.Changed.AddClassHandler<ListBoxItem>(OnIsItemClickEnabledPropertyChanged);
|
||||
}
|
||||
|
||||
private static void OnIsItemClickEnabledPropertyChanged(ListBoxItem sender,
|
||||
AvaloniaPropertyChangedEventArgs args)
|
||||
{
|
||||
bool TrySetupListBox()
|
||||
{
|
||||
if (sender.GetLogicalAncestors().OfType<ListBox>().FirstOrDefault() is ListBox listBox)
|
||||
{
|
||||
void OnItemInvoked(object? _, SelectionChangedEventArgs args)
|
||||
{
|
||||
if (args.AddedItems is { Count: > 0 })
|
||||
{
|
||||
if (sender.DataContext == listBox.SelectedItem)
|
||||
{
|
||||
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemInvokedEvent });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sender.DataContext == listBox.SelectedItem)
|
||||
{
|
||||
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemInvokedEvent });
|
||||
}
|
||||
|
||||
listBox.SelectionChanged += OnItemInvoked;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TrySetupListBox())
|
||||
{
|
||||
void OnAttachedToVisualTree(object? _, VisualTreeAttachmentEventArgs __)
|
||||
{
|
||||
sender.AttachedToVisualTree -= OnAttachedToVisualTree;
|
||||
TrySetupListBox();
|
||||
}
|
||||
|
||||
sender.AttachedToVisualTree += OnAttachedToVisualTree;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool GetIsItemInvokedEnabled(ListBoxItem element) =>
|
||||
element.GetValue(IsItemInvokedEnabledProperty);
|
||||
|
||||
public static void SetIsItemInvokedEnabled(ListBoxItem element, bool value) =>
|
||||
element.SetValue(IsItemInvokedEnabledProperty, value);
|
||||
|
||||
public static void AddItemInvokedHandler(ListBoxItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.AddHandler(ItemInvokedEvent, handler);
|
||||
|
||||
public static void RemoveItemInvokedHandler(ListBoxItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.RemoveHandler(ItemInvokedEvent, handler);
|
||||
}
|
||||
@@ -5,22 +5,22 @@ using Toolkit.UI.Controls.Avalonia;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class NavigationViewItemExtension
|
||||
public class NavigationViewExtension
|
||||
{
|
||||
public static readonly AttachedProperty<bool> IsItemClickEnabledProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigationViewItem, bool>("IsItemClickEnabled",
|
||||
typeof(NavigationViewItemExtension), false);
|
||||
public static readonly AttachedProperty<bool> IsItemInvokedEnabledProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigationViewItem, bool>("IsItemInvokedEnabled",
|
||||
typeof(NavigationViewExtension), false);
|
||||
|
||||
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemClickEvent =
|
||||
RoutedEvent.Register<ItemInvokedEventArgs>("ItemClick",
|
||||
RoutingStrategies.Bubble, typeof(NavigationViewItemExtension));
|
||||
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemInvokedEvent =
|
||||
RoutedEvent.Register<ItemInvokedEventArgs>("ItemInvoked",
|
||||
RoutingStrategies.Bubble, typeof(NavigationViewExtension));
|
||||
|
||||
static NavigationViewItemExtension()
|
||||
static NavigationViewExtension()
|
||||
{
|
||||
IsItemClickEnabledProperty.Changed.AddClassHandler<NavigationViewItem>(OnIsItemClickEnabledPropertyChanged);
|
||||
IsItemInvokedEnabledProperty.Changed.AddClassHandler<NavigationViewItem>(OnIsItemInvokedEnabledPropertyChanged);
|
||||
}
|
||||
|
||||
private static void OnIsItemClickEnabledPropertyChanged(NavigationViewItem sender,
|
||||
private static void OnIsItemInvokedEnabledPropertyChanged(NavigationViewItem sender,
|
||||
AvaloniaPropertyChangedEventArgs args)
|
||||
{
|
||||
bool TrySetupNavigationView()
|
||||
@@ -31,7 +31,7 @@ public class NavigationViewItemExtension
|
||||
{
|
||||
if (args.InvokedItemContainer == sender)
|
||||
{
|
||||
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemClickEvent });
|
||||
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemInvokedEvent });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,15 +54,15 @@ public class NavigationViewItemExtension
|
||||
}
|
||||
}
|
||||
|
||||
public static bool GetIsItemClickEnabled(NavigationViewItem element) =>
|
||||
element.GetValue(IsItemClickEnabledProperty);
|
||||
public static bool GetIsItemInvokedEnabled(NavigationViewItem element) =>
|
||||
element.GetValue(IsItemInvokedEnabledProperty);
|
||||
|
||||
public static void SetIsItemClickEnabled(NavigationViewItem element, bool value) =>
|
||||
element.SetValue(IsItemClickEnabledProperty, value);
|
||||
public static void SetIsItemInvokedEnabled(NavigationViewItem element, bool value) =>
|
||||
element.SetValue(IsItemInvokedEnabledProperty, value);
|
||||
|
||||
public static void AddItemClickHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.AddHandler(ItemClickEvent, handler);
|
||||
public static void AddItemInvokedHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.AddHandler(ItemInvokedEvent, handler);
|
||||
|
||||
public static void RemoveItemClickHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.RemoveHandler(ItemClickEvent, handler);
|
||||
public static void RemoveItemInvokedHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.RemoveHandler(ItemInvokedEvent, handler);
|
||||
}
|
||||
Reference in New Issue
Block a user