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 11, 2024
1 parent 3dd75b1 commit 9e9808e
Show file tree
Hide file tree
Showing 13 changed files with 359 additions and 284 deletions.
10 changes: 10 additions & 0 deletions v2rayN/v2rayN/Enums/EViewAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum EViewAction
ProfilesFocus,
ShareSub,
ShareServer,
ShowHideWindow,
SubEditWindow,
RoutingRuleSettingWindow,
RoutingRuleDetailsWindow,
Expand All @@ -19,5 +20,14 @@ public enum EViewAction
OptionSettingWindow,
GlobalHotkeySettingWindow,
SubSettingWindow,
DispatcherSpeedTest,
DispatcherRefreshConnections,
DispatcherRefreshProxyGroups,
DispatcherProxiesDelayTest,

DispatcherStatistics,
DispatcherServerAvailability,
DispatcherReload,
DispatcherRefreshServersBiz,
}
}
6 changes: 3 additions & 3 deletions v2rayN/v2rayN/Handler/HotkeyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Load()
bool isSuccess = false;
string msg;

Application.Current.Dispatcher.Invoke(() =>
Application.Current?.Dispatcher.Invoke(() =>
{
isSuccess = RegisterHotKey(IntPtr.Zero, _hotkeyCode, hotkeyInfo.fsModifiers, hotkeyInfo.vKey);
});
Expand All @@ -96,7 +96,7 @@ public void ReLoad()
{
foreach (var hotkey in _hotkeyTriggerDic.Keys)
{
Application.Current.Dispatcher.Invoke(() =>
Application.Current?.Dispatcher.Invoke(() =>
{
UnregisterHotKey(IntPtr.Zero, hotkey);
});
Expand Down Expand Up @@ -137,7 +137,7 @@ private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled)
var _hotKeyCode = (int)msg.lParam;
if (IsPause)
{
Application.Current.Dispatcher.Invoke(() =>
Application.Current?.Dispatcher.Invoke(() =>
{
UIElement? element = Keyboard.FocusedElement as UIElement;
if (element != null)
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/v2rayN/Handler/SpeedtestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ internal class SpeedtestHandler
private CoreHandler _coreHandler;
private List<ServerTestItem> _selecteds;
private ESpeedActionType _actionType;
private Action<string, string, string> _updateFunc;
private Action<SpeedTestResult> _updateFunc;

public SpeedtestHandler(Config config)
{
_config = config;
}

public SpeedtestHandler(Config config, CoreHandler coreHandler, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<string, string, string> update)
public SpeedtestHandler(Config config, CoreHandler coreHandler, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> update)
{
_config = config;
_coreHandler = coreHandler;
Expand Down Expand Up @@ -408,7 +408,7 @@ private string FormatOut(object time, string unit)

private void UpdateFunc(string indexId, string delay, string speed = "")
{
_updateFunc(indexId, delay, speed);
_updateFunc(new() { IndexId = indexId, Delay = delay, Speed = speed });
}
}
}
12 changes: 12 additions & 0 deletions v2rayN/v2rayN/Models/SpeedTestResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace v2rayN.Models
{
[Serializable]
public class SpeedTestResult
{
public string? IndexId { get; set; }

public string? Delay { get; set; }

public string? Speed { get; set; }
}
}
11 changes: 4 additions & 7 deletions v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using ReactiveUI.Fody.Helpers;
using System.Reactive;
using System.Reactive.Linq;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
Expand All @@ -30,9 +29,10 @@ public class ClashConnectionsViewModel : MyReactiveObject
[Reactive]
public bool AutoRefresh { get; set; }

public ClashConnectionsViewModel()
public ClashConnectionsViewModel(Func<EViewAction, object?, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_updateView = updateView;
SortingSelected = _config.clashUIItem.connectionsSorting;
AutoRefresh = _config.clashUIItem.connectionsAutoRefresh;

Expand Down Expand Up @@ -110,14 +110,11 @@ private void GetClashConnections()
return;
}

Application.Current?.Dispatcher.Invoke((Action)(() =>
{
RefreshConnections(it?.connections);
}));
_updateView?.Invoke(EViewAction.DispatcherRefreshConnections, it?.connections);
});
}

private void RefreshConnections(List<ConnectionItem>? connections)
public void RefreshConnections(List<ConnectionItem>? connections)
{
_connectionItems.Clear();

Expand Down
78 changes: 32 additions & 46 deletions v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Splat;
using System.Reactive;
using System.Reactive.Linq;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
Expand Down Expand Up @@ -48,10 +47,11 @@ public class ClashProxiesViewModel : MyReactiveObject
[Reactive]
public bool AutoRefresh { get; set; }

public ClashProxiesViewModel()
public ClashProxiesViewModel(Func<EViewAction, object?, bool>? updateView)
{
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
_config = LazyConfig.Instance.GetConfig();
_updateView = updateView;

SelectedGroup = new();
SelectedDetail = new();
Expand Down Expand Up @@ -149,20 +149,6 @@ public void ProxiesReload()
ProxiesDelayTest();
}

public void ProxiesClear()
{
proxies = null;
providers = null;

ClashApiHandler.Instance.SetProxies(proxies);

Application.Current?.Dispatcher.Invoke((Action)(() =>
{
_proxyGroups.Clear();
_proxyDetails.Clear();
}));
}

public void ProxiesDelayTest()
{
ProxiesDelayTest(true);
Expand Down Expand Up @@ -197,15 +183,12 @@ private void GetClashProxies(bool refreshUI)
}
if (refreshUI)
{
Application.Current?.Dispatcher.Invoke((Action)(() =>
{
RefreshProxyGroups();
}));
_updateView?.Invoke(EViewAction.DispatcherRefreshProxyGroups, null);
}
});
}

private void RefreshProxyGroups()
public void RefreshProxyGroups()
{
var selectedName = SelectedGroup?.name;
_proxyGroups.Clear();
Expand Down Expand Up @@ -425,34 +408,37 @@ private void ProxiesDelayTest(bool blAll)
{
return;
}
Application.Current?.Dispatcher.Invoke((Action)(() =>
{
//UpdateHandler(false, $"{item.name}={result}");
var detail = _proxyDetails.Where(it => it.name == item.name).FirstOrDefault();
if (detail != null)
{
var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(result);
if (dicResult != null && dicResult.ContainsKey("delay"))
{
detail.delay = Convert.ToInt32(dicResult["delay"].ToString());
detail.delayName = $"{detail.delay}ms";
}
else if (dicResult != null && dicResult.ContainsKey("message"))
{
detail.delay = delayTimeout;
detail.delayName = $"{dicResult["message"]}";
}
else
{
detail.delay = delayTimeout;
detail.delayName = String.Empty;
}
_proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail));
}
}));

_updateView?.Invoke(EViewAction.DispatcherProxiesDelayTest, new SpeedTestResult() { IndexId = item.name, Delay = result });
});
}

public void ProxiesDelayTestResult(SpeedTestResult result)
{
//UpdateHandler(false, $"{item.name}={result}");
var detail = _proxyDetails.Where(it => it.name == result.IndexId).FirstOrDefault();
if (detail != null)
{
var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(result.Delay);
if (dicResult != null && dicResult.ContainsKey("delay"))
{
detail.delay = Convert.ToInt32(dicResult["delay"].ToString());
detail.delayName = $"{detail.delay}ms";
}
else if (dicResult != null && dicResult.ContainsKey("message"))
{
detail.delay = delayTimeout;
detail.delayName = $"{dicResult["message"]}";
}
else
{
detail.delay = delayTimeout;
detail.delayName = String.Empty;
}
_proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail));
}
}

#endregion proxy function

#region task
Expand Down
Loading

0 comments on commit 9e9808e

Please sign in to comment.