using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Input; using ComplianceNFs.Core.Application; using ComplianceNFs.Core.Entities; namespace ComplianceNFs.Monitor { public class MonitorViewModel : INotifyPropertyChanged { public ObservableCollection RecentInvoices { get; } = []; public ICommand ForceScanCommand { get; } public event PropertyChangedEventHandler? PropertyChanged; public IInvoiceStatusStream? StatusStream { get; } public MonitorViewModel(IInvoiceStatusStream statusStream) { StatusStream = statusStream; foreach (var inv in statusStream.GetRecent(100)) RecentInvoices.Add(inv); statusStream.StatusUpdated += OnStatusUpdated; ForceScanCommand = new RelayCommand(_ => ForceScan()); } private void OnStatusUpdated(EnergyInvoice invoice) { RecentInvoices.Add(invoice); OnPropertyChanged(nameof(RecentInvoices)); } private static void ForceScan() { // TODO: Call service to force ingestion cycle } protected void OnPropertyChanged([CallerMemberName] string? name = null) { if (name != null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } } public class RelayCommand(Action execute, Func? canExecute = null) : ICommand { public bool CanExecute(object? parameter) => parameter != null && (canExecute == null || canExecute(parameter)); public void Execute(object? parameter) { if (parameter != null) { execute(parameter); } } public event EventHandler? CanExecuteChanged { add { } remove { } } } }