using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using DHT.Desktop.Models; namespace DHT.Desktop.Dialogs { public class CheckBoxDialogModel : BaseModel { public string Title { get; init; } = ""; private IReadOnlyList items = Array.Empty(); public IReadOnlyList Items { get => items; protected set { foreach (var item in items) { item.PropertyChanged -= OnItemPropertyChanged; } items = value; foreach (var item in items) { item.PropertyChanged += OnItemPropertyChanged; } } } private bool pauseCheckEvents = false; public bool AreAllSelected => Items.All(item => item.Checked); public bool AreNoneSelected => Items.All(item => !item.Checked); public void SelectAll() => SetAllChecked(true); public void SelectNone() => SetAllChecked(false); private void SetAllChecked(bool isChecked) { pauseCheckEvents = true; foreach (var item in Items) { item.Checked = isChecked; } pauseCheckEvents = false; UpdateBulkButtons(); } private void UpdateBulkButtons() { OnPropertyChanged(nameof(AreAllSelected)); OnPropertyChanged(nameof(AreNoneSelected)); } private void OnItemPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (!pauseCheckEvents && e.PropertyName == nameof(CheckBoxItem.Checked)) { UpdateBulkButtons(); } } } public class CheckBoxDialogModel : CheckBoxDialogModel { public new IReadOnlyList> Items { get; } public IEnumerable> SelectedItems => Items.Where(item => item.Checked); public CheckBoxDialogModel(IEnumerable> items) { this.Items = new List>(items); base.Items = this.Items; } } }