Improve and optimize the active rules code in routing settings
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

This commit is contained in:
2dust 2025-07-06 18:15:07 +08:00
parent 1d01476523
commit da3d4c36a9
5 changed files with 32 additions and 13 deletions

View file

@ -1857,12 +1857,25 @@ public class ConfigHandler
/// <returns>0 if successful</returns>
public static async Task<int> SetDefaultRouting(Config config, RoutingItem routingItem)
{
if (await SQLiteHelper.Instance.TableAsync<RoutingItem>().Where(t => t.Id == routingItem.Id).CountAsync() > 0)
var items = await AppHandler.Instance.RoutingItems();
if (items.Any(t => t.Id == routingItem.Id && t.IsActive == true))
{
config.RoutingBasicItem.RoutingIndexId = routingItem.Id;
return -1;
}
await SaveConfig(config);
foreach (var item in items)
{
if (item.Id == routingItem.Id)
{
item.IsActive = true;
}
else
{
item.IsActive = false;
}
}
await SQLiteHelper.Instance.UpdateAllAsync(items);
return 0;
}
@ -1875,7 +1888,7 @@ public class ConfigHandler
/// <returns>The default routing item</returns>
public static async Task<RoutingItem> GetDefaultRouting(Config config)
{
var item = await AppHandler.Instance.GetRoutingItem(config.RoutingBasicItem.RoutingIndexId);
var item = await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.IsActive == true);
if (item is null)
{
var item2 = await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync();
@ -1983,6 +1996,18 @@ public class ConfigHandler
if (!blImportAdvancedRules && items.Count > 0)
{
//migrate
//TODO Temporary code to be removed later
if (config.RoutingBasicItem.RoutingIndexId.IsNotEmpty())
{
var item = items.FirstOrDefault(t => t.Id == config.RoutingBasicItem.RoutingIndexId);
if (item != null)
{
await SetDefaultRouting(config, item);
}
config.RoutingBasicItem.RoutingIndexId = string.Empty;
}
return 0;
}

View file

@ -19,4 +19,5 @@ public class RoutingItem
public string DomainStrategy { get; set; }
public string DomainStrategy4Singbox { get; set; }
public int Sort { get; set; }
public bool IsActive { get; set; }
}

View file

@ -3,5 +3,4 @@ namespace ServiceLib.Models;
[Serializable]
public class RoutingItemModel : RoutingItem
{
public bool IsActive { get; set; }
}

View file

@ -91,11 +91,9 @@ public class RoutingSettingViewModel : MyReactiveObject
var routings = await AppHandler.Instance.RoutingItems();
foreach (var item in routings)
{
var def = item.Id == _config.RoutingBasicItem.RoutingIndexId;
var it = new RoutingItemModel()
{
IsActive = def,
IsActive = item.IsActive,
RuleNum = item.RuleNum,
Id = item.Id,
Remarks = item.Remarks,

View file

@ -372,7 +372,7 @@ public class StatusBarViewModel : MyReactiveObject
foreach (var item in routings)
{
_routingItems.Add(item);
if (item.Id == _config.RoutingBasicItem.RoutingIndexId)
if (item.IsActive)
{
SelectedRouting = item;
}
@ -396,10 +396,6 @@ public class StatusBarViewModel : MyReactiveObject
{
return;
}
if (_config.RoutingBasicItem.RoutingIndexId == item.Id)
{
return;
}
if (await ConfigHandler.SetDefaultRouting(_config, item) == 0)
{