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 10, 2024
1 parent 8ff04dc commit 2504b47
Show file tree
Hide file tree
Showing 25 changed files with 180 additions and 94 deletions.
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Base/MyReactiveObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace v2rayN.Base
public class MyReactiveObject : ReactiveObject
{
protected static Config? _config;
protected Func<EViewAction, bool>? _updateView;
protected Func<EViewAction, object?, bool>? _updateView;
protected NoticeHandler? _noticeHandler;
}
}
12 changes: 12 additions & 0 deletions v2rayN/v2rayN/Enums/EViewAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,17 @@ public enum EViewAction
CloseWindow,
ShowYesNo,
AddBatchRoutingRulesYesNo,
SubEditWindow,
SubShare,
RoutingRuleSettingWindow,
RoutingRuleDetailsWindow,
AddServerWindow,
AddServer2Window,
ShareServer,
DNSSettingWindow,
RoutingSettingWindow,
OptionSettingWindow,
GlobalHotkeySettingWindow,
SubSettingWindow,
}
}
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/Handler/NoticeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace v2rayN.Handler
{
public class NoticeHandler
{
private readonly ISnackbarMessageQueue _snackbarMessageQueue;
private readonly ISnackbarMessageQueue? _snackbarMessageQueue;

public NoticeHandler(ISnackbarMessageQueue snackbarMessageQueue)
public NoticeHandler(ISnackbarMessageQueue? snackbarMessageQueue)
{
_snackbarMessageQueue = snackbarMessageQueue ?? throw new ArgumentNullException(nameof(snackbarMessageQueue));
}
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AddServer2ViewModel : MyReactiveObject
public ReactiveCommand<Unit, Unit> SaveServerCmd { get; }
public bool IsModified { get; set; }

public AddServer2ViewModel(ProfileItem profileItem, Func<EViewAction, bool>? updateView)
public AddServer2ViewModel(ProfileItem profileItem, Func<EViewAction, object?, bool>? updateView)
{
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
_config = LazyConfig.Instance.GetConfig();
Expand Down Expand Up @@ -84,7 +84,7 @@ private void SaveServer()
if (ConfigHandler.EditCustomServer(_config, item) == 0)
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/ViewModels/AddServerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class AddServerViewModel : MyReactiveObject

public ReactiveCommand<Unit, Unit> SaveCmd { get; }

public AddServerViewModel(ProfileItem profileItem, Func<EViewAction, bool>? updateView)
public AddServerViewModel(ProfileItem profileItem, Func<EViewAction, object?, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
Expand Down Expand Up @@ -135,7 +135,7 @@ private void SaveServer()
if (ret == 0)
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DNSSettingViewModel : MyReactiveObject
public ReactiveCommand<Unit, Unit> ImportDefConfig4V2rayCmd { get; }
public ReactiveCommand<Unit, Unit> ImportDefConfig4SingboxCmd { get; }

public DNSSettingViewModel(Func<EViewAction, bool>? updateView)
public DNSSettingViewModel(Func<EViewAction, object?, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
Expand Down Expand Up @@ -112,7 +112,7 @@ private void SaveSetting()
ConfigHandler.SaveDNSItems(_config, item2);

_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
}
}
23 changes: 10 additions & 13 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DynamicData.Binding;
using MaterialDesignThemes.Wpf;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;
Expand All @@ -17,7 +16,6 @@
using v2rayN.Handler.Statistics;
using v2rayN.Models;
using v2rayN.Resx;
using v2rayN.Views;

namespace v2rayN.ViewModels
{
Expand Down Expand Up @@ -170,14 +168,13 @@ public class MainWindowViewModel : MyReactiveObject

#region Init

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

ThreadPool.RegisterWaitForSingleObject(App.ProgramStarted, OnProgramStarted, null, -1, false);
Locator.CurrentMutable.RegisterLazySingleton(() => _noticeHandler, typeof(NoticeHandler));
MessageBus.Current.Listen<string>(Global.CommandRefreshProfiles).Subscribe(x => RefreshServersBiz());

SelectedRouting = new();
Expand Down Expand Up @@ -307,7 +304,7 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue, Func<EVie
});
GlobalHotkeySettingCmd = ReactiveCommand.Create(() =>
{
if ((new GlobalHotkeySettingWindow()).ShowDialog() == true)
if (_updateView?.Invoke(EViewAction.GlobalHotkeySettingWindow, null) == true)
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
}
Expand Down Expand Up @@ -444,7 +441,7 @@ private void UpdateTaskHandler(bool success, string msg)
}
if (_config.uiItem.enableAutoAdjustMainLvColWidth)
{
_updateView(EViewAction.AdjustMainLvColWidth);
_updateView?.Invoke(EViewAction.AdjustMainLvColWidth, null);
}
}
}
Expand Down Expand Up @@ -610,11 +607,11 @@ public void AddServer(bool blNew, EConfigType eConfigType)
bool? ret = false;
if (eConfigType == EConfigType.Custom)
{
ret = (new AddServer2Window(item)).ShowDialog();
ret = _updateView?.Invoke(EViewAction.AddServer2Window, item);
}
else
{
ret = (new AddServerWindow(item)).ShowDialog();
ret = _updateView?.Invoke(EViewAction.AddServerWindow, item);
}
if (ret == true)
{
Expand Down Expand Up @@ -734,7 +731,7 @@ public void TestServerAvailability()

private void SubSetting()
{
if ((new SubSettingWindow()).ShowDialog() == true)
if (_updateView?.Invoke(EViewAction.SubSettingWindow, null) == true)
{
RefreshSubscriptions();
}
Expand All @@ -751,7 +748,7 @@ private void UpdateSubscriptionProcess(string subId, bool blProxy)

private void OptionSetting()
{
var ret = (new OptionSettingWindow()).ShowDialog();
var ret = _updateView?.Invoke(EViewAction.OptionSettingWindow, null);
if (ret == true)
{
//RefreshServers();
Expand All @@ -761,7 +758,7 @@ private void OptionSetting()

private void RoutingSetting()
{
var ret = (new RoutingSettingWindow()).ShowDialog();
var ret = _updateView?.Invoke(EViewAction.RoutingSettingWindow, null);
if (ret == true)
{
ConfigHandler.InitBuiltinRouting(_config);
Expand All @@ -773,7 +770,7 @@ private void RoutingSetting()

private void DNSSetting()
{
var ret = (new DNSSettingWindow()).ShowDialog();
var ret = _updateView?.Invoke(EViewAction.DNSSettingWindow, null);
if (ret == true)
{
Reload();
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public class OptionSettingViewModel : MyReactiveObject

public ReactiveCommand<Unit, Unit> SaveCmd { get; }

public OptionSettingViewModel(Func<EViewAction, bool>? updateView)
public OptionSettingViewModel(Func<EViewAction, object?, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
Expand Down Expand Up @@ -359,7 +359,7 @@ private void SaveSetting()
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
}
_updateView?.Invoke(EViewAction.CloseWindow);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
else
{
Expand Down
25 changes: 8 additions & 17 deletions v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using DynamicData;
using DynamicData.Binding;
using MaterialDesignThemes.Wpf;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;
Expand All @@ -15,7 +14,6 @@
using v2rayN.Handler.Statistics;
using v2rayN.Models;
using v2rayN.Resx;
using v2rayN.Views;

namespace v2rayN.ViewModels
{
Expand Down Expand Up @@ -102,7 +100,7 @@ public class ProfilesViewModel : MyReactiveObject

#region Init

public ProfilesViewModel(Func<EViewAction, bool>? updateView)
public ProfilesViewModel(Func<EViewAction, object?, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
Expand Down Expand Up @@ -327,7 +325,7 @@ private void SubSelectedChanged(bool c)

RefreshServers();

_updateView(EViewAction.ProfilesFocus);
_updateView?.Invoke(EViewAction.ProfilesFocus, null);
}

private void ServerFilterChanged(bool c)
Expand Down Expand Up @@ -484,11 +482,11 @@ public void EditServer(bool blNew, EConfigType eConfigType)
bool? ret = false;
if (eConfigType == EConfigType.Custom)
{
ret = (new AddServer2Window(item)).ShowDialog();
ret = _updateView?.Invoke(EViewAction.AddServer2Window, item);
}
else
{
ret = (new AddServerWindow(item)).ShowDialog();
ret = _updateView?.Invoke(EViewAction.AddServerWindow, item);
}
if (ret == true)
{
Expand All @@ -506,7 +504,7 @@ public void RemoveServer()
{
return;
}
if (_updateView?.Invoke(EViewAction.ShowYesNo) == false)
if (_updateView?.Invoke(EViewAction.ShowYesNo, null) == false)
{
return;
}
Expand Down Expand Up @@ -593,7 +591,7 @@ private void ServerSelectedChanged(bool c)
SetDefaultServer(SelectedServer.ID);
}

public async void ShareServer()
public void ShareServer()
{
var item = LazyConfig.Instance.GetProfileItem(SelectedProfile.indexId);
if (item is null)
Expand All @@ -606,14 +604,8 @@ public async void ShareServer()
{
return;
}
var img = QRCodeHelper.GetQRCode(url);
var dialog = new QrcodeView()
{
imgQrcode = { Source = img },
txtContent = { Text = url },
};

await DialogHost.Show(dialog, "RootDialog");
_updateView?.Invoke(EViewAction.ShareServer, url);
}

private void SetDefaultMultipleServer(ECoreType coreType)
Expand Down Expand Up @@ -781,8 +773,7 @@ private void EditSub(bool blNew)
return;
}
}
var ret = (new SubEditWindow(item)).ShowDialog();
if (ret == true)
if (_updateView?.Invoke(EViewAction.SubEditWindow, item) == true)
{
RefreshSubscriptions();
SubSelectedChanged(true);
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/ViewModels/RoutingRuleDetailsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class RoutingRuleDetailsViewModel : MyReactiveObject

public ReactiveCommand<Unit, Unit> SaveCmd { get; }

public RoutingRuleDetailsViewModel(RulesItem rulesItem, Func<EViewAction, bool>? updateView)
public RoutingRuleDetailsViewModel(RulesItem rulesItem, Func<EViewAction, object?, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
Expand Down Expand Up @@ -93,7 +93,7 @@ private void SaveRules()
return;
}
//_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
}
}
12 changes: 5 additions & 7 deletions v2rayN/v2rayN/ViewModels/RoutingRuleSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;
using v2rayN.Views;
using Application = System.Windows.Application;

namespace v2rayN.ViewModels
Expand Down Expand Up @@ -41,7 +40,7 @@ public class RoutingRuleSettingViewModel : MyReactiveObject

public ReactiveCommand<Unit, Unit> SaveCmd { get; }

public RoutingRuleSettingViewModel(RoutingItem routingItem, Func<EViewAction, bool>? updateView)
public RoutingRuleSettingViewModel(RoutingItem routingItem, Func<EViewAction, object?, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
Expand Down Expand Up @@ -151,8 +150,7 @@ public void RuleEdit(bool blNew)
return;
}
}
var ret = (new RoutingRuleDetailsWindow(item)).ShowDialog();
if (ret == true)
if (_updateView?.Invoke(EViewAction.RoutingRuleDetailsWindow, item) == true)
{
if (blNew)
{
Expand All @@ -169,7 +167,7 @@ public void RuleRemove()
_noticeHandler?.Enqueue(ResUI.PleaseSelectRules);
return;
}
if (_updateView?.Invoke(EViewAction.ShowYesNo) == false)
if (_updateView?.Invoke(EViewAction.ShowYesNo, null) == false)
{
return;
}
Expand Down Expand Up @@ -249,7 +247,7 @@ private void SaveRouting()
if (ConfigHandler.SaveRoutingItem(_config, item) == 0)
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow);
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
else
{
Expand Down Expand Up @@ -318,7 +316,7 @@ private async Task ImportRulesFromUrl()
private int AddBatchRoutingRules(RoutingItem routingItem, string? clipboardData)
{
bool blReplace = false;
if (_updateView?.Invoke(EViewAction.AddBatchRoutingRulesYesNo) == false)
if (_updateView?.Invoke(EViewAction.AddBatchRoutingRulesYesNo, null) == false)
{
blReplace = true;
}
Expand Down
Loading

0 comments on commit 2504b47

Please sign in to comment.