Skip to content

Commit

Permalink
Merge pull request #3499 from BeyondDimension/feature/network_test_do…
Browse files Browse the repository at this point in the history
…main_validation

✨ 网络测试页面域名检测输入检查
  • Loading branch information
rmbadmin authored Oct 9, 2024
2 parents e0c36a9 + 42df02f commit 59356f2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
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

0 comments on commit 59356f2

Please sign in to comment.