Refactor code to decouple view and viewmodel

pull/5550/head
2dust 2024-08-12 20:25:31 +08:00
parent 9aa5c0d135
commit 372f3991e1
6 changed files with 38 additions and 32 deletions

View File

@ -12,6 +12,8 @@
ShowHideWindow,
ScanScreenTask,
Shutdown,
BrowseServer,
ImportRulesFromFile,
SubEditWindow,
RoutingRuleSettingWindow,
RoutingRuleDetailsWindow,

View File

@ -5,7 +5,6 @@ using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using v2rayN.Enums;
using v2rayN.Models;
using v2rayN.Resx;
@ -535,22 +534,22 @@ namespace v2rayN.Handler
private async Task AskToDownload(DownloadHandle downloadHandle, string url, bool blAsk)
{
bool blDownload = false;
if (blAsk)
{
if (UI.ShowYesNo(string.Format(ResUI.DownloadYesNo, url)) == MessageBoxResult.Yes)
{
blDownload = true;
}
}
else
{
blDownload = true;
}
if (blDownload)
{
//bool blDownload = false;
//if (blAsk)
//{
// if (UI.ShowYesNo(string.Format(ResUI.DownloadYesNo, url)) == MessageBoxResult.Yes)
// {
// blDownload = true;
// }
//}
//else
//{
// blDownload = true;
//}
//if (blDownload)
//{
await downloadHandle.DownloadFileAsync(url, true, 600);
}
//}
}
private async Task UpdateGeoFile(string geoName, Config config, Action<bool, string> update)

View File

@ -38,7 +38,7 @@ namespace v2rayN.ViewModels
BrowseServerCmd = ReactiveCommand.Create(() =>
{
BrowseServer();
_updateView?.Invoke(EViewAction.BrowseServer, null);
});
EditServerCmd = ReactiveCommand.Create(() =>
@ -92,15 +92,8 @@ namespace v2rayN.ViewModels
}
}
private void BrowseServer()
public void BrowseServer(string fileName)
{
//_noticeHandler?.Enqueue(ResUI.CustomServerTips);
if (UI.OpenFileDialog(out string fileName,
"Config|*.json|YAML|*.yaml;*.yml|All|*.*") != true)
{
return;
}
if (Utils.IsNullOrEmpty(fileName))
{
return;

View File

@ -69,7 +69,7 @@ namespace v2rayN.ViewModels
});
ImportRulesFromFileCmd = ReactiveCommand.Create(() =>
{
ImportRulesFromFile();
_updateView?.Invoke(EViewAction.ImportRulesFromFile, null);
});
ImportRulesFromClipboardCmd = ReactiveCommand.Create(() =>
{
@ -256,13 +256,8 @@ namespace v2rayN.ViewModels
#region Import rules
private void ImportRulesFromFile()
public void ImportRulesFromFile(string fileName)
{
if (UI.OpenFileDialog(out string fileName,
"Rules|*.json|All|*.*") != true)
{
return;
}
if (Utils.IsNullOrEmpty(fileName))
{
return;

View File

@ -47,6 +47,15 @@ namespace v2rayN.Views
{
this.DialogResult = true;
}
else if (action == EViewAction.BrowseServer)
{
if (UI.OpenFileDialog(out string fileName, "Config|*.json|YAML|*.yaml;*.yml|All|*.*") != true)
{
return false;
}
ViewModel?.BrowseServer(fileName);
}
return true;
}

View File

@ -88,6 +88,14 @@ namespace v2rayN.Views
if (obj is null) return false;
return (new RoutingRuleDetailsWindow((RulesItem)obj)).ShowDialog() ?? false;
}
else if (action == EViewAction.ImportRulesFromFile)
{
if (UI.OpenFileDialog(out string fileName, "Rules|*.json|All|*.*") != true)
{
return false;
}
ViewModel?.ImportRulesFromFile(fileName);
}
return true;
}