using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Toolkit.Foundation; namespace Wallet; public partial class ItemEntryCollectionViewModel : ObservableCollection, IItemEntryViewModel, IHandler, bool>, INotificationHandler>, INotificationHandler>, INotificationHandler> where TItem : notnull, IDisposable { private readonly IItemEntryConfiguration configuration; [ObservableProperty] private bool isConcealed; [ObservableProperty] private bool isRevealed; [ObservableProperty] private ItemState state; [ObservableProperty] private double width; public ItemEntryCollectionViewModel(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, ISubscriber subscriber, IDisposer disposer, ItemState state, IItemEntryConfiguration configuration, string key, TValue value, bool isConcealed, bool isRevealed, double width) : base(provider, factory, mediator, publisher, subscriber, disposer, key, value) { this.configuration = configuration; State = state; IsConcealed = isConcealed; IsRevealed = isRevealed; Width = width; Track(nameof(Value), () => Value, x => Value = x); } public ItemEntryCollectionViewModel(IServiceProvider provider, IServiceFactory factory, IMediator mediator, IPublisher publisher, ISubscriber subscriber, IDisposer disposer, IEnumerable items, ItemState state, IItemEntryConfiguration configuration, string key, TValue value, bool isConcealed, bool isRevealed, double width) : base(provider, factory, mediator, publisher, subscriber, disposer, items, key, value) { this.configuration = configuration; State = state; IsConcealed = isConcealed; IsRevealed = isRevealed; Width = width; Track(nameof(Value), () => Value, x => Value = x); } public Task Handle(UpdateEventArgs args) { State = ItemState.Write; OnStateChanged(); return Task.CompletedTask; } public Task Handle(CancelEventArgs args) { Revert(); State = ItemState.Read; OnStateChanged(); return Task.CompletedTask; } public Task Handle(ConfirmEventArgs args) { Commit(); State = ItemState.Read; OnStateChanged(); return Task.CompletedTask; } public async Task Handle(ValidateEventArgs args, CancellationToken cancellationToken) { if (configuration is not null) { configuration.Value = Value; } return await Task.FromResult(true); } protected virtual void OnStateChanged() { } [RelayCommand] private void Copy() => Publisher.Publish(Write.As(new Clipboard($"{Value}"))); [RelayCommand] private void Hide() => IsRevealed = false; [RelayCommand] private void Remove() => Dispose(); [RelayCommand] private void Reveal() => IsRevealed = true; }