mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-15 17:05:03 +03:00
Move DefaultServer function to ConfigTextHandler
This commit is contained in:
@@ -187,73 +187,6 @@ public static class ConfigDbHandler
|
||||
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>
|
||||
/// Move a server in the list to a different position
|
||||
/// Supports moving to top, up, down, bottom or specific position
|
||||
@@ -1415,7 +1348,6 @@ public static class ConfigDbHandler
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Remove servers with invalid test results (timeout)
|
||||
/// Useful for cleaning up subscription lists
|
||||
@@ -1971,7 +1903,7 @@ public static class ConfigDbHandler
|
||||
var existItem = FindMatchedProfileItem(lstSub, activeProfile);
|
||||
if (existItem != null)
|
||||
{
|
||||
await ConfigDbHandler.SetDefaultServerIndex(config, existItem.IndexId);
|
||||
await ConfigTextHandler.SetDefaultServerIndex(existItem.IndexId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -303,4 +303,72 @@ public static class ConfigTextHandler
|
||||
}
|
||||
|
||||
#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;
|
||||
}
|
||||
});
|
||||
var profileItem = await ConfigDbHandler.GetDefaultServer(_config);
|
||||
var profileItem = await ConfigTextHandler.GetDefaultServer();
|
||||
if (profileItem == null)
|
||||
{
|
||||
NoticeManager.Instance.Enqueue(ResUI.CheckServerSettings);
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
namespace ServiceLib.ViewModels;
|
||||
|
||||
public partial class ProfilesViewModel : MyReactiveObject
|
||||
@@ -401,7 +399,7 @@ public partial class ProfilesViewModel : MyReactiveObject
|
||||
{
|
||||
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 lstProfileExs = await ProfileExManager.Instance.GetProfileExs();
|
||||
@@ -585,7 +583,7 @@ public partial class ProfilesViewModel : MyReactiveObject
|
||||
return;
|
||||
}
|
||||
|
||||
if (await ConfigDbHandler.SetDefaultServerIndex(_config, indexId) == 0)
|
||||
if (await ConfigTextHandler.SetDefaultServerIndex(indexId) == 0)
|
||||
{
|
||||
await RefreshServers();
|
||||
Reload();
|
||||
|
||||
@@ -267,7 +267,7 @@ public partial class StatusBarViewModel : MyReactiveObject
|
||||
await RefreshServersMenu();
|
||||
|
||||
//display running server
|
||||
var running = await ConfigDbHandler.GetDefaultServer(_config);
|
||||
var running = await ConfigTextHandler.GetDefaultServer();
|
||||
if (running != null)
|
||||
{
|
||||
RunningServerDisplay = running.GetSummary();
|
||||
@@ -319,7 +319,7 @@ public partial class StatusBarViewModel : MyReactiveObject
|
||||
|
||||
public async Task<AvailabilityCheckResult?> TestServerAvailability()
|
||||
{
|
||||
var item = await ConfigDbHandler.GetDefaultServer(_config);
|
||||
var item = await ConfigTextHandler.GetDefaultServer();
|
||||
if (item == null)
|
||||
{
|
||||
return null;
|
||||
|
||||
@@ -269,7 +269,7 @@ public sealed partial class V2rayRuntime
|
||||
}
|
||||
if (removesCurrent)
|
||||
{
|
||||
_ = await ConfigDbHandler.GetDefaultServer(Config);
|
||||
_ = await ConfigTextHandler.GetDefaultServer();
|
||||
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -268,7 +268,7 @@ public sealed partial class V2rayRuntime(
|
||||
Config.SubIndexId = normalizedSubIndexId;
|
||||
}
|
||||
var selectedProfileId = Config.IndexId;
|
||||
var defaultProfile = await ConfigDbHandler.GetDefaultServer(Config);
|
||||
var defaultProfile = await ConfigTextHandler.GetDefaultServer();
|
||||
await _mutations.RunAsync(() => EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()));
|
||||
await ProfileExManager.Instance.Init();
|
||||
await CertPemManager.Instance.Init(Config);
|
||||
@@ -502,7 +502,7 @@ public sealed partial class V2rayRuntime(
|
||||
}
|
||||
if (removesCurrentProfile)
|
||||
{
|
||||
_ = await ConfigDbHandler.GetDefaultServer(Config);
|
||||
_ = await ConfigTextHandler.GetDefaultServer();
|
||||
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());
|
||||
}
|
||||
});
|
||||
@@ -789,7 +789,7 @@ public sealed partial class V2rayRuntime(
|
||||
|
||||
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)
|
||||
{
|
||||
await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig());
|
||||
|
||||
Reference in New Issue
Block a user