Skip to content

Commit

Permalink
add: validation for file names in stores
Browse files Browse the repository at this point in the history
  • Loading branch information
etogood committed Jul 4, 2024
1 parent bfe867a commit f8c7776
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/Asv.Drones.Gui.Api/RS.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Asv.Drones.Gui.Api/RS.resx
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,7 @@
<data name="AnchorsEditorView_TextBlock_Actions" xml:space="preserve">
<value>Actions</value>
</data>
<data name="StoreFileName_Validation_ErrorMessage" xml:space="preserve">
<value>Not valid name</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Asv.Drones.Gui.Api/RS.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,7 @@
<data name="AnchorsEditorView_TextBlock_Actions" xml:space="preserve">
<value>Действия</value>
</data>
<data name="StoreFileName_Validation_ErrorMessage" xml:space="preserve">
<value>Неверное имя</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Collections.ObjectModel;
using System.Reactive;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using Asv.Common;
using Asv.Mavlink;
using DynamicData;
using DynamicData.Binding;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ReactiveUI.Validation.Extensions;

namespace Asv.Drones.Gui.Api;

Expand All @@ -15,17 +18,16 @@ public enum HierarchicalStoreEntryAction
Delete
}

public class HierarchicalStoreEntryViewModel : DisposableReactiveObject
public partial class HierarchicalStoreEntryViewModel : DisposableReactiveObjectWithValidation
{
public HierarchicalStoreEntryViewModel()
{
BeginEditName = ReactiveCommand.Create(() => { IsInEditNameMode = true; }).DisposeItWith(Disposable);
;
EndEditName = ReactiveCommand.Create(() =>
{
IsInEditNameMode = false;
Rename(Name);
}).DisposeItWith(Disposable);
}, CanEndEditName).DisposeItWith(Disposable);
EndEditName.ThrownExceptions
.Subscribe(ex => OnError(HierarchicalStoreEntryAction.Rename, ex))
.DisposeItWith(Disposable);
Expand All @@ -49,6 +51,9 @@ public HierarchicalStoreEntryViewModel()
this.WhenValueChanged(x => x.IsNotInMoveMode)
.Subscribe(x => IsInMoveMode = !x)
.DisposeItWith(Disposable);
this.ValidationRule(x => x.Name,
s => s != null && FileNameRegex().IsMatch(s),
RS.StoreFileName_Validation_ErrorMessage);
}


Expand All @@ -66,6 +71,11 @@ public HierarchicalStoreEntryViewModel()
public virtual ReadOnlyObservableCollection<HierarchicalStoreEntryViewModel> Items { get; set; }
[Reactive] public bool IsInEditNameMode { get; set; } = false;

[GeneratedRegex(@"^[A-Za-z][A-Za-z0-9_\- +]{0,28}")]
private partial Regex FileNameRegex();
private IObservable<bool> CanEndEditName => this.WhenPropertyChanged(x => x.Name)
.Select(x => x.Value != null && FileNameRegex().IsMatch(x.Value));

public ReactiveCommand<Unit, Unit> DeleteEntry { get; }
public ReactiveCommand<Unit, Unit> BeginEditName { get; }
public ReactiveCommand<Unit, Unit> EndEditName { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
using DynamicData.Binding;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ReactiveUI.Validation.Helpers;

namespace Asv.Drones.Gui.Api;

public class HierarchicalStoreViewModel : ViewModelBase
public class HierarchicalStoreViewModel : ViewModelBaseWithValidation
{
protected HierarchicalStoreViewModel(Uri id) : base(id)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Asv.Drones.Gui/RS.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Asv.Drones.Gui/RS.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1868,4 +1868,7 @@
<data name="PlanningPageViewModel_MissionSavingBrowserDialog_ConfirmationText_ContentText" xml:space="preserve">
<value>A file with the same name already exists. After performing this action, overwriting will occur. Are you sure you want to continue?</value>
</data>
<data name="StoreFileName_Validation_ErrorMessage" xml:space="preserve">
<value>Not valid name</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/Asv.Drones.Gui/RS.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1900,4 +1900,10 @@
<data name="PlanningPageViewModel_MissionSavingBrowserDialog_ConfirmationText_ContentText" xml:space="preserve">
<value>Файл с таким именем уже существует. После выполнения данного действия произойдёт перезапись. Вы уверены, что хотите продолжить?</value>
</data>
<data name="StoreFileName_ValidationErrorMessage" xml:space="preserve">
<value>Неверное имя</value>
</data>
<data name="StoreFileName_Validation_ErrorMessage" xml:space="preserve">
<value>Неверное имя</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Composition;
using System.Linq;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using Asv.Drones.Gui.Api;
using Asv.Mavlink;
using Asv.Mavlink.V2.Common;
Expand All @@ -14,10 +15,13 @@
using Material.Icons;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ReactiveUI.Validation.Abstractions;
using ReactiveUI.Validation.Contexts;
using ReactiveUI.Validation.Extensions;

namespace Asv.Drones.Gui;

public class PlaningMissionSavingBrowserViewModel : HierarchicalStoreViewModel<Guid, PlaningMissionFile>
public partial class PlaningMissionSavingBrowserViewModel : HierarchicalStoreViewModel<Guid, PlaningMissionFile>
{
private readonly IPlaningMission _svc;

Expand All @@ -33,8 +37,14 @@ public PlaningMissionSavingBrowserViewModel(IPlaningMission svc, ILogService log
_svc = svc;
FileName = name;
using var a = Refresh.Execute().Subscribe();
this.ValidationRule(x => x.FileName,
s => s != null && FileNameRegex().IsMatch(s),
RS.StoreFileName_Validation_ErrorMessage);
}

[GeneratedRegex(@"^[A-Za-z][A-Za-z0-9_\- +]{0,28}")]
private partial Regex FileNameRegex();

[Reactive] public string? FileName { get; set; } = string.Empty;

protected override void RefreshImpl()
Expand Down Expand Up @@ -129,4 +139,6 @@ public void SaveAsImpl(out Guid id)
id = fileId;
Refresh.Execute().Subscribe(_ => { });
}


}

0 comments on commit f8c7776

Please sign in to comment.