diff --git a/v2rayN/ServiceLib/Enums/EMsgCommand.cs b/v2rayN/ServiceLib/Enums/EMsgCommand.cs index 23be4ac1..fe7d01cc 100644 --- a/v2rayN/ServiceLib/Enums/EMsgCommand.cs +++ b/v2rayN/ServiceLib/Enums/EMsgCommand.cs @@ -5,6 +5,5 @@ public enum EMsgCommand ClearMsg, SendMsgView, SendSnackMsg, - RefreshProfiles, AppExit } diff --git a/v2rayN/ServiceLib/Handler/AppEvents.cs b/v2rayN/ServiceLib/Handler/AppEvents.cs new file mode 100644 index 00000000..b2992721 --- /dev/null +++ b/v2rayN/ServiceLib/Handler/AppEvents.cs @@ -0,0 +1,9 @@ +using System.Reactive; +using System.Reactive.Subjects; + +namespace ServiceLib.Handler; + +public static class AppEvents +{ + public static readonly Subject ProfilesRefreshRequested = new(); +} diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index eab61655..1e64d22a 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -260,7 +260,7 @@ public class MainWindowViewModel : MyReactiveObject if (success) { var indexIdOld = _config.IndexId; - RefreshServers(); + await RefreshServers(); if (indexIdOld != _config.IndexId) { await Reload(); @@ -350,9 +350,11 @@ public class MainWindowViewModel : MyReactiveObject #region Servers && Groups - private void RefreshServers() + private async Task RefreshServers() { - MessageBus.Current.SendMessage("", EMsgCommand.RefreshProfiles.ToString()); + AppEvents.ProfilesRefreshRequested.OnNext(Unit.Default); + + await Task.Delay(200); } private void RefreshSubscriptions() @@ -384,7 +386,7 @@ public class MainWindowViewModel : MyReactiveObject } if (ret == true) { - RefreshServers(); + await RefreshServers(); if (item.IndexId == _config.IndexId) { await Reload(); @@ -399,11 +401,11 @@ public class MainWindowViewModel : MyReactiveObject await _updateView?.Invoke(EViewAction.AddServerViaClipboard, null); return; } - int ret = await ConfigHandler.AddBatchServers(_config, clipboardData, _config.SubIndexId, false); + var ret = await ConfigHandler.AddBatchServers(_config, clipboardData, _config.SubIndexId, false); if (ret > 0) { RefreshSubscriptions(); - RefreshServers(); + await RefreshServers(); NoticeManager.Instance.Enqueue(string.Format(ResUI.SuccessfullyImportedServerViaClipboard, ret)); } else @@ -449,11 +451,11 @@ public class MainWindowViewModel : MyReactiveObject } else { - int ret = await ConfigHandler.AddBatchServers(_config, result, _config.SubIndexId, false); + var ret = await ConfigHandler.AddBatchServers(_config, result, _config.SubIndexId, false); if (ret > 0) { RefreshSubscriptions(); - RefreshServers(); + await RefreshServers(); NoticeManager.Instance.Enqueue(ResUI.SuccessfullyImportedServerViaScan); } else @@ -532,7 +534,7 @@ public class MainWindowViewModel : MyReactiveObject private async Task ClearServerStatistics() { await StatisticsManager.Instance.ClearAllServerStatistics(); - RefreshServers(); + await RefreshServers(); } private async Task OpenTheFileLocation() diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index 8362ecea..49e47949 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -126,7 +126,7 @@ public class ProfilesViewModel : MyReactiveObject this.WhenAnyValue( x => x.ServerFilter, y => y != null && _serverFilter != y) - .Subscribe(c => ServerFilterChanged(c)); + .Subscribe(async c => await ServerFilterChanged(c)); //servers delete EditServerCmd = ReactiveCommand.CreateFromTask(async () => @@ -249,7 +249,10 @@ public class ProfilesViewModel : MyReactiveObject if (_updateView != null) { - MessageBus.Current.Listen(EMsgCommand.RefreshProfiles.ToString()).Subscribe(OnNext); + AppEvents.ProfilesRefreshRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async _ => await RefreshServersBiz());//.DisposeWith(_disposables); } _ = Init(); @@ -263,18 +266,13 @@ public class ProfilesViewModel : MyReactiveObject SelectedServer = new(); await RefreshSubscriptions(); - RefreshServers(); + await RefreshServers(); } #endregion Init #region Actions - private async void OnNext(string x) - { - await _updateView?.Invoke(EViewAction.DispatcherRefreshServersBiz, null); - } - private void Reload() { Locator.Current.GetService()?.Reload(); @@ -353,12 +351,12 @@ public class ProfilesViewModel : MyReactiveObject } _config.SubIndexId = SelectedSub?.Id; - RefreshServers(); + await RefreshServers(); await _updateView?.Invoke(EViewAction.ProfilesFocus, null); } - private void ServerFilterChanged(bool c) + private async Task ServerFilterChanged(bool c) { if (!c) { @@ -367,16 +365,18 @@ public class ProfilesViewModel : MyReactiveObject _serverFilter = ServerFilter; if (_serverFilter.IsNullOrEmpty()) { - RefreshServers(); + await RefreshServers(); } } - public void RefreshServers() + public async Task RefreshServers() { - MessageBus.Current.SendMessage("", EMsgCommand.RefreshProfiles.ToString()); + AppEvents.ProfilesRefreshRequested.OnNext(Unit.Default); + + await Task.Delay(200); } - public async Task RefreshServersBiz() + private async Task RefreshServersBiz() { var lstModel = await GetProfileItemsEx(_config.SubIndexId, _serverFilter); _lstProfile = JsonUtils.Deserialize>(JsonUtils.Serialize(lstModel)) ?? []; @@ -395,6 +395,8 @@ public class ProfilesViewModel : MyReactiveObject SelectedProfile = lstModel.First(); } } + + await _updateView?.Invoke(EViewAction.DispatcherRefreshServersBiz, null); } public async Task RefreshSubscriptions() @@ -514,7 +516,7 @@ public class ProfilesViewModel : MyReactiveObject } if (ret == true) { - RefreshServers(); + await RefreshServers(); if (item.IndexId == _config.IndexId) { Reload(); @@ -541,7 +543,7 @@ public class ProfilesViewModel : MyReactiveObject { _profileItems.Clear(); } - RefreshServers(); + await RefreshServers(); if (exists) { Reload(); @@ -553,7 +555,7 @@ public class ProfilesViewModel : MyReactiveObject var tuple = await ConfigHandler.DedupServerList(_config, _config.SubIndexId); if (tuple.Item1 > 0 || tuple.Item2 > 0) { - RefreshServers(); + await RefreshServers(); Reload(); } NoticeManager.Instance.Enqueue(string.Format(ResUI.RemoveDuplicateServerResult, tuple.Item1, tuple.Item2)); @@ -568,7 +570,7 @@ public class ProfilesViewModel : MyReactiveObject } if (await ConfigHandler.CopyServer(_config, lstSelected) == 0) { - RefreshServers(); + await RefreshServers(); NoticeManager.Instance.Enqueue(ResUI.OperationSuccess); } } @@ -601,7 +603,7 @@ public class ProfilesViewModel : MyReactiveObject if (await ConfigHandler.SetDefaultServerIndex(_config, indexId) == 0) { - RefreshServers(); + await RefreshServers(); Reload(); } } @@ -652,7 +654,7 @@ public class ProfilesViewModel : MyReactiveObject } if (ret?.Data?.ToString() == _config.IndexId) { - RefreshServers(); + await RefreshServers(); Reload(); } else @@ -675,13 +677,13 @@ public class ProfilesViewModel : MyReactiveObject return; } _dicHeaderSort[colName] = !asc; - RefreshServers(); + await RefreshServers(); } public async Task RemoveInvalidServerResult() { var count = await ConfigHandler.RemoveInvalidServerResult(_config, _config.SubIndexId); - RefreshServers(); + await RefreshServers(); NoticeManager.Instance.Enqueue(string.Format(ResUI.RemoveInvalidServerResultTip, count)); } @@ -702,7 +704,7 @@ public class ProfilesViewModel : MyReactiveObject await ConfigHandler.MoveToGroup(_config, lstSelected, SelectedMoveToGroup.Id); NoticeManager.Instance.Enqueue(ResUI.OperationSuccess); - RefreshServers(); + await RefreshServers(); SelectedMoveToGroup = null; SelectedMoveToGroup = new(); } @@ -723,7 +725,7 @@ public class ProfilesViewModel : MyReactiveObject } if (await ConfigHandler.MoveServer(_config, _lstProfile, index, eMove) == 0) { - RefreshServers(); + await RefreshServers(); } } @@ -734,7 +736,7 @@ public class ProfilesViewModel : MyReactiveObject { if (await ConfigHandler.MoveServer(_config, _lstProfile, startIndex, EMove.Position, targetIndex) == 0) { - RefreshServers(); + await RefreshServers(); } } } diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs index ae097c3c..1be54da7 100644 --- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs @@ -1,4 +1,5 @@ using System.Reactive; +using System.Reactive.Linq; using System.Text; using DynamicData.Binding; using ReactiveUI; @@ -216,15 +217,13 @@ public class StatusBarViewModel : MyReactiveObject _updateView = updateView; if (_updateView != null) { - MessageBus.Current.Listen(EMsgCommand.RefreshProfiles.ToString()).Subscribe(OnNext); + AppEvents.ProfilesRefreshRequested + .AsObservable() + .ObserveOn(RxApp.MainThreadScheduler) + .Subscribe(async _ => await RefreshServersBiz()); //.DisposeWith(_disposables); } } - private async void OnNext(string x) - { - await _updateView?.Invoke(EViewAction.DispatcherRefreshServersBiz, null); - } - private async Task CopyProxyCmdToClipboard() { var cmd = Utils.IsWindows() ? "set" : "export"; @@ -263,7 +262,7 @@ public class StatusBarViewModel : MyReactiveObject await service.UpdateSubscriptionProcess("", blProxy); } - public async Task RefreshServersBiz() + private async Task RefreshServersBiz() { await RefreshServersMenu(); diff --git a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs index 492dc237..73115ed7 100644 --- a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs @@ -187,11 +187,7 @@ public partial class ProfilesView : ReactiveUserControl break; case EViewAction.DispatcherRefreshServersBiz: - Dispatcher.UIThread.Post(() => - { - _ = RefreshServersBiz(); - }, - DispatcherPriority.Default); + Dispatcher.UIThread.Post(RefreshServersBiz, DispatcherPriority.Default); break; } @@ -209,13 +205,8 @@ public partial class ProfilesView : ReactiveUserControl await DialogHost.Show(dialog); } - public async Task RefreshServersBiz() + public void RefreshServersBiz() { - if (ViewModel != null) - { - await ViewModel.RefreshServersBiz(); - } - if (lstProfiles.SelectedIndex >= 0) { lstProfiles.ScrollIntoView(lstProfiles.SelectedItem, null); diff --git a/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs index 34ae1953..e4458721 100644 --- a/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs @@ -63,12 +63,7 @@ public partial class StatusBarView : ReactiveUserControl ViewModel?.TestServerAvailabilityResult((string)obj), DispatcherPriority.Default); break; - - case EViewAction.DispatcherRefreshServersBiz: - Dispatcher.UIThread.Post(() => - ViewModel?.RefreshServersBiz(), - DispatcherPriority.Default); - break; + case EViewAction.DispatcherRefreshIcon: Dispatcher.UIThread.Post(() => diff --git a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs index 265325d3..456e6ddd 100644 --- a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs +++ b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs @@ -168,10 +168,7 @@ public partial class ProfilesView break; case EViewAction.DispatcherRefreshServersBiz: - Application.Current?.Dispatcher.Invoke((() => - { - _ = RefreshServersBiz(); - }), DispatcherPriority.Normal); + Application.Current?.Dispatcher.Invoke(RefreshServersBiz, DispatcherPriority.Normal); break; } @@ -190,13 +187,8 @@ public partial class ProfilesView await DialogHost.Show(dialog, "RootDialog"); } - public async Task RefreshServersBiz() + public void RefreshServersBiz() { - if (ViewModel != null) - { - await ViewModel.RefreshServersBiz(); - } - if (lstProfiles.SelectedIndex > 0) { lstProfiles.ScrollIntoView(lstProfiles.SelectedItem, null); diff --git a/v2rayN/v2rayN/Views/StatusBarView.xaml.cs b/v2rayN/v2rayN/Views/StatusBarView.xaml.cs index db92793d..c29fbdc3 100644 --- a/v2rayN/v2rayN/Views/StatusBarView.xaml.cs +++ b/v2rayN/v2rayN/Views/StatusBarView.xaml.cs @@ -86,12 +86,7 @@ public partial class StatusBarView }), DispatcherPriority.Normal); break; - case EViewAction.DispatcherRefreshServersBiz: - Application.Current?.Dispatcher.Invoke((() => - { - ViewModel?.RefreshServersBiz(); - }), DispatcherPriority.Normal); - break; + case EViewAction.DispatcherRefreshIcon: Application.Current?.Dispatcher.Invoke((async () =>