From ca8307d1a95cfc0c8a346e6897f72a636aedea14 Mon Sep 17 00:00:00 2001 From: indiff Date: Thu, 20 Apr 2023 21:18:21 +0800 Subject: [PATCH] easy add group and delete group --- .../v2rayN/ViewModels/MainWindowViewModel.cs | 99 +++++++++++++++++++ v2rayN/v2rayN/Views/MainWindow.xaml | 18 ++++ v2rayN/v2rayN/Views/MainWindow.xaml.cs | 5 + v2rayN/v2rayN/Views/SubEditWindow.xaml.cs | 14 +++ 4 files changed, 136 insertions(+) diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index c18c8e3c..68a8557b 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -13,6 +13,7 @@ using System.IO; using System.Reactive; using System.Reactive.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Windows; using System.Windows.Media; using v2rayN.Base; @@ -129,6 +130,8 @@ namespace v2rayN.ViewModels public ReactiveCommand SubSettingCmd { get; } public ReactiveCommand AddSubCmd { get; } + public ReactiveCommand AddGroupCmd { get; } + public ReactiveCommand DelGroupCmd { get; } public ReactiveCommand SubUpdateCmd { get; } public ReactiveCommand SubUpdateViaProxyCmd { get; } public ReactiveCommand SubGroupUpdateCmd { get; } @@ -434,6 +437,18 @@ namespace v2rayN.ViewModels { SubSetting(); }); + + AddGroupCmd = ReactiveCommand.Create(() => + { + AddGroup(); + }); + + DelGroupCmd = ReactiveCommand.Create(() => + { + DelGroup(); + }); + + AddSubCmd = ReactiveCommand.Create(() => { AddSub(); @@ -550,6 +565,8 @@ namespace v2rayN.ViewModels Global.ShowInTaskbar = true; } + + private void Init() { ConfigHandler.InitBuiltinRouting(ref _config); @@ -895,6 +912,25 @@ namespace v2rayN.ViewModels } } + private void InitSubscriptionViewLast() + { + _subItems.Clear(); + + _subItems.Add(new SubItem { remarks = ResUI.AllGroupServers }); + foreach (var item in LazyConfig.Instance.SubItems().OrderBy(t => t.sort)) + { + _subItems.Add(item); + } + if (_subId != null && _subItems.FirstOrDefault(t => t.id == _subId) != null) + { + SelectedSub = _subItems.FirstOrDefault(t => t.id == _subId); + } + else + { + SelectedSub = _subItems[_subItems.Count - 1]; + } + } + #endregion Servers && Groups #region Add Servers @@ -1328,6 +1364,69 @@ namespace v2rayN.ViewModels SubSelectedChanged(true); } } + + private void AddGroup() + { + SubItem newItem = new(); + var lastNum = 1; + var preffixStr = "g"; + foreach (var config in LazyConfig.Instance.SubItems().OrderBy(t => t.sort)) + { + var flag = config.remarks.StartsWith(preffixStr); + if (flag) + { + var numStr = config.remarks.Substring( 1 ); + if (Regex.IsMatch(numStr, @"^\d+$")) + { + var num = int.Parse(numStr); + if (lastNum == num) + { + lastNum += 1; + } + } + } + } + newItem.remarks = preffixStr + lastNum; + var ret = true; + + var item = LazyConfig.Instance.GetSubItem(newItem.id); + if (item is null) + { + item = newItem; + } + else + { + item.remarks = newItem.remarks; + item.url = newItem.url; + item.moreUrl = newItem.moreUrl; + item.enabled = newItem.enabled; + item.autoUpdateInterval = newItem.autoUpdateInterval; + item.userAgent = newItem.userAgent; + item.sort = newItem.sort; + item.filter = newItem.filter; + } + + ret = (ConfigHandler.AddSubItem(ref _config, item) == 0); + + if (ret == true) + { + InitSubscriptionViewLast(); + SubSelectedChanged(true); + // _noticeHandler?.Enqueue(ResUI.OperationSuccess); + } + } + + private void DelGroup() + { + // MessageBox.Show("delete"); + ConfigHandler.DeleteSubItem(ref _config, SelectedSub?.id); + InitSubscriptionViewLast(); + SubSelectedChanged(true); + + // RefreshSubItems(); + // _noticeHandler?.Enqueue(ResUI.OperationSuccess); + // v.lstGroup.SelectedItem + } private void UpdateSubscriptionProcess(string subId, bool blProxy) { diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml b/v2rayN/v2rayN/Views/MainWindow.xaml index 136cb7a9..5b83c225 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml +++ b/v2rayN/v2rayN/Views/MainWindow.xaml @@ -377,6 +377,24 @@ + + + +