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 d893ee4 commit 8ff04dc
Show file tree
Hide file tree
Showing 33 changed files with 246 additions and 159 deletions.
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/App.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Application
x:Class="v2rayN.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:conv="clr-namespace:v2rayN.Converters"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
ShutdownMode="OnExplicitShutdown"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
Expand Down
14 changes: 14 additions & 0 deletions v2rayN/v2rayN/Base/MyReactiveObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ReactiveUI;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;

namespace v2rayN.Base
{
public class MyReactiveObject : ReactiveObject
{
protected static Config? _config;
protected Func<EViewAction, bool>? _updateView;
protected NoticeHandler? _noticeHandler;
}
}
5 changes: 4 additions & 1 deletion v2rayN/v2rayN/Enums/EViewAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
public enum EViewAction
{
AdjustMainLvColWidth,
ProfilesFocus
ProfilesFocus,
CloseWindow,
ShowYesNo,
AddBatchRoutingRulesYesNo,
}
}
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ internal class Global
public static readonly List<string> DomainDNSAddress = ["223.5.5.5", "223.6.6.6", "localhost"];
public static readonly List<string> SingboxDomainDNSAddress = ["223.5.5.5", "223.6.6.6", "dhcp://auto"];
public static readonly List<string> Languages = new() { "zh-Hans", "zh-Hant", "en", "fa-Ir", "ru" };
public static readonly List<string> Alpns = new() { "h3", "h2", "http/1.1", "h3,h2", "h2,http/1.1", "h3,h2,http/1.1", "" };
public static readonly List<string> Alpns = new() { "h3", "h2", "http/1.1", "h3,h2", "h2,http/1.1", "h3,h2,http/1.1", "" };
public static readonly List<string> LogLevels = new() { "debug", "info", "warning", "error", "none" };
public static readonly List<string> InboundTags = new() { "socks", "http", "socks2", "http2" };
public static readonly List<string> RuleProtocols = new() { "http", "tls", "bittorrent" };
Expand Down
19 changes: 6 additions & 13 deletions v2rayN/v2rayN/ViewModels/AddServer2ViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ReactiveUI.Validation.Helpers;
using Splat;
using System.IO;
using System.Reactive;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;

namespace v2rayN.ViewModels
{
public class AddServer2ViewModel : ReactiveValidationObject
public class AddServer2ViewModel : MyReactiveObject
{
private static Config _config;
private NoticeHandler? _noticeHandler;
private Window _view;

[Reactive]
public ProfileItem SelectedSource { get; set; }

Expand All @@ -25,10 +21,11 @@ public class AddServer2ViewModel : ReactiveValidationObject
public ReactiveCommand<Unit, Unit> SaveServerCmd { get; }
public bool IsModified { get; set; }

public AddServer2ViewModel(ProfileItem profileItem, Window view)
public AddServer2ViewModel(ProfileItem profileItem, Func<EViewAction, bool>? updateView)
{
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
_config = LazyConfig.Instance.GetConfig();
_updateView = updateView;

if (profileItem.indexId.IsNullOrEmpty())
{
Expand All @@ -39,8 +36,6 @@ public AddServer2ViewModel(ProfileItem profileItem, Window view)
SelectedSource = JsonUtils.DeepCopy(profileItem);
}

_view = view;

BrowseServerCmd = ReactiveCommand.Create(() =>
{
BrowseServer();
Expand All @@ -55,8 +50,6 @@ public AddServer2ViewModel(ProfileItem profileItem, Window view)
{
SaveServer();
});

Utils.SetDarkBorder(view, _config.uiItem.followSystemTheme ? !Utils.IsLightTheme() : _config.uiItem.colorModeDark);
}

private void SaveServer()
Expand Down Expand Up @@ -91,7 +84,7 @@ private void SaveServer()
if (ConfigHandler.EditCustomServer(_config, item) == 0)
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_view.DialogResult = true;
_updateView?.Invoke(EViewAction.CloseWindow);
}
else
{
Expand Down
17 changes: 5 additions & 12 deletions v2rayN/v2rayN/ViewModels/AddServerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@
using ReactiveUI.Fody.Helpers;
using Splat;
using System.Reactive;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;

namespace v2rayN.ViewModels
{
public class AddServerViewModel : ReactiveObject
public class AddServerViewModel : MyReactiveObject
{
private static Config _config;
private NoticeHandler? _noticeHandler;
private Window _view;

[Reactive]
public ProfileItem SelectedSource { get; set; }

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

public AddServerViewModel(ProfileItem profileItem, Window view)
public AddServerViewModel(ProfileItem profileItem, Func<EViewAction, bool>? updateView)
{
_config = LazyConfig.Instance.GetConfig();
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
_view = view;
_updateView = updateView;

if (profileItem.id.IsNullOrEmpty())
{
Expand All @@ -44,8 +40,6 @@ public AddServerViewModel(ProfileItem profileItem, Window view)
{
SaveServer();
});

Utils.SetDarkBorder(view, _config.uiItem.followSystemTheme ? !Utils.IsLightTheme() : _config.uiItem.colorModeDark);
}

private void SaveServer()
Expand Down Expand Up @@ -141,8 +135,7 @@ private void SaveServer()
if (ret == 0)
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_view.DialogResult = true;
//_view?.Close();
_updateView?.Invoke(EViewAction.CloseWindow);
}
else
{
Expand Down
5 changes: 2 additions & 3 deletions v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
using System.Reactive;
using System.Reactive.Linq;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;

namespace v2rayN.ViewModels
{
public class ClashConnectionsViewModel : ReactiveObject
public class ClashConnectionsViewModel : MyReactiveObject
{
private static Config _config;

private IObservableCollection<ClashConnectionModel> _connectionItems = new ObservableCollectionExtended<ClashConnectionModel>();

public IObservableCollection<ClashConnectionModel> ConnectionItems => _connectionItems;
Expand Down
5 changes: 2 additions & 3 deletions v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Reactive;
using System.Reactive.Linq;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
Expand All @@ -15,10 +16,8 @@

namespace v2rayN.ViewModels
{
public class ClashProxiesViewModel : ReactiveObject
public class ClashProxiesViewModel : MyReactiveObject
{
private static Config _config;
private NoticeHandler? _noticeHandler;
private Dictionary<String, ProxiesItem>? proxies;
private Dictionary<String, ProvidersItem>? providers;
private int delayTimeout = 99999999;
Expand Down
18 changes: 6 additions & 12 deletions v2rayN/v2rayN/ViewModels/DNSSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
using ReactiveUI.Fody.Helpers;
using Splat;
using System.Reactive;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;

namespace v2rayN.ViewModels
{
public class DNSSettingViewModel : ReactiveObject
public class DNSSettingViewModel : MyReactiveObject
{
private static Config _config;
private NoticeHandler? _noticeHandler;
private Window _view;

[Reactive] public bool useSystemHosts { get; set; }
[Reactive] public string domainStrategy4Freedom { get; set; }
[Reactive] public string domainDNSAddress { get; set; }
Expand All @@ -30,11 +26,11 @@ public class DNSSettingViewModel : ReactiveObject
public ReactiveCommand<Unit, Unit> ImportDefConfig4V2rayCmd { get; }
public ReactiveCommand<Unit, Unit> ImportDefConfig4SingboxCmd { get; }

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

var item = LazyConfig.Instance.GetDNSItem(ECoreType.Xray);
useSystemHosts = item.useSystemHosts;
Expand Down Expand Up @@ -63,8 +59,6 @@ public DNSSettingViewModel(Window view)
normalDNS2 = Utils.GetEmbedText(Global.DNSSingboxNormalFileName);
tunDNS2 = Utils.GetEmbedText(Global.TunSingboxDNSFileName);
});

Utils.SetDarkBorder(view, _config.uiItem.followSystemTheme ? !Utils.IsLightTheme() : _config.uiItem.colorModeDark);
}

private void SaveSetting()
Expand Down Expand Up @@ -114,11 +108,11 @@ private void SaveSetting()
item2.domainStrategy4Freedom = domainStrategy4Freedom2;
item2.domainDNSAddress = domainDNSAddress2;
item2.normalDNS = JsonUtils.Serialize(JsonUtils.ParseJson(normalDNS2));
item2.tunDNS = JsonUtils.Serialize(JsonUtils.ParseJson(tunDNS2));;
item2.tunDNS = JsonUtils.Serialize(JsonUtils.ParseJson(tunDNS2)); ;
ConfigHandler.SaveDNSItems(_config, item2);

_noticeHandler?.Enqueue(ResUI.OperationSuccess);
_view.DialogResult = true;
_updateView?.Invoke(EViewAction.CloseWindow);
}
}
}
16 changes: 7 additions & 9 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text;
using System.Windows;
using System.Windows.Media;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Handler.Statistics;
Expand All @@ -20,14 +21,12 @@

namespace v2rayN.ViewModels
{
public class MainWindowViewModel : ReactiveObject
public class MainWindowViewModel : MyReactiveObject
{
#region private prop

private CoreHandler _coreHandler;
private static Config _config;
private NoticeHandler? _noticeHandler;
private Action<EViewAction> _updateView;

private bool _showInTaskbar;

#endregion private prop
Expand Down Expand Up @@ -171,15 +170,14 @@ public class MainWindowViewModel : ReactiveObject

#region Init

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

ThreadPool.RegisterWaitForSingleObject(App.ProgramStarted, OnProgramStarted, null, -1, false);

_noticeHandler = new NoticeHandler(snackbarMessageQueue);
Locator.CurrentMutable.RegisterLazySingleton(() => _noticeHandler, typeof(NoticeHandler));
_config = LazyConfig.Instance.GetConfig();

MessageBus.Current.Listen<string>(Global.CommandRefreshProfiles).Subscribe(x => RefreshServersBiz());

SelectedRouting = new();
Expand Down
16 changes: 5 additions & 11 deletions v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
using ReactiveUI.Fody.Helpers;
using Splat;
using System.Reactive;
using System.Windows;
using v2rayN.Base;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;

namespace v2rayN.ViewModels
{
public class OptionSettingViewModel : ReactiveObject
public class OptionSettingViewModel : MyReactiveObject
{
private static Config _config;
private NoticeHandler? _noticeHandler;
private Window _view;

#region Core

[Reactive] public int localPort { get; set; }
Expand Down Expand Up @@ -109,11 +105,11 @@ public class OptionSettingViewModel : ReactiveObject

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

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

#region Core

Expand Down Expand Up @@ -201,8 +197,6 @@ public OptionSettingViewModel(Window view)
{
SaveSetting();
});

Utils.SetDarkBorder(view, _config.uiItem.followSystemTheme ? !Utils.IsLightTheme() : _config.uiItem.colorModeDark);
}

private void InitCoreType()
Expand Down Expand Up @@ -365,7 +359,7 @@ private void SaveSetting()
{
_noticeHandler?.Enqueue(ResUI.OperationSuccess);
}
_view.DialogResult = true;
_updateView?.Invoke(EViewAction.CloseWindow);
}
else
{
Expand Down
Loading

0 comments on commit 8ff04dc

Please sign in to comment.