From dc7219f114b1f4047078b49fbbc89034e8c159d2 Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Mon, 4 Jun 2018 09:46:20 -0700 Subject: [PATCH 1/2] Fix up null checks when getting lask known location. --- .../Geolocation/Geolocation.android.cs | 5 +---- .../Geolocation/Geolocation.ios.cs | 2 +- .../Geolocation/Geolocation.uwp.cs | 16 ++-------------- .../Types/LocationExtensions.uwp.cs | 17 +++++++++++++---- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/Xamarin.Essentials/Geolocation/Geolocation.android.cs b/Xamarin.Essentials/Geolocation/Geolocation.android.cs index 2aae636cf..b4b5ff834 100644 --- a/Xamarin.Essentials/Geolocation/Geolocation.android.cs +++ b/Xamarin.Essentials/Geolocation/Geolocation.android.cs @@ -30,10 +30,7 @@ static async Task PlatformLastKnownLocationAsync() bestLocation = location; } - if (bestLocation == null) - return null; - - return bestLocation.ToLocation(); + return bestLocation?.ToLocation(); } static async Task PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) diff --git a/Xamarin.Essentials/Geolocation/Geolocation.ios.cs b/Xamarin.Essentials/Geolocation/Geolocation.ios.cs index bd112c876..1f9072b11 100644 --- a/Xamarin.Essentials/Geolocation/Geolocation.ios.cs +++ b/Xamarin.Essentials/Geolocation/Geolocation.ios.cs @@ -19,7 +19,7 @@ static async Task PlatformLastKnownLocationAsync() var manager = new CLLocationManager(); var location = manager.Location; - return location.ToLocation(); + return location?.ToLocation(); } static async Task PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) diff --git a/Xamarin.Essentials/Geolocation/Geolocation.uwp.cs b/Xamarin.Essentials/Geolocation/Geolocation.uwp.cs index 95ddbdaed..af7f12f16 100644 --- a/Xamarin.Essentials/Geolocation/Geolocation.uwp.cs +++ b/Xamarin.Essentials/Geolocation/Geolocation.uwp.cs @@ -20,10 +20,7 @@ static async Task PlatformLastKnownLocationAsync() var location = await geolocator.GetGeopositionAsync().AsTask(); - if (location?.Coordinate == null) - return null; - - return location.ToLocation(); + return location?.Coordinate?.ToLocation(); } static async Task PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) @@ -39,16 +36,7 @@ static async Task PlatformLocationAsync(GeolocationRequest request, Ca var location = await geolocator.GetGeopositionAsync().AsTask(cancellationToken); - if (location?.Coordinate == null) - return null; - - return new Location - { - Latitude = location.Coordinate.Point.Position.Latitude, - Longitude = location.Coordinate.Point.Position.Longitude, - TimestampUtc = location.Coordinate.Timestamp, - Accuracy = location.Coordinate.Accuracy - }; + return location?.Coordinate?.ToLocation(); } } } diff --git a/Xamarin.Essentials/Types/LocationExtensions.uwp.cs b/Xamarin.Essentials/Types/LocationExtensions.uwp.cs index 7ff6ab7be..bad873aaa 100644 --- a/Xamarin.Essentials/Types/LocationExtensions.uwp.cs +++ b/Xamarin.Essentials/Types/LocationExtensions.uwp.cs @@ -23,12 +23,21 @@ internal static IEnumerable ToLocations(this MapLocationFinderResult r result?.ToLocations(); internal static Location ToLocation(this Geoposition location) => + new Location + { + Latitude = location.Coordinate.Point.Position.Latitude, + Longitude = location.Coordinate.Point.Position.Longitude, + TimestampUtc = location.Coordinate.Timestamp, + Accuracy = location.Coordinate.Accuracy + }; + + internal static Location ToLocation(this Geocoordinate coordinate) => new Location { - Latitude = location.Coordinate.Point.Position.Latitude, - Longitude = location.Coordinate.Point.Position.Longitude, - TimestampUtc = location.Coordinate.Timestamp, - Accuracy = location.Coordinate.Accuracy + Latitude = coordinate.Point.Position.Latitude, + Longitude = coordinate.Point.Position.Longitude, + TimestampUtc = coordinate.Timestamp, + Accuracy = coordinate.Accuracy }; } } From 633ffbdc7e9ca05a34a2d754af43c3540bf43275 Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Mon, 4 Jun 2018 09:47:01 -0700 Subject: [PATCH 2/2] Additional Null checks --- Xamarin.Essentials/Geolocation/Geolocation.ios.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Xamarin.Essentials/Geolocation/Geolocation.ios.cs b/Xamarin.Essentials/Geolocation/Geolocation.ios.cs index 1f9072b11..adda3867f 100644 --- a/Xamarin.Essentials/Geolocation/Geolocation.ios.cs +++ b/Xamarin.Essentials/Geolocation/Geolocation.ios.cs @@ -49,10 +49,7 @@ static async Task PlatformLocationAsync(GeolocationRequest request, Ca var clLocation = await tcs.Task; - if (clLocation == null) - return null; - - return clLocation.ToLocation(); + return clLocation?.ToLocation(); void HandleLocation(CLLocation location) { @@ -81,7 +78,7 @@ public override void LocationsUpdated(CLLocationManager manager, CLLocation[] lo wasRaised = true; - var location = locations.LastOrDefault(); + var location = locations?.LastOrDefault(); if (location == null) return;