Move DefaultServer function to ConfigTextHandler

This commit is contained in:
2dust
2026-11-15 14:56:32 +08:00
parent 33fa9865e6
commit a6624e0370
7 changed files with 78 additions and 80 deletions
@@ -187,73 +187,6 @@ public static class ConfigDbHandler
return 0; return 0;
} }
/// <summary>
/// Set the default server by its index ID
/// Updates the configuration to use the specified server as default
/// </summary>
/// <param name="config">Current configuration</param>
/// <param name="indexId">Index ID of the server to set as default</param>
/// <returns>0 if successful, -1 if failed</returns>
public static async Task<int> SetDefaultServerIndex(Config config, string? indexId)
{
if (indexId.IsNullOrEmpty())
{
return -1;
}
config.IndexId = indexId;
await SaveConfig();
return 0;
}
/// <summary>
/// Set a default server from the provided list of profiles
/// Ensures there's always a valid default server selected
/// </summary>
/// <param name="config">Current configuration</param>
/// <param name="lstProfile">List of profile models to choose from</param>
/// <returns>Result of SetDefaultServerIndex operation</returns>
public static async Task<int> SetDefaultServer(Config config, List<ProfileItemModel> lstProfile)
{
if (lstProfile.Exists(t => t.IndexId == config.IndexId))
{
return 0;
}
if (await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(t => t.IndexId == config.IndexId) != null)
{
return 0;
}
if (lstProfile.Count > 0)
{
return await SetDefaultServerIndex(config, lstProfile.FirstOrDefault(t => t.Port > 0)?.IndexId);
}
var item = await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(t => t.Port > 0);
return await SetDefaultServerIndex(config, item?.IndexId);
}
/// <summary>
/// Get the current default server profile
/// If the current default is invalid, selects a new default
/// </summary>
/// <param name="config">Current configuration</param>
/// <returns>The default profile item or null if none exists</returns>
public static async Task<ProfileItem?> GetDefaultServer(Config config)
{
var item = await ConfigDbQuery.GetProfileItem(config.IndexId);
if (item is null)
{
var item2 = await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync();
await SetDefaultServerIndex(config, item2?.IndexId);
return item2;
}
return item;
}
/// <summary> /// <summary>
/// Move a server in the list to a different position /// Move a server in the list to a different position
/// Supports moving to top, up, down, bottom or specific position /// Supports moving to top, up, down, bottom or specific position
@@ -1415,7 +1348,6 @@ public static class ConfigDbHandler
return result; return result;
} }
/// <summary> /// <summary>
/// Remove servers with invalid test results (timeout) /// Remove servers with invalid test results (timeout)
/// Useful for cleaning up subscription lists /// Useful for cleaning up subscription lists
@@ -1971,7 +1903,7 @@ public static class ConfigDbHandler
var existItem = FindMatchedProfileItem(lstSub, activeProfile); var existItem = FindMatchedProfileItem(lstSub, activeProfile);
if (existItem != null) if (existItem != null)
{ {
await ConfigDbHandler.SetDefaultServerIndex(config, existItem.IndexId); await ConfigTextHandler.SetDefaultServerIndex(existItem.IndexId);
} }
} }
@@ -303,4 +303,72 @@ public static class ConfigTextHandler
} }
#endregion UIItem #endregion UIItem
#region DefaultServer
/// <summary>
/// Set the default server by its index ID
/// Updates the configuration to use the specified server as default
/// </summary>
/// <param name="indexId">Index ID of the server to set as default</param>
/// <returns>0 if successful, -1 if failed</returns>
public static async Task<int> SetDefaultServerIndex(string? indexId)
{
if (indexId.IsNullOrEmpty())
{
return -1;
}
Config.IndexId = indexId;
await SaveConfig();
return 0;
}
/// <summary>
/// Set a default server from the provided list of profiles
/// Ensures there's always a valid default server selected
/// </summary>
/// <param name="lstProfile">List of profile models to choose from</param>
/// <returns>Result of SetDefaultServerIndex operation</returns>
public static async Task<int> SetDefaultServer(List<ProfileItemModel> lstProfile)
{
if (lstProfile.Exists(t => t.IndexId == Config.IndexId))
{
return 0;
}
if (await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(t => t.IndexId == Config.IndexId) != null)
{
return 0;
}
if (lstProfile.Count > 0)
{
return await SetDefaultServerIndex(lstProfile.FirstOrDefault(t => t.Port > 0)?.IndexId);
}
var item = await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(t => t.Port > 0);
return await SetDefaultServerIndex(item?.IndexId);
}
/// <summary>
/// Get the current default server profile
/// If the current default is invalid, selects a new default
/// </summary>
/// <returns>The default profile item or null if none exists</returns>
public static async Task<ProfileItem?> GetDefaultServer()
{
var item = await ConfigDbQuery.GetProfileItem(Config.IndexId);
if (item is null)
{
var item2 = await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync();
await SetDefaultServerIndex(item2?.IndexId);
return item2;
}
return item;
}
#endregion DefaultServer
} }
@@ -696,7 +696,7 @@ public partial class MainWindowViewModel : MyReactiveObject
TabMainSelectedIndex = 0; TabMainSelectedIndex = 0;
} }
}); });
var profileItem = await ConfigDbHandler.GetDefaultServer(_config); var profileItem = await ConfigTextHandler.GetDefaultServer();
if (profileItem == null) if (profileItem == null)
{ {
NoticeManager.Instance.Enqueue(ResUI.CheckServerSettings); NoticeManager.Instance.Enqueue(ResUI.CheckServerSettings);
@@ -1,5 +1,3 @@
namespace ServiceLib.ViewModels; namespace ServiceLib.ViewModels;
public partial class ProfilesViewModel : MyReactiveObject public partial class ProfilesViewModel : MyReactiveObject
@@ -401,7 +399,7 @@ public partial class ProfilesViewModel : MyReactiveObject
{ {
var lstModel = await ConfigDbQuery.ProfileModels(_config.SubIndexId, filter); var lstModel = await ConfigDbQuery.ProfileModels(_config.SubIndexId, filter);
await ConfigDbHandler.SetDefaultServer(_config, lstModel); await ConfigTextHandler.SetDefaultServer(lstModel);
var lstServerStat = (_config.GuiItem.EnableStatistics ? StatisticsManager.Instance.ServerStat : null) ?? []; var lstServerStat = (_config.GuiItem.EnableStatistics ? StatisticsManager.Instance.ServerStat : null) ?? [];
var lstProfileExs = await ProfileExManager.Instance.GetProfileExs(); var lstProfileExs = await ProfileExManager.Instance.GetProfileExs();
@@ -585,7 +583,7 @@ public partial class ProfilesViewModel : MyReactiveObject
return; return;
} }
if (await ConfigDbHandler.SetDefaultServerIndex(_config, indexId) == 0) if (await ConfigTextHandler.SetDefaultServerIndex(indexId) == 0)
{ {
await RefreshServers(); await RefreshServers();
Reload(); Reload();
@@ -267,7 +267,7 @@ public partial class StatusBarViewModel : MyReactiveObject
await RefreshServersMenu(); await RefreshServersMenu();
//display running server //display running server
var running = await ConfigDbHandler.GetDefaultServer(_config); var running = await ConfigTextHandler.GetDefaultServer();
if (running != null) if (running != null)
{ {
RunningServerDisplay = running.GetSummary(); RunningServerDisplay = running.GetSummary();
@@ -319,7 +319,7 @@ public partial class StatusBarViewModel : MyReactiveObject
public async Task<AvailabilityCheckResult?> TestServerAvailability() public async Task<AvailabilityCheckResult?> TestServerAvailability()
{ {
var item = await ConfigDbHandler.GetDefaultServer(_config); var item = await ConfigTextHandler.GetDefaultServer();
if (item == null) if (item == null)
{ {
return null; return null;
@@ -269,7 +269,7 @@ public sealed partial class V2rayRuntime
} }
if (removesCurrent) if (removesCurrent)
{ {
_ = await ConfigDbHandler.GetDefaultServer(Config); _ = await ConfigTextHandler.GetDefaultServer();
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()); await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());
} }
}); });
@@ -268,7 +268,7 @@ public sealed partial class V2rayRuntime(
Config.SubIndexId = normalizedSubIndexId; Config.SubIndexId = normalizedSubIndexId;
} }
var selectedProfileId = Config.IndexId; var selectedProfileId = Config.IndexId;
var defaultProfile = await ConfigDbHandler.GetDefaultServer(Config); var defaultProfile = await ConfigTextHandler.GetDefaultServer();
await _mutations.RunAsync(() => EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig())); await _mutations.RunAsync(() => EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()));
await ProfileExManager.Instance.Init(); await ProfileExManager.Instance.Init();
await CertPemManager.Instance.Init(Config); await CertPemManager.Instance.Init(Config);
@@ -502,7 +502,7 @@ public sealed partial class V2rayRuntime(
} }
if (removesCurrentProfile) if (removesCurrentProfile)
{ {
_ = await ConfigDbHandler.GetDefaultServer(Config); _ = await ConfigTextHandler.GetDefaultServer();
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()); await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());
} }
}); });
@@ -789,7 +789,7 @@ public sealed partial class V2rayRuntime(
private async Task<ProfileItem?> GetDefaultProfileAsync() => await _mutations.RunAsync(async () => private async Task<ProfileItem?> GetDefaultProfileAsync() => await _mutations.RunAsync(async () =>
{ {
var selected = await ConfigDbHandler.GetDefaultServer(Config); var selected = await ConfigTextHandler.GetDefaultServer();
if (selected is not null) if (selected is not null)
{ {
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()); await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());