Refactoring Project

This commit is contained in:
2dust
2024-08-20 14:15:29 +08:00
parent 61bea05f63
commit 8d1d10b783
53 changed files with 278 additions and 249 deletions

View File

@@ -0,0 +1,120 @@
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;
using System.Reactive;
namespace ServiceLib.ViewModels
{
public class AddServer2ViewModel : MyReactiveObject
{
[Reactive]
public ProfileItem SelectedSource { get; set; }
public ReactiveCommand<Unit, Unit> BrowseServerCmd { get; }
public ReactiveCommand<Unit, Unit> EditServerCmd { get; }
public ReactiveCommand<Unit, Unit> SaveServerCmd { get; }
public bool IsModified { get; set; }
public AddServer2ViewModel(ProfileItem profileItem, Func<EViewAction, object?, bool>? updateView)
{
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
_config = LazyConfig.Instance.Config;
_updateView = updateView;
if (profileItem.indexId.IsNullOrEmpty())
{
SelectedSource = profileItem;
}
else
{
SelectedSource = JsonUtils.DeepCopy(profileItem);
}
BrowseServerCmd = ReactiveCommand.Create(() =>
{
_updateView?.Invoke(EViewAction.BrowseServer, null);
});
EditServerCmd = ReactiveCommand.Create(() =>
{
EditServer();
});
SaveServerCmd = ReactiveCommand.Create(() =>
{
SaveServer();
});
}
private void SaveServer()
{
string remarks = SelectedSource.remarks;
if (Utils.IsNullOrEmpty(remarks))
{
_noticeHandler?.Enqueue(ResUI.PleaseFillRemarks);
return;
}
if (Utils.IsNullOrEmpty(SelectedSource.address))
{
_noticeHandler?.Enqueue(ResUI.FillServerAddressCustom);
return;
}
if (ConfigHandler.EditCustomServer(_config, SelectedSource) == 0)
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
else
{
_noticeHandler?.Enqueue(ResUI.OperationFailed);
}
}
public void BrowseServer(string fileName)
{
if (Utils.IsNullOrEmpty(fileName))
{
return;
}
var item = LazyConfig.Instance.GetProfileItem(SelectedSource.indexId);
item ??= SelectedSource;
item.address = fileName;
if (ConfigHandler.AddCustomServer(_config, item, false) == 0)
{
_noticeHandler?.Enqueue(ResUI.SuccessfullyImportedCustomServer);
if (!Utils.IsNullOrEmpty(item.indexId))
{
SelectedSource = JsonUtils.DeepCopy(item);
}
IsModified = true;
}
else
{
_noticeHandler?.Enqueue(ResUI.FailedImportedCustomServer);
}
}
private void EditServer()
{
var address = SelectedSource.address;
if (Utils.IsNullOrEmpty(address))
{
_noticeHandler?.Enqueue(ResUI.FillServerAddressCustom);
return;
}
address = Utils.GetConfigPath(address);
if (File.Exists(address))
{
Utils.ProcessStart(address);
}
else
{
_noticeHandler?.Enqueue(ResUI.FailedReadConfiguration);
}
}
}
}