Jelajahi Sumber

Small cahnge to selectdatabase screen to prevent constant reloading of already retrieved data

Kenric Nugteren 11 bulan lalu
induk
melakukan
623c8807ac
1 mengubah file dengan 197 tambahan dan 194 penghapusan
  1. 197 194
      prs.desktop/Utils/SelectDatabase.xaml.cs

+ 197 - 194
prs.desktop/Utils/SelectDatabase.xaml.cs

@@ -22,235 +22,238 @@ using PRSServer;
 using Image = System.Windows.Controls.Image;
 using Size = System.Drawing.Size;
 
-namespace PRSDesktop
+namespace PRSDesktop;
+
+/// <summary>
+///     Interaction logic for SelectDatabase.xaml
+/// </summary>
+public partial class SelectDatabase : ThemableWindow
 {
-    /// <summary>
-    ///     Interaction logic for SelectDatabase.xaml
-    /// </summary>
-    public partial class SelectDatabase : ThemableWindow
-    {
 
-        private CancellationTokenSource cancel = new CancellationTokenSource();
-        
-        public SelectDatabase(Dictionary<string, DatabaseSettings> databases)
-        {
-            InitializeComponent();
+    private CancellationTokenSource cancel = new CancellationTokenSource();
+    
+    public SelectDatabase(Dictionary<string, DatabaseSettings> databases)
+    {
+        InitializeComponent();
 
-            var active = databases.Where(x => x.Value.IsActive).ToArray().Length;
+        var active = databases.Where(x => x.Value.IsActive).ToArray().Length;
 
-            var cols = (int)Math.Ceiling(Math.Sqrt(active));
-            for (var iCol = 0; iCol < cols; iCol++)
-                Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
-            var rows = (int)Math.Ceiling(active / (double)cols);
-            for (var iRow = 0; iRow < cols; iRow++)
-                Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
+        var cols = (int)Math.Ceiling(Math.Sqrt(active));
+        for (var iCol = 0; iCol < cols; iCol++)
+            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
+        var rows = (int)Math.Ceiling(active / (double)cols);
+        for (var iRow = 0; iRow < cols; iRow++)
+            Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
 
-            var i = 0;
-            foreach (var key in databases.Keys)
-                if (databases[key].IsActive)
+        var i = 0;
+        foreach (var key in databases.Keys)
+            if (databases[key].IsActive)
+            {
+                var db = databases[key];
+                
+                var button = new Button
                 {
-                    var db = databases[key];
-                    
-                    var button = new Button
-                    {
-                        Background = new SolidColorBrush(Colors.Transparent),
-                        BorderThickness = new Thickness(0),
-                        Padding = new Thickness(0)
-                    };
+                    Background = new SolidColorBrush(Colors.Transparent),
+                    BorderThickness = new Thickness(0),
+                    Padding = new Thickness(0)
+                };
 
-                    var color = Colors.Red;
-                    // try
-                    // {
-                    //     color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(db.ColorScheme);
-                    // }
-                    // catch (Exception e)
-                    // {
-                    //     color = Colors.Red;
-                    // }
-                    
-                    var border = new Border
-                    {
-                        CornerRadius = new CornerRadius(5),
-                        BorderBrush = new SolidColorBrush(color),
-                        BorderThickness = new Thickness(0.75),
-                        Background = db.DatabaseType == DatabaseType.Standalone ? 
-                            new SolidColorBrush(Colors.WhiteSmoke) : 
-                            new SolidColorBrush(Colors.White),
-                        Width = 200,
-                        Height = 200
-                    };
+                var color = Colors.Red;
+                // try
+                // {
+                //     color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(db.ColorScheme);
+                // }
+                // catch (Exception e)
+                // {
+                //     color = Colors.Red;
+                // }
+                
+                var border = new Border
+                {
+                    CornerRadius = new CornerRadius(5),
+                    BorderBrush = new SolidColorBrush(color),
+                    BorderThickness = new Thickness(0.75),
+                    Background = db.DatabaseType == DatabaseType.Standalone ? 
+                        new SolidColorBrush(Colors.WhiteSmoke) : 
+                        new SolidColorBrush(Colors.White),
+                    Width = 200,
+                    Height = 200
+                };
 
-                    var panel = new DockPanel { VerticalAlignment = VerticalAlignment.Stretch };
-                    border.Child = panel;
+                var panel = new DockPanel { VerticalAlignment = VerticalAlignment.Stretch };
+                border.Child = panel;
 
-                    StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center };
-                    stack.SetValue(DockPanel.DockProperty, Dock.Top);
-                    Label dbtype = new Label()
-                    {
-                        Content = db.DatabaseType.ToString(), 
-                        Margin = new Thickness(5,2,0,0), 
-                        HorizontalContentAlignment = HorizontalAlignment.Left,
-                        FontSize = 8,
-                        Foreground = new SolidColorBrush(Colors.DimGray)
-                    };
-                    stack.Children.Add(dbtype);                    
-                    Label dbver = new Label()
-                    {
-                        Margin = new Thickness(0,2,5,0), 
-                        HorizontalContentAlignment = HorizontalAlignment.Right,
-                        FontSize = 8,
-                        Foreground = new SolidColorBrush(Colors.DimGray)
-                    };
-                    stack.Children.Add(dbver);
-                    panel.Children.Add(stack);
-                    
-                    var label = new Label
-                    {
-                        Content = key,
-                        HorizontalContentAlignment = HorizontalAlignment.Center,
-                        VerticalContentAlignment = VerticalAlignment.Center,
-                        Width = 150,
-                        Height = 35
-                    };
-                    label.SetValue(DockPanel.DockProperty, Dock.Bottom);
-                    panel.Children.Add(label);
-                    
-                    var image = new Image();
-                    image.Height = 150;
-                    image.Width = 150;
-                    image.SetValue(DockPanel.DockProperty, Dock.Top);
-                    panel.Children.Add(image);
+                StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center };
+                stack.SetValue(DockPanel.DockProperty, Dock.Top);
+                Label dbtype = new Label()
+                {
+                    Content = db.DatabaseType.ToString(), 
+                    Margin = new Thickness(5,2,0,0), 
+                    HorizontalContentAlignment = HorizontalAlignment.Left,
+                    FontSize = 8,
+                    Foreground = new SolidColorBrush(Colors.DimGray)
+                };
+                stack.Children.Add(dbtype);                    
+                Label dbver = new Label()
+                {
+                    Margin = new Thickness(0,2,5,0), 
+                    HorizontalContentAlignment = HorizontalAlignment.Right,
+                    FontSize = 8,
+                    Foreground = new SolidColorBrush(Colors.DimGray)
+                };
+                stack.Children.Add(dbver);
+                panel.Children.Add(stack);
+                
+                var label = new Label
+                {
+                    Content = key,
+                    HorizontalContentAlignment = HorizontalAlignment.Center,
+                    VerticalContentAlignment = VerticalAlignment.Center,
+                    Width = 150,
+                    Height = 35
+                };
+                label.SetValue(DockPanel.DockProperty, Dock.Bottom);
+                panel.Children.Add(label);
+                
+                var image = new Image();
+                image.Height = 150;
+                image.Width = 150;
+                image.SetValue(DockPanel.DockProperty, Dock.Top);
+                panel.Children.Add(image);
 
-                    button.Content = border;
-                    button.Margin = new Thickness(5, 5, 0, 0);
-                    button.Click += Button_Click;
-                    button.Height = 200;
-                    button.Width = 200;
-                    button.Tag = key;
-                    var col = i % cols;
-                    var row = i / cols;
-                    button.SetValue(Grid.ColumnProperty, col);
-                    button.SetValue(Grid.RowProperty, row);
-                    Grid.Children.Add(button);
+                button.Content = border;
+                button.Margin = new Thickness(5, 5, 0, 0);
+                button.Click += Button_Click;
+                button.Height = 200;
+                button.Width = 200;
+                button.Tag = key;
+                var col = i % cols;
+                var row = i / cols;
+                button.SetValue(Grid.ColumnProperty, col);
+                button.SetValue(Grid.RowProperty, row);
+                Grid.Children.Add(button);
 
-                    Task.Run(
-                        () =>
+                Task.Run(
+                    () =>
+                    {
+                        while (!cancel.IsCancellationRequested)
                         {
-                            while (!cancel.IsCancellationRequested)
-                            {
-                                DatabaseInfo? info = null;
+                            DatabaseInfo? info = null;
 
-                                if(db.DatabaseType == DatabaseType.Standalone)
-                                {
-                                    info = new DatabaseInfo(db.ColorScheme, db.Logo, CoreUtils.GetVersion(), true, DbFactory.RestPort, DbFactory.RPCPort, DbFactory.ID);
-                                }
-                                if (db.DatabaseType == DatabaseType.Local)
-                                {
-                                    info = new IPCClient<User>(DatabaseServerProperties.GetPipeName(db.LocalServerName, false)).Info();
-                                    //info = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(db.LocalServerName, true)).Info();
-                                }
-                                else if (db.DatabaseType == DatabaseType.Networked)
+                            if(db.DatabaseType == DatabaseType.Standalone)
+                            {
+                                info = new DatabaseInfo(db.ColorScheme, db.Logo, CoreUtils.GetVersion(), true, DbFactory.RestPort, DbFactory.RPCPort, DbFactory.ID);
+                            }
+                            if (db.DatabaseType == DatabaseType.Local)
+                            {
+                                info = new IPCClient<User>(DatabaseServerProperties.GetPipeName(db.LocalServerName, false)).Info();
+                                //info = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(db.LocalServerName, true)).Info();
+                            }
+                            else if (db.DatabaseType == DatabaseType.Networked)
+                            {
+                                if (db.Protocol == SerializerProtocol.RPC)
+                                    info = new RpcClientSocketTransport(db.URLs).Info();
+                                else
                                 {
-                                    if (db.Protocol == SerializerProtocol.RPC)
-                                        info = new RpcClientSocketTransport(db.URLs).Info();
-                                    else
-                                    {
-                                        RestClient<User>.Ping(db.URLs, out DatabaseInfo i);
-                                        info = i;
-                                    }
+                                    RestClient<User>.Ping(db.URLs, out DatabaseInfo i);
+                                    info = i;
                                 }
+                            }
 
-                                UpdateInfo(key, db, info, border, image, dbver);
-                                Task.Delay(1000).Wait();
+                            UpdateInfo(key, db, info, border, image, dbver);
+
+                            if(info is not null)
+                            {
+                                break;
                             }
+                            Task.Delay(1000).Wait();
                         }
-                        ,cancel.Token);
+                    }, cancel.Token);
 
-                    i++;
-                }
-        }
+                i++;
+            }
+    }
 
-        private bool LogoEqual(byte[]? first, byte[]? second)
-        {
-            if ((first == null) && (second == null))
-                return true;
-            if ((first == null) || (second == null))
-                return false;
-            return first.SequenceEqual(second);
-        }
+    private bool LogoEqual(byte[]? first, byte[]? second)
+    {
+        if ((first == null) && (second == null))
+            return true;
+        if ((first == null) || (second == null))
+            return false;
+        return first.SequenceEqual(second);
+    }
 
-        private void UpdateInfo(String key, DatabaseSettings db, DatabaseInfo? info, Border border, Image image, Label version)
-        {
+    private void UpdateInfo(String key, DatabaseSettings db, DatabaseInfo? info, Border border, Image image, Label version)
+    {
 
-            
-            Dispatcher.Invoke(() =>
+        
+        Dispatcher.Invoke(() =>
+        {
+            if (info == null)
             {
-                if (info == null)
-                {
-                    border.BorderBrush = new SolidColorBrush(Colors.Red);
-                    border.Background = db.DatabaseType == DatabaseType.Standalone
-                        ? new SolidColorBrush(Colors.WhiteSmoke)
-                        : new SolidColorBrush(Colors.White);
-                    image.Source = null;
-                    version.Content = "";
-                    return;
-                }
+                border.BorderBrush = new SolidColorBrush(Colors.Red);
+                border.Background = db.DatabaseType == DatabaseType.Standalone
+                    ? new SolidColorBrush(Colors.WhiteSmoke)
+                    : new SolidColorBrush(Colors.White);
+                image.Source = null;
+                version.Content = "";
+                return;
+            }
 
-                if (info?.ColorScheme is not null)
-                {
-                    try
-                    {
-                        var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(info.ColorScheme);
-                        border.BorderBrush = new SolidColorBrush(color);
-                        border.Background = new SolidColorBrush(color.AdjustLightness(90));
-                    }
-                    catch
-                    {
-                    }
-                }
-
-                Bitmap? bitmap = null; //PRSDesktop.Resources.splash_small;
+            if (info?.ColorScheme is not null)
+            {
                 try
                 {
-                    if (info?.Logo != null && info.Logo.Any())
-                        using (var ms = new MemoryStream(info.Logo))
-                            bitmap = new Bitmap(ms);
+                    var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(info.ColorScheme);
+                    border.BorderBrush = new SolidColorBrush(color);
+                    border.Background = new SolidColorBrush(color.AdjustLightness(90));
                 }
                 catch
                 {
                 }
+            }
 
-                if (bitmap != null)
-                {
-                    var size = new Size(bitmap.Width, bitmap.Height).Adjust(150, 150);
-                    image.Source = bitmap.AsBitmapImage(size.Height, size.Width);
-                }
+            Bitmap? bitmap = null; //PRSDesktop.Resources.splash_small;
+            try
+            {
+                if (info?.Logo != null && info.Logo.Any())
+                    using (var ms = new MemoryStream(info.Logo))
+                        bitmap = new Bitmap(ms);
+            }
+            catch
+            {
+            }
 
-                try
-                {
-                    version.Content = String.Equals(info?.Version,"???") ? "" : info?.Version;
-                }
-                catch
-                {
-                }
+            if (bitmap != null)
+            {
+                var size = new Size(bitmap.Width, bitmap.Height).Adjust(150, 150);
+                image.Source = bitmap.AsBitmapImage(size.Height, size.Width);
+            }
 
-                if (!string.Equals(db.ColorScheme, info?.ColorScheme) || !LogoEqual(db.Logo, info?.Logo))
-                {
-                    db.ColorScheme = info?.ColorScheme ?? DatabaseSettings.DefaultColorScheme;
-                    db.Logo = info?.Logo;
-                    new LocalConfiguration<DatabaseSettings>(key).Save(db);
-                }
-            });
-        }
+            try
+            {
+                version.Content = String.Equals(info?.Version,"???") ? "" : info?.Version;
+            }
+            catch
+            {
+            }
 
-        public string Database { get; private set; }
+            if (!string.Equals(db.ColorScheme, info?.ColorScheme) || !LogoEqual(db.Logo, info?.Logo))
+            {
+                db.ColorScheme = info?.ColorScheme ?? DatabaseSettings.DefaultColorScheme;
+                db.Logo = info?.Logo;
+                new LocalConfiguration<DatabaseSettings>(key).Save(db);
+            }
+        });
+    }
 
-        private void Button_Click(object sender, RoutedEventArgs e)
-        {
-            cancel.Cancel();
-            Database = (sender as Button).Tag as string;
-            DialogResult = true;
-        }
+    public string Database { get; private set; }
 
+    private void Button_Click(object sender, RoutedEventArgs e)
+    {
+        cancel.Cancel();
+        Database = (sender as Button).Tag as string;
+        DialogResult = true;
     }
+
 }