Browse Source

avalonia: AvaloniaDataGrid now responds to changes in CoreRepositories

Kenric Nugteren 1 week ago
parent
commit
ef4ffdf981

+ 17 - 0
InABox.Avalonia/Components/AvaloniaDataGrid/AvaloniaDataGrid.axaml.cs

@@ -6,6 +6,7 @@ using Avalonia.Controls.Primitives;
 using Avalonia.Data;
 using Avalonia.Data.Converters;
 using Avalonia.Input;
+using Avalonia.Threading;
 using Avalonia.VisualTree;
 using CommunityToolkit.Mvvm.Input;
 using InABox.Core;
@@ -133,6 +134,14 @@ public partial class AvaloniaDataGrid : UserControl, INotifyPropertyChanged
     private static void ItemsSource_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
     {
         grid.Grid.ItemsSource = grid.ItemsSource;
+        if(args.OldValue is ICoreRepository oldRepo)
+        {
+            oldRepo.Changed -= grid.Repository_Changed;
+        }
+        if(grid.ItemsSource is ICoreRepository repo)
+        {
+            repo.Changed += grid.Repository_Changed;
+        }
         if (grid.Grid.CollectionView is not null)
         {
             grid.Grid.CollectionView.CollectionChanged -= grid.CollectionView_CollectionChanged;
@@ -141,6 +150,14 @@ public partial class AvaloniaDataGrid : UserControl, INotifyPropertyChanged
         grid.ItemsChanged();
     }
 
+    private void Repository_Changed(object sender, CoreRepositoryChangedEventArgs args)
+    {
+        Dispatcher.UIThread.InvokeAsync(() =>
+        {
+            Grid.CollectionView?.Refresh();
+        });
+    }
+
     #endregion
 
     public AvaloniaDataGrid()

+ 42 - 9
InABox.Avalonia/DataModels/CoreRepository.cs

@@ -1,5 +1,6 @@
 using System.Collections;
 using System.Collections.ObjectModel;
+using System.Collections.Specialized;
 using System.ComponentModel;
 using System.Diagnostics.CodeAnalysis;
 using System.Linq.Expressions;
@@ -111,7 +112,11 @@ namespace InABox.Avalonia
         protected CoreRepository(IModelHost host, Func<Filter<TEntity>> filter, Func<string>? filename = null)
         {
             AllItems = new CoreObservableCollection<TItem>();
-            AllItems.CollectionChanged += (sender, args) => ItemsChanged(AllItems);
+            AllItems.CollectionChanged += (sender, args) =>
+            {
+                _items = null;
+                ItemsChanged(AllItems);
+            };
             // EnableSynchronization(AllItems);
 
             //Items = new CoreObservableCollection<TItem>(); 
@@ -325,7 +330,15 @@ namespace InABox.Avalonia
         
         private CoreTable _table = new CoreTable();
 
-        public IEnumerable<TItem> Items => SearchAndSort();
+        private IEnumerable<TItem>? _items;
+        public IEnumerable<TItem> Items
+        {
+            get
+            {
+                _items ??= SearchAndSort();
+                return _items;
+            }
+        }
 
         public int ItemCount => Items.Count();
         
@@ -398,14 +411,33 @@ namespace InABox.Avalonia
         void ICoreRepository.ToggleSelection(object item) => ToggleSelection(item as TItem);
         bool ICoreRepository.IsSelected(object item) => IsSelected(item as TItem);
         void ICoreRepository.SetSelectedItems(IEnumerable<object> items) => SetSelectedItems(items.OfType<TItem>());
-        
+
         #endregion
-        
+
         #region Searching
-        
-        public Func<TItem, bool>? SearchPredicate { get; set; }
-        
-        public Func<List<TItem>,List<TItem>>? SortPredicate { get; set; }
+
+        private Func<TItem, bool>? _searchPredicate;
+        public Func<TItem, bool>? SearchPredicate
+        {
+            get => _searchPredicate;
+            set
+            {
+                _searchPredicate = value;
+                _items = null;
+            }
+        }
+
+        private Func<List<TItem>, List<TItem>>? _sortPredicate;
+        public Func<List<TItem>,List<TItem>>? SortPredicate
+        {
+            get => _sortPredicate;
+            set
+            {
+                _sortPredicate = value;
+                _items = null;
+            }
+        }
+            
 
         public ICoreRepository Search(Func<TItem, bool> searchpredicate, Func<List<TItem>,List<TItem>> sortpredicate)
         {
@@ -446,6 +478,7 @@ namespace InABox.Avalonia
         
         public ICoreRepository Search()
         {
+            _items = null;
             OnPropertyChanged(nameof(Items));
             OnPropertyChanged(nameof(ItemCount));
             return this;
@@ -692,7 +725,7 @@ namespace InABox.Avalonia
         #region CRUD Operations
         
         public event CoreRepositoryItemCreatedEvent<TItem>? ItemAdded;
-        
+
         private T CreateItem<T>(CoreRow row) 
             where T : Shell<TParent,TEntity>, new()
         {