v2rayN/v2rayN/ServiceLib/ViewModels/AddServer2ViewModel.cs

123 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
if (profileItem.indexId.IsNullOrEmpty())
{
SelectedSource = profileItem;
}
else
{
2024-03-26 06:26:03 +00:00
SelectedSource = JsonUtils.DeepCopy(profileItem);
2023-01-01 11:42:01 +00:00
}
2024-08-27 05:18:18 +00:00
CoreType = SelectedSource?.coreType?.ToString();
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);
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-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;
}
2024-03-26 06:26:03 +00:00
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;
}
2024-08-30 05:15:25 +00:00
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
2024-10-21 01:45:33 +00:00
var item = await AppHandler.Instance.GetProfileItem(SelectedSource.indexId);
2023-02-19 05:34:22 +00:00
item ??= SelectedSource;
2023-01-01 11:42:01 +00:00
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);
2024-09-17 08:52:41 +00:00
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))
{
2024-03-26 06:26:03 +00:00
Utils.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
}
}
}
2023-04-14 12:49:36 +00:00
}