-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Geolocation: Add #nullable for Essentials Geolocation code #13371
Changes from 4 commits
120ce41
e38f534
70a8e18
877c808
2bc9e6d
cdf8702
c6387da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#nullable enable | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
|
@@ -10,14 +11,14 @@ namespace Microsoft.Maui.Devices.Sensors | |
{ | ||
partial class GeolocationImplementation : IGeolocation | ||
{ | ||
CLLocationManager listeningManager; | ||
CLLocationManager? listeningManager; | ||
|
||
/// <summary> | ||
/// Indicates if currently listening to location updates while the app is in foreground. | ||
/// </summary> | ||
public bool IsListeningForeground { get => listeningManager != null; } | ||
|
||
public async Task<Location> GetLastKnownLocationAsync() | ||
public async Task<Location?> GetLastKnownLocationAsync() | ||
{ | ||
if (!CLLocationManager.LocationServicesEnabled) | ||
throw new FeatureNotEnabledException("Location services are not enabled on device."); | ||
|
@@ -37,7 +38,7 @@ public async Task<Location> GetLastKnownLocationAsync() | |
return location?.ToLocation(reducedAccuracy); | ||
} | ||
|
||
public async Task<Location> GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) | ||
public async Task<Location?> GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
|
||
|
@@ -50,7 +51,7 @@ public async Task<Location> GetLocationAsync(GeolocationRequest request, Cancell | |
// so just use the main loop | ||
var manager = MainThread.InvokeOnMainThread(() => new CLLocationManager()); | ||
|
||
var tcs = new TaskCompletionSource<CLLocation>(manager); | ||
var tcs = new TaskCompletionSource<CLLocation?>(manager); | ||
|
||
var listener = new SingleLocationListener(); | ||
listener.LocationHandler += HandleLocation; | ||
|
@@ -155,7 +156,7 @@ public async Task<bool> StartListeningForegroundAsync(GeolocationListeningReques | |
|
||
void HandleLocation(CLLocation clLocation) | ||
{ | ||
OnLocationChanged(clLocation?.ToLocation(reducedAccuracy)); | ||
OnLocationChanged(clLocation.ToLocation(reducedAccuracy)); | ||
} | ||
|
||
void HandleError(GeolocationError error) | ||
|
@@ -172,7 +173,8 @@ void HandleError(GeolocationError error) | |
/// </summary> | ||
public void StopListeningForeground() | ||
{ | ||
if (!IsListeningForeground) | ||
if (!IsListeningForeground || | ||
listeningManager is null) | ||
return; | ||
|
||
listeningManager.StopUpdatingLocation(); | ||
|
@@ -183,7 +185,7 @@ public void StopListeningForeground() | |
listener.ErrorHandler = null; | ||
} | ||
|
||
listeningManager.Delegate = null; | ||
listeningManager.WeakDelegate = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure setting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jfversluis the third... |
||
|
||
listeningManager = null; | ||
} | ||
|
@@ -193,7 +195,7 @@ class SingleLocationListener : CLLocationManagerDelegate | |
{ | ||
bool wasRaised = false; | ||
|
||
internal Action<CLLocation> LocationHandler { get; set; } | ||
internal Action<CLLocation>? LocationHandler { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public override void LocationsUpdated(CLLocationManager manager, CLLocation[] locations) | ||
|
@@ -217,9 +219,9 @@ public override void LocationsUpdated(CLLocationManager manager, CLLocation[] lo | |
|
||
class ContinuousLocationListener : CLLocationManagerDelegate | ||
{ | ||
internal Action<CLLocation> LocationHandler { get; set; } | ||
internal Action<CLLocation>? LocationHandler { get; set; } | ||
|
||
internal Action<GeolocationError> ErrorHandler { get; set; } | ||
internal Action<GeolocationError>? ErrorHandler { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public override void LocationsUpdated(CLLocationManager manager, CLLocation[] locations) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#nullable enable | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#nullable enable | ||
using System; | ||
|
||
namespace Microsoft.Maui.Devices.Sensors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#nullable enable | ||
|
||
namespace Microsoft.Maui.Devices.Sensors | ||
{ | ||
public partial class GeolocationRequest | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if LocationManager can ever be null on a sane Android environment, but there might be some weird custom ROMs; I guess the best is to throw
FeatureNotSupportedException
then...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jfversluis there are still my three questions...