#nullable enable using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using ReminderOrganizer.Core; using ReminderOrganizer.Models; using ReminderOrganizer.Services; namespace ReminderOrganizer { /// Edit anniversary categories (name, icon, color, text). Works on copies until "Save". public partial class CategoryWindow : Window { private readonly ObservableCollection _items; private readonly Func _usage; private readonly ILocalizationService _loc; public IReadOnlyList Glyphs => Category.Glyphs; public IReadOnlyList Palette => Folder.Palette; /// The edited list (valid after DialogResult == true). public List Result => _items.ToList(); public CategoryWindow(IEnumerable categories, bool darkMode, Func usageCount) { InitializeComponent(); DialogAnimation.Attach(this); Theme.Apply(Resources, darkMode); _loc = (ILocalizationService)ServiceLocator.Services.GetService(typeof(ILocalizationService))!; _usage = usageCount; _items = new ObservableCollection(categories.Select(c => c.Clone())); foreach (var c in _items) c.PropertyChanged += OnCategoryChanged; Rows.ItemsSource = _items; DataContext = new LocalizedTexts(_loc); } /// Indexer for {Binding [Key]} in this window. private sealed class LocalizedTexts { private readonly ILocalizationService _loc; public LocalizedTexts(ILocalizationService loc) => _loc = loc; public string this[string key] => _loc.GetString(key); } private string Example(Category c) { var culture = ReminderTask.DisplayCulture?.Invoke() ?? System.Globalization.CultureInfo.CurrentCulture; return c.FormatYears(25, culture, _loc.GetString); } private void Example_Loaded(object sender, RoutedEventArgs e) { if (sender is TextBlock tb && tb.Tag is Category c) tb.Text = Example(c); } private void OnCategoryChanged(object? sender, PropertyChangedEventArgs e) { // keep the "example" column live while typing if (sender is not Category c) return; foreach (var tb in FindVisualChildren(Rows)) if (ReferenceEquals(tb.Tag, c)) tb.Text = Example(c); } private static IEnumerable FindVisualChildren(DependencyObject root) where T : DependencyObject { for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(root); i++) { var child = System.Windows.Media.VisualTreeHelper.GetChild(root, i); if (child is T t) yield return t; foreach (var d in FindVisualChildren(child)) yield return d; } } private void Add_Click(object sender, RoutedEventArgs e) { var c = new Category { Name = _loc.GetString("NewCategory"), Glyph = Category.Glyphs[_items.Count % Category.Glyphs.Length], Color = Folder.Palette[_items.Count % Folder.Palette.Length], Format = _loc.GetString("CatJubileeFormat") }; c.PropertyChanged += OnCategoryChanged; _items.Add(c); } private void Delete_Click(object sender, RoutedEventArgs e) { if (sender is not FrameworkElement { Tag: Category c }) return; if (_items.Count <= 1) return; int used = _usage(c.Id); if (used > 0 && MessageBox.Show(this, string.Format(_loc.GetString("ConfirmDeleteCategory"), c.Name, used), _loc.GetString("AppTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) return; _items.Remove(c); } private void Save_Click(object sender, RoutedEventArgs e) { foreach (var c in _items) { c.Name = string.IsNullOrWhiteSpace(c.Name) ? _loc.GetString("NewCategory") : c.Name.Trim(); if (string.IsNullOrWhiteSpace(c.Format)) c.Format = "{0}"; } DialogResult = true; } private void Cancel_Click(object sender, RoutedEventArgs e) => DialogResult = false; private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed && e.OriginalSource is not TextBox) DragMove(); } } }