diff --git a/v2rayN/ServiceLib/Handler/AppEvents.cs b/v2rayN/ServiceLib/Handler/AppEvents.cs index ccc4e5cb..a4c2917d 100644 --- a/v2rayN/ServiceLib/Handler/AppEvents.cs +++ b/v2rayN/ServiceLib/Handler/AppEvents.cs @@ -6,22 +6,22 @@ namespace ServiceLib.Handler; public static class AppEvents { public static readonly Subject ProfilesRefreshRequested = new(); - public static readonly Subject SubscriptionsRefreshRequested = new(); - - public static readonly Subject ProxiesReloadRequested = new(); + public static readonly Subject ProxiesReloadRequested = new(); + public static readonly Subject DispatcherStatisticsRequested = new(); public static readonly Subject SendSnackMsgRequested = new(); - public static readonly Subject SendMsgViewRequested = new(); public static readonly Subject AppExitRequested = new(); - public static readonly Subject ShutdownRequested = new(); public static readonly Subject AdjustMainLvColWidthRequested = new(); - public static readonly Subject DispatcherStatisticsRequested = new(); - public static readonly Subject SetDefaultServerRequested = new(); + + public static readonly Subject RoutingsMenuRefreshRequested = new(); + public static readonly Subject TestServerRequested = new(); + public static readonly Subject InboundDisplayRequested = new(); + public static readonly Subject SysProxyChangeRequested = new(); } diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 249a4322..3492670c 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -2,7 +2,6 @@ using System.Reactive; using System.Reactive.Concurrency; using ReactiveUI; using ReactiveUI.Fody.Helpers; -using Splat; namespace ServiceLib.ViewModels; @@ -240,7 +239,6 @@ public class MainWindowViewModel : MyReactiveObject BlReloadEnabled = true; await Reload(); await AutoHideStartup(); - Locator.Current.GetService()?.RefreshRoutingsMenu(); } #endregion Init @@ -433,7 +431,7 @@ public class MainWindowViewModel : MyReactiveObject var ret = await _updateView?.Invoke(EViewAction.OptionSettingWindow, null); if (ret == true) { - Locator.Current.GetService()?.InboundDisplayStatus(); + AppEvents.InboundDisplayRequested.OnNext(Unit.Default); await Reload(); } } @@ -444,7 +442,7 @@ public class MainWindowViewModel : MyReactiveObject if (ret == true) { await ConfigHandler.InitBuiltinRouting(_config); - Locator.Current.GetService()?.RefreshRoutingsMenu(); + AppEvents.RoutingsMenuRefreshRequested.OnNext(Unit.Default); await Reload(); } } @@ -518,7 +516,7 @@ public class MainWindowViewModel : MyReactiveObject await SysProxyHandler.UpdateSysProxy(_config, false); await Task.Delay(1000); }); - Locator.Current.GetService()?.TestServerAvailability(); + AppEvents.TestServerRequested.OnNext(Unit.Default); var showClashUI = _config.IsRunningCore(ECoreType.sing_box); if (showClashUI) @@ -572,7 +570,7 @@ public class MainWindowViewModel : MyReactiveObject { await ConfigHandler.ApplyRegionalPreset(_config, type); await ConfigHandler.InitRouting(_config); - Locator.Current.GetService()?.RefreshRoutingsMenu(); + AppEvents.RoutingsMenuRefreshRequested.OnNext(Unit.Default); await ConfigHandler.SaveConfig(_config); await new UpdateService().UpdateGeoFileAll(_config, UpdateTaskHandler); diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs index e51f9418..9465e6a2 100644 --- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs @@ -209,6 +209,26 @@ public class StatusBarViewModel : MyReactiveObject .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(async result => await UpdateStatistics(result)); + AppEvents.RoutingsMenuRefreshRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async _ => await RefreshRoutingsMenu()); + + AppEvents.TestServerRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async _ => await TestServerAvailability()); + + AppEvents.InboundDisplayRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async _ => await InboundDisplayStatus()); + + AppEvents.SysProxyChangeRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async result => await SetListenerType(result)); + #endregion AppEvents _ = Init(); @@ -329,7 +349,7 @@ public class StatusBarViewModel : MyReactiveObject { return; } - AppEvents.SetDefaultServerRequested.OnNext(SelectedServer.ID); + AppEvents.SetDefaultServerRequested.OnNext(SelectedServer.ID); } public async Task TestServerAvailability() @@ -364,7 +384,7 @@ public class StatusBarViewModel : MyReactiveObject #region System proxy and Routings - public async Task SetListenerType(ESysProxyType type) + private async Task SetListenerType(ESysProxyType type) { if (_config.SystemProxyItem.SysProxyType == type) { @@ -393,7 +413,7 @@ public class StatusBarViewModel : MyReactiveObject } } - public async Task RefreshRoutingsMenu() + private async Task RefreshRoutingsMenu() { RoutingItems.Clear(); @@ -501,7 +521,7 @@ public class StatusBarViewModel : MyReactiveObject #region UI - public async Task InboundDisplayStatus() + private async Task InboundDisplayStatus() { StringBuilder sb = new(); sb.Append($"[{EInboundProtocol.mixed}:{AppManager.Instance.GetLocalPort(EInboundProtocol.socks)}"); diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index d72c37a7..532bda6f 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -259,7 +259,7 @@ public partial class MainWindow : WindowBase case EGlobalHotkey.SystemProxySet: case EGlobalHotkey.SystemProxyUnchanged: case EGlobalHotkey.SystemProxyPac: - Locator.Current.GetService()?.SetListenerType((ESysProxyType)((int)e - 1)); + AppEvents.SysProxyChangeRequested.OnNext((ESysProxyType)((int)e - 1)); break; } } diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index a804775e..28073be8 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -249,7 +249,7 @@ public partial class MainWindow case EGlobalHotkey.SystemProxySet: case EGlobalHotkey.SystemProxyUnchanged: case EGlobalHotkey.SystemProxyPac: - Locator.Current.GetService()?.SetListenerType((ESysProxyType)((int)e - 1)); + AppEvents.SysProxyChangeRequested.OnNext((ESysProxyType)((int)e - 1)); break; } } @@ -287,11 +287,7 @@ public partial class MainWindow var clipboardData = WindowsUtils.GetClipboardData(); if (clipboardData.IsNotEmpty()) { - var service = Locator.Current.GetService(); - if (service != null) - { - _ = service.AddServerViaClipboardAsync(clipboardData); - } + ViewModel?.AddServerViaClipboardAsync(clipboardData); } break; diff --git a/v2rayN/v2rayN/Views/StatusBarView.xaml.cs b/v2rayN/v2rayN/Views/StatusBarView.xaml.cs index 8a3a5df4..f9b551c2 100644 --- a/v2rayN/v2rayN/Views/StatusBarView.xaml.cs +++ b/v2rayN/v2rayN/Views/StatusBarView.xaml.cs @@ -3,7 +3,6 @@ using System.Windows; using System.Windows.Input; using System.Windows.Threading; using ReactiveUI; -using Splat; using v2rayN.Manager; namespace v2rayN.Views; @@ -17,7 +16,6 @@ public partial class StatusBarView InitializeComponent(); _config = AppManager.Instance.Config; ViewModel = new StatusBarViewModel(UpdateViewHandler); - Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(StatusBarViewModel)); menuExit.Click += menuExit_Click; txtRunningServerDisplay.PreviewMouseDown += txtRunningInfoDisplay_MouseDoubleClick;