diff --git a/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Pages/ProxyScriptManagePage.axaml b/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Pages/ProxyScriptManagePage.axaml index cec173cab..f12891dae 100644 --- a/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Pages/ProxyScriptManagePage.axaml +++ b/src/ST.Client.Desktop.Avalonia/Application/UI/Views/Pages/ProxyScriptManagePage.axaml @@ -10,117 +10,118 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" + x:Name="u" Background="Transparent" x:DataType="vm:ProxyScriptManagePageViewModel" x:CompileBindings="True" x:Class="System.Application.UI.Views.Pages.ProxyScriptManagePage"> - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ST.Client.Desktop/Services/Mvvm/ProxyService.cs b/src/ST.Client.Desktop/Services/Mvvm/ProxyService.cs index 3b2fa9e9a..92c3aa121 100644 --- a/src/ST.Client.Desktop/Services/Mvvm/ProxyService.cs +++ b/src/ST.Client.Desktop/Services/Mvvm/ProxyService.cs @@ -15,287 +15,287 @@ using System.Windows; namespace System.Application.Services { - public class ProxyService : ReactiveObject - { - public static ProxyService Current { get; } = new ProxyService(); - readonly IHttpProxyService httpProxyService = DI.Get(); + public class ProxyService : ReactiveObject + { + public static ProxyService Current { get; } = new ProxyService(); + readonly IHttpProxyService httpProxyService = DI.Get(); - private ObservableCollection? _ProxyDomains; - public ObservableCollection? ProxyDomains - { - get => _ProxyDomains; - set - { - if (_ProxyDomains != value) - { - _ProxyDomains = value; - this.RaisePropertyChanged(); - } - } - } + private ObservableCollection? _ProxyDomains; + public ObservableCollection? ProxyDomains + { + get => _ProxyDomains; + set + { + if (_ProxyDomains != value) + { + _ProxyDomains = value; + this.RaisePropertyChanged(); + } + } + } - private AccelerateProjectGroupDTO? _SelectGroup; - public AccelerateProjectGroupDTO? SelectGroup - { - get => _SelectGroup; - set => this.RaiseAndSetIfChanged(ref _SelectGroup, value); - } + private AccelerateProjectGroupDTO? _SelectGroup; + public AccelerateProjectGroupDTO? SelectGroup + { + get => _SelectGroup; + set => this.RaiseAndSetIfChanged(ref _SelectGroup, value); + } - private ObservableCollection? _ProxyScripts; - public ObservableCollection? ProxyScripts - { - get => _ProxyScripts; - set - { - if (_ProxyScripts != value) - { - _ProxyScripts = value; - this.RaisePropertyChanged(); - } - } - } + private ObservableCollection? _ProxyScripts; + public ObservableCollection? ProxyScripts + { + get => _ProxyScripts; + set + { + if (_ProxyScripts != value) + { + _ProxyScripts = value; + this.RaisePropertyChanged(); + } + } + } - public IReadOnlyCollection? EnableProxyDomains - { - get - { - if (!ProxyDomains.Any_Nullable()) - return null; - return ProxyDomains.SelectMany(s => - { - return s.Items.Where(w => w.Enable); - }).ToArray(); - } - } + public IReadOnlyCollection? EnableProxyDomains + { + get + { + if (!ProxyDomains.Any_Nullable()) + return null; + return ProxyDomains.SelectMany(s => + { + return s.Items.Where(w => w.Enable); + }).ToArray(); + } + } - public IReadOnlyCollection? EnableProxyScripts - { - get - { - if (IsEnableScript == false) - return null; - if (!ProxyScripts.Any_Nullable()) - return null; - return ProxyScripts.Where(w => w.Enable).ToArray(); - } - } + public IReadOnlyCollection? EnableProxyScripts + { + get + { + if (IsEnableScript == false) + return null; + if (!ProxyScripts.Any_Nullable()) + return null; + return ProxyScripts.Where(w => w.Enable).ToArray(); + } + } - private DateTime _AccelerateTime = new(); - public DateTime AccelerateTime - { - get => _AccelerateTime; - set - { - if (_AccelerateTime != value) - { - _AccelerateTime = value; - this.RaisePropertyChanged(); - } - } - } + private DateTime _AccelerateTime = new(); + public DateTime AccelerateTime + { + get => _AccelerateTime; + set + { + if (_AccelerateTime != value) + { + _AccelerateTime = value; + this.RaisePropertyChanged(); + } + } + } - public bool IsEnableScript - { - get => ProxySettings.IsEnableScript.Value; - set - { - ProxySettings.IsEnableScript.Value = value; - httpProxyService.IsEnableScript = value; - this.RaisePropertyChanged(); - this.RaisePropertyChanged(nameof(EnableProxyScripts)); - } - } + public bool IsEnableScript + { + get => ProxySettings.IsEnableScript.Value; + set + { + ProxySettings.IsEnableScript.Value = value; + httpProxyService.IsEnableScript = value; + this.RaisePropertyChanged(); + this.RaisePropertyChanged(nameof(EnableProxyScripts)); + } + } - public bool IsOnlyWorkSteamBrowser - { - get => ProxySettings.IsOnlyWorkSteamBrowser.Value; - set - { - if (ProxySettings.IsOnlyWorkSteamBrowser.Value != value) - { - ProxySettings.IsOnlyWorkSteamBrowser.Value = value; - httpProxyService.IsOnlyWorkSteamBrowser = value; - this.RaisePropertyChanged(); - } - } - } + public bool IsOnlyWorkSteamBrowser + { + get => ProxySettings.IsOnlyWorkSteamBrowser.Value; + set + { + if (ProxySettings.IsOnlyWorkSteamBrowser.Value != value) + { + ProxySettings.IsOnlyWorkSteamBrowser.Value = value; + httpProxyService.IsOnlyWorkSteamBrowser = value; + this.RaisePropertyChanged(); + } + } + } - #region 代理状态启动退出 - public bool ProxyStatus - { - get { return httpProxyService.ProxyRunning; } - set - { - if (value != httpProxyService.ProxyRunning) - { - if (value) - { - //if (!EnableProxyDomains.Any_Nullable()) - //{ - // Toast.Show(AppResources.CommunityFix_NoSelectAcceleration); - // return; - //} - httpProxyService.ProxyDomains = EnableProxyDomains; - httpProxyService.Scripts = EnableProxyScripts; - this.RaisePropertyChanged(nameof(EnableProxyDomains)); - this.RaisePropertyChanged(nameof(EnableProxyScripts)); + #region 代理状态启动退出 + public bool ProxyStatus + { + get { return httpProxyService.ProxyRunning; } + set + { + if (value != httpProxyService.ProxyRunning) + { + if (value) + { + //if (!EnableProxyDomains.Any_Nullable()) + //{ + // Toast.Show(AppResources.CommunityFix_NoSelectAcceleration); + // return; + //} + httpProxyService.ProxyDomains = EnableProxyDomains; + httpProxyService.Scripts = EnableProxyScripts; + this.RaisePropertyChanged(nameof(EnableProxyDomains)); + this.RaisePropertyChanged(nameof(EnableProxyScripts)); - var hosts = httpProxyService.ProxyDomains.SelectMany(s => - { - return s?.HostsArray.Select(host => - { - if (host.Contains(" ")) - { - var h = host.Split(' '); - return (h[0], h[1]); - } - return (IPAddress.Loopback.ToString(), host); - }); - }).Where(w => !string.IsNullOrEmpty(w.Item1)); + var hosts = httpProxyService.ProxyDomains.SelectMany(s => + { + return s?.HostsArray.Select(host => + { + if (host.Contains(" ")) + { + var h = host.Split(' '); + return (h[0], h[1]); + } + return (IPAddress.Loopback.ToString(), host); + }); + }).Where(w => !string.IsNullOrEmpty(w.Item1)); - var isRun = httpProxyService.StartProxy(ProxySettings.EnableWindowsProxy.Value, ProxySettings.IsProxyGOG.Value); + var isRun = httpProxyService.StartProxy(ProxySettings.EnableWindowsProxy.Value, ProxySettings.IsProxyGOG.Value); - if (isRun) - { - StartTiming(); - IHostsFileService.Instance.UpdateHosts(hosts); - Toast.Show(AppResources.CommunityFix_StartProxySuccess); - } - else - { - MessageBoxCompat.Show(AppResources.CommunityFix_StartProxyFaild); - } - } - else - { - httpProxyService.StopProxy(); - IHostsFileService.Instance.RemoveHostsByTag(); - //Toast.Show(SteamTools.Properties.Resources.ProxyStop); - } - this.RaisePropertyChanged(); - } - } - } - #endregion + if (isRun) + { + StartTiming(); + IHostsFileService.Instance.UpdateHosts(hosts); + Toast.Show(AppResources.CommunityFix_StartProxySuccess); + } + else + { + MessageBoxCompat.Show(AppResources.CommunityFix_StartProxyFaild); + } + } + else + { + httpProxyService.StopProxy(); + IHostsFileService.Instance.RemoveHostsByTag(); + //Toast.Show(SteamTools.Properties.Resources.ProxyStop); + } + this.RaisePropertyChanged(); + } + } + } + #endregion - public async void Initialize() - { - #region 加载代理服务数据 - var client = ICloudServiceClient.Instance.Accelerate; - var result = await client.All(); - if (!result.IsSuccess) - { - return; - } - ProxyDomains = new ObservableCollection(result.Content); - SelectGroup = ProxyDomains.FirstOrDefault(); + public async void Initialize() + { + #region 加载代理服务数据 + var client = ICloudServiceClient.Instance.Accelerate; + var result = await client.All(); + if (!result.IsSuccess) + { + return; + } + ProxyDomains = new ObservableCollection(result.Content); + SelectGroup = ProxyDomains.FirstOrDefault(); - if (ProxySettings.SupportProxyServicesStatus.Value.Any_Nullable() && ProxyDomains.Any_Nullable()) - { - var items = ProxyDomains.SelectMany(s => s.Items); - foreach (var item in items) - { - if (ProxySettings.SupportProxyServicesStatus.Value.Contains(item.Id.ToString())) - { - item.Enable = true; - } - } - } + if (ProxySettings.SupportProxyServicesStatus.Value.Any_Nullable() && ProxyDomains.Any_Nullable()) + { + var items = ProxyDomains.SelectMany(s => s.Items); + foreach (var item in items) + { + if (ProxySettings.SupportProxyServicesStatus.Value.Contains(item.Id.ToString())) + { + item.Enable = true; + } + } + } - this.WhenAnyValue(v => v.ProxyDomains) - .Subscribe(domain => domain? - .ToObservableChangeSet() - .AutoRefresh(x => x.ObservableItems) - .TransformMany(t => t.ObservableItems ?? new ObservableCollection()) - .AutoRefresh(x => x.Enable) - .WhenPropertyChanged(x => x.Enable, false) - .Subscribe(_ => - { - if (EnableProxyDomains != null) - { - ProxySettings.SupportProxyServicesStatus.Value = EnableProxyDomains.Where(w => w?.Id != null).Select(k => k.Id.ToString()).ToList(); - } - })); - #endregion + this.WhenAnyValue(v => v.ProxyDomains) + .Subscribe(domain => domain? + .ToObservableChangeSet() + .AutoRefresh(x => x.ObservableItems) + .TransformMany(t => t.ObservableItems ?? new ObservableCollection()) + .AutoRefresh(x => x.Enable) + .WhenPropertyChanged(x => x.Enable, false) + .Subscribe(_ => + { + if (EnableProxyDomains != null) + { + ProxySettings.SupportProxyServicesStatus.Value = EnableProxyDomains.Where(w => w?.Id != null).Select(k => k.Id.ToString()).ToList(); + } + })); + #endregion - #region 加载脚本数据 + #region 加载脚本数据 - //var response =// await client.Scripts(); - //if (!response.IsSuccess) - //{ - // return; - //} - //new ObservableCollection(response.Content); - var scriptList = await DI.Get().GetAllScript(); - ProxyScripts = new ObservableCollection(scriptList); - httpProxyService.IsEnableScript = IsEnableScript; - if (ProxySettings.ScriptsStatus.Value.Any_Nullable() && ProxyScripts.Any_Nullable()) - { - foreach (var item in ProxyScripts) - { - if (item.LocalId != null && ProxySettings.ScriptsStatus.Value.Contains(item.LocalId.Value)) - { - item.Enable = true; - } - } - } + //var response =// await client.Scripts(); + //if (!response.IsSuccess) + //{ + // return; + //} + //new ObservableCollection(response.Content); + var scriptList = await DI.Get().GetAllScript(); + ProxyScripts = new ObservableCollection(scriptList); + httpProxyService.IsEnableScript = IsEnableScript; + if (ProxySettings.ScriptsStatus.Value.Any_Nullable() && ProxyScripts.Any_Nullable()) + { + foreach (var item in ProxyScripts) + { + if (item.LocalId != null && ProxySettings.ScriptsStatus.Value.Contains(item.LocalId.Value)) + { + item.Enable = true; + } + } + } - this.WhenAnyValue(v => v.ProxyScripts) - .Subscribe(script => script? - .ToObservableChangeSet() - .AutoRefresh(x => x.Enable) - .WhenPropertyChanged(x => x.Enable, false) - .Subscribe(_ => - { - if (EnableProxyScripts != null) - { - ProxySettings.ScriptsStatus.Value = EnableProxyScripts.Where(w => w?.LocalId != null).Select(k => k.LocalId.Value).ToList(); - } - })); - #endregion - } + this.WhenAnyValue(v => v.ProxyScripts) + .Subscribe(script => script? + .ToObservableChangeSet() + .AutoRefresh(x => x.Enable) + .WhenPropertyChanged(x => x.Enable, false) + .Subscribe(_ => + { + if (EnableProxyScripts != null) + { + ProxySettings.ScriptsStatus.Value = EnableProxyScripts.Where(w => w?.LocalId != null).Select(k => k.LocalId.Value).ToList(); + } + })); + #endregion + } - public void StartTiming() - { - Task.Run(() => - { - Thread.CurrentThread.IsBackground = true; - while (ProxyStatus) - { - AccelerateTime = AccelerateTime.AddSeconds(1); - Thread.Sleep(1000); - } - }); - } + public void StartTiming() + { + Task.Run(() => + { + Thread.CurrentThread.IsBackground = true; + while (ProxyStatus) + { + AccelerateTime = AccelerateTime.AddSeconds(1); + Thread.Sleep(1000); + } + }); + } - public void Dispose() - { - IHostsFileService.OnExitRestoreHosts(); - httpProxyService.Dispose(); - } + public void Dispose() + { + IHostsFileService.OnExitRestoreHosts(); + httpProxyService.Dispose(); + } - public async void AddNewScript(string filename) - { - var item = await DI.Get().AddScriptAsync(filename).ConfigureAwait(true); - if (item.state) - { - //var scriptList = await DI.Get().GetAllScript(); - if (ProxyScripts != null) - ProxyScripts.Add(item.model); - else - ProxyScripts = new ObservableCollection() { item.model }; - if(!string.IsNullOrWhiteSpace(item.msg)) - Toast.Show(item.msg); - } - else - Toast.Show(item.msg); - } - public async void RefreshScript() - { - var scriptList = await DI.Get().GetAllScript(); - ProxyScripts = new ObservableCollection(scriptList); - } + public async void AddNewScript(string filename) + { + var item = await DI.Get().AddScriptAsync(filename).ConfigureAwait(true); + if (item.state) + { + //var scriptList = await DI.Get().GetAllScript(); + if (ProxyScripts != null) + ProxyScripts.Add(item.model); + else + ProxyScripts = new ObservableCollection() { item.model }; + if (!string.IsNullOrWhiteSpace(item.msg)) + Toast.Show(item.msg); + } + else + Toast.Show(item.msg); + } + public async void RefreshScript() + { + var scriptList = await DI.Get().GetAllScript(); + ProxyScripts = new ObservableCollection(scriptList); + } - } + } } diff --git a/src/ST.Client.Desktop/UI/ViewModels/Pages/ProxyScriptManagePageViewModel.cs b/src/ST.Client.Desktop/UI/ViewModels/Pages/ProxyScriptManagePageViewModel.cs index f75f9a800..debb9f956 100644 --- a/src/ST.Client.Desktop/UI/ViewModels/Pages/ProxyScriptManagePageViewModel.cs +++ b/src/ST.Client.Desktop/UI/ViewModels/Pages/ProxyScriptManagePageViewModel.cs @@ -27,126 +27,127 @@ using static Newtonsoft.Json.JsonConvert; namespace System.Application.UI.ViewModels { - public class ProxyScriptManagePageViewModel : TabItemViewModel - { - private readonly Subject updateSource = new(); - public bool IsReloading { get; set; } + public class ProxyScriptManagePageViewModel : TabItemViewModel + { + private readonly Subject updateSource = new(); + public bool IsReloading { get; set; } - public override string Name - { - get => AppResources.ScriptConfig; - protected set { throw new NotImplementedException(); } - } + public override string Name + { + get => AppResources.ScriptConfig; + protected set { throw new NotImplementedException(); } + } - private IObservable UpdateAsync() - { - bool Predicate(ScriptDTO s) - { - if (string.IsNullOrEmpty(SerachText)) - return true; - if (!string.IsNullOrEmpty(SerachText)) - { - if (s.Name.Contains(SerachText, StringComparison.OrdinalIgnoreCase) || - s.Author.Contains(SerachText, StringComparison.OrdinalIgnoreCase) || - s.Description.Contains(SerachText, StringComparison.OrdinalIgnoreCase)) - { - return true; - } - } - return false; - } + private IObservable UpdateAsync() + { + bool Predicate(ScriptDTO s) + { + if (string.IsNullOrEmpty(SerachText)) + return true; + if (!string.IsNullOrEmpty(SerachText)) + { + if (s.Name.Contains(SerachText, StringComparison.OrdinalIgnoreCase) || + s.Author.Contains(SerachText, StringComparison.OrdinalIgnoreCase) || + s.Description.Contains(SerachText, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + return false; + } - return Observable.Start(() => - { - var list = ProxyService.Current.ProxyScripts?.Where(x => Predicate(x)).OrderBy(x => x.Name).ToList(); - if (list.Any_Nullable()) - ProxyScripts = new ObservableCollection(list); - else - ProxyScripts = null; - }); - } + return Observable.Start(() => + { + var list = ProxyService.Current.ProxyScripts?.Where(x => Predicate(x)).OrderBy(x => x.Name).ToList(); + if (list.Any_Nullable()) + ProxyScripts = new ObservableCollection(list); + else + ProxyScripts = null; + }); + } - public void Update() - { - updateSource.OnNext(Unit.Default); - } + public void Update() + { + updateSource.OnNext(Unit.Default); + } - public ProxyScriptManagePageViewModel() - { - IconKey = nameof(ProxyScriptManagePageViewModel).Replace("ViewModel", "Svg"); + public ProxyScriptManagePageViewModel() + { + IconKey = nameof(ProxyScriptManagePageViewModel).Replace("ViewModel", "Svg"); - MenuItems = new ObservableCollection() - { - new MenuItemViewModel (nameof(AppResources.CommunityFix_EnableScriptService)), - new MenuItemViewModel (nameof(AppResources.CommunityFix_ScriptManage)), - }; - this.updateSource - .Do(_ => this.IsReloading = true) - .SelectMany(x => this.UpdateAsync()) - .Do(_ => this.IsReloading = false) - .Subscribe() - .AddTo(this); + MenuItems = new ObservableCollection() + { + new MenuItemViewModel (nameof(AppResources.CommunityFix_EnableScriptService)), + new MenuItemViewModel (nameof(AppResources.CommunityFix_ScriptManage)), + }; + this.updateSource + .Do(_ => this.IsReloading = true) + .SelectMany(x => this.UpdateAsync()) + .Do(_ => this.IsReloading = false) + .Subscribe() + .AddTo(this); - ProxyService.Current - .WhenAnyValue(x => x.ProxyScripts) - .Subscribe(_ => Update()); + ProxyService.Current + .WhenAnyValue(x => x.ProxyScripts) + .Subscribe(_ => Update()); - this.WhenAnyValue(x => x.SerachText) - .Subscribe(_ => - { - Update(); - }); - //ProxyService.Current.Subscribe(nameof(ProxyService.Current.ProxyScripts), this.Update).AddTo(this); - } + this.WhenAnyValue(x => x.SerachText) + .Subscribe(_ => + { + Update(); + }); + //ProxyService.Current.Subscribe(nameof(ProxyService.Current.ProxyScripts), this.Update).AddTo(this); + } - private string? _SerachText; - public string? SerachText - { - get => _SerachText; - set => this.RaiseAndSetIfChanged(ref _SerachText, value); - } + private string? _SerachText; + public string? SerachText + { + get => _SerachText; + set => this.RaiseAndSetIfChanged(ref _SerachText, value); + } - private IList? _ProxyScripts; - public IList? ProxyScripts - { - get => _ProxyScripts; - set - { - if (_ProxyScripts != value) - { - _ProxyScripts = value; - this.RaisePropertyChanged(); - this.RaisePropertyChanged(nameof(IsProxyScriptsEmpty)); - } - } - } + private IList? _ProxyScripts; + public IList? ProxyScripts + { + get => _ProxyScripts; + set + { + if (_ProxyScripts != value) + { + _ProxyScripts = value; + this.RaisePropertyChanged(); + this.RaisePropertyChanged(nameof(IsProxyScriptsEmpty)); + } + } + } - public bool IsProxyScriptsEmpty => !ProxyScripts.Any_Nullable(); + public bool IsProxyScriptsEmpty => !ProxyScripts.Any_Nullable(); - public void UpdateAllScript() - { + public void UpdateAllScript() + { - } + } - public void RefreshScriptButton() - { - ProxyService.Current.RefreshScript(); - Toast.Show("刷新成功"); - } - public async void RefreshScriptItemButton(ScriptDTO? script) - { - if (script!= null) - if (script.FilePath != null) - { - var item = await DI.Get().AddScriptAsync(script.FilePath); - if (item.state) - script = item.model; - else - { - script.Enable = false; - Toast.Show(item.msg); - } - } - } - } + public void RefreshScriptButton() + { + ProxyService.Current.RefreshScript(); + Toast.Show("刷新成功"); + } + + public async void RefreshScriptItemButton(ScriptDTO script) + { + if (script != null) + if (script.FilePath != null) + { + var item = await DI.Get().AddScriptAsync(script.FilePath); + if (item.state) + script = item.model; + else + { + script.Enable = false; + Toast.Show(item.msg); + } + } + } + } } \ No newline at end of file