diff --git a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs index c87f6737..579a2288 100644 --- a/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/CheckUpdateViewModel.cs @@ -13,11 +13,11 @@ namespace ServiceLib.ViewModels; public class CheckUpdateViewModel : MyReactiveObject { private const string _geo = "GeoFiles"; - private string _v2rayN = ECoreType.v2rayN.ToString(); + private readonly string _v2rayN = ECoreType.v2rayN.ToString(); private List _lstUpdated = []; + private static readonly string _tag = "CheckUpdateViewModel"; - private IObservableCollection _checkUpdateModel = new ObservableCollectionExtended(); - public IObservableCollection CheckUpdateModels => _checkUpdateModel; + public IObservableCollection CheckUpdateModels { get; } = new ObservableCollectionExtended(); public ReactiveCommand CheckUpdateCmd { get; } [Reactive] public bool EnableCheckPreReleaseUpdate { get; set; } @@ -26,9 +26,11 @@ public class CheckUpdateViewModel : MyReactiveObject _config = AppManager.Instance.Config; _updateView = updateView; - CheckUpdateCmd = ReactiveCommand.CreateFromTask(async () => + CheckUpdateCmd = ReactiveCommand.CreateFromTask(CheckUpdate); + CheckUpdateCmd.ThrownExceptions.Subscribe(ex => { - await CheckUpdate(); + Logging.SaveLog(_tag, ex); + _ = UpdateView(_v2rayN, ex.Message); }); EnableCheckPreReleaseUpdate = _config.CheckUpdateItem.CheckPreReleaseUpdate; @@ -43,20 +45,20 @@ public class CheckUpdateViewModel : MyReactiveObject private void RefreshCheckUpdateItems() { - _checkUpdateModel.Clear(); + CheckUpdateModels.Clear(); if (RuntimeInformation.ProcessArchitecture != Architecture.X86) { - _checkUpdateModel.Add(GetCheckUpdateModel(_v2rayN)); + CheckUpdateModels.Add(GetCheckUpdateModel(_v2rayN)); //Not Windows and under Win10 if (!(Utils.IsWindows() && Environment.OSVersion.Version.Major < 10)) { - _checkUpdateModel.Add(GetCheckUpdateModel(ECoreType.Xray.ToString())); - _checkUpdateModel.Add(GetCheckUpdateModel(ECoreType.mihomo.ToString())); - _checkUpdateModel.Add(GetCheckUpdateModel(ECoreType.sing_box.ToString())); + CheckUpdateModels.Add(GetCheckUpdateModel(ECoreType.Xray.ToString())); + CheckUpdateModels.Add(GetCheckUpdateModel(ECoreType.mihomo.ToString())); + CheckUpdateModels.Add(GetCheckUpdateModel(ECoreType.sing_box.ToString())); } } - _checkUpdateModel.Add(GetCheckUpdateModel(_geo)); + CheckUpdateModels.Add(GetCheckUpdateModel(_geo)); } private CheckUpdateModel GetCheckUpdateModel(string coreType) @@ -71,7 +73,7 @@ public class CheckUpdateViewModel : MyReactiveObject private async Task SaveSelectedCoreTypes() { - _config.CheckUpdateItem.SelectedCoreTypes = _checkUpdateModel.Where(t => t.IsSelected == true).Select(t => t.CoreType ?? "").ToList(); + _config.CheckUpdateItem.SelectedCoreTypes = CheckUpdateModels.Where(t => t.IsSelected == true).Select(t => t.CoreType ?? "").ToList(); await ConfigHandler.SaveConfig(_config); } @@ -83,13 +85,13 @@ public class CheckUpdateViewModel : MyReactiveObject private async Task CheckUpdateTask() { _lstUpdated.Clear(); - _lstUpdated = _checkUpdateModel.Where(x => x.IsSelected == true) + _lstUpdated = CheckUpdateModels.Where(x => x.IsSelected == true) .Select(x => new CheckUpdateModel() { CoreType = x.CoreType }).ToList(); await SaveSelectedCoreTypes(); - for (var k = _checkUpdateModel.Count - 1; k >= 0; k--) + for (var k = CheckUpdateModels.Count - 1; k >= 0; k--) { - var item = _checkUpdateModel[k]; + var item = CheckUpdateModels[k]; if (item.IsSelected != true) { continue; @@ -320,7 +322,7 @@ public class CheckUpdateViewModel : MyReactiveObject public async Task UpdateViewResult(CheckUpdateModel model) { - var found = _checkUpdateModel.FirstOrDefault(t => t.CoreType == model.CoreType); + var found = CheckUpdateModels.FirstOrDefault(t => t.CoreType == model.CoreType); if (found == null) { return; @@ -328,6 +330,6 @@ public class CheckUpdateViewModel : MyReactiveObject var itemCopy = JsonUtils.DeepCopy(found); itemCopy.Remarks = model.Remarks; - _checkUpdateModel.Replace(found, itemCopy); + CheckUpdateModels.Replace(found, itemCopy); } } diff --git a/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs index 1fd9015c..d45b8e7d 100644 --- a/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ClashConnectionsViewModel.cs @@ -10,8 +10,7 @@ namespace ServiceLib.ViewModels; public class ClashConnectionsViewModel : MyReactiveObject { - private IObservableCollection _connectionItems = new ObservableCollectionExtended(); - public IObservableCollection ConnectionItems => _connectionItems; + public IObservableCollection ConnectionItems { get; } = new ObservableCollectionExtended(); [Reactive] public ClashConnectionModel SelectedSource { get; set; } @@ -74,7 +73,7 @@ public class ClashConnectionsViewModel : MyReactiveObject public async Task RefreshConnections(List? connections) { - _connectionItems.Clear(); + ConnectionItems.Clear(); var dtNow = DateTime.Now; var lstModel = new List(); @@ -104,7 +103,7 @@ public class ClashConnectionsViewModel : MyReactiveObject return; } - _connectionItems.AddRange(lstModel); + ConnectionItems.AddRange(lstModel); } public async Task ClashConnectionClose(bool all) @@ -121,7 +120,7 @@ public class ClashConnectionsViewModel : MyReactiveObject } else { - _connectionItems.Clear(); + ConnectionItems.Clear(); } await ClashApiManager.Instance.ClashConnectionClose(id); await GetClashConnections(); diff --git a/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs index 531b4a8e..54cbdc8d 100644 --- a/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ClashProxiesViewModel.cs @@ -17,11 +17,8 @@ public class ClashProxiesViewModel : MyReactiveObject private Dictionary? _providers; private readonly int _delayTimeout = 99999999; - private IObservableCollection _proxyGroups = new ObservableCollectionExtended(); - private IObservableCollection _proxyDetails = new ObservableCollectionExtended(); - - public IObservableCollection ProxyGroups => _proxyGroups; - public IObservableCollection ProxyDetails => _proxyDetails; + public IObservableCollection ProxyGroups { get; } = new ObservableCollectionExtended(); + public IObservableCollection ProxyDetails { get; } = new ObservableCollectionExtended(); [Reactive] public ClashProxyModel SelectedGroup { get; set; } @@ -182,7 +179,7 @@ public class ClashProxiesViewModel : MyReactiveObject } var selectedName = SelectedGroup?.Name; - _proxyGroups.Clear(); + ProxyGroups.Clear(); var proxyGroups = ClashApiManager.Instance.GetClashProxyGroups(); if (proxyGroups != null && proxyGroups.Count > 0) @@ -198,7 +195,7 @@ public class ClashProxiesViewModel : MyReactiveObject { continue; } - _proxyGroups.Add(new ClashProxyModel() + ProxyGroups.Add(new ClashProxyModel() { Now = item.now, Name = item.name, @@ -214,12 +211,12 @@ public class ClashProxiesViewModel : MyReactiveObject { continue; } - var item = _proxyGroups.FirstOrDefault(t => t.Name == kv.Key); + var item = ProxyGroups.FirstOrDefault(t => t.Name == kv.Key); if (item != null && item.Name.IsNotEmpty()) { continue; } - _proxyGroups.Add(new ClashProxyModel() + ProxyGroups.Add(new ClashProxyModel() { Now = kv.Value.now, Name = kv.Key, @@ -227,15 +224,15 @@ public class ClashProxiesViewModel : MyReactiveObject }); } - if (_proxyGroups != null && _proxyGroups.Count > 0) + if (ProxyGroups != null && ProxyGroups.Count > 0) { - if (selectedName != null && _proxyGroups.Any(t => t.Name == selectedName)) + if (selectedName != null && ProxyGroups.Any(t => t.Name == selectedName)) { - SelectedGroup = _proxyGroups.FirstOrDefault(t => t.Name == selectedName); + SelectedGroup = ProxyGroups.FirstOrDefault(t => t.Name == selectedName); } else { - SelectedGroup = _proxyGroups.First(); + SelectedGroup = ProxyGroups.First(); } } else @@ -246,7 +243,7 @@ public class ClashProxiesViewModel : MyReactiveObject private void RefreshProxyDetails(bool c) { - _proxyDetails.Clear(); + ProxyDetails.Clear(); if (!c) { return; @@ -299,7 +296,7 @@ public class ClashProxiesViewModel : MyReactiveObject default: break; } - _proxyDetails.AddRange(lstDetails); + ProxyDetails.AddRange(lstDetails); } private ProxiesItem? TryGetProxy(string name) @@ -361,12 +358,12 @@ public class ClashProxiesViewModel : MyReactiveObject await ClashApiManager.Instance.ClashSetActiveProxy(name, nameNode); selectedProxy.now = nameNode; - var group = _proxyGroups.FirstOrDefault(it => it.Name == SelectedGroup.Name); + var group = ProxyGroups.FirstOrDefault(it => it.Name == SelectedGroup.Name); if (group != null) { group.Now = nameNode; var group2 = JsonUtils.DeepCopy(group); - _proxyGroups.Replace(group, group2); + ProxyGroups.Replace(group, group2); SelectedGroup = group2; } @@ -375,7 +372,7 @@ public class ClashProxiesViewModel : MyReactiveObject private async Task ProxiesDelayTest(bool blAll = true) { - ClashApiManager.Instance.ClashProxiesDelayTest(blAll, _proxyDetails.ToList(), async (item, result) => + ClashApiManager.Instance.ClashProxiesDelayTest(blAll, ProxyDetails.ToList(), async (item, result) => { if (item == null || result.IsNullOrEmpty()) { @@ -395,7 +392,7 @@ public class ClashProxiesViewModel : MyReactiveObject public async Task ProxiesDelayTestResult(SpeedTestResult result) { //UpdateHandler(false, $"{item.name}={result}"); - var detail = _proxyDetails.FirstOrDefault(it => it.Name == result.IndexId); + var detail = ProxyDetails.FirstOrDefault(it => it.Name == result.IndexId); if (detail == null) { return; @@ -417,7 +414,7 @@ public class ClashProxiesViewModel : MyReactiveObject detail.Delay = _delayTimeout; detail.DelayName = string.Empty; } - _proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail)); + ProxyDetails.Replace(detail, JsonUtils.DeepCopy(detail)); } #endregion proxy function diff --git a/v2rayN/ServiceLib/ViewModels/MsgViewModel.cs b/v2rayN/ServiceLib/ViewModels/MsgViewModel.cs index 87a3016c..8fc62dfe 100644 --- a/v2rayN/ServiceLib/ViewModels/MsgViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MsgViewModel.cs @@ -8,8 +8,8 @@ namespace ServiceLib.ViewModels; public class MsgViewModel : MyReactiveObject { - private ConcurrentQueue _queueMsg = new(); - private int _numMaxMsg = 500; + private readonly ConcurrentQueue _queueMsg = new(); + private readonly int _numMaxMsg = 500; private bool _lastMsgFilterNotAvailable; private bool _blLockShow = false; diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index 058855a8..5209f4dd 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -23,13 +23,9 @@ public class ProfilesViewModel : MyReactiveObject #region ObservableCollection - private IObservableCollection _profileItems = new ObservableCollectionExtended(); - public IObservableCollection ProfileItems => _profileItems; + public IObservableCollection ProfileItems { get; } = new ObservableCollectionExtended(); - private IObservableCollection _subItems = new ObservableCollectionExtended(); - public IObservableCollection SubItems => _subItems; - - private IObservableCollection _servers = new ObservableCollectionExtended(); + public IObservableCollection SubItems { get; } = new ObservableCollectionExtended(); [Reactive] public ProfileItemModel SelectedProfile { get; set; } @@ -293,7 +289,7 @@ public class ProfilesViewModel : MyReactiveObject NoticeManager.Instance.Enqueue(result.Delay); return; } - var item = _profileItems.FirstOrDefault(it => it.IndexId == result.IndexId); + var item = ProfileItems.FirstOrDefault(it => it.IndexId == result.IndexId); if (item == null) { return; @@ -323,7 +319,7 @@ public class ProfilesViewModel : MyReactiveObject try { - var item = _profileItems.FirstOrDefault(it => it.IndexId == update.IndexId); + var item = ProfileItems.FirstOrDefault(it => it.IndexId == update.IndexId); if (item != null) { item.TodayDown = Utils.HumanFy(update.TodayDown); @@ -390,8 +386,8 @@ public class ProfilesViewModel : MyReactiveObject var lstModel = await GetProfileItemsEx(_config.SubIndexId, _serverFilter); _lstProfile = JsonUtils.Deserialize>(JsonUtils.Serialize(lstModel)) ?? []; - _profileItems.Clear(); - _profileItems.AddRange(lstModel); + ProfileItems.Clear(); + ProfileItems.AddRange(lstModel); if (lstModel.Count > 0) { var selected = lstModel.FirstOrDefault(t => t.IndexId == _config.IndexId); @@ -410,21 +406,21 @@ public class ProfilesViewModel : MyReactiveObject public async Task RefreshSubscriptions() { - _subItems.Clear(); + SubItems.Clear(); - _subItems.Add(new SubItem { Remarks = ResUI.AllGroupServers }); + SubItems.Add(new SubItem { Remarks = ResUI.AllGroupServers }); foreach (var item in await AppManager.Instance.SubItems()) { - _subItems.Add(item); + SubItems.Add(item); } - if (_config.SubIndexId != null && _subItems.FirstOrDefault(t => t.Id == _config.SubIndexId) != null) + if (_config.SubIndexId != null && SubItems.FirstOrDefault(t => t.Id == _config.SubIndexId) != null) { - SelectedSub = _subItems.FirstOrDefault(t => t.Id == _config.SubIndexId); + SelectedSub = SubItems.FirstOrDefault(t => t.Id == _config.SubIndexId); } else { - SelectedSub = _subItems.First(); + SelectedSub = SubItems.First(); } } @@ -548,9 +544,9 @@ public class ProfilesViewModel : MyReactiveObject await ConfigHandler.RemoveServers(_config, lstSelected); NoticeManager.Instance.Enqueue(ResUI.OperationSuccess); - if (lstSelected.Count == _profileItems.Count) + if (lstSelected.Count == ProfileItems.Count) { - _profileItems.Clear(); + ProfileItems.Clear(); } await RefreshServers(); if (exists) @@ -740,7 +736,7 @@ public class ProfilesViewModel : MyReactiveObject public async Task MoveServerTo(int startIndex, ProfileItemModel targetItem) { - var targetIndex = _profileItems.IndexOf(targetItem); + var targetIndex = ProfileItems.IndexOf(targetItem); if (startIndex >= 0 && targetIndex >= 0 && startIndex != targetIndex) { if (await ConfigHandler.MoveServer(_config, _lstProfile, startIndex, EMove.Position, targetIndex) == 0) @@ -754,7 +750,7 @@ public class ProfilesViewModel : MyReactiveObject { if (actionType == ESpeedActionType.Mixedtest) { - SelectedProfiles = _profileItems; + SelectedProfiles = ProfileItems; } var lstSelected = await GetProfileItems(false); if (lstSelected == null) diff --git a/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs index c3acc7ba..f176e29b 100644 --- a/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingRuleSettingViewModel.cs @@ -13,9 +13,7 @@ public class RoutingRuleSettingViewModel : MyReactiveObject [Reactive] public RoutingItem SelectedRouting { get; set; } - - private IObservableCollection _rulesItems = new ObservableCollectionExtended(); - public IObservableCollection RulesItems => _rulesItems; + public IObservableCollection RulesItems { get; } = new ObservableCollectionExtended(); [Reactive] public RulesItemModel SelectedSource { get; set; } @@ -101,7 +99,7 @@ public class RoutingRuleSettingViewModel : MyReactiveObject public void RefreshRulesItems() { - _rulesItems.Clear(); + RulesItems.Clear(); foreach (var item in _rules) { @@ -118,7 +116,7 @@ public class RoutingRuleSettingViewModel : MyReactiveObject Enabled = item.Enabled, Remarks = item.Remarks, }; - _rulesItems.Add(it); + RulesItems.Add(it); } } diff --git a/v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs index 8aa36f0c..5237a8d2 100644 --- a/v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs @@ -9,8 +9,7 @@ public class RoutingSettingViewModel : MyReactiveObject { #region Reactive - private IObservableCollection _routingItems = new ObservableCollectionExtended(); - public IObservableCollection RoutingItems => _routingItems; + public IObservableCollection RoutingItems { get; } = new ObservableCollectionExtended(); [Reactive] public RoutingItemModel SelectedSource { get; set; } @@ -82,7 +81,7 @@ public class RoutingSettingViewModel : MyReactiveObject public async Task RefreshRoutingItems() { - _routingItems.Clear(); + RoutingItems.Clear(); var routings = await AppManager.Instance.RoutingItems(); foreach (var item in routings) @@ -98,7 +97,7 @@ public class RoutingSettingViewModel : MyReactiveObject CustomRulesetPath4Singbox = item.CustomRulesetPath4Singbox, Sort = item.Sort, }; - _routingItems.Add(it); + RoutingItems.Add(it); } } diff --git a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs index c89f55cb..e9ee033e 100644 --- a/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/StatusBarViewModel.cs @@ -13,11 +13,9 @@ public class StatusBarViewModel : MyReactiveObject { #region ObservableCollection - private IObservableCollection _routingItems = new ObservableCollectionExtended(); - public IObservableCollection RoutingItems => _routingItems; + public IObservableCollection RoutingItems { get; } = new ObservableCollectionExtended(); - private IObservableCollection _servers = new ObservableCollectionExtended(); - public IObservableCollection Servers => _servers; + public IObservableCollection Servers { get; } = new ObservableCollectionExtended(); [Reactive] public RoutingItem SelectedRouting { get; set; } @@ -295,7 +293,7 @@ public class StatusBarViewModel : MyReactiveObject { var lstModel = await AppManager.Instance.ProfileItems(_config.SubIndexId, ""); - _servers.Clear(); + Servers.Clear(); if (lstModel.Count > _config.GuiItem.TrayMenuServersLimit) { BlServers = false; @@ -309,7 +307,7 @@ public class StatusBarViewModel : MyReactiveObject string name = it.GetSummary(); var item = new ComboItem() { ID = it.IndexId, Text = name }; - _servers.Add(item); + Servers.Add(item); if (_config.IndexId == it.IndexId) { SelectedServer = item; @@ -397,13 +395,13 @@ public class StatusBarViewModel : MyReactiveObject public async Task RefreshRoutingsMenu() { - _routingItems.Clear(); + RoutingItems.Clear(); BlRouting = true; var routings = await AppManager.Instance.RoutingItems(); foreach (var item in routings) { - _routingItems.Add(item); + RoutingItems.Add(item); if (item.IsActive) { SelectedRouting = item; diff --git a/v2rayN/ServiceLib/ViewModels/SubSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/SubSettingViewModel.cs index e9c657c4..88f33619 100644 --- a/v2rayN/ServiceLib/ViewModels/SubSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/SubSettingViewModel.cs @@ -8,8 +8,7 @@ namespace ServiceLib.ViewModels; public class SubSettingViewModel : MyReactiveObject { - private IObservableCollection _subItems = new ObservableCollectionExtended(); - public IObservableCollection SubItems => _subItems; + public IObservableCollection SubItems { get; } = new ObservableCollectionExtended(); [Reactive] public SubItem SelectedSource { get; set; } @@ -60,8 +59,8 @@ public class SubSettingViewModel : MyReactiveObject public async Task RefreshSubItems() { - _subItems.Clear(); - _subItems.AddRange(await AppManager.Instance.SubItems()); + SubItems.Clear(); + SubItems.AddRange(await AppManager.Instance.SubItems()); } public async Task EditSubAsync(bool blNew)