Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mediaplayer] Add nullability to (generated and manual) bindings #15147

Merged
merged 5 commits into from
Jun 6, 2022
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
10 changes: 5 additions & 5 deletions src/MediaPlayer/MPMediaItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,39 @@ public partial class MPMediaItem {
ulong UInt64ForProperty (NSString property)
{
var prop = ValueForProperty (property) as NSNumber;
if (prop == null)
if (prop is null)
return 0;
return prop.UInt64Value;
}

uint UInt32ForProperty (NSString property)
{
var prop = ValueForProperty (property) as NSNumber;
if (prop == null)
if (prop is null)
return 0;
return prop.UInt32Value;
}

int Int32ForProperty (NSString property)
{
var prop = ValueForProperty (property) as NSNumber;
if (prop == null)
if (prop is null)
return 0;
return prop.Int32Value;
}

double DoubleForProperty (NSString property)
{
var prop = ValueForProperty (property) as NSNumber;
if (prop == null)
if (prop is null)
return 0;
return prop.DoubleValue;
}

bool BoolForProperty (NSString property)
{
var prop = ValueForProperty (property) as NSNumber;
if (prop == null)
if (prop is null)
return false;
return prop.BoolValue;
}
Expand Down
32 changes: 16 additions & 16 deletions src/MediaPlayer/MPNowPlayingInfoCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,25 @@ internal NSDictionary ToDictionary ()
if (DefaultPlaybackRate.HasValue)
Add (dict, MPNowPlayingInfoCenter.PropertyDefaultPlaybackRate, new NSNumber (DefaultPlaybackRate.Value));

if (AvailableLanguageOptions != null && AvailableLanguageOptions.Length != 0)
if (AvailableLanguageOptions is not null && AvailableLanguageOptions.Length != 0)
Add (dict, MPNowPlayingInfoCenter.PropertyAvailableLanguageOptions, NSArray.FromObjects (AvailableLanguageOptions));
if (CurrentLanguageOptions != null && CurrentLanguageOptions.Length != 0)
if (CurrentLanguageOptions is not null && CurrentLanguageOptions.Length != 0)
Add (dict, MPNowPlayingInfoCenter.PropertyCurrentLanguageOptions, NSArray.FromObjects (CurrentLanguageOptions));
if (CollectionIdentifier != null)
if (CollectionIdentifier is not null)
Add (dict, MPNowPlayingInfoCenter.PropertyCollectionIdentifier, new NSString (CollectionIdentifier));
if (ExternalContentIdentifier != null)
if (ExternalContentIdentifier is not null)
Add (dict, MPNowPlayingInfoCenter.PropertyExternalContentIdentifier, new NSString (ExternalContentIdentifier));
if (ExternalUserProfileIdentifier != null)
if (ExternalUserProfileIdentifier is not null)
Add (dict, MPNowPlayingInfoCenter.PropertyExternalUserProfileIdentifier, new NSString (ExternalUserProfileIdentifier));
if (PlaybackProgress.HasValue)
Add (dict, MPNowPlayingInfoCenter.PropertyPlaybackProgress, new NSNumber (PlaybackProgress.Value));
if (MediaType.HasValue)
Add (dict, MPNowPlayingInfoCenter.PropertyMediaType, new NSNumber ((int)MediaType.Value));
if (IsLiveStream.HasValue)
Add (dict, MPNowPlayingInfoCenter.PropertyIsLiveStream, new NSNumber (IsLiveStream.Value));
if (AssetUrl != null)
if (AssetUrl is not null)
Add (dict, MPNowPlayingInfoCenter.PropertyAssetUrl, AssetUrl);
if (CurrentPlaybackDate != null)
if (CurrentPlaybackDate is not null)
Add (dict, MPNowPlayingInfoCenter.PropertyCurrentPlaybackDate, CurrentPlaybackDate);

if (AlbumTrackCount.HasValue)
Expand All @@ -204,39 +204,39 @@ internal NSDictionary ToDictionary ()
if (PlaybackDuration.HasValue)
dict.Add (MPMediaItem.PlaybackDurationProperty, new NSNumber (PlaybackDuration.Value));

if (AlbumTitle != null)
if (AlbumTitle is not null)
dict.Add (MPMediaItem.AlbumTitleProperty, new NSString (AlbumTitle));
if (Artist != null)
if (Artist is not null)
dict.Add (MPMediaItem.ArtistProperty, new NSString (Artist));
if (Artwork != null)
if (Artwork is not null)
dict.Add (MPMediaItem.ArtworkProperty, Artwork);
if (Composer != null)
if (Composer is not null)
dict.Add (MPMediaItem.ComposerProperty, new NSString (Composer));
if (Genre != null)
if (Genre is not null)
dict.Add (MPMediaItem.GenreProperty, new NSString (Genre));
if (Title != null)
if (Title is not null)
dict.Add (MPMediaItem.TitleProperty, new NSString (Title));

return dict;
}

void Add (NSMutableDictionary dictionary, NSObject key, NSObject value)
{
if (key != null)
if (key is not null)
dictionary.Add (key, value);
}

bool TryGetValue (NSDictionary source, NSObject? key, [NotNullWhen (true)] out NSObject? result)
{
if (key != null)
if (key is not null)
return source.TryGetValue (key, out result);
result = null;
return false;
}

internal MPNowPlayingInfo (NSDictionary? source)
{
if (source == null)
if (source is null)
return;

NSObject? result;
Expand Down
4 changes: 2 additions & 2 deletions src/MediaPlayer/MPSkipIntervalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public partial class MPSkipIntervalCommand {
public double[]? PreferredIntervals {
get {
NSArray a = _PreferredIntervals;
if ((a == null) || (a.Count == 0))
if ((a is null) || (a.Count == 0))
return null;

return NSArray.ArrayFromHandle<double> (a.Handle, input => {
return new NSNumber (input).DoubleValue;
});
}
set {
if (value == null)
if (value is null)
_PreferredIntervals = new NSArray ();
else {
NSObject [] nsoa = new NSObject [value.Length];
Expand Down
37 changes: 37 additions & 0 deletions tests/monotouch-test/MediaPlayer/MPRemoteCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Unit tests for MPRemoteCommandTest
//
// Authors:
// TJ Lambert <TJ.Lambert@microsoft.com>
//
//
// Copyright 2022 Microsoft. All rights reserved.
//

using System;
using Foundation;
using MediaPlayer;
using NUnit.Framework;

namespace MonoTouchFixtures.MediaPlayer {

[TestFixture]
[Preserve (AllMembers = true)]
public class MPRemoteCommandTest {

[Test]
public void RemoteTargetNull ()
{
var center = MPRemoteCommandCenter.Shared;
Func<MPRemoteCommandEvent, MPRemoteCommandHandlerStatus> handler = m => MPRemoteCommandHandlerStatus.Success;
center.PlayCommand.AddTarget (handler);

// at this point, the PlayCommand contains the target.

Assert.DoesNotThrow (delegate { center.PlayCommand.RemoveTarget (null, null); },
"MPRemoteCommand.RemoveTarget should not throw an ArgumentNullException.");

// at this point, the PlayCommand does not contain any targets as expected.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@
!missing-selector! MPMediaItem::userGrouping not bound
!missing-selector! MPMediaItem::isPreorder not bound

# Initial result from new rule extra-null-allowed
# Headers do not show that null is allowed, but docs do allow it. Testing shows nullable functionality does work.
!extra-null-allowed! 'System.Void MediaPlayer.MPRemoteCommand::RemoveTarget(Foundation.NSObject,ObjCRuntime.Selector)' has a extraneous [NullAllowed] on parameter #0
2 changes: 1 addition & 1 deletion tests/xtro-sharpie/common-MediaPlayer.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@
!missing-selector! MPMediaItem::userGrouping not bound
!missing-selector! MPMediaItem::isPreorder not bound

# Initial result from new rule extra-null-allowed
# Headers do not show that null is allowed, but docs do allow it. Testing shows nullable functionality does work.
!extra-null-allowed! 'System.Void MediaPlayer.MPRemoteCommand::RemoveTarget(Foundation.NSObject,ObjCRuntime.Selector)' has a extraneous [NullAllowed] on parameter #0