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

Enable Nullable on XamarinCommunityToolkit #1023

Merged
merged 15 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion samples/XCT.Sample/Pages/Base/BasePage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.CommunityToolkit.Sample.Models;
Expand All @@ -9,7 +10,13 @@ namespace Xamarin.CommunityToolkit.Sample.Pages
public class BasePage : ContentPage
{
public BasePage() =>
NavigateCommand = CommandFactory.Create<SectionModel>(sectionModel => Navigation.PushAsync(PreparePage(sectionModel)));
NavigateCommand = CommandFactory.Create<SectionModel>(sectionModel =>
{
if (sectionModel != null)
return Navigation.PushAsync(PreparePage(sectionModel));

return Task.CompletedTask;
});

public Color DetailColor { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class MediaElementViewModel : BindableObject
{
public string VideoAsString { get; set; } = "https://tipcalculator.appwithkiss.com/video/Hint_1_2_EN_12.mov";

public MediaSource VideoAsMediaSource => MediaSource.FromUri(VideoAsString);
public MediaSource? VideoAsMediaSource => MediaSource.FromUri(VideoAsString);
}
}
62 changes: 17 additions & 45 deletions samples/XCT.Sample/Resx/AppResources.Designer.cs

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

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class ItemSelectedEventArgsViewModel
};

public ICommand ItemSelectedCommand { get; } =
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancelf"));
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person?.Name, "Cancel"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ItemTappedEventArgsViewModel
};

public ICommand ItemTappedCommand { get; } =
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancel"));
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person?.Name, "Cancel"));
}

public class Person
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async void OnDisplayPopup(Type popupType)
{
var view = (VisualElement)Activator.CreateInstance(popupType);

if (view is Popup<string> popup)
if (view is Popup<string?> popup)
{
var result = await Navigation.ShowPopupAsync(popup);
await Application.Current.MainPage.DisplayAlert("Popup Result", result, "OKAY");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void ByteArrayToImageSourceConverter()

var result = byteArrayToImageSourceConverter.Convert(byteArray, typeof(ByteArrayToImageSourceConverter), null, CultureInfo.CurrentCulture);

Assert.True(StreamEquals(GetStreamFromImageSource((ImageSource)result), memoryStream));
Assert.True(StreamEquals(GetStreamFromImageSource((ImageSource?)result), memoryStream));
}

[Theory]
Expand All @@ -34,28 +34,32 @@ public void InvalidConverterValuesReturnsNull(object value)
Assert.Throws<ArgumentException>(() => byteArrayToImageSourceConverter.Convert(value, typeof(ByteArrayToImageSourceConverter), null, CultureInfo.CurrentCulture));
}

Stream GetStreamFromImageSource(ImageSource imageSource)
Stream? GetStreamFromImageSource(ImageSource? imageSource)
{
var streamImageSource = (StreamImageSource)imageSource;
var streamImageSource = (StreamImageSource?)imageSource;

var cancellationToken = System.Threading.CancellationToken.None;
var task = streamImageSource.Stream(cancellationToken);
return task.Result;
var task = streamImageSource?.Stream(cancellationToken);
return task?.Result;
}

bool StreamEquals(Stream a, Stream b)
bool StreamEquals(Stream? a, Stream? b)
{
if (a == b)
return true;

if (a == null ||
b == null ||
a.Length != b.Length)
if (a == null
|| b == null
|| a.Length != b.Length)
{
return false;
}

for (var i = 0; i < a.Length; i++)
{
if (a.ReadByte() != b.ReadByte())
return false;
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ void HandleDelegateTest(object sender, PropertyChangedEventArgs e)
}

// Act
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
propertyChangedWeakEventManager.RaiseEvent(null, new PropertyChangedEventArgs("Test"), nameof(PropertyChanged));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.

// Assert
Assert.True(didEventFire);
Expand Down Expand Up @@ -173,7 +175,9 @@ public void WeakEventManagerDelegate_UnassignedEventManager()
void HandleDelegateTest(object sender, PropertyChangedEventArgs e) => didEventFire = true;

// Act
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
unassignedEventManager.RaiseEvent(null, null, nameof(PropertyChanged));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.

// Assert
Assert.False(didEventFire);
Expand Down Expand Up @@ -205,9 +209,7 @@ public void WeakEventManagerDelegate_AddEventHandler_NullHandler()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.AddEventHandler(null));
#pragma warning restore CS8625
}

[Fact]
Expand All @@ -231,9 +233,7 @@ public void WeakEventManagerDelegate_AddEventHandler_EmptyEventName()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.AddEventHandler(null, string.Empty));
#pragma warning restore CS8625
}

[Fact]
Expand All @@ -244,9 +244,7 @@ public void WeakEventManagerDelegate_AddEventHandler_WhitespaceEventName()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.AddEventHandler(null, " "));
#pragma warning restore CS8625
}

[Fact]
Expand All @@ -257,9 +255,7 @@ public void WeakEventManagerDelegate_RemoveEventHandler_NullHandler()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.RemoveEventHandler(null));
#pragma warning restore CS8625
}

[Fact]
Expand All @@ -283,9 +279,7 @@ public void WeakEventManagerDelegate_RemoveEventHandler_EmptyEventName()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.RemoveEventHandler(null, string.Empty));
#pragma warning restore CS8625
}

[Fact]
Expand All @@ -296,9 +290,7 @@ public void WeakEventManagerDelegate_RemoveEventHandler_WhiteSpaceEventName()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference
Assert.Throws<ArgumentNullException>(() => propertyChangedWeakEventManager.RemoveEventHandler(null, " "));
#pragma warning restore CS8625
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ void HandleTestEvent(object? sender, EventArgs e)
}

// Act
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
TestWeakEventManager.RaiseEvent(null, new EventArgs(), nameof(TestEvent));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.

// Assert
Assert.True(didEventFire);
Expand Down Expand Up @@ -145,7 +147,9 @@ public void WeakEventManager_UnassignedEvent()
void HandleTestEvent(object? sender, EventArgs e) => didEventFire = true;

// Act
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
TestWeakEventManager.RaiseEvent(null, null, nameof(TestEvent));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.

// Assert
Assert.False(didEventFire);
Expand All @@ -162,7 +166,9 @@ public void WeakEventManager_UnassignedEventManager()
void HandleTestEvent(object? sender, EventArgs e) => didEventFire = true;

// Act
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
unassignedEventManager.RaiseEvent(null, null, nameof(TestEvent));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.

// Assert
Assert.False(didEventFire);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ public void WeakEventManagerActionT_AddEventHandler_NullHandler()
// Act

// Assert
#pragma warning disable CS8604 // Possible null reference argument.
Assert.Throws<ArgumentNullException>(() => actionEventManager.AddEventHandler(nullAction, nameof(ActionEvent)));
#pragma warning restore CS8604 // Possible null reference argument.

}

Expand All @@ -152,7 +154,9 @@ public void WeakEventManagerActionT_AddEventHandler_NullEventName()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => actionEventManager.AddEventHandler(s => { var temp = s; }, null));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}

[Fact]
Expand All @@ -164,7 +168,9 @@ public void WeakEventManagerActionT_AddEventHandler_EmptyEventName()
// Act

// Assert
#pragma warning disable CS8604 // Possible null reference argument.
Assert.Throws<ArgumentNullException>(() => actionEventManager.AddEventHandler(nullAction, string.Empty));
#pragma warning restore CS8604 // Possible null reference argument.
}

[Fact]
Expand All @@ -187,7 +193,9 @@ public void WeakEventManagerActionT_RemoveEventHandler_NullHandler()
// Act

// Assert
#pragma warning disable CS8604 // Possible null reference argument.
Assert.Throws<ArgumentNullException>(() => actionEventManager.RemoveEventHandler(nullAction));
#pragma warning restore CS8604 // Possible null reference argument.
}

[Fact]
Expand All @@ -198,7 +206,9 @@ public void WeakEventManagerActionT_RemoveEventHandler_NullEventName()
// Act

// Assert
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => actionEventManager.RemoveEventHandler(s => { var temp = s; }, null));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}

[Fact]
Expand Down
Loading