Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Enable Nullability on Xamarin.CommunityToolkit.Sample (#1014)
Browse files Browse the repository at this point in the history
* Enable Nullable on Unit Tests

* Enable Nullable on Xamarin.CommunityToolkit.Markup

* Enable Nullable on Xamarin.CommunityToolkit.Sample
  • Loading branch information
brminnick committed Mar 4, 2021
1 parent 968bdc0 commit 1e07fe6
Show file tree
Hide file tree
Showing 39 changed files with 304 additions and 408 deletions.
84 changes: 38 additions & 46 deletions samples/XCT.Sample/Helpers/RelayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@ namespace Xamarin.CommunityToolkit.Sample
{
public class RelayCommand : ICommand
{
readonly Action execute;
readonly Func<Task> asyncExecute;
readonly Action? execute;
readonly Func<Task>? asyncExecute;
readonly Func<bool>? canExecute;

Func<bool> canExecute;
int executingCount;

public RelayCommand(Action execute, Func<bool> canExecute = null)
public RelayCommand(Action execute, Func<bool>? canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
this.execute = execute;
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

protected RelayCommand(Func<Task> execute, Func<bool> canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
protected RelayCommand(Func<Task> execute, Func<bool>? canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
asyncExecute = execute;
asyncExecute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

Expand All @@ -34,7 +30,7 @@ public RelayCommand(Action execute, Func<bool> canExecute = null)
/// </summary>
/// <param name="parameter">Ignored; this is the paremeterless command class</param>
/// <returns></returns>
public bool CanExecute(object parameter = null)
public bool CanExecute(object? parameter = null)
{
try
{
Expand All @@ -47,15 +43,12 @@ public bool CanExecute(object parameter = null)
}
}

public event EventHandler CanExecuteChanged;
public event EventHandler? CanExecuteChanged;

public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

// Asynchronous command handling based on http://stackoverflow.com/a/31595509/6043538
public async void Execute(object parameter = null)
public async void Execute(object? parameter = null)
{
var couldExecuteBeforeExecute = CanExecute();
if (!couldExecuteBeforeExecute)
Expand All @@ -70,8 +63,10 @@ public async void Execute(object parameter = null)
{
if (execute != null)
execute();
else
else if (asyncExecute != null)
await asyncExecute();
else
throw new Exception("Execute is null");
}
catch (Exception ex)
{
Expand All @@ -89,31 +84,30 @@ public async void Execute(object parameter = null)

public class RelayCommandAsync : RelayCommand
{
public RelayCommandAsync(Func<Task> execute, Func<bool> canExecute = null)
: base(execute, canExecute) { } // This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
public RelayCommandAsync(Func<Task> execute, Func<bool>? canExecute = null)
: base(execute, canExecute)
{
// This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
}
}

public class RelayCommand<TParameter> : ICommand
{
readonly Action<TParameter> execute;
readonly Func<TParameter, Task> asyncExecute;
readonly Action<TParameter>? execute;
readonly Func<TParameter, Task>? asyncExecute;
readonly Func<TParameter?, bool>? canExecute;

Func<TParameter, bool> canExecute;
int executingCount;

public RelayCommand(Action<TParameter> execute, Func<TParameter, bool> canExecute = null)
public RelayCommand(Action<TParameter> execute, Func<TParameter?, bool>? canExecute = null)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
this.execute = execute;
this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

protected RelayCommand(Func<TParameter, Task> execute, Func<TParameter, bool> canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
protected RelayCommand(Func<TParameter, Task> execute, Func<TParameter?, bool>? canExecute = null) // This ctor is protected here and public in a derived class, to allow simple initialization like new RelayCommand(MyMethod) without errors due to ambiguity
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
asyncExecute = execute;
asyncExecute = execute ?? throw new ArgumentNullException(nameof(execute));
this.canExecute = canExecute;
}

Expand All @@ -122,11 +116,11 @@ public RelayCommand(Action<TParameter> execute, Func<TParameter, bool> canExecut
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter = null)
public bool CanExecute(object? parameter = null)
{
try
{
return canExecute != null ? canExecute((TParameter)parameter) : executingCount == 0;
return canExecute != null ? canExecute((TParameter?)parameter) : executingCount == 0;
}
catch (Exception ex)
{
Expand All @@ -135,12 +129,9 @@ public bool CanExecute(object parameter = null)
}
}

public event EventHandler CanExecuteChanged;
public event EventHandler? CanExecuteChanged;

public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);

// Asynchronous command handling based on http://stackoverflow.com/a/31595509/6043538
public async void Execute(object parameterAsObject)
Expand All @@ -159,13 +150,11 @@ public async void Execute(object parameterAsObject)
var parameter = (TParameter)parameterAsObject;

if (execute != null)
{
execute(parameter);
}
else if (asyncExecute != null)
await asyncExecute.Invoke(parameter);
else
{
await asyncExecute(parameter);
}
throw new Exception("Execute is null");
}
catch (Exception ex)
{
Expand All @@ -183,7 +172,10 @@ public async void Execute(object parameterAsObject)

public class RelayCommandAsync<TParameter> : RelayCommand<TParameter>
{
public RelayCommandAsync(Func<TParameter, Task> execute, Func<TParameter, bool> canExecute = null)
: base(execute, canExecute) { } // This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
public RelayCommandAsync(Func<TParameter, Task> execute, Func<TParameter?, bool>? canExecute = null)
: base(execute, canExecute)
{
// This ctor is public here and protected in the base class, to allow simple initialization like new RelayCommandAsync(MyMethod) without errors due to ambiguity
}
}
}
26 changes: 13 additions & 13 deletions samples/XCT.Sample/Helpers/XLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Xamarin.CommunityToolkit.Sample
/// </summary>
public static class XLog
{
static string rootFolderPattern = null;
static string? rootFolderPattern = null;
#if WINDOWS_UWP
static LoggingChannel loggingChannel;
#endif
Expand All @@ -22,7 +22,7 @@ public static class XLog
/// Call this before logging starts.
/// </summary>
/// <param name="rootFolderPattern">Should match the top folder name(s) within the source control repository, e.g. @"\MobileRealtimePush\MobileRealtimePush\". Any folders before the first match of this pattern are omitted from the logged source file paths</param>
public static void Init(string rootFolderPattern = null)
public static void Init(string? rootFolderPattern = null)
{
XLog.rootFolderPattern = rootFolderPattern;
#if WINDOWS_UWP
Expand All @@ -46,10 +46,10 @@ public static void Init(string rootFolderPattern = null)
/// <param name="sourceLineNumber">supplied by compiler, no need to specify in code unless you want to pass a deeper call context</param>
[Conditional("DEBUG")]
public static void Debug(
object data = null,
string tag = null,
[CallerMemberName] string memberName = null,
[CallerFilePath] string sourceFilePath = null,
object? data = null,
string? tag = null,
[CallerMemberName] string? memberName = null,
[CallerFilePath] string? sourceFilePath = null,
[CallerLineNumber] int sourceLineNumber = -1)
{
var message = FormatLogString(data, tag, memberName, sourceFilePath, sourceLineNumber);
Expand All @@ -75,10 +75,10 @@ public static void Init(string rootFolderPattern = null)
/// <param name="sourceLineNumber">supplied by compiler, no need to specify in code unless you want to pass a deeper call context</param>
[Conditional("TRACE")]
public static void Trace(
object data = null,
string tag = null,
[CallerMemberName] string memberName = null,
[CallerFilePath] string sourceFilePath = null,
object? data = null,
string? tag = null,
[CallerMemberName] string? memberName = null,
[CallerFilePath] string? sourceFilePath = null,
[CallerLineNumber] int sourceLineNumber = -1)
{
var message = FormatLogString(data, tag, memberName, sourceFilePath, sourceLineNumber);
Expand All @@ -90,9 +90,9 @@ public static void Init(string rootFolderPattern = null)
#endif
}

public static string TruncateAt(this string s, int maxLength, string truncatedSuffix = "...") => s?.Length <= maxLength ? s : s.Substring(0, maxLength) + truncatedSuffix;
public static string TruncateAt(this string? s, int maxLength, string truncatedSuffix = "...") => s?.Length <= maxLength ? s : s?.Substring(0, maxLength) + truncatedSuffix;

static string FormatLogString(object data = null, string tag = null, string memberName = null, string sourceFilePath = null, int sourceLineNumber = -1)
static string FormatLogString(object? data, string? tag, string? memberName, string? sourceFilePath, int sourceLineNumber)
{
var line = new StringBuilder();

Expand Down Expand Up @@ -121,7 +121,7 @@ static string FormatLogString(object data = null, string tag = null, string memb
line.Append(dataString);
}

if (!string.IsNullOrEmpty(sourceFilePath))
if (sourceFilePath != null && !string.IsNullOrEmpty(sourceFilePath))
{
if (!string.IsNullOrEmpty(rootFolderPattern))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class BoolToObjectConverterPage
public partial class BoolToObjectConverterPage
{
public BoolToObjectConverterPage()
{
InitializeComponent();
}
public BoolToObjectConverterPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class DoubleToIntConverterPage
public partial class DoubleToIntConverterPage
{
public DoubleToIntConverterPage()
{
InitializeComponent();
}
public DoubleToIntConverterPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class EnumToBoolConverterPage
{
public EnumToBoolConverterPage()
{
InitializeComponent();
}
}
public partial class EnumToBoolConverterPage
{
public EnumToBoolConverterPage() => InitializeComponent();
}
}
14 changes: 3 additions & 11 deletions samples/XCT.Sample/Pages/Converters/EqualConverterPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class EqualConverterPage
public partial class EqualConverterPage
{
public EqualConverterPage()
{
InitializeComponent();
}
public EqualConverterPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class IndexToArrayItemConverterPage
public partial class IndexToArrayItemConverterPage
{
public IndexToArrayItemConverterPage()
{
InitializeComponent();
}
public IndexToArrayItemConverterPage() => InitializeComponent();
}
}
14 changes: 3 additions & 11 deletions samples/XCT.Sample/Pages/Converters/IntToBoolConverterPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class IntToBoolConverterPage
public partial class IntToBoolConverterPage
{
public IntToBoolConverterPage()
{
InitializeComponent();
}
public IntToBoolConverterPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class InvertedBoolConverterPage
public partial class InvertedBoolConverterPage
{
public InvertedBoolConverterPage()
{
InitializeComponent();
}
public InvertedBoolConverterPage() => InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class IsNotNullOrEmptyConverterPage
public partial class IsNotNullOrEmptyConverterPage
{
public IsNotNullOrEmptyConverterPage()
{
InitializeComponent();
}
public IsNotNullOrEmptyConverterPage() => InitializeComponent();
}
}
Loading

0 comments on commit 1e07fe6

Please sign in to comment.