This commit is contained in:
rmbgame
2021-04-23 14:47:28 +08:00
parent d6084e9b58
commit 33f343456b
3 changed files with 474 additions and 472 deletions
@@ -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">
<Panel HorizontalAlignment="Stretch">
<DockPanel>
<Border DockPanel.Dock="Top" Classes="paper" Background="{DynamicResource ThemeBrushKey}" Padding="10" Margin="20 0 20 10">
<Panel HorizontalAlignment="Stretch">
<DockPanel>
<StackPanel Orientation="Horizontal" Spacing="8" HorizontalAlignment="Left">
<Button Background="Transparent" Cursor="Hand" Click="AddNewScriptButton_Click">
<PathIcon Width="16" Data="{StaticResource AddDrawing}" />
</Button>
<Button Background="Transparent" Cursor="Hand"
Command="{ReflectionBinding RefreshScriptButton}">
<PathIcon Width="16" Data="{StaticResource RefreshDrawing}" />
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="15" HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBox Watermark="{ReflectionBinding Path=Res.Search,Mode=OneWay,Source={x:Static resx:R.Current}}" Text="{CompiledBinding SerachText,Mode=TwoWay}" VerticalAlignment="Center" MinWidth="300">
<TextBox.InnerLeftContent>
<PathIcon Data="{StaticResource SearchDrawing}"
Width="20" Height="20"
Opacity="0.8"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="15,8,0,0" />
</TextBox.InnerLeftContent>
</TextBox>
<!--<TextBlock Text="{ReflectionBinding Path=Res.CommunityFix_EnableScriptService,Mode=OneWay,Source={x:Static resx:R.Current}}" VerticalAlignment="Center"></TextBlock>-->
<ToggleSwitch Content="" OnContent="{ReflectionBinding Path=Res.CommunityFix_EnableScriptService,Mode=OneWay,Source={x:Static resx:R.Current}}" OffContent="{ReflectionBinding Path=Res.CommunityFix_EnableScriptService,Mode=OneWay,Source={x:Static resx:R.Current}}" IsChecked="{Binding Source={x:Static service:ProxyService.Current},Path=IsEnableScript}"></ToggleSwitch>
</StackPanel>
</DockPanel>
</Border>
<Panel>
<TextBlock IsVisible="{Binding IsProxyScriptsEmpty}"
Text="{ReflectionBinding Path=Res.UserChange_NoUserTip,Mode=OneWay,Source={x:Static resx:R.Current}}"
TextWrapping="WrapWithOverflow"
VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" Padding="20 0">
<ItemsRepeater Items="{Binding ProxyScripts,Mode=OneWay}">
<ItemsRepeater.Layout>
<WrapLayout VerticalSpacing="20" HorizontalSpacing="20" Orientation="Horizontal"/>
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<Border Width="450" Classes="paper" Background="{DynamicResource ThemeBrushKey}" Padding="20 10">
<DockPanel>
<Image Source="{Binding Icon}" DockPanel.Dock="Left"></Image>
<StackPanel Spacing="8" DockPanel.Dock="Right">
<DockPanel>
<controls:ScrollingTextBlock
Width="{Binding $parent.Width}"
Text="{Binding Name,Mode=OneWay}"
FontSize="16"
VerticalAlignment="Center"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
<WrapPanel HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBlock
Text="{Binding Author}"
FontSize="14"
VerticalAlignment="Center"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
<TextBlock Text="/ version." FontSize="14" Margin="5 0"></TextBlock>
<TextBlock
Text="{Binding Version}"
FontSize="14"
VerticalAlignment="Center"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
</WrapPanel>
</DockPanel>
<Separator Classes="Separator" Margin="0 0 0 5"></Separator>
<TextBlock
Width="{Binding $parent.Width}"
TextWrapping="WrapWithOverflow"
Text="{Binding Description}"
FontSize="12"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
<StackPanel Orientation="Horizontal" Spacing="10" VerticalAlignment="Bottom">
<ToggleSwitch OnContent="" OffContent="" IsChecked="{Binding Enable,Mode=TwoWay}">
</ToggleSwitch>
<Button Background="Transparent" Cursor="Hand">
<PathIcon Width="20" Data="{StaticResource CodeDrawing}" />
<Border DockPanel.Dock="Top" Classes="paper" Background="{DynamicResource ThemeBrushKey}" Padding="10" Margin="20 0 20 10">
<DockPanel>
<StackPanel Orientation="Horizontal" Spacing="8" HorizontalAlignment="Left">
<Button Background="Transparent" Cursor="Hand" Click="AddNewScriptButton_Click">
<PathIcon Width="16" Data="{StaticResource AddDrawing}" />
</Button>
<Button Background="Transparent" Cursor="Hand"
Command="{ReflectionBinding RefreshScriptItemButton}"
CommandParameter="{ReflectionBinding}">
<PathIcon Width="16" Data="{StaticResource RefreshDrawing}" />
Command="{ReflectionBinding RefreshScriptButton}">
<PathIcon Width="16" Data="{StaticResource RefreshDrawing}" />
</Button>
<Button Background="Transparent" Cursor="Hand">
<PathIcon Width="16" Data="{StaticResource HomeDrawing}" />
</Button>
<Button Background="Transparent" Cursor="Hand" HorizontalAlignment="Right">
<PathIcon Width="20" Data="{StaticResource DeleteDrawing}" />
</Button>
</StackPanel>
</StackPanel>
</DockPanel>
</Border>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</ScrollViewer>
</Panel>
</DockPanel>
</Panel>
<StackPanel Orientation="Horizontal" Spacing="15" HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBox Watermark="{ReflectionBinding Path=Res.Search,Mode=OneWay,Source={x:Static resx:R.Current}}" Text="{CompiledBinding SerachText,Mode=TwoWay}" VerticalAlignment="Center" MinWidth="300">
<TextBox.InnerLeftContent>
<PathIcon Data="{StaticResource SearchDrawing}"
Width="20" Height="20"
Opacity="0.8"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="15,8,0,0" />
</TextBox.InnerLeftContent>
</TextBox>
<!--<TextBlock Text="{ReflectionBinding Path=Res.CommunityFix_EnableScriptService,Mode=OneWay,Source={x:Static resx:R.Current}}" VerticalAlignment="Center"></TextBlock>-->
<ToggleSwitch Content="" OnContent="{ReflectionBinding Path=Res.CommunityFix_EnableScriptService,Mode=OneWay,Source={x:Static resx:R.Current}}" OffContent="{ReflectionBinding Path=Res.CommunityFix_EnableScriptService,Mode=OneWay,Source={x:Static resx:R.Current}}" IsChecked="{Binding Source={x:Static service:ProxyService.Current},Path=IsEnableScript}"></ToggleSwitch>
</StackPanel>
</DockPanel>
</Border>
<Panel>
<TextBlock IsVisible="{Binding IsProxyScriptsEmpty}"
Text="{ReflectionBinding Path=Res.UserChange_NoUserTip,Mode=OneWay,Source={x:Static resx:R.Current}}"
TextWrapping="WrapWithOverflow"
VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
<ScrollViewer HorizontalScrollBarVisibility="Disabled" Padding="20 0">
<ItemsRepeater Items="{Binding ProxyScripts}">
<ItemsRepeater.Layout>
<WrapLayout VerticalSpacing="20" HorizontalSpacing="20" Orientation="Horizontal"/>
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<Border Width="450" Classes="paper" Background="{DynamicResource ThemeBrushKey}" Padding="20 10">
<DockPanel>
<Image Source="{Binding Icon}" DockPanel.Dock="Left"></Image>
<StackPanel Spacing="8" DockPanel.Dock="Right">
<DockPanel>
<controls:ScrollingTextBlock
Width="{Binding $parent.Width}"
Text="{Binding Name,Mode=OneWay}"
FontSize="16"
VerticalAlignment="Center"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
<WrapPanel HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBlock
Text="{Binding Author}"
FontSize="14"
VerticalAlignment="Center"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
<TextBlock Text="/ version." FontSize="14" Margin="5 0"></TextBlock>
<TextBlock
Text="{Binding Version}"
FontSize="14"
VerticalAlignment="Center"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
</WrapPanel>
</DockPanel>
<Separator Classes="Separator" Margin="0 0 0 5"></Separator>
<TextBlock
Width="{Binding $parent.Width}"
TextWrapping="WrapWithOverflow"
Text="{Binding Description}"
FontSize="12"
Foreground="{DynamicResource HighlightForegroundBrushKey}"/>
<StackPanel Orientation="Horizontal" Spacing="10" VerticalAlignment="Bottom">
<ToggleSwitch OnContent="" OffContent="" IsChecked="{Binding Enable,Mode=TwoWay}">
</ToggleSwitch>
<Button Background="Transparent" Cursor="Hand">
<PathIcon Width="20" Data="{StaticResource CodeDrawing}" />
</Button>
<Button Background="Transparent" Cursor="Hand"
Command="{ReflectionBinding #u.DataContext.RefreshScriptItemButton}"
CommandParameter="{Binding}">
<PathIcon Width="16" Data="{StaticResource RefreshDrawing}" />
</Button>
<Button Background="Transparent" Cursor="Hand">
<PathIcon Width="16" Data="{StaticResource HomeDrawing}" />
</Button>
<Button Background="Transparent" Cursor="Hand" HorizontalAlignment="Right">
<PathIcon Width="20" Data="{StaticResource DeleteDrawing}" />
</Button>
</StackPanel>
</StackPanel>
</DockPanel>
</Border>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</ScrollViewer>
</Panel>
</DockPanel>
</Panel>
</UserControl>
@@ -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<IHttpProxyService>();
public class ProxyService : ReactiveObject
{
public static ProxyService Current { get; } = new ProxyService();
readonly IHttpProxyService httpProxyService = DI.Get<IHttpProxyService>();
private ObservableCollection<AccelerateProjectGroupDTO>? _ProxyDomains;
public ObservableCollection<AccelerateProjectGroupDTO>? ProxyDomains
{
get => _ProxyDomains;
set
{
if (_ProxyDomains != value)
{
_ProxyDomains = value;
this.RaisePropertyChanged();
}
}
}
private ObservableCollection<AccelerateProjectGroupDTO>? _ProxyDomains;
public ObservableCollection<AccelerateProjectGroupDTO>? 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<ScriptDTO>? _ProxyScripts;
public ObservableCollection<ScriptDTO>? ProxyScripts
{
get => _ProxyScripts;
set
{
if (_ProxyScripts != value)
{
_ProxyScripts = value;
this.RaisePropertyChanged();
}
}
}
private ObservableCollection<ScriptDTO>? _ProxyScripts;
public ObservableCollection<ScriptDTO>? ProxyScripts
{
get => _ProxyScripts;
set
{
if (_ProxyScripts != value)
{
_ProxyScripts = value;
this.RaisePropertyChanged();
}
}
}
public IReadOnlyCollection<AccelerateProjectDTO>? EnableProxyDomains
{
get
{
if (!ProxyDomains.Any_Nullable())
return null;
return ProxyDomains.SelectMany(s =>
{
return s.Items.Where(w => w.Enable);
}).ToArray();
}
}
public IReadOnlyCollection<AccelerateProjectDTO>? EnableProxyDomains
{
get
{
if (!ProxyDomains.Any_Nullable())
return null;
return ProxyDomains.SelectMany(s =>
{
return s.Items.Where(w => w.Enable);
}).ToArray();
}
}
public IReadOnlyCollection<ScriptDTO>? EnableProxyScripts
{
get
{
if (IsEnableScript == false)
return null;
if (!ProxyScripts.Any_Nullable())
return null;
return ProxyScripts.Where(w => w.Enable).ToArray();
}
}
public IReadOnlyCollection<ScriptDTO>? 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<AccelerateProjectGroupDTO>(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<AccelerateProjectGroupDTO>(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<AccelerateProjectDTO>())
.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<AccelerateProjectDTO>())
.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<ScriptDTO>(response.Content);
var scriptList = await DI.Get<IScriptManagerService>().GetAllScript();
ProxyScripts = new ObservableCollection<ScriptDTO>(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<ScriptDTO>(response.Content);
var scriptList = await DI.Get<IScriptManagerService>().GetAllScript();
ProxyScripts = new ObservableCollection<ScriptDTO>(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<IScriptManagerService>().AddScriptAsync(filename).ConfigureAwait(true);
if (item.state)
{
//var scriptList = await DI.Get<IScriptManagerService>().GetAllScript();
if (ProxyScripts != null)
ProxyScripts.Add(item.model);
else
ProxyScripts = new ObservableCollection<ScriptDTO>() { 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<IScriptManagerService>().GetAllScript();
ProxyScripts = new ObservableCollection<ScriptDTO>(scriptList);
}
public async void AddNewScript(string filename)
{
var item = await DI.Get<IScriptManagerService>().AddScriptAsync(filename).ConfigureAwait(true);
if (item.state)
{
//var scriptList = await DI.Get<IScriptManagerService>().GetAllScript();
if (ProxyScripts != null)
ProxyScripts.Add(item.model);
else
ProxyScripts = new ObservableCollection<ScriptDTO>() { 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<IScriptManagerService>().GetAllScript();
ProxyScripts = new ObservableCollection<ScriptDTO>(scriptList);
}
}
}
}
@@ -27,126 +27,127 @@ using static Newtonsoft.Json.JsonConvert;
namespace System.Application.UI.ViewModels
{
public class ProxyScriptManagePageViewModel : TabItemViewModel
{
private readonly Subject<Unit> updateSource = new();
public bool IsReloading { get; set; }
public class ProxyScriptManagePageViewModel : TabItemViewModel
{
private readonly Subject<Unit> 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<Unit> 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<Unit> 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<ScriptDTO>(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<ScriptDTO>(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<MenuItemViewModel>()
{
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<MenuItemViewModel>()
{
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<ScriptDTO>? _ProxyScripts;
public IList<ScriptDTO>? ProxyScripts
{
get => _ProxyScripts;
set
{
if (_ProxyScripts != value)
{
_ProxyScripts = value;
this.RaisePropertyChanged();
this.RaisePropertyChanged(nameof(IsProxyScriptsEmpty));
}
}
}
private IList<ScriptDTO>? _ProxyScripts;
public IList<ScriptDTO>? 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<IScriptManagerService>().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<IScriptManagerService>().AddScriptAsync(script.FilePath);
if (item.state)
script = item.model;
else
{
script.Enable = false;
Toast.Show(item.msg);
}
}
}
}
}