Skip to content

Commit

Permalink
Merge pull request #56 from jamesmontemagno/nullable
Browse files Browse the repository at this point in the history
Update to nullable reference types and C# 8
  • Loading branch information
jamesmontemagno authored Nov 8, 2019
2 parents 4908696 + 2ed1ee1 commit e9d6deb
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 104 deletions.
17 changes: 17 additions & 0 deletions MvvmHelpers.UnitTests/AsyncCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
using MvvmHelpers.Commands;
using System;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MvvmHelpers.UnitTests
{
[TestClass]
public class AsyncCommandTests
{

ICommand refreshCommand;
public ICommand RefreshCommand => refreshCommand ??
(refreshCommand = new AsyncCommand<bool>((t) => ExecuteLoadCommand(true)));
async Task ExecuteLoadCommand(bool forceRefresh)
{
await Task.Delay(1000);
}

#region Events
protected event EventHandler TestEvent
{
Expand Down Expand Up @@ -45,6 +55,13 @@ protected async Task IntParameterDelayedNullReferenceExceptionTask(int delay)
protected bool CanExecuteDynamic(object booleanParameter) => (bool)booleanParameter;
#endregion

[TestMethod]
public void AsyncCommand_UsingICommand()
{
//Arrange
RefreshCommand.Execute(true);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void AsyncCommand_NullExecuteParameter()
Expand Down
14 changes: 0 additions & 14 deletions MvvmHelpers.UnitTests/CommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ public void ThrowsWithNullParameterizedConstructor()
new Command((Action<object>)null);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ThrowsWithNullCanExecute()
{
new Command(() => { }, null);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ThrowsWithNullParameterizedCanExecute()
{
new Command(o => { }, null);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ThrowsWithNullExecuteValidCanExecute()
Expand Down
11 changes: 5 additions & 6 deletions MvvmHelpers.UnitTests/MvvmHelpers.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="MSBuild.Sdk.Extras" Version="1.2.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion MvvmHelpers.UnitTests/ObservableObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public void ValidateEventException()
person.Validate = (oldValue, newValue) =>
{
throw new ArgumentOutOfRangeException();
return false;
};

Assert.ThrowsException<ArgumentOutOfRangeException>(() => person.FirstName = "Motz", "Should throw ArgumentOutOfRangeException");
Expand Down
27 changes: 27 additions & 0 deletions MvvmHelpers.UnitTests/TestFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using MvvmHelpers.Commands;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;


#nullable enable

namespace MvvmHelpers.UnitTests
{
public class TestFile
{
public AsyncCommand<string> MyCommand { get; }

public TestFile()
{
MyCommand = new AsyncCommand<string>(Test, null);
}

Task Test(string test)
{

return Task.CompletedTask;
}
}
}
20 changes: 10 additions & 10 deletions MvvmHelpers/BaseViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ namespace MvvmHelpers
/// </summary>
public class BaseViewModel : ObservableObject
{
string title = string.Empty;
string? title = string.Empty;

/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
public string Title
public string? Title
{
get => title;
set => SetProperty(ref title, value);
}

string subtitle = string.Empty;
string? subtitle = string.Empty;

/// <summary>
/// Gets or sets the subtitle.
/// </summary>
/// <value>The subtitle.</value>
public string Subtitle
public string? Subtitle
{
get => subtitle;
set => SetProperty(ref subtitle, value);
}

string icon = string.Empty;
string? icon = string.Empty;

/// <summary>
/// Gets or sets the icon.
/// </summary>
/// <value>The icon.</value>
public string Icon
public string? Icon
{
get => icon;
set => SetProperty(ref icon, value);
Expand Down Expand Up @@ -86,25 +86,25 @@ public bool CanLoadMore
}


string header = string.Empty;
string? header = string.Empty;

/// <summary>
/// Gets or sets the header.
/// </summary>
/// <value>The header.</value>
public string Header
public string? Header
{
get => header;
set => SetProperty(ref header, value);
}

string footer = string.Empty;
string? footer = string.Empty;

/// <summary>
/// Gets or sets the footer.
/// </summary>
/// <value>The footer.</value>
public string Footer
public string? Footer
{
get => footer;
set => SetProperty(ref footer, value);
Expand Down
16 changes: 8 additions & 8 deletions MvvmHelpers/Commands/AsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace MvvmHelpers.Commands
public class AsyncCommand : IAsyncCommand
{
readonly Func<Task> execute;
readonly Func<object, bool> canExecute;
readonly Action<Exception> onException;
readonly Func<object, bool>? canExecute;
readonly Action<Exception>? onException;
readonly bool continueOnCapturedContext;
readonly WeakEventManager weakEventManager = new WeakEventManager();

Expand All @@ -28,8 +28,8 @@ public class AsyncCommand : IAsyncCommand
/// <param name="onException">Action callback when an exception occurs</param>
/// <param name="continueOnCapturedContext">If the context should be captured on exception</param>
public AsyncCommand(Func<Task> execute,
Func<object, bool> canExecute = null,
Action<Exception> onException = null,
Func<object, bool>? canExecute = null,
Action<Exception>? onException = null,
bool continueOnCapturedContext = false)
{
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
Expand Down Expand Up @@ -76,8 +76,8 @@ public class AsyncCommand<T> : IAsyncCommand<T>
{

readonly Func<T, Task> execute;
readonly Func<object, bool> canExecute;
readonly Action<Exception> onException;
readonly Func<object, bool>? canExecute;
readonly Action<Exception>? onException;
readonly bool continueOnCapturedContext;
readonly WeakEventManager weakEventManager = new WeakEventManager();

Expand All @@ -89,8 +89,8 @@ public class AsyncCommand<T> : IAsyncCommand<T>
/// <param name="onException">Action callback when an exception occurs</param>
/// <param name="continueOnCapturedContext">If the context should be captured on exception</param>
public AsyncCommand(Func<T, Task> execute,
Func<object, bool> canExecute = null,
Action<Exception> onException = null,
Func<object, bool>? canExecute = null,
Action<Exception>? onException = null,
bool continueOnCapturedContext = false)
{
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
Expand Down
13 changes: 7 additions & 6 deletions MvvmHelpers/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Command(Action<T> execute, Func<T, bool> canExecute)
/// </summary>
public class Command : ICommand
{
readonly Func<object, bool> canExecute;
readonly Func<object, bool>? canExecute;
readonly Action<object> execute;
readonly WeakEventManager weakEventManager = new WeakEventManager();

Expand Down Expand Up @@ -82,22 +82,23 @@ public Command(Action execute) : this(o => execute())
/// </summary>
/// <param name="execute">Action to execute.</param>
/// <param name="canExecute">Function to determine if can execute.</param>
public Command(Action<object> execute, Func<object, bool> canExecute) : this(execute)
public Command(Action<object> execute, Func<object, bool>? canExecute) : this(execute)
{
this.canExecute = canExecute ?? throw new ArgumentNullException(nameof(canExecute));
this.canExecute = canExecute;
}

/// <summary>
/// Command that takes an action to execute.
/// </summary>
/// <param name="execute">Action to execute.</param>
/// <param name="canExecute">Function to determine if can execute.</param>
public Command(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute())
public Command(Action execute, Func<bool>? canExecute) : this(o => execute())
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
if (canExecute == null)
throw new ArgumentNullException(nameof(canExecute));

if (canExecute != null)
this.canExecute = o => canExecute();
}

/// <summary>
Expand Down
Binary file added MvvmHelpers/Images/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 50 additions & 44 deletions MvvmHelpers/MvvmHelpers.csproj
Original file line number Diff line number Diff line change
@@ -1,52 +1,58 @@
<Project Sdk="MSBuild.Sdk.Extras/2.0.43">
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">

<PropertyGroup>
<TargetFrameworks>net461;netstandard1.0;netstandard2.0</TargetFrameworks>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyFileVersion>1.0.0.0</AssemblyFileVersion>
<Version>1.0.0.0</Version>
<PackageVersion>1.0.0.0</PackageVersion>
<Authors>James Montemagno</Authors>
<PackageId>Refractored.MvvmHelpers</PackageId>
<PackOnBuild>true</PackOnBuild>
<PackageIconUrl>https://raw.githubusercontent.com/jamesmontemagno/mvvm-helpers/master/art/Icon.png</PackageIconUrl>
<NeutralLanguage>en</NeutralLanguage>
<PropertyGroup>
<TargetFrameworks>net461;netstandard1.0;netstandard2.0</TargetFrameworks>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyFileVersion>1.0.0.0</AssemblyFileVersion>
<Version>1.0.0.0</Version>
<PackageVersion>1.0.0.0</PackageVersion>
<Authors>James Montemagno</Authors>
<PackageId>Refractored.MvvmHelpers</PackageId>
<PackOnBuild>true</PackOnBuild>
<PackageIcon>Icon.png</PackageIcon>
<NeutralLanguage>en</NeutralLanguage>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Owners>James Montemagno</Owners>
<PackageProjectUrl>https://github.com/jamesmontemagno/mvvm-helpers</PackageProjectUrl>
<Summary>Collection of MVVM Helpers such as ObservableRangeCollection, BaseViewModel, Grouping, and others.</Summary>
<PackageTags>mvvm, observable collection, view model, grouping, xamarin, pcl, xam.pcl, plugin, plugin for xamarin, windows phone, winphone, wp8, winrt, android, xamarin.forms, ios</PackageTags>
<Title>MVVM Helpers for Xamarin and Windows 🐒</Title>
<Description>Collection of MVVM Helpers such as ObservableRangeCollection, BaseViewModel, Grouping, and others.</Description>
<Copyright>2017 Refractored LLC &amp; James Montemagno</Copyright>
<RepositoryUrl>https://github.com/jamesmontemagno/mvvm-helpers</RepositoryUrl>
<PackageReleaseNotes>See: https://github.com/jamesmontemagno/mvvm-helpers/blob/master/CHANGELOG.md </PackageReleaseNotes>
<Owners>James Montemagno</Owners>
<PackageProjectUrl>https://github.com/jamesmontemagno/mvvm-helpers</PackageProjectUrl>
<Summary>Collection of MVVM Helpers such as ObservableRangeCollection, BaseViewModel, Grouping, and others.</Summary>
<PackageTags>mvvm, observable collection, view model, grouping, xamarin, pcl, xam.pcl, plugin, plugin for xamarin, windows phone, winphone, wp8, winrt, android, xamarin.forms, ios</PackageTags>
<Title>MVVM Helpers for Xamarin and Windows 🐒</Title>
<Description>Collection of MVVM Helpers such as ObservableRangeCollection, BaseViewModel, Grouping, and others.</Description>
<Copyright>2019 Refractored LLC &amp; James Montemagno</Copyright>
<RepositoryUrl>https://github.com/jamesmontemagno/mvvm-helpers</RepositoryUrl>
<PackageReleaseNotes>See: https://github.com/jamesmontemagno/mvvm-helpers/blob/master/CHANGELOG.md </PackageReleaseNotes>

<LangVersion>7.3</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RootNamespace>MvvmHelpers</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>8.0</LangVersion>

<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<RootNamespace>MvvmHelpers</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)'=='Debug' ">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)'=='Release' And '$(OS)' == 'Windows_NT' ">
<DebugType>portable</DebugType>
<!-- sourcelink: Declare that the Repository URL can be published to NuSpec -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- sourcelink: Embed source files that are not tracked by the source control manager to the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- sourcelink: Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup> <ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19367-01" PrivateAssets="All" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<PropertyGroup Condition=" '$(Configuration)'=='Debug' ">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)'=='Release' And '$(OS)' == 'Windows_NT' ">
<DebugType>portable</DebugType>
<!-- sourcelink: Declare that the Repository URL can be published to NuSpec -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- sourcelink: Embed source files that are not tracked by the source control manager to the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- sourcelink: Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<ItemGroup>
<None Include="Images\Icon.png" Pack="true" PackagePath="\"/>

<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19554-01" PrivateAssets="All" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions MvvmHelpers/ObservableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class ObservableObject : INotifyPropertyChanged
protected virtual bool SetProperty<T>(
ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null,
Func<T, T, bool> validateValue = null)
Action? onChanged = null,
Func<T, T, bool>? validateValue = null)
{
//if value didn't change
if (EqualityComparer<T>.Default.Equals(backingStore, value))
Expand All @@ -44,7 +44,7 @@ protected virtual bool SetProperty<T>(
/// <summary>
/// Occurs when property changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;

/// <summary>
/// Raises the property changed event.
Expand Down
Loading

0 comments on commit e9d6deb

Please sign in to comment.