Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 网络测试页面域名检测输入检查 #3499

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@

namespace BD.WTTS.UI.ViewModels;

public class NetworkCheckControlViewModel : ViewModelBase
public partial class NetworkCheckControlViewModel : ViewModelBase
{
[GeneratedRegex(DomainValidationAttribute.RegPattern)]
private static partial Regex DomainRegExp();

public class DomainValidationAttribute : RegularExpressionAttribute
{
public const string RegPattern = @"^[^/]+\.[^/]{2,}$";

public override string FormatErrorMessage(string name) => "请填入不带分隔符 \"/\" 的域名";

public DomainValidationAttribute() : base(RegPattern)
{
}
}

private readonly INetworkTestService _networkTestService = INetworkTestService.Instance;

public record NATFetchResult(string PublicEndPoint, string LocalEndPoint, string NATLevel, string NATTypeTip, bool PingResult);

public string DefaultTestDomain { get; } = "store.steampowered.com";

[Reactive]
public string SelectedSTUNAddress { get; set; }

Expand Down Expand Up @@ -48,6 +64,7 @@ public record NATFetchResult(string PublicEndPoint, string LocalEndPoint, string
[ObservableAsProperty]
public bool IsIPv6Checking { get; }

[DomainValidation]
[Reactive]
public string DomainPendingTest { get; set; } = string.Empty;

Expand Down Expand Up @@ -126,9 +143,11 @@ public NetworkCheckControlViewModel()
.Merge(hidePingResultStream)
.ToPropertyEx(this, x => x.PingErrorVisible);

var canDNSCheck = this.WhenAnyValue(x => x.DomainPendingTest)
.Select(domain => domain == string.Empty || DomainRegExp().IsMatch(domain));
DNSCheckCommand = ReactiveCommand.CreateFromTask(async () =>
{
var testDomain = DomainPendingTest == string.Empty ? "store.steampowered.com" : DomainPendingTest;
var testDomain = DomainPendingTest == string.Empty ? DefaultTestDomain : DomainPendingTest;
try
{
long delayMs;
Expand All @@ -155,7 +174,7 @@ public NetworkCheckControlViewModel()
DNSTestDelay = string.Empty;
DNSTestResult = "error";
}
});
}, canDNSCheck);
DNSCheckCommand
.IsExecuting
.ToPropertyEx(this, x => x.IsDNSChecking);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,50 @@
<TextBox
Width="220"
Text="{Binding DomainPendingTest, Mode=TwoWay}"
Watermark="store.steampowered.com" />
Watermark="{Binding DefaultTestDomain}">
<TextBox.Styles>
<Style Selector="DataValidationErrors">
<Setter Property="Template">
<ControlTemplate>
<DockPanel LastChildFill="True">
<ContentControl
Content="{Binding (DataValidationErrors.Errors)}"
ContentTemplate="{TemplateBinding ErrorTemplate}"
DataContext="{TemplateBinding Owner}"
DockPanel.Dock="Right"
IsVisible="{Binding (DataValidationErrors.HasErrors)}" />
<ContentPresenter
Name="PART_ContentPresenter"
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
CornerRadius="{TemplateBinding CornerRadius}" />
</DockPanel>
</ControlTemplate>
</Setter>
<Setter Property="ErrorTemplate">
<DataTemplate x:DataType="{x:Type x:Object}">
<Canvas
Width="14"
Height="14"
Margin="4,0,1,0"
Background="Transparent">
<ToolTip.Tip>
<ItemsControl ItemsSource="{Binding}" />
</ToolTip.Tip>
<Path
Data="M14,7 A7,7 0 0,0 0,7 M0,7 A7,7 0 1,0 14,7 M7,3l0,5 M7,9l0,2"
Stroke="Red"
StrokeThickness="2" />
</Canvas>
</DataTemplate>
</Setter>
</Style>
</TextBox.Styles>
</TextBox>
</ui:SettingsExpanderItem.Footer>
</ui:SettingsExpanderItem>

Expand Down
Loading