Skip to content

Commit

Permalink
Refactor code to decouple view and viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Aug 12, 2024
1 parent 9aa5c0d commit 372f399
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 32 deletions.
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Enums/EViewAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public enum EViewAction
ShowHideWindow,
ScanScreenTask,
Shutdown,
BrowseServer,
ImportRulesFromFile,
SubEditWindow,
RoutingRuleSettingWindow,
RoutingRuleDetailsWindow,
Expand Down
31 changes: 15 additions & 16 deletions v2rayN/v2rayN/Handler/UpdateHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using v2rayN.Enums;
using v2rayN.Models;
using v2rayN.Resx;
Expand Down Expand Up @@ -535,22 +534,22 @@ private void ResponseHandler(ECoreType type, string gitHubReleaseApi, bool preRe

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)
Expand Down
11 changes: 2 additions & 9 deletions v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AddServer2ViewModel(ProfileItem profileItem, Func<EViewAction, object?, b

BrowseServerCmd = ReactiveCommand.Create(() =>
{
BrowseServer();
_updateView?.Invoke(EViewAction.BrowseServer, null);
});

EditServerCmd = ReactiveCommand.Create(() =>
Expand Down Expand Up @@ -92,15 +92,8 @@ private void SaveServer()
}
}

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;
Expand Down
9 changes: 2 additions & 7 deletions v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public RoutingRuleSettingViewModel(RoutingItem routingItem, Func<EViewAction, ob
});
ImportRulesFromFileCmd = ReactiveCommand.Create(() =>
{
ImportRulesFromFile();
_updateView?.Invoke(EViewAction.ImportRulesFromFile, null);
});
ImportRulesFromClipboardCmd = ReactiveCommand.Create(() =>
{
Expand Down Expand Up @@ -256,13 +256,8 @@ private void SaveRouting()

#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;
Expand Down
9 changes: 9 additions & 0 deletions v2rayN/v2rayN/Views/AddServer2Window.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ private bool UpdateViewHandler(EViewAction action, object? obj)
{
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;
}

Expand Down
8 changes: 8 additions & 0 deletions v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ private bool UpdateViewHandler(EViewAction action, object? obj)
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;
}

Expand Down

0 comments on commit 372f399

Please sign in to comment.