Refactor UpdateService to use instance config and callbacks
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release Linux / rpm (push) Blocked by required conditions
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

This commit is contained in:
2dust 2025-11-02 21:04:23 +08:00
parent ed2c77062e
commit 091b79f7cf
6 changed files with 45 additions and 47 deletions

View file

@ -9,7 +9,7 @@ public class Utils
{
private static readonly string _tag = "Utils";
#region
#region Conversion Functions
/// <summary>
/// Convert to comma-separated string
@ -462,9 +462,9 @@ public class Utils
return (domain, port);
}
#endregion
#endregion Conversion Functions
#region
#region Data Checks
/// <summary>
/// Determine if the input is a number
@ -586,9 +586,9 @@ public class Utils
return false;
}
#endregion
#endregion Data Checks
#region
#region Speed Test
private static bool PortInUse(int port)
{
@ -641,9 +641,9 @@ public class Utils
return 59090;
}
#endregion
#endregion Speed Test
#region
#region Miscellaneous
public static bool UpgradeAppExists(out string upgradeFileName)
{
@ -793,7 +793,7 @@ public class Utils
return null;
}
#endregion
#endregion Miscellaneous
#region TempPath

View file

@ -111,11 +111,10 @@ public class TaskManager
{
Logging.SaveLog("Execute update geo files");
var updateHandle = new UpdateService();
await updateHandle.UpdateGeoFileAll(_config, async (success, msg) =>
await new UpdateService(_config, async (success, msg) =>
{
await _updateFunc?.Invoke(false, msg);
});
}).UpdateGeoFileAll();
}
}
}

View file

@ -2,13 +2,19 @@ namespace ServiceLib.Services;
public class UpdateService
{
private Func<bool, string, Task>? _updateFunc;
private readonly Config? _config;
private readonly Func<bool, string, Task>? _updateFunc;
private readonly int _timeout = 30;
private static readonly string _tag = "UpdateService";
public async Task CheckUpdateGuiN(Config config, Func<bool, string, Task> updateFunc, bool preRelease)
public UpdateService(Config config, Func<bool, string, Task> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
}
public async Task CheckUpdateGuiN(bool preRelease)
{
var url = string.Empty;
var fileName = string.Empty;
@ -47,9 +53,8 @@ public class UpdateService
}
}
public async Task CheckUpdateCore(ECoreType type, Config config, Func<bool, string, Task> updateFunc, bool preRelease)
public async Task CheckUpdateCore(ECoreType type, bool preRelease)
{
_updateFunc = updateFunc;
var url = string.Empty;
var fileName = string.Empty;
@ -101,11 +106,11 @@ public class UpdateService
}
}
public async Task UpdateGeoFileAll(Config config, Func<bool, string, Task> updateFunc)
public async Task UpdateGeoFileAll()
{
await UpdateGeoFiles(config, updateFunc);
await UpdateOtherFiles(config, updateFunc);
await UpdateSrsFileAll(config, updateFunc);
await UpdateGeoFiles();
await UpdateOtherFiles();
await UpdateSrsFileAll();
await UpdateFunc(true, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, "geo"));
}
@ -330,13 +335,11 @@ public class UpdateService
#region Geo private
private async Task UpdateGeoFiles(Config config, Func<bool, string, Task> updateFunc)
private async Task UpdateGeoFiles()
{
_updateFunc = updateFunc;
var geoUrl = string.IsNullOrEmpty(config?.ConstItem.GeoSourceUrl)
var geoUrl = string.IsNullOrEmpty(_config?.ConstItem.GeoSourceUrl)
? Global.GeoUrl
: config.ConstItem.GeoSourceUrl;
: _config.ConstItem.GeoSourceUrl;
List<string> files = ["geosite", "geoip"];
foreach (var geoName in files)
@ -345,33 +348,29 @@ public class UpdateService
var targetPath = Utils.GetBinPath($"{fileName}");
var url = string.Format(geoUrl, geoName);
await DownloadGeoFile(url, fileName, targetPath, updateFunc);
await DownloadGeoFile(url, fileName, targetPath);
}
}
private async Task UpdateOtherFiles(Config config, Func<bool, string, Task> updateFunc)
private async Task UpdateOtherFiles()
{
//If it is not in China area, no update is required
if (config.ConstItem.GeoSourceUrl.IsNotEmpty())
if (_config.ConstItem.GeoSourceUrl.IsNotEmpty())
{
return;
}
_updateFunc = updateFunc;
foreach (var url in Global.OtherGeoUrls)
{
var fileName = Path.GetFileName(url);
var targetPath = Utils.GetBinPath($"{fileName}");
await DownloadGeoFile(url, fileName, targetPath, updateFunc);
await DownloadGeoFile(url, fileName, targetPath);
}
}
private async Task UpdateSrsFileAll(Config config, Func<bool, string, Task> updateFunc)
private async Task UpdateSrsFileAll()
{
_updateFunc = updateFunc;
var geoipFiles = new List<string>();
var geoSiteFiles = new List<string>();
@ -414,29 +413,29 @@ public class UpdateService
}
foreach (var item in geoipFiles.Distinct())
{
await UpdateSrsFile("geoip", item, config, updateFunc);
await UpdateSrsFile("geoip", item);
}
foreach (var item in geoSiteFiles.Distinct())
{
await UpdateSrsFile("geosite", item, config, updateFunc);
await UpdateSrsFile("geosite", item);
}
}
private async Task UpdateSrsFile(string type, string srsName, Config config, Func<bool, string, Task> updateFunc)
private async Task UpdateSrsFile(string type, string srsName)
{
var srsUrl = string.IsNullOrEmpty(config.ConstItem.SrsSourceUrl)
var srsUrl = string.IsNullOrEmpty(_config.ConstItem.SrsSourceUrl)
? Global.SingboxRulesetUrl
: config.ConstItem.SrsSourceUrl;
: _config.ConstItem.SrsSourceUrl;
var fileName = $"{type}-{srsName}.srs";
var targetPath = Path.Combine(Utils.GetBinPath("srss"), fileName);
var url = string.Format(srsUrl, type, $"{type}-{srsName}", srsName);
await DownloadGeoFile(url, fileName, targetPath, updateFunc);
await DownloadGeoFile(url, fileName, targetPath);
}
private async Task DownloadGeoFile(string url, string fileName, string targetPath, Func<bool, string, Task> updateFunc)
private async Task DownloadGeoFile(string url, string fileName, string targetPath)
{
var tmpFileName = Utils.GetTempPath(Utils.GetGuid());

View file

@ -148,7 +148,7 @@ public class CheckUpdateViewModel : MyReactiveObject
UpdatedPlusPlus(_geo, "");
}
}
await new UpdateService().UpdateGeoFileAll(_config, _updateUI)
await new UpdateService(_config, _updateUI).UpdateGeoFileAll()
.ContinueWith(t => UpdatedPlusPlus(_geo, ""));
}
@ -163,7 +163,7 @@ public class CheckUpdateViewModel : MyReactiveObject
UpdatedPlusPlus(_v2rayN, msg);
}
}
await new UpdateService().CheckUpdateGuiN(_config, _updateUI, preRelease)
await new UpdateService(_config, _updateUI).CheckUpdateGuiN(preRelease)
.ContinueWith(t => UpdatedPlusPlus(_v2rayN, ""));
}
@ -180,7 +180,7 @@ public class CheckUpdateViewModel : MyReactiveObject
}
}
var type = (ECoreType)Enum.Parse(typeof(ECoreType), model.CoreType);
await new UpdateService().CheckUpdateCore(type, _config, _updateUI, preRelease)
await new UpdateService(_config, _updateUI).CheckUpdateCore(type, preRelease)
.ContinueWith(t => UpdatedPlusPlus(model.CoreType, ""));
}

View file

@ -596,7 +596,7 @@ public class MainWindowViewModel : MyReactiveObject
AppEvents.RoutingsMenuRefreshRequested.Publish();
await ConfigHandler.SaveConfig(_config);
await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler);
await new UpdateService(_config, UpdateTaskHandler).UpdateGeoFileAll();
await Reload();
}

View file

@ -90,7 +90,7 @@ public partial class ProfilesSelectWindow : WindowBase<ProfilesSelectViewModel>
private void LstProfiles_DoubleTapped(object? sender, TappedEventArgs e)
{
// 忽略表头区域的双击
// Ignore double-taps on the column header area
if (e.Source is Control src)
{
if (src.FindAncestorOfType<DataGridColumnHeader>() != null)
@ -99,7 +99,7 @@ public partial class ProfilesSelectWindow : WindowBase<ProfilesSelectViewModel>
return;
}
// 仅当在数据行或其子元素上双击时才触发选择
// Only trigger selection when double-tapped on a data row or its child element
if (src.FindAncestorOfType<DataGridRow>() != null)
{
ViewModel?.SelectFinish();
@ -110,7 +110,7 @@ public partial class ProfilesSelectWindow : WindowBase<ProfilesSelectViewModel>
private void LstProfiles_Sorting(object? sender, DataGridColumnEventArgs e)
{
// 自定义排序,防止默认行为导致误触发
// Custom sort to prevent unintended default behavior
e.Handled = true;
if (ViewModel != null && e.Column?.Tag?.ToString() != null)
{