Files
2dust_v2rayN/v2rayN/ServiceLib/ViewModels/FullConfigTemplateViewModel.cs
T
DHR60 9664ee380e ViewModel-First (#9621)
* Interaction

* Remove view action

* ViewModel-first

* MainGirdOrientation Hot Reload

* Fix avalonia preview

* Remove CloseWindowInteraction

* Fix

* Avoid threading issues

* Fix

* ProfilesSelect

* Try fix previewer

* Remove AppEvents.ProfilesRefreshRequested

* Remove AppEvents.SubscriptionsRefreshRequested

* Remove AppEvents.ProxiesReloadRequested

* Remove AppEvents.AdjustMainLvColWidthRequested

* Remove AppEvents.SetDefaultServerRequested

* Remove AppEvents.RoutingsMenuRefreshRequested

* Remove AppEvents.TestServerRequested

* Remove AppEvents.InboundDisplayRequested

* Remove AppEvents.SubscriptionsUpdateRequested

* Remove AppEvents.ShowHideWindowRequested

* Remove AppEvents.ReloadRequested

* Remove AppEvents.AddServerRequested

* Fix
2026-07-14 09:05:05 +08:00

127 lines
3.6 KiB
C#

namespace ServiceLib.ViewModels;
public class FullConfigTemplateViewModel : MyReactiveObject, ICloseable
{
public event EventHandler? RequestClose;
#region Reactive
[Reactive]
public bool EnableFullConfigTemplate4Ray { get; set; }
[Reactive]
public bool EnableFullConfigTemplate4Singbox { get; set; }
[Reactive]
public string FullConfigTemplate4Ray { get; set; } = string.Empty;
[Reactive]
public string FullTunConfigTemplate4Ray { get; set; } = string.Empty;
[Reactive]
public string FullConfigTemplate4Singbox { get; set; } = string.Empty;
[Reactive]
public string FullTunConfigTemplate4Singbox { get; set; } = string.Empty;
[Reactive]
public bool AddProxyOnly4Ray { get; set; }
[Reactive]
public bool AddProxyOnly4Singbox { get; set; }
[Reactive]
public string ProxyDetour4Ray { get; set; } = string.Empty;
[Reactive]
public string ProxyDetour4Singbox { get; set; } = string.Empty;
public ReactiveCommand<Unit, Unit> SaveCmd { get; }
#endregion Reactive
public FullConfigTemplateViewModel()
{
_config = AppManager.Instance.Config;
SaveCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SaveSettingAsync();
});
_ = Init();
}
private async Task Init()
{
var item = await AppManager.Instance.GetFullConfigTemplateItem(ECoreType.Xray);
if (item == null)
{
return;
}
EnableFullConfigTemplate4Ray = item.Enabled;
FullConfigTemplate4Ray = item.Config ?? string.Empty;
FullTunConfigTemplate4Ray = item.TunConfig ?? string.Empty;
AddProxyOnly4Ray = item.AddProxyOnly ?? false;
ProxyDetour4Ray = item.ProxyDetour ?? string.Empty;
var item2 = await AppManager.Instance.GetFullConfigTemplateItem(ECoreType.sing_box);
EnableFullConfigTemplate4Singbox = item2?.Enabled ?? false;
FullConfigTemplate4Singbox = item2?.Config ?? string.Empty;
FullTunConfigTemplate4Singbox = item2?.TunConfig ?? string.Empty;
AddProxyOnly4Singbox = item2?.AddProxyOnly ?? false;
ProxyDetour4Singbox = item2?.ProxyDetour ?? string.Empty;
}
private async Task SaveSettingAsync()
{
if (!await SaveXrayConfigAsync())
{
return;
}
if (!await SaveSingboxConfigAsync())
{
return;
}
NoticeManager.Instance.Enqueue(ResUI.OperationSuccess);
RequestClose?.Invoke(this, EventArgs.Empty);
}
private async Task<bool> SaveXrayConfigAsync()
{
var item = await AppManager.Instance.GetFullConfigTemplateItem(ECoreType.Xray);
if (item == null)
{
return false;
}
item.Enabled = EnableFullConfigTemplate4Ray;
item.Config = FullConfigTemplate4Ray;
item.TunConfig = FullTunConfigTemplate4Ray;
item.AddProxyOnly = AddProxyOnly4Ray;
item.ProxyDetour = ProxyDetour4Ray;
await ConfigHandler.SaveFullConfigTemplate(_config, item);
return true;
}
private async Task<bool> SaveSingboxConfigAsync()
{
var item = await AppManager.Instance.GetFullConfigTemplateItem(ECoreType.sing_box);
if (item == null)
{
return false;
}
item.Enabled = EnableFullConfigTemplate4Singbox;
item.Config = FullConfigTemplate4Singbox;
item.TunConfig = FullTunConfigTemplate4Singbox;
item.AddProxyOnly = AddProxyOnly4Singbox;
item.ProxyDetour = ProxyDetour4Singbox;
await ConfigHandler.SaveFullConfigTemplate(_config, item);
return true;
}
}