Compare commits

..

4 commits

Author SHA1 Message Date
2dust
555960e210 Optimize and improve code
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run
2025-02-26 17:01:57 +08:00
2dust
a18ae5582b Jump to the selected item when refreshing the server list
https://github.com/2dust/v2rayN/issues/6800
2025-02-26 16:36:36 +08:00
2dust
6d6894591c Update Global.cs 2025-02-26 15:51:47 +08:00
2dust
984b97fc14 Optimize latency and IP address information testing
If the first test fails, it will be tested again after 500ms.
2025-02-26 14:30:10 +08:00
17 changed files with 135 additions and 44 deletions

View file

@ -69,7 +69,7 @@ namespace ServiceLib
public const int MinFontSize = 8; public const int MinFontSize = 8;
public const string RebootAs = "rebootas"; public const string RebootAs = "rebootas";
public const string AvaAssets = "avares://v2rayN/Assets/"; public const string AvaAssets = "avares://v2rayN/Assets/";
public const string LocalAppData = "V2RAYN_LOCAL_APPLICATION_DATA"; public const string LocalAppData = "V2RAYN_LOCAL_APPLICATION_DATA_V2";
public const string V2RayLocalAsset = "V2RAY_LOCATION_ASSET"; public const string V2RayLocalAsset = "V2RAY_LOCATION_ASSET";
public const string XrayLocalAsset = "XRAY_LOCATION_ASSET"; public const string XrayLocalAsset = "XRAY_LOCATION_ASSET";
public const int SpeedTestPageSize = 1000; public const int SpeedTestPageSize = 1000;

View file

@ -1,4 +1,4 @@
using System.Diagnostics; using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Sockets; using System.Net.Sockets;
@ -222,20 +222,20 @@ namespace ServiceLib.Services
public async Task<int> RunAvailabilityCheck(IWebProxy? webProxy) public async Task<int> RunAvailabilityCheck(IWebProxy? webProxy)
{ {
var responseTime = -1;
try try
{ {
webProxy ??= await GetWebProxy(true); webProxy ??= await GetWebProxy(true);
var config = AppHandler.Instance.Config;
try for (var i = 0; i < 2; i++)
{ {
var config = AppHandler.Instance.Config; responseTime = await GetRealPingTime(config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10);
var responseTime = await GetRealPingTime(config.SpeedTestItem.SpeedPingTestUrl, webProxy, 10); if (responseTime > 0)
return responseTime; {
} break;
catch (Exception ex) }
{ await Task.Delay(500);
Logging.SaveLog(_tag, ex);
return -1;
} }
} }
catch (Exception ex) catch (Exception ex)
@ -243,6 +243,7 @@ namespace ServiceLib.Services
Logging.SaveLog(_tag, ex); Logging.SaveLog(_tag, ex);
return -1; return -1;
} }
return responseTime;
} }
public async Task<int> GetRealPingTime(string url, IWebProxy? webProxy, int downloadTimeout) public async Task<int> GetRealPingTime(string url, IWebProxy? webProxy, int downloadTimeout)

View file

@ -1,7 +1,5 @@
using System.Reactive; using System.Reactive;
using System.Text;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers;
namespace ServiceLib.ViewModels namespace ServiceLib.ViewModels
{ {

View file

@ -316,6 +316,8 @@ namespace ServiceLib.ViewModels
return; return;
} }
_updateView?.Invoke(EViewAction.DispatcherServerAvailability, ResUI.Speedtesting);
var msg = await (new UpdateService()).RunAvailabilityCheck(); var msg = await (new UpdateService()).RunAvailabilityCheck();
NoticeHandler.Instance.SendMessageEx(msg); NoticeHandler.Instance.SendMessageEx(msg);

View file

@ -247,7 +247,10 @@ namespace v2rayN.Desktop.Views
case EViewAction.AddServerViaClipboard: case EViewAction.AddServerViaClipboard:
var clipboardData = await AvaUtils.GetClipboardData(this); var clipboardData = await AvaUtils.GetClipboardData(this);
ViewModel?.AddServerViaClipboardAsync(clipboardData); if (ViewModel != null)
{
await ViewModel.AddServerViaClipboardAsync(clipboardData);
}
break; break;
case EViewAction.AdjustMainLvColWidth: case EViewAction.AdjustMainLvColWidth:
@ -294,7 +297,10 @@ namespace v2rayN.Desktop.Views
break; break;
case WindowCloseReason.ApplicationShutdown or WindowCloseReason.OSShutdown: case WindowCloseReason.ApplicationShutdown or WindowCloseReason.OSShutdown:
await ViewModel?.MyAppExitAsync(true); if (ViewModel != null)
{
await ViewModel.MyAppExitAsync(true);
}
break; break;
} }
@ -309,7 +315,10 @@ namespace v2rayN.Desktop.Views
{ {
case Key.V: case Key.V:
var clipboardData = await AvaUtils.GetClipboardData(this); var clipboardData = await AvaUtils.GetClipboardData(this);
ViewModel?.AddServerViaClipboardAsync(clipboardData); if (ViewModel != null)
{
await ViewModel.AddServerViaClipboardAsync(clipboardData);
}
break; break;
case Key.S: case Key.S:
@ -358,7 +367,11 @@ namespace v2rayN.Desktop.Views
{ {
return; return;
} }
await ViewModel?.ScanImageResult(fileName);
if (ViewModel != null)
{
await ViewModel.ScanImageResult(fileName);
}
} }
private void MenuCheckUpdate_Click(object? sender, RoutedEventArgs e) private void MenuCheckUpdate_Click(object? sender, RoutedEventArgs e)
@ -382,8 +395,10 @@ namespace v2rayN.Desktop.Views
_blCloseByUser = true; _blCloseByUser = true;
StorageUI(); StorageUI();
if (ViewModel != null)
await ViewModel?.MyAppExitAsync(false); {
await ViewModel.MyAppExitAsync(false);
}
} }
#endregion Event #endregion Event

View file

@ -248,7 +248,10 @@ namespace v2rayN.Desktop.Views
private void ClbdestOverride_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void ClbdestOverride_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {
ViewModel.destOverride = clbdestOverride.SelectedItems.Cast<string>().ToList(); if (ViewModel != null)
{
ViewModel.destOverride = clbdestOverride.SelectedItems.Cast<string>().ToList();
}
} }
} }
} }

View file

@ -102,11 +102,16 @@ namespace v2rayN.Desktop.Views
private async void LstProfiles_Sorting(object? sender, DataGridColumnEventArgs e) private async void LstProfiles_Sorting(object? sender, DataGridColumnEventArgs e)
{ {
e.Handled = true; e.Handled = true;
await ViewModel?.SortServer(e.Column.Tag.ToString());
if (ViewModel != null && e.Column?.Tag?.ToString() != null)
{
await ViewModel.SortServer(e.Column.Tag.ToString());
}
e.Handled = false; e.Handled = false;
} }
//#region Event #region Event
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj) private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
{ {
@ -179,7 +184,9 @@ namespace v2rayN.Desktop.Views
case EViewAction.DispatcherRefreshServersBiz: case EViewAction.DispatcherRefreshServersBiz:
Dispatcher.UIThread.Post(() => Dispatcher.UIThread.Post(() =>
ViewModel?.RefreshServersBiz(), {
_ = RefreshServersBiz();
},
DispatcherPriority.Default); DispatcherPriority.Default);
break; break;
} }
@ -198,9 +205,25 @@ namespace v2rayN.Desktop.Views
await DialogHost.Show(dialog); await DialogHost.Show(dialog);
} }
public async Task RefreshServersBiz()
{
if (ViewModel != null)
{
await ViewModel.RefreshServersBiz();
}
if (lstProfiles.SelectedIndex > 0)
{
lstProfiles.ScrollIntoView(lstProfiles.SelectedItem, null);
}
}
private void lstProfiles_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void lstProfiles_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {
ViewModel.SelectedProfiles = lstProfiles.SelectedItems.Cast<ProfileItemModel>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedProfiles = lstProfiles.SelectedItems.Cast<ProfileItemModel>().ToList();
}
} }
private void LstProfiles_DoubleTapped(object? sender, Avalonia.Input.TappedEventArgs e) private void LstProfiles_DoubleTapped(object? sender, Avalonia.Input.TappedEventArgs e)
@ -333,9 +356,9 @@ namespace v2rayN.Desktop.Views
} }
} }
//#endregion Event #endregion Event
//#region UI #region UI
private void RestoreUI() private void RestoreUI()
{ {
@ -388,9 +411,9 @@ namespace v2rayN.Desktop.Views
_config.UiItem.MainColumnItem = lvColumnItem; _config.UiItem.MainColumnItem = lvColumnItem;
} }
//#endregion UI #endregion UI
//#region Drag and Drop #region Drag and Drop
//private Point startPoint = new(); //private Point startPoint = new();
//private int startIndex = -1; //private int startIndex = -1;
@ -480,6 +503,6 @@ namespace v2rayN.Desktop.Views
// } // }
//} //}
//#endregion Drag and Drop #endregion Drag and Drop
} }
} }

View file

@ -85,12 +85,18 @@ namespace v2rayN.Desktop.Views
private void ClbProtocol_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void ClbProtocol_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {
ViewModel.ProtocolItems = clbProtocol.SelectedItems.Cast<string>().ToList(); if (ViewModel != null)
{
ViewModel.ProtocolItems = clbProtocol.SelectedItems.Cast<string>().ToList();
}
} }
private void ClbInboundTag_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void ClbInboundTag_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {
ViewModel.InboundTagItems = clbInboundTag.SelectedItems.Cast<string>().ToList(); if (ViewModel != null)
{
ViewModel.InboundTagItems = clbInboundTag.SelectedItems.Cast<string>().ToList();
}
} }
private void linkRuleobjectDoc_Click(object? sender, RoutedEventArgs e) private void linkRuleobjectDoc_Click(object? sender, RoutedEventArgs e)

View file

@ -167,7 +167,10 @@ namespace v2rayN.Desktop.Views
private void lstRules_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void lstRules_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {
ViewModel.SelectedSources = lstRules.SelectedItems.Cast<RulesItemModel>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedSources = lstRules.SelectedItems.Cast<RulesItemModel>().ToList();
}
} }
private void LstRules_DoubleTapped(object? sender, Avalonia.Input.TappedEventArgs e) private void LstRules_DoubleTapped(object? sender, Avalonia.Input.TappedEventArgs e)

View file

@ -108,7 +108,10 @@ namespace v2rayN.Desktop.Views
private void lstRoutings_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void lstRoutings_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {
ViewModel.SelectedSources = lstRoutings.SelectedItems.Cast<RoutingItemModel>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedSources = lstRoutings.SelectedItems.Cast<RoutingItemModel>().ToList();
}
} }
private void LstRoutings_DoubleTapped(object? sender, TappedEventArgs e) private void LstRoutings_DoubleTapped(object? sender, TappedEventArgs e)

View file

@ -84,7 +84,10 @@ namespace v2rayN.Desktop.Views
private void LstSubscription_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void LstSubscription_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {
ViewModel.SelectedSources = lstSubscription.SelectedItems.Cast<SubItem>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedSources = lstSubscription.SelectedItems.Cast<SubItem>().ToList();
}
} }
private void menuClose_Click(object? sender, RoutedEventArgs e) private void menuClose_Click(object? sender, RoutedEventArgs e)

View file

@ -259,7 +259,10 @@ namespace v2rayN.Views
private void ClbdestOverride_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private void ClbdestOverride_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
ViewModel.destOverride = clbdestOverride.SelectedItems.Cast<string>().ToList(); if (ViewModel != null)
{
ViewModel.destOverride = clbdestOverride.SelectedItems.Cast<string>().ToList();
}
} }
} }
} }

View file

@ -166,7 +166,7 @@ namespace v2rayN.Views
case EViewAction.DispatcherRefreshServersBiz: case EViewAction.DispatcherRefreshServersBiz:
Application.Current?.Dispatcher.Invoke((() => Application.Current?.Dispatcher.Invoke((() =>
{ {
ViewModel?.RefreshServersBiz(); _ = RefreshServersBiz();
}), DispatcherPriority.Normal); }), DispatcherPriority.Normal);
break; break;
} }
@ -186,9 +186,25 @@ namespace v2rayN.Views
await DialogHost.Show(dialog, "RootDialog"); await DialogHost.Show(dialog, "RootDialog");
} }
public async Task RefreshServersBiz()
{
if (ViewModel != null)
{
await ViewModel.RefreshServersBiz();
}
if (lstProfiles.SelectedIndex > 0)
{
lstProfiles.ScrollIntoView(lstProfiles.SelectedItem, null);
}
}
private void lstProfiles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private void lstProfiles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
ViewModel.SelectedProfiles = lstProfiles.SelectedItems.Cast<ProfileItemModel>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedProfiles = lstProfiles.SelectedItems.Cast<ProfileItemModel>().ToList();
}
} }
private void LstProfiles_LoadingRow(object? sender, DataGridRowEventArgs e) private void LstProfiles_LoadingRow(object? sender, DataGridRowEventArgs e)

View file

@ -79,12 +79,18 @@ namespace v2rayN.Views
private void ClbProtocol_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private void ClbProtocol_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
ViewModel.ProtocolItems = clbProtocol.SelectedItems.Cast<string>().ToList(); if (ViewModel != null)
{
ViewModel.ProtocolItems = clbProtocol.SelectedItems.Cast<string>().ToList();
}
} }
private void ClbInboundTag_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private void ClbInboundTag_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
ViewModel.InboundTagItems = clbInboundTag.SelectedItems.Cast<string>().ToList(); if (ViewModel != null)
{
ViewModel.InboundTagItems = clbInboundTag.SelectedItems.Cast<string>().ToList();
}
} }
private void linkRuleobjectDoc_Click(object sender, RoutedEventArgs e) private void linkRuleobjectDoc_Click(object sender, RoutedEventArgs e)

View file

@ -162,7 +162,10 @@ namespace v2rayN.Views
private void lstRules_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private void lstRules_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
ViewModel.SelectedSources = lstRules.SelectedItems.Cast<RulesItemModel>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedSources = lstRules.SelectedItems.Cast<RulesItemModel>().ToList();
}
} }
private void LstRules_MouseDoubleClick(object sender, MouseButtonEventArgs e) private void LstRules_MouseDoubleClick(object sender, MouseButtonEventArgs e)

View file

@ -113,7 +113,10 @@ namespace v2rayN.Views
private void lstRoutings_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private void lstRoutings_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
ViewModel.SelectedSources = lstRoutings.SelectedItems.Cast<RoutingItemModel>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedSources = lstRoutings.SelectedItems.Cast<RoutingItemModel>().ToList();
}
} }
private void LstRoutings_MouseDoubleClick(object sender, MouseButtonEventArgs e) private void LstRoutings_MouseDoubleClick(object sender, MouseButtonEventArgs e)

View file

@ -94,7 +94,10 @@ namespace v2rayN.Views
private void LstSubscription_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) private void LstSubscription_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{ {
ViewModel.SelectedSources = lstSubscription.SelectedItems.Cast<SubItem>().ToList(); if (ViewModel != null)
{
ViewModel.SelectedSources = lstSubscription.SelectedItems.Cast<SubItem>().ToList();
}
} }
private void menuClose_Click(object sender, System.Windows.RoutedEventArgs e) private void menuClose_Click(object sender, System.Windows.RoutedEventArgs e)