v2rayN/v2rayN/ServiceLib/ViewModels/SubEditViewModel.cs

64 lines
1.8 KiB
C#
Raw Normal View History

2023-01-01 11:42:01 +00:00
using System.Reactive;
2025-01-30 09:10:05 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
2023-01-01 11:42:01 +00:00
namespace ServiceLib.ViewModels;
public class SubEditViewModel : MyReactiveObject
2023-01-01 11:42:01 +00:00
{
[Reactive]
public SubItem SelectedSource { get; set; }
2023-01-01 11:42:01 +00:00
public ReactiveCommand<Unit, Unit> SaveCmd { get; }
2023-01-01 11:42:01 +00:00
public SubEditViewModel(SubItem subItem, Func<EViewAction, object?, Task<bool>>? updateView)
{
2025-08-17 09:31:55 +00:00
_config = AppManager.Instance.Config;
_updateView = updateView;
SaveCmd = ReactiveCommand.CreateFromTask(async () =>
2023-01-01 11:42:01 +00:00
{
await SaveSubAsync();
});
2023-01-01 11:42:01 +00:00
SelectedSource = subItem.Id.IsNullOrEmpty() ? subItem : JsonUtils.DeepCopy(subItem);
}
2024-10-21 05:46:13 +00:00
private async Task SaveSubAsync()
{
var remarks = SelectedSource.Remarks;
if (remarks.IsNullOrEmpty())
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.PleaseFillRemarks);
return;
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
var url = SelectedSource.Url;
if (url.IsNotEmpty())
2023-01-01 11:42:01 +00:00
{
var uri = Utils.TryUri(url);
if (uri == null)
2023-01-01 11:42:01 +00:00
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.InvalidUrlTip);
2023-01-01 11:42:01 +00:00
return;
}
//Do not allow http protocol
if (url.StartsWith(Global.HttpProtocol) && !Utils.IsPrivateNetwork(uri.IdnHost))
2024-10-24 09:09:07 +00:00
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.InsecureUrlProtocol);
//return;
2024-10-24 09:09:07 +00:00
}
}
2024-10-24 09:09:07 +00:00
if (await ConfigHandler.AddSubItem(_config, SelectedSource) == 0)
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
else
{
2025-08-17 09:31:55 +00:00
NoticeManager.Instance.Enqueue(ResUI.OperationFailed);
2023-01-01 11:42:01 +00:00
}
}
2025-01-30 09:10:05 +00:00
}