Jelajahi Sumber

Added some utility functions, comments and improved some code in MultiSelectDialog

Kenric Nugteren 6 bulan lalu
induk
melakukan
daa8f8e7c3
1 mengubah file dengan 52 tambahan dan 24 penghapusan
  1. 52 24
      inabox.wpf/DynamicGrid/Controls/MultiSelectDialog.cs

+ 52 - 24
inabox.wpf/DynamicGrid/Controls/MultiSelectDialog.cs

@@ -1,5 +1,6 @@
 using System;
 using System.ComponentModel;
+using System.Diagnostics.CodeAnalysis;
 using System.Linq;
 using System.Runtime.CompilerServices;
 using System.Windows;
@@ -27,6 +28,14 @@ namespace InABox.DynamicGrid
         public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
     }
 
+    /// <summary>
+    /// Represents a dialog to select <typeparamref name="T"/>(s), given a filter and a set of columns. It can either do multi-selecting or single-selecting.
+    /// </summary>
+    /// <remarks>
+    /// This is the standard way to do a selection dialog. To access all selected IDs, use <see cref="IDs"/>; to access the data according to the columns
+    /// provided in the constructor, use <see cref="Data"/>; use <see cref="Items"/> if you want to load all the items with the selected IDs with a custom
+    /// set of columns.
+    /// </remarks>
     public class MultiSelectDialog<T> : IMultiSelectDialog where T : Entity, IRemotable, IPersistent, new()
     {
         //private Expression<Func<T, object>>[] _columns = new Expression<Func<T, object>>[] { };
@@ -39,11 +48,6 @@ namespace InABox.DynamicGrid
         private readonly Button OKButton;
         private ThemableWindow? window;
         
-        public MultiSelectDialog() : this(null, null)
-        {
-        }
-
-        //public MultiSelectDialog(Filter<T> filter, Expression<Func<T, object>>[] columns, bool multiselect = true) : base()
         public MultiSelectDialog(Filter<T>? filter, Columns<T>? columns, bool multiselect = true)
         {
             
@@ -161,13 +165,16 @@ namespace InABox.DynamicGrid
             var window = GetWindow();
             if (window.DialogResult == true)
             {
-                if (datagrid?.Data != null && datagrid.SelectedRows.Any())
-                    return datagrid.SelectedRows.Select(r => r.Get<T, Guid>(x => x.ID)).ToArray();
+                return datagrid.SelectedRows.ToArray(r => r.Get<T, Guid>(x => x.ID));
             }
             else if (window.DialogResult == false)
-                return new Guid[] { Guid.Empty };
-
-            return Array.Empty<Guid>();
+            {
+                return [Guid.Empty];
+            }
+            else
+            {
+                return Array.Empty<Guid>();
+            }
         }
 
         public CoreTable Data()
@@ -212,23 +219,16 @@ namespace InABox.DynamicGrid
 
             if (window.DialogResult == true)
             {
-                if (datagrid.Data != null && datagrid.SelectedRows.Any())
+                var ids = datagrid.SelectedRows.ToArray(r => r.Get<T, Guid>(x => x.ID));
+                if (ids.Length > 0)
                 {
-                    Filter<T>? flt = null;
-                    foreach (var row in datagrid.SelectedRows)
-                    {
-                        var id = row.Get<T, Guid>(x => x.ID);
-                        flt = flt == null ? new Filter<T>(x => x.ID).IsEqualTo(id) : flt.Or(x => x.ID).IsEqualTo(id);
-                    }
-
-                    var items = new Client<T>().Query(flt, columns).Rows.Select(r => r.ToObject<T>());
-                    return items.ToArray();
+                    return new Client<T>().Query(new Filter<T>(x => x.ID).InList(ids), columns).ToArray<T>();
                 }
             }
             else if (window.DialogResult == false)
-                return new T[] { new T() };
+                return [new T()];
 
-            return Array.Empty<T>();
+            return [];
         }
 
         Entity[] IMultiSelectDialog.Items(IColumns? columns) => Items(columns as Columns<T>);
@@ -262,12 +262,40 @@ namespace InABox.DynamicGrid
             window.Close();
         }
 
-
-
         private void Grid_OnReload(object sender, Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sortby)
         {
             if (_filter != null)
                 criteria.Add(_filter);
         }
+
+        public static bool SelectItem([NotNullWhen(true)] out T? item, Filter<T>? filter = null, Columns<T>? columns = null, string? title = null)
+        {
+            var dlg = new MultiSelectDialog<T>(filter, columns, multiselect: false);
+            if (dlg.ShowDialogInternal(title, null, null, default))
+            {
+                item = dlg.Data().ToObjects<T>().FirstOrDefault();
+                return item is not null;
+            }
+            else
+            {
+                item = null;
+                return false;
+            }
+        }
+
+        public static bool SelectItems([NotNullWhen(true)] out T[]? items, Filter<T>? filter = null, Columns<T>? columns = null, string? title = null)
+        {
+            var dlg = new MultiSelectDialog<T>(filter, columns, multiselect: true);
+            if (dlg.ShowDialogInternal(title, null, null, default))
+            {
+                items = dlg.Data().ToArray<T>();
+                return true;
+            }
+            else
+            {
+                items = null;
+                return false;
+            }
+        }
     }
 }