Fix some edge cases
This commit is contained in:
@@ -50,10 +50,10 @@ public class ContentTemplate :
|
||||
await deactivated.OnDeactivated();
|
||||
}
|
||||
|
||||
if (content is IDisposable disposable)
|
||||
{
|
||||
disposable.Dispose();
|
||||
}
|
||||
//if (content is IDisposable disposable)
|
||||
//{
|
||||
// disposable.Dispose();
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ public class ComponentBuilder :
|
||||
|
||||
services.AddScoped<SubscriptionCollection>();
|
||||
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
|
||||
services.AddScoped<IDisposer, Disposer>();
|
||||
services.AddSingleton<IDisposer, Disposer>();
|
||||
|
||||
services.AddTransient<ISubscriber, Subscriber>();
|
||||
services.AddTransient<IPublisher, Publisher>();
|
||||
|
||||
@@ -23,9 +23,11 @@ public class DefaultHostBuilder :
|
||||
services.AddSingleton<IComponentHostCollection,
|
||||
ComponentHostCollection>();
|
||||
|
||||
services.AddSingleton<IDisposer, Disposer>();
|
||||
|
||||
services.AddScoped<SubscriptionCollection>();
|
||||
|
||||
services.AddTransient<ISubscriptionManager, SubscriptionManager>();
|
||||
|
||||
services.AddTransient<ISubscriber, Subscriber>();
|
||||
|
||||
services.AddTransient<IPublisher, Publisher>();
|
||||
@@ -40,8 +42,6 @@ public class DefaultHostBuilder :
|
||||
services.AddScoped<IProxyService<IComponentHostCollection>>(provider =>
|
||||
new ProxyService<IComponentHostCollection>(provider.GetRequiredService<IComponentHostCollection>()));
|
||||
|
||||
services.AddScoped<IDisposer, Disposer>();
|
||||
|
||||
services.AddTransient<IContentTemplateDescriptorProvider, ContentTemplateDescriptorProvider>();
|
||||
|
||||
services.AddTransient<INavigationProvider, NavigationProvider>();
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Delete
|
||||
{
|
||||
public static DeleteEventArgs<TValue> As<TValue>(TValue value) =>
|
||||
new(value);
|
||||
|
||||
public static DeleteEventArgs<TValue> As<TValue>() where TValue : new() =>
|
||||
new(new TValue());
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record DeleteEventArgs<TValue>(TValue Value);
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record Edit
|
||||
{
|
||||
public static EditEventArgs<TValue> As<TValue>(TValue value) =>
|
||||
new(value);
|
||||
|
||||
public static EditEventArgs<TValue> As<TValue>() where TValue : new() =>
|
||||
new(new TValue());
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record EditEventArgs<TValue>(TValue Value);
|
||||
@@ -9,4 +9,6 @@ public interface IConfigurationSource<TConfiguration>
|
||||
void Set(TConfiguration value);
|
||||
|
||||
void Set(object value);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IRemovable : IDisposable;
|
||||
@@ -29,11 +29,18 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
{
|
||||
private readonly ObservableCollection<TViewModel> collection = [];
|
||||
|
||||
private bool clearing;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isInitialized;
|
||||
|
||||
[ObservableProperty]
|
||||
private int selectedIndex = 0;
|
||||
|
||||
private bool selfDisposing;
|
||||
|
||||
public ObservableCollectionViewModel(IServiceProvider provider,
|
||||
IServiceFactory factory,
|
||||
IServiceFactory factory,
|
||||
IMediator mediator,
|
||||
IPublisher publisher,
|
||||
ISubscriber subscriber,
|
||||
@@ -121,9 +128,6 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Task OnActivated() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public TViewModel Add()
|
||||
{
|
||||
TViewModel? item = Factory.Create<TViewModel>();
|
||||
@@ -196,12 +200,16 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (TViewModel item in collection)
|
||||
clearing = true;
|
||||
|
||||
foreach (TViewModel item in this.ToList())
|
||||
{
|
||||
Disposer.Dispose(item);
|
||||
}
|
||||
|
||||
ClearItems();
|
||||
|
||||
clearing = false;
|
||||
}
|
||||
|
||||
public bool Contains(TViewModel item) =>
|
||||
@@ -222,18 +230,28 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual Task OnDeactivated() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual Task OnDeactivating() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
selfDisposing = true;
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
Disposer.Dispose(this);
|
||||
}
|
||||
|
||||
public async Task Enumerate()
|
||||
{
|
||||
if (this.GetAttribute<EnumerateAttribute>() is EnumerateAttribute attribute)
|
||||
{
|
||||
if (attribute.Mode == EnumerateMode.Reset)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
object? key = this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key;
|
||||
await Publisher.PublishUI(PrepareEnumeration(key));
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<TViewModel> GetEnumerator() =>
|
||||
collection.GetEnumerator();
|
||||
|
||||
@@ -316,24 +334,6 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
await Enumerate();
|
||||
}
|
||||
|
||||
|
||||
public async Task Enumerate()
|
||||
{
|
||||
if (this.GetAttribute<EnumerateAttribute>() is EnumerateAttribute attribute)
|
||||
{
|
||||
if (attribute.Mode == EnumerateMode.Reset)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
object? key = this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key;
|
||||
await Publisher.PublishUI(PrepareEnumeration(key));
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual IEnumerate PrepareEnumeration(object? key) =>
|
||||
new EnumerateEventArgs<TViewModel>() with { Key = key };
|
||||
|
||||
public void Insert(int index, TViewModel item) =>
|
||||
InsertItem(index, item);
|
||||
|
||||
@@ -360,6 +360,14 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual Task OnActivated() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual Task OnDeactivated() =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public virtual Task OnDeactivating() =>
|
||||
Task.CompletedTask;
|
||||
public bool Remove(TViewModel item)
|
||||
{
|
||||
int index = collection.IndexOf(item);
|
||||
@@ -402,23 +410,30 @@ public partial class ObservableCollectionViewModel<TViewModel> :
|
||||
}
|
||||
|
||||
protected virtual void ClearItems() =>
|
||||
collection.Clear();
|
||||
collection.Clear();
|
||||
|
||||
protected virtual void InsertItem(int index,
|
||||
TViewModel item)
|
||||
{
|
||||
Disposer.Add(this, item);
|
||||
Disposer.Add(item, item, Disposable.Create(() =>
|
||||
Disposer.Add(item, Disposable.Create(() =>
|
||||
{
|
||||
if (item is IList collection)
|
||||
{
|
||||
collection.Clear();
|
||||
}
|
||||
|
||||
if (item is IRemovable && !clearing)
|
||||
{
|
||||
Remove(item);
|
||||
}
|
||||
}));
|
||||
|
||||
collection.Insert(index, item);
|
||||
}
|
||||
|
||||
protected virtual IEnumerate PrepareEnumeration(object? key) =>
|
||||
new EnumerateEventArgs<TViewModel>() with { Key = key };
|
||||
protected virtual void RemoveItem(int index) =>
|
||||
collection.RemoveAt(index);
|
||||
|
||||
|
||||
@@ -9,7 +9,8 @@ public partial class ObservableViewModel :
|
||||
IActivated,
|
||||
IDeactivating,
|
||||
IDeactivated,
|
||||
IDeactivatable
|
||||
IDeactivatable,
|
||||
IDisposable
|
||||
{
|
||||
[ObservableProperty]
|
||||
private bool isInitialized;
|
||||
|
||||
Reference in New Issue
Block a user