feat: add validator support
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.4.0" />
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
|
||||
<PackageReference Include="DotNetZip" Version="1.9.1.8" />
|
||||
<PackageReference Include="LanguageExt.Core" Version="4.4.9" />
|
||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="10.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -56,8 +56,7 @@ namespace BallanceTasEditor.Frontend.Shared {
|
||||
var dialog = new Views.NewFileDialog();
|
||||
dialog.Owner = m_Parent;
|
||||
if (dialog.ShowDialog() is true) {
|
||||
// TODO: Finish result extraction
|
||||
return new NewFileDialogResult() { Count = 0, Fps = 60 };
|
||||
return dialog.ViewModel.GetUserInput();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BallanceTasEditor.Frontend.Shared {
|
||||
public static class BrowserHelper {
|
||||
public static void OpenInDefaultBrowser(string url) {
|
||||
public static class ProcessHelper {
|
||||
public static void OpenUrl(string url) {
|
||||
if (string.IsNullOrWhiteSpace(url)) {
|
||||
throw new ArgumentException("The content of URL should not be empty.", nameof(url));
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BallanceTasEditor.Frontend.Validator {
|
||||
|
||||
public sealed class ValidatorAdapter<TIn, TOut, V> where V: IValidator<TIn, TOut> {
|
||||
public ValidatorAdapter(IValidator<TIn, TOut> validator) {
|
||||
m_Validator = validator;
|
||||
}
|
||||
|
||||
private readonly IValidator<TIn, TOut> m_Validator;
|
||||
|
||||
public ValidationResult? Validate(TIn value, ValidationContext validationContext) {
|
||||
// YYC MARK:
|
||||
// Due to the shitty behavior of LanguageExt
|
||||
// which do not allow I return nullable class from Match,
|
||||
// I was forcely use MatchUnsafe.
|
||||
return m_Validator.Validate(value).MatchUnsafe(
|
||||
Left: v => ValidationResult.Success,
|
||||
Right: err => new ValidationResult(err)
|
||||
);
|
||||
}
|
||||
|
||||
public TOut Conclude(TIn value) {
|
||||
return m_Validator.Validate(value).Match(
|
||||
Left: v => v,
|
||||
Right: _ => throw new InvalidOperationException("Can not unwrap an error casting.")
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using LanguageExt;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BallanceTasEditor.Frontend.Validator {
|
||||
|
||||
public interface IValidator<TIn, TOut> {
|
||||
Either<TOut, string> Validate(TIn value);
|
||||
}
|
||||
|
||||
public sealed class FpsValidator : IValidator<string, uint> {
|
||||
public Either<uint, string> Validate(string value) {
|
||||
if (uint.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, out uint fps)) {
|
||||
if (Backend.FpsConverter.IsValidFps(fps)) {
|
||||
return fps;
|
||||
} else {
|
||||
return "Given FPS is out of range.";
|
||||
}
|
||||
} else {
|
||||
return "Given string can not be parsed as unsigned integer.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class CountValidator : IValidator<string, int> {
|
||||
public Either<int, string> Validate(string value) {
|
||||
if (int.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, out int count)) {
|
||||
if (count > 0) {
|
||||
return count;
|
||||
} else {
|
||||
return "Given count must be greater than zero.";
|
||||
}
|
||||
} else {
|
||||
return "Given string can not be parsed as integer.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -158,7 +158,7 @@ namespace BallanceTasEditor.Frontend.ViewModels {
|
||||
[RelayCommand]
|
||||
private void ReportBug() {
|
||||
try {
|
||||
Shared.BrowserHelper.OpenInDefaultBrowser(Shared.Constant.REPORT_BUG_URL);
|
||||
Shared.ProcessHelper.OpenUrl(Shared.Constant.REPORT_BUG_URL);
|
||||
} catch (Exception) {
|
||||
m_DialogService.ShowManuallyReportBugDialog();
|
||||
}
|
||||
|
||||
@@ -9,57 +9,49 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace BallanceTasEditor.Frontend.ViewModels {
|
||||
|
||||
public partial class NewFileDialog : ObservableObject {
|
||||
public partial class NewFileDialog : ObservableValidator {
|
||||
public NewFileDialog() {
|
||||
Count = Shared.Constant.DEFAULT_NEW_COUNT.ToString();
|
||||
Fps = Shared.Constant.DEFAULT_FPS.ToString();
|
||||
}
|
||||
|
||||
// YYC MARK:
|
||||
// 经过无数次的尝试,我发现将int类型绑定到TextBox中所需要涉及的事情太多了,
|
||||
// 尤其是这种绑定到处存在于这个程序中,以至于每次都要重新写一遍。
|
||||
// 也许后面我会做一个只能接受数字输入的文本框,但现在我累了。
|
||||
//
|
||||
// 具体来说,事情是这样的。我一开始就是使用一个int类型的数据,
|
||||
// 然后按照CommunityToolkit.Mvvm的标准,将其应用了Required和Range Attribute,然后将其绑定到了TextBox的Text之上。
|
||||
// 然而最终的效果很奇怪,当我删除TextBox中的所有字符后,绑定什么也没做(后来我才知道要在Required里改一个选项)。
|
||||
// 其次,当我输入不被Range接受的数值时,它仍会将其同步绑定到属性上。比如我输入0,它仍会将其绑定到Fps上,
|
||||
// 进而导致Delta Time的转换显示抛出除以零的错误。这些都不是我想要的。
|
||||
// 同时,这样做的话,我没有办法检测这些字段到底是不是合规的。
|
||||
//
|
||||
// 然后我就将int改写为了int?类型,用null表示当前值不可接受,非null表示绑定的值。
|
||||
// 然后定义了一个转换器,负责在int?和string之间进行转换,并同时去除了所有的验证性Attribute
|
||||
// 然而遗憾的是,这么做也不符合我的要求。当我尝试输入一个数值的时候,如果我输入了任何无效值,
|
||||
// 那么转换器会将其转换为null值,同时将该值赋给Fps,而被赋值的Fps又反向传播,把这个null传递给了转换器,
|
||||
// 转换器将其转换成为空白值,并显示在界面上。这么做的视觉效果就是一旦我输入无效值,整个文本框就会被清空,非常的反人类。
|
||||
// 而且这一问题是不可调和的,我不能在接收到null时,选择不更新文本框的值,因为有可能这个null是我手动放进去的,而不是从转换器接受的。
|
||||
//
|
||||
// 所以最终,我想通了,我决定抛弃将int和TextBox.Text绑定在一起的想法。
|
||||
// 就直接把string绑定到TextBox.Text上,然后再辅以我自己定义的一套可复用验证逻辑。
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyDataErrorInfo]
|
||||
[CustomValidation(typeof(NewFileDialog), nameof(ValidateCount))]
|
||||
[NotifyCanExecuteChangedFor(nameof(OkCommand))]
|
||||
private string count;
|
||||
[ObservableProperty]
|
||||
[NotifyDataErrorInfo]
|
||||
[CustomValidation(typeof(NewFileDialog), nameof(ValidateFps))]
|
||||
[NotifyCanExecuteChangedFor(nameof(OkCommand))]
|
||||
private string fps;
|
||||
|
||||
//[ObservableProperty]
|
||||
////[CustomValidation(typeof(NewFileDialog), nameof(ValidateCount))]
|
||||
//[NotifyCanExecuteChangedFor(nameof(OkCommand))]
|
||||
//private string count;
|
||||
#region Validators
|
||||
|
||||
//[ObservableProperty]
|
||||
////[CustomValidation(typeof(NewFileDialog), nameof(ValidateFps))]
|
||||
//[NotifyCanExecuteChangedFor(nameof(OkCommand))]
|
||||
//private string fps;
|
||||
private static readonly Validator.ValidatorAdapter<string, int, Validator.CountValidator> g_CountValidator =
|
||||
new Validator.ValidatorAdapter<string, int, Validator.CountValidator>(new Validator.CountValidator());
|
||||
|
||||
////public static ValidationResult ValidateCount(string count, ValidationContext context) {
|
||||
//// return CountValidator.Instance.Validate(count);
|
||||
////}
|
||||
////public static ValidationResult ValidateFps(string fps, ValidationContext context) {
|
||||
//// return FpsValidator.Instance.Validate(fps);
|
||||
////}
|
||||
public static ValidationResult? ValidateCount(string value, ValidationContext context) {
|
||||
return g_CountValidator.Validate(value, context);
|
||||
}
|
||||
|
||||
private static readonly Validator.ValidatorAdapter<string, uint, Validator.FpsValidator> g_FpsValidator =
|
||||
new Validator.ValidatorAdapter<string, uint, Validator.FpsValidator>(new Validator.FpsValidator());
|
||||
|
||||
public static ValidationResult? ValidateFps(string value, ValidationContext context) {
|
||||
return g_FpsValidator.Validate(value, context);
|
||||
}
|
||||
|
||||
public Shared.NewFileDialogResult GetUserInput() {
|
||||
return new Shared.NewFileDialogResult {
|
||||
Count = g_CountValidator.Conclude(Count),
|
||||
Fps = g_FpsValidator.Conclude(Fps)
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanOk))]
|
||||
private void Ok() {
|
||||
@@ -67,8 +59,7 @@ namespace BallanceTasEditor.Frontend.ViewModels {
|
||||
}
|
||||
|
||||
private bool CanOk() {
|
||||
// TODO
|
||||
return true;
|
||||
return !HasErrors;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
@@ -76,19 +67,13 @@ namespace BallanceTasEditor.Frontend.ViewModels {
|
||||
OnRequestCloseDialog(false);
|
||||
}
|
||||
|
||||
|
||||
public event Shared.RequestCloseDialogEventHandler? RequestCloseDialog;
|
||||
|
||||
private void OnRequestCloseDialog(bool result) {
|
||||
RequestCloseDialog?.Invoke(new Shared.RequestCloseDialogEventArgs { Result = result });
|
||||
}
|
||||
|
||||
//public NewFileDialogResult ToResult() {
|
||||
// return new NewFileDialogResult {
|
||||
// Count = CountValidator.Instance.Fetch(Count),
|
||||
// DeltaTime = FpsConverter.ToDelta(FpsValidator.Instance.Fetch(Fps)),
|
||||
// };
|
||||
//}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,9 +47,9 @@
|
||||
<TextBlock Margin="5" Grid.Column="1" Grid.Row="2" Text="Delta Time" VerticalAlignment="Center"/>
|
||||
|
||||
<TextBox Margin="5" Padding="3" Grid.Row="0" Grid.Column="2" VerticalAlignment="Center"
|
||||
Text="{Binding Count, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
Text="{Binding Count, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"/>
|
||||
<TextBox Margin="5" Padding="3" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center"
|
||||
Text="{Binding Fps, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
Text="{Binding Fps, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"/>
|
||||
<TextBlock Margin="5" Padding="3" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center"
|
||||
Text="{Binding Fps, Mode=OneWay, Converter={x:Static conveter:FpsConverter.Instance}, FallbackValue=N/A}"/>
|
||||
|
||||
|
||||
@@ -20,11 +20,13 @@ namespace BallanceTasEditor.Frontend.Views {
|
||||
public NewFileDialog() {
|
||||
InitializeComponent();
|
||||
|
||||
var vm = new ViewModels.NewFileDialog();
|
||||
vm.RequestCloseDialog += ViewModel_RequestCloseDialog;
|
||||
this.DataContext = vm;
|
||||
ViewModel = new ViewModels.NewFileDialog();
|
||||
ViewModel.RequestCloseDialog += ViewModel_RequestCloseDialog;
|
||||
this.DataContext = ViewModel;
|
||||
}
|
||||
|
||||
public ViewModels.NewFileDialog ViewModel { get; private set; }
|
||||
|
||||
private void ViewModel_RequestCloseDialog(Shared.RequestCloseDialogEventArgs e) {
|
||||
this.DialogResult = e.Result;
|
||||
this.Close();
|
||||
|
||||
Reference in New Issue
Block a user