v2rayN/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs

116 lines
3.8 KiB
C#
Raw Normal View History

2023-01-01 11:42:01 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System.Reactive;
2024-08-20 06:15:29 +00:00
namespace ServiceLib.ViewModels
2023-01-01 11:42:01 +00:00
{
public class AddServer2ViewModel : MyReactiveObject
2023-01-01 11:42:01 +00:00
{
[Reactive]
public ProfileItem SelectedSource { get; set; }
2024-09-04 08:15:31 +00:00
2024-08-27 05:18:18 +00:00
[Reactive]
public string? CoreType { get; set; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> BrowseServerCmd { get; }
public ReactiveCommand<Unit, Unit> EditServerCmd { get; }
public ReactiveCommand<Unit, Unit> SaveServerCmd { get; }
2023-01-30 05:16:48 +00:00
public bool IsModified { get; set; }
2023-01-01 11:42:01 +00:00
2024-08-22 11:51:10 +00:00
public AddServer2ViewModel(ProfileItem profileItem, Func<EViewAction, object?, Task<bool>>? updateView)
2023-01-01 11:42:01 +00:00
{
2024-10-07 01:51:41 +00:00
_config = AppHandler.Instance.Config;
_updateView = updateView;
2023-01-01 11:42:01 +00:00
BrowseServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
2024-10-08 06:55:06 +00:00
_updateView?.Invoke(EViewAction.BrowseServer, null);
await Task.CompletedTask;
2023-01-01 11:42:01 +00:00
});
EditServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await EditServer();
2023-01-01 11:42:01 +00:00
});
SaveServerCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await SaveServerAsync();
2023-01-01 11:42:01 +00:00
});
2024-10-21 05:46:13 +00:00
SelectedSource = profileItem.IndexId.IsNullOrEmpty() ? profileItem : JsonUtils.DeepCopy(profileItem);
CoreType = SelectedSource?.CoreType?.ToString();
2023-01-01 11:42:01 +00:00
}
2024-08-22 11:51:10 +00:00
private async Task SaveServerAsync()
2023-01-01 11:42:01 +00:00
{
string remarks = SelectedSource.Remarks;
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(remarks))
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.PleaseFillRemarks);
2023-01-01 11:42:01 +00:00
return;
}
if (Utils.IsNullOrEmpty(SelectedSource.Address))
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FillServerAddressCustom);
2023-01-01 11:42:01 +00:00
return;
}
SelectedSource.CoreType = CoreType.IsNullOrEmpty() ? null : (ECoreType)Enum.Parse(typeof(ECoreType), CoreType);
2023-01-01 11:42:01 +00:00
2024-10-20 03:51:05 +00:00
if (await ConfigHandler.EditCustomServer(_config, SelectedSource) == 0)
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
2024-10-08 06:55:06 +00:00
_updateView?.Invoke(EViewAction.CloseWindow, null);
2023-01-01 11:42:01 +00:00
}
else
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
2023-01-01 11:42:01 +00:00
}
}
2024-10-20 03:51:05 +00:00
public async Task BrowseServer(string fileName)
2023-01-01 11:42:01 +00:00
{
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(fileName))
2023-01-01 11:42:01 +00:00
{
return;
}
2024-01-11 10:24:32 +00:00
var item = await AppHandler.Instance.GetProfileItem(SelectedSource.IndexId);
2023-02-19 05:34:22 +00:00
item ??= SelectedSource;
item.Address = fileName;
2024-10-20 03:51:05 +00:00
if (await ConfigHandler.AddCustomServer(_config, item, false) == 0)
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.SuccessfullyImportedCustomServer);
if (Utils.IsNotEmpty(item.IndexId))
2023-01-30 05:16:48 +00:00
{
2024-03-26 06:26:03 +00:00
SelectedSource = JsonUtils.DeepCopy(item);
2023-01-30 05:16:48 +00:00
}
IsModified = true;
2023-01-01 11:42:01 +00:00
}
else
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FailedImportedCustomServer);
2023-01-01 11:42:01 +00:00
}
}
private async Task EditServer()
2023-01-01 11:42:01 +00:00
{
var address = SelectedSource.Address;
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(address))
2023-01-01 11:42:01 +00:00
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FillServerAddressCustom);
2023-01-01 11:42:01 +00:00
return;
}
2024-03-26 06:26:03 +00:00
address = Utils.GetConfigPath(address);
2023-01-01 11:42:01 +00:00
if (File.Exists(address))
{
2025-01-03 12:56:51 +00:00
ProcUtils.ProcessStart(address);
2023-01-01 11:42:01 +00:00
}
else
{
2024-10-07 02:59:13 +00:00
NoticeHandler.Instance.Enqueue(ResUI.FailedReadConfiguration);
2023-01-01 11:42:01 +00:00
}
await Task.CompletedTask;
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
}