diff --git a/Toolkit.Foundation/CommandViewModel.cs b/Toolkit.Foundation/CommandViewModel.cs index 7c1e9ee..6022cbf 100644 --- a/Toolkit.Foundation/CommandViewModel.cs +++ b/Toolkit.Foundation/CommandViewModel.cs @@ -8,7 +8,7 @@ public partial class CommandViewModel(IServiceProvider provider, IPublisher publisher, ISubscription subscriber, IDisposer disposer) : - ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer) + Observable(provider, factory, mediator, publisher, subscriber, disposer) { public IRelayCommand InvokeCommand => new AsyncRelayCommand(InvokeAsync); diff --git a/Toolkit.Foundation/EnumerateEventArgs.cs b/Toolkit.Foundation/EnumerateEventArgs.cs index 1895294..a4c519c 100644 --- a/Toolkit.Foundation/EnumerateEventArgs.cs +++ b/Toolkit.Foundation/EnumerateEventArgs.cs @@ -5,8 +5,6 @@ public record EnumerateEventArgs : { public object? Key { get; init; } - public EnumerateMode Mode { get; init; } - public static Enumerate With(TOptions options) where TOptions : class { return new Enumerate(options); diff --git a/Toolkit.Foundation/IEnumerate.cs b/Toolkit.Foundation/IEnumerate.cs index 88782ca..8be8bac 100644 --- a/Toolkit.Foundation/IEnumerate.cs +++ b/Toolkit.Foundation/IEnumerate.cs @@ -3,6 +3,4 @@ public interface IEnumerate { object? Key { get; init; } - - EnumerateMode Mode { get; init; } } diff --git a/Toolkit.Foundation/Notify.cs b/Toolkit.Foundation/Notify.cs new file mode 100644 index 0000000..fec1dbe --- /dev/null +++ b/Toolkit.Foundation/Notify.cs @@ -0,0 +1,10 @@ +namespace Toolkit.Foundation; + +public class Notify +{ + public static NotifyEventArgs As(TValue value) => + new(value); + + public static NotifyEventArgs As() where TValue : new() => + new(new TValue()); +} diff --git a/Toolkit.Foundation/NotifyEventArgs.cs b/Toolkit.Foundation/NotifyEventArgs.cs new file mode 100644 index 0000000..b7b03c7 --- /dev/null +++ b/Toolkit.Foundation/NotifyEventArgs.cs @@ -0,0 +1,3 @@ +namespace Toolkit.Foundation; + +public record NotifyEventArgs(TValue Value); diff --git a/Toolkit.Foundation/ObservableViewModel.cs b/Toolkit.Foundation/Observable.cs similarity index 81% rename from Toolkit.Foundation/ObservableViewModel.cs rename to Toolkit.Foundation/Observable.cs index adaa76d..7a156cb 100644 --- a/Toolkit.Foundation/ObservableViewModel.cs +++ b/Toolkit.Foundation/Observable.cs @@ -2,7 +2,7 @@ namespace Toolkit.Foundation; -public partial class ObservableViewModel : +public partial class Observable : ObservableObject, IObservableViewModel, IInitializer, @@ -20,7 +20,7 @@ public partial class ObservableViewModel : [ObservableProperty] private bool isInitialized; - public ObservableViewModel(IServiceProvider provider, + public Observable(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, @@ -81,25 +81,25 @@ public partial class ObservableViewModel : } } -public partial class ObservableViewModel(IServiceProvider provider, +public partial class Observable(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, ISubscription subscriber, - IDisposer disposer) : ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer) + IDisposer disposer) : Observable(provider, factory, mediator, publisher, subscriber, disposer) where TValue : notnull { [ObservableProperty] private TValue? value; } -public partial class ObservableViewModel(IServiceProvider provider, +public partial class Observable(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, ISubscription subscriber, IDisposer disposer, - TValue? value = null) : ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer) + TValue? value = null) : Observable(provider, factory, mediator, publisher, subscriber, disposer) where TValue : class { [ObservableProperty] diff --git a/Toolkit.Foundation/ObservableCollectionViewModel.cs b/Toolkit.Foundation/ObservableCollection.cs similarity index 72% rename from Toolkit.Foundation/ObservableCollectionViewModel.cs rename to Toolkit.Foundation/ObservableCollection.cs index 383cbf1..8db9f96 100644 --- a/Toolkit.Foundation/ObservableCollectionViewModel.cs +++ b/Toolkit.Foundation/ObservableCollection.cs @@ -1,38 +1,37 @@ using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.Extensions.DependencyInjection; using System.Collections; -using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Reactive.Disposables; namespace Toolkit.Foundation; -public partial class ObservableCollectionViewModel : +public partial class ObservableCollection : ObservableObject, - IObservableCollectionViewModel, + IObservableCollectionViewModel, IInitializer, IActivated, IDeactivating, IDeactivated, IDeactivatable, - IList, + IList, IList, - IReadOnlyList, + IReadOnlyList, INotifyCollectionChanged, IServiceProviderRequired, IServiceFactoryRequired, IMediatorRequired, IPublisherRequired, IDisposerRequired, - INotificationHandler>, - INotificationHandler>, - INotificationHandler>, - INotificationHandler>, - INotificationHandler> - where TViewModel : + INotificationHandler>, + INotificationHandler>, + INotificationHandler>, + INotificationHandler>, + INotificationHandler> + where TItem : notnull { - private readonly ObservableCollection collection = []; + private readonly System.Collections.ObjectModel.ObservableCollection collection = []; private bool clearing; @@ -42,7 +41,7 @@ public partial class ObservableCollectionViewModel : [ObservableProperty] private int selectedIndex = 0; - public ObservableCollectionViewModel(IServiceProvider provider, + public ObservableCollection(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, @@ -60,13 +59,13 @@ public partial class ObservableCollectionViewModel : collection.CollectionChanged += OnCollectionChanged; } - public ObservableCollectionViewModel(IServiceProvider provider, + public ObservableCollection(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, ISubscription subscriber, IDisposer disposer, - IEnumerable items) + IEnumerable items) { Provider = provider; Factory = factory; @@ -92,7 +91,7 @@ public partial class ObservableCollectionViewModel : bool IList.IsFixedSize => false; - bool ICollection.IsReadOnly => false; + bool ICollection.IsReadOnly => false; bool IList.IsReadOnly => false; @@ -106,7 +105,7 @@ public partial class ObservableCollectionViewModel : object ICollection.SyncRoot => this; - public TViewModel this[int index] + public TItem this[int index] { get => collection[index]; set => SetItem(index, value); @@ -117,11 +116,11 @@ public partial class ObservableCollectionViewModel : get => collection[index]; set { - TViewModel? item = default; + TItem? item = default; try { - item = (TViewModel)value!; + item = (TItem)value!; } catch (InvalidCastException) { @@ -131,16 +130,16 @@ public partial class ObservableCollectionViewModel : } } - public TViewModel Add() + public TItem Add() { - TViewModel? item = Factory.Create(); + TItem? item = Factory.Create(); Add(item); return item; } - public TViewModel Add(params object?[] parameters) - where T : TViewModel + public TItem Add(params object?[] parameters) + where T : TItem { T? item = Factory.Create(parameters); Add(item); @@ -148,9 +147,9 @@ public partial class ObservableCollectionViewModel : return item; } - public TViewModel Add(bool scope = false) + public TItem Add(bool scope = false) where T : - TViewModel + TItem { IServiceFactory? factory = null; if (scope) @@ -165,7 +164,7 @@ public partial class ObservableCollectionViewModel : return item; } - public void Add(TViewModel item) + public void Add(TItem item) { int index = collection.Count; InsertItem(index, item); @@ -174,16 +173,16 @@ public partial class ObservableCollectionViewModel : public void Add(object item) { int index = collection.Count; - InsertItem(index, (TViewModel)item); + InsertItem(index, (TItem)item); } int IList.Add(object? value) { - TViewModel? item = default; + TItem? item = default; try { - item = (TViewModel)value!; + item = (TItem)value!; } catch (InvalidCastException) { @@ -194,9 +193,9 @@ public partial class ObservableCollectionViewModel : return Count - 1; } - public void AddRange(IEnumerable items) + public void AddRange(IEnumerable items) { - foreach (TViewModel? item in items) + foreach (TItem? item in items) { Add(item); } @@ -206,7 +205,7 @@ public partial class ObservableCollectionViewModel : { clearing = true; - foreach (TViewModel item in this.ToList()) + foreach (TItem item in this.ToList()) { Disposer.Dispose(item); } @@ -215,17 +214,17 @@ public partial class ObservableCollectionViewModel : clearing = false; } - public bool Contains(TViewModel item) => + public bool Contains(TItem item) => collection.Contains(item); bool IList.Contains(object? value) => - IsCompatibleObject(value) && Contains((TViewModel)value!); + IsCompatibleObject(value) && Contains((TItem)value!); - public void CopyTo(TViewModel[] array, int index) => + public void CopyTo(TItem[] array, int index) => collection.CopyTo(array, index); void ICollection.CopyTo(Array array, int index) => - collection.CopyTo((TViewModel[])array, index); + collection.CopyTo((TItem[])array, index); public Task Deactivate() { @@ -253,15 +252,15 @@ public partial class ObservableCollectionViewModel : } } - public IEnumerator GetEnumerator() => + public IEnumerator GetEnumerator() => collection.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)collection).GetEnumerator(); - public Task Handle(RemoveEventArgs args) + public Task Handle(RemoveEventArgs args) { - foreach (TViewModel item in this.ToList()) + foreach (TItem item in this.ToList()) { if (args.Value is not null && args.Value.Equals(item)) { @@ -272,9 +271,9 @@ public partial class ObservableCollectionViewModel : return Task.CompletedTask; } - public Task Handle(CreateEventArgs args) + public Task Handle(CreateEventArgs args) { - if (args.Value is TViewModel item) + if (args.Value is TItem item) { Add(item); } @@ -282,9 +281,9 @@ public partial class ObservableCollectionViewModel : return Task.CompletedTask; } - public Task Handle(InsertEventArgs args) + public Task Handle(InsertEventArgs args) { - if (args.Value is TViewModel item) + if (args.Value is TItem item) { Insert(args.Index, item); } @@ -292,9 +291,9 @@ public partial class ObservableCollectionViewModel : return Task.CompletedTask; } - public Task Handle(MoveEventArgs args) + public Task Handle(MoveEventArgs args) { - if (args.Value is TViewModel item) + if (args.Value is TItem item) { Move(args.Index, item); } @@ -302,9 +301,9 @@ public partial class ObservableCollectionViewModel : return Task.CompletedTask; } - public Task Handle(ReplaceEventArgs args) + public Task Handle(ReplaceEventArgs args) { - if (args.Value is TViewModel item) + if (args.Value is TItem item) { Replace(args.Index, item); } @@ -312,12 +311,12 @@ public partial class ObservableCollectionViewModel : return Task.CompletedTask; } - public int IndexOf(TViewModel item) => + public int IndexOf(TItem item) => collection.IndexOf(item); int IList.IndexOf(object? value) => IsCompatibleObject(value) ? - IndexOf((TViewModel)value!) : -1; + IndexOf((TItem)value!) : -1; public Task Initialize() { @@ -332,19 +331,19 @@ public partial class ObservableCollectionViewModel : return Task.CompletedTask; } - public void Insert(int index, TViewModel item) => + public void Insert(int index, TItem item) => InsertItem(index, item); void IList.Insert(int index, object? value) { - if (value is TViewModel item) + if (value is TItem item) { Insert(index, item); } } - public bool Move(int index, TViewModel item) + public bool Move(int index, TItem item) { int oldIndex = collection.IndexOf(item); if (oldIndex < 0) @@ -367,7 +366,7 @@ public partial class ObservableCollectionViewModel : public virtual Task OnDeactivating() => Task.CompletedTask; - public bool Remove(TViewModel item) + public bool Remove(TItem item) { int index = collection.IndexOf(item); if (index < 0) @@ -385,7 +384,7 @@ public partial class ObservableCollectionViewModel : { if (IsCompatibleObject(value)) { - Remove((TViewModel)value!); + Remove((TItem)value!); } } @@ -393,7 +392,7 @@ public partial class ObservableCollectionViewModel : RemoveItem(index); public bool Replace(int index, - TViewModel item) + TItem item) { if (index <= Count - 1) { @@ -412,7 +411,7 @@ public partial class ObservableCollectionViewModel : collection.Clear(); protected virtual void InsertItem(int index, - TViewModel item) + TItem item) { Disposer.Add(this, item); Disposer.Add(item, Disposable.Create(() => @@ -432,16 +431,16 @@ public partial class ObservableCollectionViewModel : } protected virtual IEnumerate PrepareEnumeration(object? key) => - new EnumerateEventArgs() with { Key = key }; + new EnumerateEventArgs() with { Key = key }; protected virtual void RemoveItem(int index) => collection.RemoveAt(index); - protected virtual void SetItem(int index, TViewModel item) => + protected virtual void SetItem(int index, TItem item) => collection[index] = item; private static bool IsCompatibleObject(object? value) => - (value is TViewModel) || (value == null && default(TViewModel) == null); + (value is TItem) || (value == null && default(TItem) == null); private void OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args) => CollectionChanged?.Invoke(this, args); @@ -460,21 +459,21 @@ public partial class ObservableCollectionViewModel : } } -public partial class ObservableCollectionViewModel(IServiceProvider provider, +public partial class ObservableCollection(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, - ISubscription subscriber, IDisposer disposer) : ObservableCollectionViewModel(provider, factory, mediator, publisher, subscriber, disposer) + ISubscription subscriber, IDisposer disposer) : ObservableCollection(provider, factory, mediator, publisher, subscriber, disposer) where TViewModel : notnull { [ObservableProperty] private TValue? value; } -public class ObservableCollectionViewModel(IServiceProvider provider, +public class ObservableCollection(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, ISubscription subscriber, IDisposer disposer) : - ObservableCollectionViewModel(provider, factory, mediator, publisher, subscriber, disposer); \ No newline at end of file + ObservableCollection(provider, factory, mediator, publisher, subscriber, disposer); \ No newline at end of file diff --git a/Toolkit.Foundation/Request.cs b/Toolkit.Foundation/Request.cs index cbf71d6..555318e 100644 --- a/Toolkit.Foundation/Request.cs +++ b/Toolkit.Foundation/Request.cs @@ -7,4 +7,4 @@ public class Request public static RequestEventArgs As() where TValue : new() => new(new TValue()); -} \ No newline at end of file +} diff --git a/Toolkit.Foundation/ValueViewModel.cs b/Toolkit.Foundation/ValueViewModel.cs index e98c488..4bfdb4f 100644 --- a/Toolkit.Foundation/ValueViewModel.cs +++ b/Toolkit.Foundation/ValueViewModel.cs @@ -8,7 +8,7 @@ public partial class ValueViewModel(IServiceProvider provider, IPublisher publisher, ISubscription subscriber, IDisposer disposer) : - ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer) + Observable(provider, factory, mediator, publisher, subscriber, disposer) { [ObservableProperty] private TValue? value;