AvaloniaModuleCollection.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;
  3. using Avalonia.Svg.Skia;
  4. using CommunityToolkit.Mvvm.ComponentModel;
  5. using CommunityToolkit.Mvvm.Input;
  6. using InABox.Core;
  7. namespace InABox.Avalonia.Components;
  8. public partial class AvaloniaModuleCollection : ObservableObject
  9. {
  10. [ObservableProperty] private ObservableCollection<AvaloniaModule> _items = new();
  11. public AvaloniaModule? this[string name] => Items.FirstOrDefault(x => Equals(x.Title, name));
  12. [ObservableProperty] private AvaloniaModule[] _visibleItems = [];
  13. public AvaloniaModuleCollection()
  14. {
  15. Items.CollectionChanged += (sender, args) => UpdateVisibleItems();
  16. }
  17. public void Add()
  18. {
  19. var module = new AvaloniaModule();
  20. module.PropertyChanged += DoPropertyChanged;
  21. Items.Add(module);
  22. }
  23. private void DoPropertyChanged(object? sender, PropertyChangedEventArgs e)
  24. {
  25. if (e.PropertyName == nameof(AvaloniaModule.IsVisible))
  26. UpdateVisibleItems();
  27. }
  28. private void UpdateVisibleItems()
  29. {
  30. VisibleItems = Items.Where(x=>x.IsVisible == true).ToArray();
  31. }
  32. public AvaloniaModule Add(string title, string description, SvgImage? image, Action action, bool? isVisible = null
  33. /* Func<PrsModule, Task>? configure = null */)
  34. {
  35. var module = new AvaloniaModule
  36. {
  37. Title = title,
  38. Description = description,
  39. Image = image,
  40. TapCommand = new RelayCommand(action),
  41. IsVisible = isVisible ?? true
  42. };
  43. module.PropertyChanged += DoPropertyChanged;
  44. Items.Add(module);
  45. // _ = configure?.Invoke(module);
  46. return module;
  47. }
  48. public AvaloniaModule Add<TViewModel>(string title, string description, SvgImage? image,
  49. /* Func<PrsModule, Task>? configure = null */ Action<TViewModel>? configure = null, bool? isVisible = null)
  50. where TViewModel : IViewModelBase
  51. {
  52. var module = new AvaloniaModule
  53. {
  54. Title = title,
  55. Description = description,
  56. Image = image,
  57. TapCommand = new RelayCommand(() => Navigation.Navigate<TViewModel>(configure)),
  58. IsVisible = isVisible ?? true
  59. };
  60. module.PropertyChanged += DoPropertyChanged;
  61. Items.Add(module);
  62. // _ = configure?.Invoke(module);
  63. return module;
  64. }
  65. public AvaloniaModule? Add<TToken, TViewModel>(string title, string description, SvgImage? image,
  66. /* Func<PrsModule, Task>? configure = null */ Action<TViewModel>? configure = null, bool? isVisible = null)
  67. where TToken : ISecurityDescriptor, new()
  68. where TViewModel : IViewModelBase
  69. {
  70. if (InABox.Core.Security.IsAllowed<TToken>())
  71. return Add<TViewModel>(title, description, image, configure, isVisible);
  72. return null;
  73. }
  74. }