From f36c06389da40205a00753215e485086d8082a81 Mon Sep 17 00:00:00 2001
From: 2dust <31833384+2dust@users.noreply.github.com>
Date: Sun, 1 Dec 2024 10:18:36 +0800
Subject: [PATCH] Improve the clash connection

---
 v2rayN/ServiceLib/Models/ConfigItems.cs       |  1 -
 .../ViewModels/ClashConnectionsViewModel.cs   | 60 +------------------
 .../Views/ClashConnectionsView.axaml          | 30 +---------
 .../Views/ClashConnectionsView.axaml.cs       |  1 -
 v2rayN/v2rayN/Views/ClashConnectionsView.xaml | 38 ++----------
 .../v2rayN/Views/ClashConnectionsView.xaml.cs |  1 -
 6 files changed, 12 insertions(+), 119 deletions(-)

diff --git a/v2rayN/ServiceLib/Models/ConfigItems.cs b/v2rayN/ServiceLib/Models/ConfigItems.cs
index 75943e2a..e7b8f079 100644
--- a/v2rayN/ServiceLib/Models/ConfigItems.cs
+++ b/v2rayN/ServiceLib/Models/ConfigItems.cs
@@ -222,7 +222,6 @@
         public int ProxiesSorting { get; set; }
         public bool ProxiesAutoRefresh { get; set; }
         public int ProxiesAutoDelayTestInterval { get; set; } = 10;
-        public int ConnectionsSorting { get; set; }
         public bool ConnectionsAutoRefresh { get; set; }
         public int ConnectionsRefreshInterval { get; set; } = 2;
     }
diff --git a/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs
index e60ddbb9..f9b8cbda 100644
--- a/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs
+++ b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs
@@ -21,9 +21,6 @@ namespace ServiceLib.ViewModels
         [Reactive]
         public string HostFilter { get; set; }
 
-        [Reactive]
-        public int SortingSelected { get; set; }
-
         [Reactive]
         public bool AutoRefresh { get; set; }
 
@@ -31,18 +28,12 @@ namespace ServiceLib.ViewModels
         {
             _config = AppHandler.Instance.Config;
             _updateView = updateView;
-            SortingSelected = _config.ClashUIItem.ConnectionsSorting;
             AutoRefresh = _config.ClashUIItem.ConnectionsAutoRefresh;
 
             var canEditRemove = this.WhenAnyValue(
              x => x.SelectedSource,
              selectedSource => selectedSource != null && Utils.IsNotEmpty(selectedSource.Id));
 
-            this.WhenAnyValue(
-              x => x.SortingSelected,
-              y => y >= 0)
-                  .Subscribe(async c => await DoSortingSelected(c));
-
             this.WhenAnyValue(
                x => x.AutoRefresh,
                y => y == true)
@@ -84,20 +75,6 @@ namespace ServiceLib.ViewModels
               });
         }
 
-        private async Task DoSortingSelected(bool c)
-        {
-            if (!c)
-            {
-                return;
-            }
-            if (SortingSelected != _config.ClashUIItem.ConnectionsSorting)
-            {
-                _config.ClashUIItem.ConnectionsSorting = SortingSelected;
-            }
-
-            await GetClashConnections();
-        }
-
         private async Task GetClashConnections()
         {
             var ret = await ClashApiHandler.Instance.GetClashConnectionsAsync(_config);
@@ -115,7 +92,7 @@ namespace ServiceLib.ViewModels
 
             var dtNow = DateTime.Now;
             var lstModel = new List<ClashConnectionModel>();
-            foreach (var item in connections ?? [])
+            foreach (var item in connections ?? new())
             {
                 var host = $"{(Utils.IsNullOrEmpty(item.metadata.host) ? item.metadata.destinationIP : item.metadata.host)}:{item.metadata.destinationPort}";
                 if (HostFilter.IsNotEmpty() && !host.Contains(HostFilter))
@@ -131,45 +108,14 @@ namespace ServiceLib.ViewModels
                 model.Host = host;
                 var sp = (dtNow - item.start);
                 model.Time = sp.TotalSeconds < 0 ? 1 : sp.TotalSeconds;
-                model.Upload = item.upload;
-                model.Download = item.download;
-                model.UploadTraffic = $"{Utils.HumanFy((long)item.upload)}";
-                model.DownloadTraffic = $"{Utils.HumanFy((long)item.download)}";
                 model.Elapsed = sp.ToString(@"hh\:mm\:ss");
-                model.Chain = item.chains?.Count > 0 ? item.chains[0] : string.Empty;
+                item.chains?.Reverse();
+                model.Chain = $"{item.rule} , {string.Join("->", item.chains ?? new())}";
 
                 lstModel.Add(model);
             }
             if (lstModel.Count <= 0) { return; }
 
-            //sort
-            switch (SortingSelected)
-            {
-                case 0:
-                    lstModel = lstModel.OrderBy(t => t.Upload / t.Time).ToList();
-                    break;
-
-                case 1:
-                    lstModel = lstModel.OrderBy(t => t.Download / t.Time).ToList();
-                    break;
-
-                case 2:
-                    lstModel = lstModel.OrderBy(t => t.Upload).ToList();
-                    break;
-
-                case 3:
-                    lstModel = lstModel.OrderBy(t => t.Download).ToList();
-                    break;
-
-                case 4:
-                    lstModel = lstModel.OrderBy(t => t.Time).ToList();
-                    break;
-
-                case 5:
-                    lstModel = lstModel.OrderBy(t => t.Host).ToList();
-                    break;
-            }
-
             _connectionItems.AddRange(lstModel);
         }
 
diff --git a/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml b/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml
index cbf48f38..dd1c94ac 100644
--- a/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml
+++ b/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml
@@ -25,22 +25,6 @@
                 VerticalContentAlignment="Center"
                 Watermark="{x:Static resx:ResUI.ConnectionsHostFilterTitle}" />
 
-            <TextBlock
-                Margin="8,0"
-                VerticalAlignment="Center"
-                Text="{x:Static resx:ResUI.TbSorting}" />
-            <ComboBox
-                x:Name="cmbSorting"
-                Width="100"
-                Margin="8,0">
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingUpSpeed}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingDownSpeed}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingUpTraffic}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingDownTraffic}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingTime}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingHost}" />
-            </ComboBox>
-
             <Button
                 x:Name="btnConnectionCloseAll"
                 Width="30"
@@ -85,11 +69,11 @@
             </DataGrid.ContextMenu>
             <DataGrid.Columns>
                 <DataGridTextColumn
-                    Width="240"
+                    Width="300"
                     Binding="{Binding Host}"
                     Header="{x:Static resx:ResUI.TbSortingHost}" />
                 <DataGridTextColumn
-                    Width="160"
+                    Width="500"
                     Binding="{Binding Chain}"
                     Header="{x:Static resx:ResUI.TbSortingChain}" />
                 <DataGridTextColumn
@@ -97,17 +81,9 @@
                     Binding="{Binding Network}"
                     Header="{x:Static resx:ResUI.TbSortingNetwork}" />
                 <DataGridTextColumn
-                    Width="100"
+                    Width="160"
                     Binding="{Binding Type}"
                     Header="{x:Static resx:ResUI.TbSortingType}" />
-                <DataGridTextColumn
-                    Width="100"
-                    Binding="{Binding UploadTraffic}"
-                    Header="{x:Static resx:ResUI.TbSortingUpTraffic}" />
-                <DataGridTextColumn
-                    Width="100"
-                    Binding="{Binding DownloadTraffic}"
-                    Header="{x:Static resx:ResUI.TbSortingDownTraffic}" />
                 <DataGridTextColumn
                     Width="100"
                     Binding="{Binding Elapsed}"
diff --git a/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml.cs
index ead03fc5..48201b7c 100644
--- a/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml.cs
+++ b/v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml.cs
@@ -22,7 +22,6 @@ namespace v2rayN.Desktop.Views
                 this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.menuConnectionCloseAll).DisposeWith(disposables);
 
                 this.Bind(ViewModel, vm => vm.HostFilter, v => v.txtHostFilter.Text).DisposeWith(disposables);
-                this.Bind(ViewModel, vm => vm.SortingSelected, v => v.cmbSorting.SelectedIndex).DisposeWith(disposables);
                 this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.btnConnectionCloseAll).DisposeWith(disposables);
                 this.Bind(ViewModel, vm => vm.AutoRefresh, v => v.togAutoRefresh.IsChecked).DisposeWith(disposables);
             });
diff --git a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml
index 8ea9ca0b..88f6b3d1 100644
--- a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml
+++ b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml
@@ -1,11 +1,11 @@
 <reactiveui:ReactiveUserControl
     x:Class="v2rayN.Views.ClashConnectionsView"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
-    xmlns:reactiveui="http://reactiveui.net"
-    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    xmlns:reactiveui="http://reactiveui.net"
     xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib"
     xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib"
     d:DesignHeight="450"
@@ -29,24 +29,6 @@
                 materialDesign:TextFieldAssist.HasClearButton="True"
                 Style="{StaticResource DefTextBox}" />
 
-            <TextBlock
-                Margin="{StaticResource MarginLeftRight8}"
-                VerticalAlignment="Center"
-                Style="{StaticResource ToolbarTextBlock}"
-                Text="{x:Static resx:ResUI.TbSorting}" />
-            <ComboBox
-                x:Name="cmbSorting"
-                Width="100"
-                Margin="{StaticResource MarginLeftRight8}"
-                Style="{StaticResource DefComboBox}">
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingUpSpeed}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingDownSpeed}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingUpTraffic}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingDownTraffic}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingTime}" />
-                <ComboBoxItem Content="{x:Static resx:ResUI.TbSortingHost}" />
-            </ComboBox>
-
             <Button
                 x:Name="btnConnectionCloseAll"
                 Width="24"
@@ -88,11 +70,11 @@
             </DataGrid.ContextMenu>
             <DataGrid.Columns>
                 <DataGridTextColumn
-                    Width="240"
+                    Width="300"
                     Binding="{Binding Host}"
                     Header="{x:Static resx:ResUI.TbSortingHost}" />
                 <DataGridTextColumn
-                    Width="160"
+                    Width="500"
                     Binding="{Binding Chain}"
                     Header="{x:Static resx:ResUI.TbSortingChain}" />
                 <DataGridTextColumn
@@ -100,17 +82,9 @@
                     Binding="{Binding Network}"
                     Header="{x:Static resx:ResUI.TbSortingNetwork}" />
                 <DataGridTextColumn
-                    Width="100"
+                    Width="160"
                     Binding="{Binding Type}"
                     Header="{x:Static resx:ResUI.TbSortingType}" />
-                <DataGridTextColumn
-                    Width="100"
-                    Binding="{Binding UploadTraffic}"
-                    Header="{x:Static resx:ResUI.TbSortingUpTraffic}" />
-                <DataGridTextColumn
-                    Width="100"
-                    Binding="{Binding DownloadTraffic}"
-                    Header="{x:Static resx:ResUI.TbSortingDownTraffic}" />
                 <DataGridTextColumn
                     Width="100"
                     Binding="{Binding Elapsed}"
diff --git a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
index 42d0ec24..059f8ad6 100644
--- a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
+++ b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
@@ -24,7 +24,6 @@ namespace v2rayN.Views
                 this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.menuConnectionCloseAll).DisposeWith(disposables);
 
                 this.Bind(ViewModel, vm => vm.HostFilter, v => v.txtHostFilter.Text).DisposeWith(disposables);
-                this.Bind(ViewModel, vm => vm.SortingSelected, v => v.cmbSorting.SelectedIndex).DisposeWith(disposables);
                 this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.btnConnectionCloseAll).DisposeWith(disposables);
                 this.Bind(ViewModel, vm => vm.AutoRefresh, v => v.togAutoRefresh.IsChecked).DisposeWith(disposables);
             });