From 27b45aee8312edda910b98460a73a00ecc8e778d Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:39:55 +0800 Subject: [PATCH] Optimization and improvement, using event subscribers --- v2rayN/ServiceLib/Handler/AppEvents.cs | 4 ++++ .../ServiceLib/ViewModels/MainWindowViewModel.cs | 2 +- v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs | 14 ++++++++++++-- v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs | 2 +- v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs | 2 -- v2rayN/v2rayN/Views/ProfilesView.xaml.cs | 2 -- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/AppEvents.cs b/v2rayN/ServiceLib/Handler/AppEvents.cs index 109ee762..1cf3204b 100644 --- a/v2rayN/ServiceLib/Handler/AppEvents.cs +++ b/v2rayN/ServiceLib/Handler/AppEvents.cs @@ -7,6 +7,8 @@ public static class AppEvents { public static readonly Subject ProfilesRefreshRequested = new(); + public static readonly Subject SubscriptionsRefreshRequested = new(); + public static readonly Subject SendSnackMsgRequested = new(); public static readonly Subject SendMsgViewRequested = new(); @@ -18,4 +20,6 @@ public static class AppEvents public static readonly Subject AdjustMainLvColWidthRequested = new(); public static readonly Subject DispatcherStatisticsRequested = new(); + + public static readonly Subject SetDefaultServerRequested = new(); } diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 4e9fc9fc..4bc9997d 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -301,7 +301,7 @@ public class MainWindowViewModel : MyReactiveObject private void RefreshSubscriptions() { - Locator.Current.GetService()?.RefreshSubscriptions(); + AppEvents.SubscriptionsRefreshRequested.OnNext(Unit.Default); } #endregion Servers && Groups diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index 4c0fd2b9..87e1d78f 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -240,11 +240,21 @@ public class ProfilesViewModel : MyReactiveObject .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(async _ => await RefreshServersBiz()); + AppEvents.SubscriptionsRefreshRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async _ => await RefreshSubscriptions()); + AppEvents.DispatcherStatisticsRequested .AsObservable() .ObserveOn(RxApp.MainThreadScheduler) .Subscribe(async result => await UpdateStatistics(result)); + AppEvents.SetDefaultServerRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async indexId => await SetDefaultServer(indexId)); + #endregion AppEvents _ = Init(); @@ -380,7 +390,7 @@ public class ProfilesViewModel : MyReactiveObject await _updateView?.Invoke(EViewAction.DispatcherRefreshServersBiz, null); } - public async Task RefreshSubscriptions() + private async Task RefreshSubscriptions() { SubItems.Clear(); @@ -565,7 +575,7 @@ public class ProfilesViewModel : MyReactiveObject await SetDefaultServer(SelectedProfile.IndexId); } - public async Task SetDefaultServer(string? indexId) + private async Task SetDefaultServer(string? indexId) { if (indexId.IsNullOrEmpty()) { diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs index e9ee033e..e51f9418 100644 --- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs @@ -329,7 +329,7 @@ public class StatusBarViewModel : MyReactiveObject { return; } - Locator.Current.GetService()?.SetDefaultServer(SelectedServer.ID); + AppEvents.SetDefaultServerRequested.OnNext(SelectedServer.ID); } public async Task TestServerAvailability() diff --git a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs index 5bade399..88763851 100644 --- a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs @@ -8,7 +8,6 @@ using Avalonia.Threading; using DialogHostAvalonia; using MsBox.Avalonia.Enums; using ReactiveUI; -using Splat; using v2rayN.Desktop.Common; namespace v2rayN.Desktop.Views; @@ -48,7 +47,6 @@ public partial class ProfilesView : ReactiveUserControl //} ViewModel = new ProfilesViewModel(UpdateViewHandler); - Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(ProfilesViewModel)); this.WhenActivated(disposables => { diff --git a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs index 8ef236ab..3c46966c 100644 --- a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs +++ b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs @@ -8,7 +8,6 @@ using System.Windows.Media; using System.Windows.Threading; using MaterialDesignThemes.Wpf; using ReactiveUI; -using Splat; using v2rayN.Base; using Point = System.Windows.Point; @@ -42,7 +41,6 @@ public partial class ProfilesView } ViewModel = new ProfilesViewModel(UpdateViewHandler); - Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(ProfilesViewModel)); this.WhenActivated(disposables => {