Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Commit

Permalink
refactor: Use file scoped namespaces (#75)
Browse files Browse the repository at this point in the history
* Use file scoped namespaces everywhere
* Use .NET 6
  • Loading branch information
worldbeater authored Jan 7, 2022
1 parent c2c394b commit cb79cd8
Show file tree
Hide file tree
Showing 38 changed files with 1,212 additions and 1,151 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Install .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.301
dotnet-version: 6.0.101

- name: NBGV
id: nbgv
Expand Down
21 changes: 12 additions & 9 deletions src/Avalonia.NameGenerator.Sandbox/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using Avalonia.Markup.Xaml;
using Avalonia.NameGenerator.Sandbox.ViewModels;
using Avalonia.NameGenerator.Sandbox.Views;

namespace Avalonia.NameGenerator.Sandbox
namespace Avalonia.NameGenerator.Sandbox;

public class App : Application
{
public class App : Application
{
public override void Initialize() => AvaloniaXamlLoader.Load(this);
public override void Initialize() => AvaloniaXamlLoader.Load(this);

public override void OnFrameworkInitializationCompleted()
public override void OnFrameworkInitializationCompleted()
{
var view = new SignUpView
{
var view = new SignUpView();
view.Show();
base.OnFrameworkInitializationCompleted();
}
ViewModel = new SignUpViewModel()
};
view.Show();
base.OnFrameworkInitializationCompleted();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5</TargetFramework>
<TargetFramework>net6</TargetFramework>
<LangVersion>preview</LangVersion>
<IsPackable>false</IsPackable>
<InstallAvalonia>true</InstallAvalonia>
Expand All @@ -19,4 +19,7 @@
<ItemGroup>
<ProjectReference Include="..\Avalonia.NameGenerator\Avalonia.NameGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ReactiveUI.Validation" Version="2.2.1" />
</ItemGroup>
</Project>
8 changes: 6 additions & 2 deletions src/Avalonia.NameGenerator.Sandbox/Controls/CustomTextBox.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using Avalonia.Controls;
using Avalonia.Styling;

namespace Avalonia.NameGenerator.Sandbox.Controls
namespace Avalonia.NameGenerator.Sandbox.Controls;

public class CustomTextBox : TextBox, IStyleable
{
public class CustomTextBox : TextBox { }
Type IStyleable.StyleKey => typeof(TextBox);
}
19 changes: 9 additions & 10 deletions src/Avalonia.NameGenerator.Sandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using Avalonia.ReactiveUI;

namespace Avalonia.NameGenerator.Sandbox
namespace Avalonia.NameGenerator.Sandbox;

internal static class Program
{
internal static class Program
{
public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
public static void Main(string[] args) => BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);

private static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UseReactiveUI()
.UsePlatformDetect()
.LogToTrace();
}
private static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UseReactiveUI()
.UsePlatformDetect()
.LogToTrace();
}
70 changes: 70 additions & 0 deletions src/Avalonia.NameGenerator.Sandbox/ViewModels/SignUpViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Reactive;
using ReactiveUI;
using ReactiveUI.Validation.Extensions;
using ReactiveUI.Validation.Helpers;

namespace Avalonia.NameGenerator.Sandbox.ViewModels;

public class SignUpViewModel : ReactiveValidationObject
{
private string _userName = string.Empty;
private string _password = string.Empty;
private string _confirmPassword = string.Empty;

public SignUpViewModel()
{
this.ValidationRule(
vm => vm.UserName,
name => !string.IsNullOrWhiteSpace(name),
"UserName is required.");

this.ValidationRule(
vm => vm.Password,
password => !string.IsNullOrWhiteSpace(password),
"Password is required.");

this.ValidationRule(
vm => vm.Password,
password => password?.Length > 2,
password => $"Password should be longer, current length: {password.Length}");

this.ValidationRule(
vm => vm.ConfirmPassword,
confirmation => !string.IsNullOrWhiteSpace(confirmation),
"Confirm password field is required.");

var passwordsObservable =
this.WhenAnyValue(
x => x.Password,
x => x.ConfirmPassword,
(password, confirmation) =>
password == confirmation);

this.ValidationRule(
vm => vm.ConfirmPassword,
passwordsObservable,
"Passwords must match.");

SignUp = ReactiveCommand.Create(() => {}, this.IsValid());
}

public ReactiveCommand<Unit, Unit> SignUp { get; }

public string UserName
{
get => _userName;
set => this.RaiseAndSetIfChanged(ref _userName, value);
}

public string Password
{
get => _password;
set => this.RaiseAndSetIfChanged(ref _password, value);
}

public string ConfirmPassword
{
get => _confirmPassword;
set => this.RaiseAndSetIfChanged(ref _confirmPassword, value);
}
}
29 changes: 8 additions & 21 deletions src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,23 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Avalonia.NameGenerator.Sandbox.Controls"
x:Class="Avalonia.NameGenerator.Sandbox.Views.SignUpView">
<StackPanel>
<!-- Regular name directives. -->
<StackPanel Margin="10">
<TextBlock Text="Sign Up" />
<controls:CustomTextBox Margin="0 10 0 0"
Name="UserNameTextBox"
Watermark="Please, enter user name..."
UseFloatingWatermark="True" />
<TextBlock Name="UserNameValidation"
x:Name="UserNameTextBox"
Watermark="Please, enter user name..."
UseFloatingWatermark="True" />
<TextBlock x:Name="UserNameValidation"
Foreground="Red"
x:FieldModifier="public"
FontSize="12" />
<TextBox Margin="0 10 0 0"
Name="PasswordTextBox"
x:FieldModifier="private"
x:Name="PasswordTextBox"
Watermark="Please, enter your password..."
UseFloatingWatermark="True"
PasswordChar="*" />
<TextBlock Name="PasswordValidation"
<TextBlock x:Name="PasswordValidation"
Foreground="Red"
FontSize="12" />

<!-- DataTemplates should be removed. -->
<ListBox x:Name="AwesomeListView">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="MeaningLessName" Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

<!-- x:Name directives. -->
<TextBox Margin="0 10 0 0"
x:Name="ConfirmPasswordTextBox"
Watermark="Please, confirm the password..."
Expand Down
67 changes: 46 additions & 21 deletions src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
using Avalonia.Controls;
using System;
using System.Reactive.Disposables;
using Avalonia.NameGenerator.Sandbox.ViewModels;
using Avalonia.ReactiveUI;
using ReactiveUI;
using ReactiveUI.Validation.Extensions;
using ReactiveUI.Validation.Formatters;

namespace Avalonia.NameGenerator.Sandbox.Views
namespace Avalonia.NameGenerator.Sandbox.Views;

/// <summary>
/// This is a sample view class with typed x:Name references generated using
/// .NET 5 source generators. The class has to be partial because x:Name
/// references are living in a separate partial class file. See also:
/// https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
/// </summary>
public partial class SignUpView : ReactiveWindow<SignUpViewModel>
{
/// <summary>
/// This is a sample view class with typed x:Name references generated at compile-time using
/// .NET 5 source generators. The class should be marked with [GenerateTypedNameReferences],
/// this attribute is also compile-time generated. The class has to be partial because x:Name
/// references are living in a separate partial class file. See also:
/// https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
/// </summary>
public partial class SignUpView : Window
public SignUpView()
{
public SignUpView()
// The InitializeComponent method is also generated automatically
// and lives in the autogenerated part of the partial class.
InitializeComponent();
this.WhenActivated(disposables =>
{
InitializeComponent();
this.Bind(ViewModel, x => x.UserName, x => x.UserNameTextBox.Text)
.DisposeWith(disposables);
this.Bind(ViewModel, x => x.Password, x => x.PasswordTextBox.Text)
.DisposeWith(disposables);
this.Bind(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordTextBox.Text)
.DisposeWith(disposables);
this.BindCommand(ViewModel, x => x.SignUp, x => x.SignUpButton)
.DisposeWith(disposables);

this.BindValidation(ViewModel, x => x.UserName, x => x.UserNameValidation.Text)
.DisposeWith(disposables);
this.BindValidation(ViewModel, x => x.Password, x => x.PasswordValidation.Text)
.DisposeWith(disposables);
this.BindValidation(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordValidation.Text)
.DisposeWith(disposables);

var newLineFormatter = new SingleLineFormatter(Environment.NewLine);
this.BindValidation(ViewModel, x => x.CompoundValidation.Text, newLineFormatter)
.DisposeWith(disposables);

// The references to text boxes below are also auto generated.
// Use Ctrl+Click in order to view the generated sources.
UserNameTextBox.Text = "Joseph!";
UserNameValidation.Text = "User name is valid.";
PasswordTextBox.Text = "qwerty";
PasswordValidation.Text = "Password is valid.";
ConfirmPasswordTextBox.Text = "qwerty";
ConfirmPasswordValidation.Text = "Password confirmation is valid.";
SignUpButton.Content = "Sign up please!";
CompoundValidation.Text = "Everything is okay.";
AwesomeListView.VirtualizationMode = ItemVirtualizationMode.None;
}
PasswordTextBox.Text = "1234";
ConfirmPasswordTextBox.Text = "1234";
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5</TargetFramework>
<TargetFramework>net6</TargetFramework>
<LangVersion>preview</LangVersion>
<IsPackable>false</IsPackable>
<InstallAvalonia>true</InstallAvalonia>
Expand Down
47 changes: 23 additions & 24 deletions src/Avalonia.NameGenerator.Tests/GlobPatternTests.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
using Avalonia.NameGenerator.Generator;
using Xunit;

namespace Avalonia.NameGenerator.Tests
namespace Avalonia.NameGenerator.Tests;

public class GlobPatternTests
{
public class GlobPatternTests
[Theory]
[InlineData("*", "anything", true)]
[InlineData("", "anything", false)]
[InlineData("Views/*", "Views/SignUpView.xaml", true)]
[InlineData("Views/*", "Extensions/SignUpView.xaml", false)]
[InlineData("*SignUpView*", "Extensions/SignUpView.xaml", true)]
[InlineData("*SignUpView.paml", "Extensions/SignUpView.xaml", false)]
[InlineData("*.xaml", "Extensions/SignUpView.xaml", true)]
public void Should_Match_Glob_Expressions(string pattern, string value, bool matches)
{
[Theory]
[InlineData("*", "anything", true)]
[InlineData("", "anything", false)]
[InlineData("Views/*", "Views/SignUpView.xaml", true)]
[InlineData("Views/*", "Extensions/SignUpView.xaml", false)]
[InlineData("*SignUpView*", "Extensions/SignUpView.xaml", true)]
[InlineData("*SignUpView.paml", "Extensions/SignUpView.xaml", false)]
[InlineData("*.xaml", "Extensions/SignUpView.xaml", true)]
public void Should_Match_Glob_Expressions(string pattern, string value, bool matches)
{
Assert.Equal(matches, new GlobPattern(pattern).Matches(value));
}
Assert.Equal(matches, new GlobPattern(pattern).Matches(value));
}

[Theory]
[InlineData("Views/SignUpView.xaml", true, new[] { "*.xaml", "Extensions/*" })]
[InlineData("Extensions/SignUpView.paml", true, new[] { "*.xaml", "Extensions/*" })]
[InlineData("Extensions/SignUpView.paml", false, new[] { "*.xaml", "Views/*" })]
[InlineData("anything", true, new[] { "*", "*" })]
[InlineData("anything", false, new[] { "", "" })]
public void Should_Match_Glob_Pattern_Groups(string value, bool matches, string[] patterns)
{
Assert.Equal(matches, new GlobPatternGroup(patterns).Matches(value));
}
[Theory]
[InlineData("Views/SignUpView.xaml", true, new[] { "*.xaml", "Extensions/*" })]
[InlineData("Extensions/SignUpView.paml", true, new[] { "*.xaml", "Extensions/*" })]
[InlineData("Extensions/SignUpView.paml", false, new[] { "*.xaml", "Views/*" })]
[InlineData("anything", true, new[] { "*", "*" })]
[InlineData("anything", false, new[] { "", "" })]
public void Should_Match_Glob_Pattern_Groups(string value, bool matches, string[] patterns)
{
Assert.Equal(matches, new GlobPatternGroup(patterns).Matches(value));
}
}
Loading

0 comments on commit cb79cd8

Please sign in to comment.