Skip to content

Commit 091b79f

Browse files
committed
Refactor UpdateService to use instance config and callbacks
1 parent ed2c770 commit 091b79f

File tree

6 files changed

+45
-47
lines changed

6 files changed

+45
-47
lines changed

v2rayN/ServiceLib/Common/Utils.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Utils
99
{
1010
private static readonly string _tag = "Utils";
1111

12-
#region 转换函数
12+
#region Conversion Functions
1313

1414
/// <summary>
1515
/// Convert to comma-separated string
@@ -462,9 +462,9 @@ private static (string domain, int port) ParseAuthority(string authority)
462462
return (domain, port);
463463
}
464464

465-
#endregion 转换函数
465+
#endregion Conversion Functions
466466

467-
#region 数据检查
467+
#region Data Checks
468468

469469
/// <summary>
470470
/// Determine if the input is a number
@@ -586,9 +586,9 @@ public static bool IsPrivateNetwork(string ip)
586586
return false;
587587
}
588588

589-
#endregion 数据检查
589+
#endregion Data Checks
590590

591-
#region 测速
591+
#region Speed Test
592592

593593
private static bool PortInUse(int port)
594594
{
@@ -641,9 +641,9 @@ public static int GetFreePort(int defaultPort = 0)
641641
return 59090;
642642
}
643643

644-
#endregion 测速
644+
#endregion Speed Test
645645

646-
#region 杂项
646+
#region Miscellaneous
647647

648648
public static bool UpgradeAppExists(out string upgradeFileName)
649649
{
@@ -793,7 +793,7 @@ public static Dictionary<string, string> GetSystemHosts()
793793
return null;
794794
}
795795

796-
#endregion 杂项
796+
#endregion Miscellaneous
797797

798798
#region TempPath
799799

v2rayN/ServiceLib/Manager/TaskManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,10 @@ private async Task UpdateTaskRunGeo(int hours)
111111
{
112112
Logging.SaveLog("Execute update geo files");
113113

114-
var updateHandle = new UpdateService();
115-
await updateHandle.UpdateGeoFileAll(_config, async (success, msg) =>
114+
await new UpdateService(_config, async (success, msg) =>
116115
{
117116
await _updateFunc?.Invoke(false, msg);
118-
});
117+
}).UpdateGeoFileAll();
119118
}
120119
}
121120
}

v2rayN/ServiceLib/Services/UpdateService.cs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ namespace ServiceLib.Services;
22

33
public class UpdateService
44
{
5-
private Func<bool, string, Task>? _updateFunc;
5+
private readonly Config? _config;
6+
private readonly Func<bool, string, Task>? _updateFunc;
67
private readonly int _timeout = 30;
78
private static readonly string _tag = "UpdateService";
89

9-
public async Task CheckUpdateGuiN(Config config, Func<bool, string, Task> updateFunc, bool preRelease)
10+
public UpdateService(Config config, Func<bool, string, Task> updateFunc)
1011
{
12+
_config = config;
1113
_updateFunc = updateFunc;
14+
}
15+
16+
public async Task CheckUpdateGuiN(bool preRelease)
17+
{
1218
var url = string.Empty;
1319
var fileName = string.Empty;
1420

@@ -47,9 +53,8 @@ public async Task CheckUpdateGuiN(Config config, Func<bool, string, Task> update
4753
}
4854
}
4955

50-
public async Task CheckUpdateCore(ECoreType type, Config config, Func<bool, string, Task> updateFunc, bool preRelease)
56+
public async Task CheckUpdateCore(ECoreType type, bool preRelease)
5157
{
52-
_updateFunc = updateFunc;
5358
var url = string.Empty;
5459
var fileName = string.Empty;
5560

@@ -101,11 +106,11 @@ public async Task CheckUpdateCore(ECoreType type, Config config, Func<bool, stri
101106
}
102107
}
103108

104-
public async Task UpdateGeoFileAll(Config config, Func<bool, string, Task> updateFunc)
109+
public async Task UpdateGeoFileAll()
105110
{
106-
await UpdateGeoFiles(config, updateFunc);
107-
await UpdateOtherFiles(config, updateFunc);
108-
await UpdateSrsFileAll(config, updateFunc);
111+
await UpdateGeoFiles();
112+
await UpdateOtherFiles();
113+
await UpdateSrsFileAll();
109114
await UpdateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
110115
}
111116

@@ -330,13 +335,11 @@ private async Task<RetResult> ParseDownloadUrl(ECoreType type, SemanticVersion v
330335

331336
#region Geo private
332337

333-
private async Task UpdateGeoFiles(Config config, Func<bool, string, Task> updateFunc)
338+
private async Task UpdateGeoFiles()
334339
{
335-
_updateFunc = updateFunc;
336-
337-
var geoUrl = string.IsNullOrEmpty(config?.ConstItem.GeoSourceUrl)
340+
var geoUrl = string.IsNullOrEmpty(_config?.ConstItem.GeoSourceUrl)
338341
? Global.GeoUrl
339-
: config.ConstItem.GeoSourceUrl;
342+
: _config.ConstItem.GeoSourceUrl;
340343

341344
List<string> files = ["geosite", "geoip"];
342345
foreach (var geoName in files)
@@ -345,33 +348,29 @@ private async Task UpdateGeoFiles(Config config, Func<bool, string, Task> update
345348
var targetPath = Utils.GetBinPath($"{fileName}");
346349
var url = string.Format(geoUrl, geoName);
347350

348-
await DownloadGeoFile(url, fileName, targetPath, updateFunc);
351+
await DownloadGeoFile(url, fileName, targetPath);
349352
}
350353
}
351354

352-
private async Task UpdateOtherFiles(Config config, Func<bool, string, Task> updateFunc)
355+
private async Task UpdateOtherFiles()
353356
{
354357
//If it is not in China area, no update is required
355-
if (config.ConstItem.GeoSourceUrl.IsNotEmpty())
358+
if (_config.ConstItem.GeoSourceUrl.IsNotEmpty())
356359
{
357360
return;
358361
}
359362

360-
_updateFunc = updateFunc;
361-
362363
foreach (var url in Global.OtherGeoUrls)
363364
{
364365
var fileName = Path.GetFileName(url);
365366
var targetPath = Utils.GetBinPath($"{fileName}");
366367

367-
await DownloadGeoFile(url, fileName, targetPath, updateFunc);
368+
await DownloadGeoFile(url, fileName, targetPath);
368369
}
369370
}
370371

371-
private async Task UpdateSrsFileAll(Config config, Func<bool, string, Task> updateFunc)
372+
private async Task UpdateSrsFileAll()
372373
{
373-
_updateFunc = updateFunc;
374-
375374
var geoipFiles = new List<string>();
376375
var geoSiteFiles = new List<string>();
377376

@@ -414,29 +413,29 @@ private async Task UpdateSrsFileAll(Config config, Func<bool, string, Task> upda
414413
}
415414
foreach (var item in geoipFiles.Distinct())
416415
{
417-
await UpdateSrsFile("geoip", item, config, updateFunc);
416+
await UpdateSrsFile("geoip", item);
418417
}
419418

420419
foreach (var item in geoSiteFiles.Distinct())
421420
{
422-
await UpdateSrsFile("geosite", item, config, updateFunc);
421+
await UpdateSrsFile("geosite", item);
423422
}
424423
}
425424

426-
private async Task UpdateSrsFile(string type, string srsName, Config config, Func<bool, string, Task> updateFunc)
425+
private async Task UpdateSrsFile(string type, string srsName)
427426
{
428-
var srsUrl = string.IsNullOrEmpty(config.ConstItem.SrsSourceUrl)
427+
var srsUrl = string.IsNullOrEmpty(_config.ConstItem.SrsSourceUrl)
429428
? Global.SingboxRulesetUrl
430-
: config.ConstItem.SrsSourceUrl;
429+
: _config.ConstItem.SrsSourceUrl;
431430

432431
var fileName = $"{type}-{srsName}.srs";
433432
var targetPath = Path.Combine(Utils.GetBinPath("srss"), fileName);
434433
var url = string.Format(srsUrl, type, $"{type}-{srsName}", srsName);
435434

436-
await DownloadGeoFile(url, fileName, targetPath, updateFunc);
435+
await DownloadGeoFile(url, fileName, targetPath);
437436
}
438437

439-
private async Task DownloadGeoFile(string url, string fileName, string targetPath, Func<bool, string, Task> updateFunc)
438+
private async Task DownloadGeoFile(string url, string fileName, string targetPath)
440439
{
441440
var tmpFileName = Utils.GetTempPath(Utils.GetGuid());
442441

v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ async Task _updateUI(bool success, string msg)
148148
UpdatedPlusPlus(_geo, "");
149149
}
150150
}
151-
await new UpdateService().UpdateGeoFileAll(_config, _updateUI)
151+
await new UpdateService(_config, _updateUI).UpdateGeoFileAll()
152152
.ContinueWith(t => UpdatedPlusPlus(_geo, ""));
153153
}
154154

@@ -163,7 +163,7 @@ async Task _updateUI(bool success, string msg)
163163
UpdatedPlusPlus(_v2rayN, msg);
164164
}
165165
}
166-
await new UpdateService().CheckUpdateGuiN(_config, _updateUI, preRelease)
166+
await new UpdateService(_config, _updateUI).CheckUpdateGuiN(preRelease)
167167
.ContinueWith(t => UpdatedPlusPlus(_v2rayN, ""));
168168
}
169169

@@ -180,7 +180,7 @@ async Task _updateUI(bool success, string msg)
180180
}
181181
}
182182
var type = (ECoreType)Enum.Parse(typeof(ECoreType), model.CoreType);
183-
await new UpdateService().CheckUpdateCore(type, _config, _updateUI, preRelease)
183+
await new UpdateService(_config, _updateUI).CheckUpdateCore(type, preRelease)
184184
.ContinueWith(t => UpdatedPlusPlus(model.CoreType, ""));
185185
}
186186

v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ public async Task ApplyRegionalPreset(EPresetType type)
596596
AppEvents.RoutingsMenuRefreshRequested.Publish();
597597

598598
await ConfigHandler.SaveConfig(_config);
599-
await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler);
599+
await new UpdateService(_config, UpdateTaskHandler).UpdateGeoFileAll();
600600
await Reload();
601601
}
602602

v2rayN/v2rayN.Desktop/Views/ProfilesSelectWindow.axaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private void LstProfiles_LoadingRow(object? sender, DataGridRowEventArgs e)
9090

9191
private void LstProfiles_DoubleTapped(object? sender, TappedEventArgs e)
9292
{
93-
// 忽略表头区域的双击
93+
// Ignore double-taps on the column header area
9494
if (e.Source is Control src)
9595
{
9696
if (src.FindAncestorOfType<DataGridColumnHeader>() != null)
@@ -99,7 +99,7 @@ private void LstProfiles_DoubleTapped(object? sender, TappedEventArgs e)
9999
return;
100100
}
101101

102-
// 仅当在数据行或其子元素上双击时才触发选择
102+
// Only trigger selection when double-tapped on a data row or its child element
103103
if (src.FindAncestorOfType<DataGridRow>() != null)
104104
{
105105
ViewModel?.SelectFinish();
@@ -110,7 +110,7 @@ private void LstProfiles_DoubleTapped(object? sender, TappedEventArgs e)
110110

111111
private void LstProfiles_Sorting(object? sender, DataGridColumnEventArgs e)
112112
{
113-
// 自定义排序,防止默认行为导致误触发
113+
// Custom sort to prevent unintended default behavior
114114
e.Handled = true;
115115
if (ViewModel != null && e.Column?.Tag?.ToString() != null)
116116
{

0 commit comments

Comments
 (0)