mirror of https://github.com/2dust/v2rayN
55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using ReactiveUI;
|
|
using ReactiveUI.Fody.Helpers;
|
|
using System.Reactive;
|
|
|
|
namespace ServiceLib.ViewModels
|
|
{
|
|
public class SubEditViewModel : MyReactiveObject
|
|
{
|
|
[Reactive]
|
|
public SubItem SelectedSource { get; set; }
|
|
|
|
public ReactiveCommand<Unit, Unit> SaveCmd { get; }
|
|
|
|
public SubEditViewModel(SubItem subItem, Func<EViewAction, object?, Task<bool>>? updateView)
|
|
{
|
|
_config = AppHandler.Instance.Config;
|
|
|
|
_updateView = updateView;
|
|
|
|
if (subItem.id.IsNullOrEmpty())
|
|
{
|
|
SelectedSource = subItem;
|
|
}
|
|
else
|
|
{
|
|
SelectedSource = JsonUtils.DeepCopy(subItem);
|
|
}
|
|
|
|
SaveCmd = ReactiveCommand.CreateFromTask(async () =>
|
|
{
|
|
await SaveSubAsync();
|
|
});
|
|
}
|
|
|
|
private async Task SaveSubAsync()
|
|
{
|
|
string remarks = SelectedSource.remarks;
|
|
if (Utils.IsNullOrEmpty(remarks))
|
|
{
|
|
NoticeHandler.Instance.Enqueue(ResUI.PleaseFillRemarks);
|
|
return;
|
|
}
|
|
|
|
if (ConfigHandler.AddSubItem(_config, SelectedSource) == 0)
|
|
{
|
|
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);
|
|
_updateView?.Invoke(EViewAction.CloseWindow, null);
|
|
}
|
|
else
|
|
{
|
|
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
|
|
}
|
|
}
|
|
}
|
|
} |