Skip to content

Commit

Permalink
feature: add GpsLocation
Browse files Browse the repository at this point in the history
switch Location Events to use GpsLocation for underlying platform locations
add GeoCoordinate
remove beacon events and properties
+semver: minor
  • Loading branch information
RLittlesII committed Jan 4, 2024
1 parent ec9ea64 commit 7fb357b
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 103 deletions.
51 changes: 13 additions & 38 deletions src/Apple/LocationEventExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using CoreLocation;
using Foundation;

Expand Down Expand Up @@ -35,22 +34,6 @@ internal static class LocationEventExtensions
public static AuthorizationChangedEvent ToNotification(this CLAuthorizationChangedEventArgs args) =>
new(AuthorizationStatuses[args.Status]);

/// <summary>
/// Converts the <see cref="CLRegionBeaconsConstraintFailedEventArgs"/> to an instance of <see cref="RegionBeaconsConstraintFailedEvent"/>.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns>The changed notification.</returns>
public static RegionBeaconsConstraintFailedEvent ToNotification(this CLRegionBeaconsConstraintFailedEventArgs args) =>
new();

/// <summary>
/// Converts the <see cref="CLRegionBeaconsConstraintRangedEventArgs"/> to an instance of <see cref="RegionBeaconsConstraintRangedEvent"/>.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns>The changed notification.</returns>
public static RegionBeaconsConstraintRangedEvent ToNotification(this CLRegionBeaconsConstraintRangedEventArgs args) =>
new();

/// <summary>
/// Converts the <see cref="CLHeadingUpdatedEventArgs"/> to an instance of <see cref="HeadingUpdatedEvent"/>.
/// </summary>
Expand All @@ -65,7 +48,7 @@ public static HeadingUpdatedEvent ToNotification(this CLHeadingUpdatedEventArgs
/// <param name="args">The arguments.</param>
/// <returns>The notification.</returns>
public static LocationsUpdatedEvent ToNotification(this CLLocationsUpdatedEventArgs args) =>
new(args.Locations.Select(x => new GeoLocation(x.Coordinate.Latitude, x.Coordinate.Longitude)));
new(args.Locations.Select(location => location.ToGpsLocation()));

/// <summary>
/// Converts the <see cref="CLLocationsUpdatedEventArgs"/> to <see cref="LocationsUpdatedEvent"/>.
Expand All @@ -74,8 +57,8 @@ public static LocationsUpdatedEvent ToNotification(this CLLocationsUpdatedEventA
/// <returns>The notification.</returns>
public static LocationUpdatedEvent ToNotification(this CLLocationUpdatedEventArgs args) =>
new(
args.OldLocation.Coordinate.ToLocation(),
args.NewLocation.Coordinate.ToLocation());
args.OldLocation.ToGpsLocation(),
args.NewLocation.ToGpsLocation());

/// <summary>
/// Converts the <see cref="CLRegionEventArgs"/> to <see cref="RegionChangedEvent"/>.
Expand All @@ -99,7 +82,7 @@ public static RegionChangedEvent ToNotification(this CLRegionStateDeterminedEven
/// <param name="args">The arguments.</param>
/// <returns>The notification.</returns>
public static ErrorEvent ToNotification(this NSErrorEventArgs args) =>
new() { };
new(new Exception(args.ToString()));

/// <summary>
/// Converts the <see cref="CLVisitedEventArgs"/> to <see cref="VisitedEvent"/>.
Expand All @@ -117,34 +100,26 @@ public static VisitedEvent ToNotification(this CLVisitedEventArgs args) =>
public static RegionErrorEvent ToNotification(this CLRegionErrorEventArgs args) =>
new(/*args.Error*/new Exception(), ToGeoRegion(args.Region));

/// <summary>
/// Converts the <see cref="CLRegionErrorEventArgs"/> to <see cref="RegionErrorEvent"/>.
/// </summary>
/// <param name="obj">The object.</param>
/// <returns>The notification.</returns>
public static Unit ToNotification(this object obj) => Unit.Default;

/// <summary>
/// Converts a <see cref="CLRegion"/> to a <see cref="GeoRegion"/>.
/// </summary>
/// <param name="region">The region.</param>
/// <returns>The converted value.</returns>
public static GeoRegion ToGeoRegion(this CLRegion region) =>
new()
{
Identifier = region.Identifier,
Center = region.Center.ToLocation(),
Radius = region.Radius,
NotifyOnEntry = region.NotifyOnEntry,
NotifyOnExit = region.NotifyOnExit
};
public static GeoRegion ToGeoRegion(this CLRegion region) => new(
region.Identifier,
region.Center.ToLocation(),
region.Radius,
region.NotifyOnEntry,
region.NotifyOnExit);

/// <summary>
/// Converts the <see cref="CLLocationCoordinate2D"/> to a <see cref="GeoLocation"/>.
/// </summary>
/// <param name="location">The location.</param>
/// <returns>The converted vale.</returns>
public static GeoLocation ToLocation(this CLLocationCoordinate2D location) => new(location.Latitude, location.Longitude);
public static GeoCoordinate ToLocation(this CLLocationCoordinate2D location) => new(location.Latitude, location.Longitude);

public static IGpsLocation ToGpsLocation(this CLLocation location) => new GpsLocation(location.Coordinate.Latitude, location.Coordinate.Longitude, location.Altitude, location.Course, location.CourseAccuracy, location.Speed, location.SpeedAccuracy, 0, location.Timestamp.ToLocalTime());

public static DateTime ToLocalTime(this NSDate nsDate)
{
Expand Down
21 changes: 0 additions & 21 deletions src/Apple/Locations/CoreLocationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@ public CoreLocationManager()
handler => _locationManager.Value.DidDetermineState -= handler)
.Select(LocationEventExtensions.ToNotification);

FailedRangingBeacons =
Observable
.FromEvent<EventHandler<CLRegionBeaconsConstraintFailedEventArgs>,
CLRegionBeaconsConstraintFailedEventArgs>(
handler => _locationManager.Value.DidFailRangingBeacons += handler,
handler => _locationManager.Value.DidFailRangingBeacons -= handler)
.Select(LocationEventExtensions.ToNotification);

RangedBeaconsSatisfyingConstraint =
Observable
.FromEvent<EventHandler<CLRegionBeaconsConstraintRangedEventArgs>, CLRegionBeaconsConstraintRangedEventArgs>(
handler => _locationManager.Value.DidRangeBeaconsSatisfyingConstraint += handler,
handler => _locationManager.Value.DidRangeBeaconsSatisfyingConstraint -= handler)
.Select(LocationEventExtensions.ToNotification);

StartedMonitoringForRegion =
Observable
.FromEvent<EventHandler<CLRegionEventArgs>, CLRegionEventArgs>(
Expand Down Expand Up @@ -138,12 +123,6 @@ public CoreLocationManager()
/// <inheritdoc />
public IObservable<RegionChangedEvent> DeterminedState { get; }

/// <inheritdoc />
public IObservable<RegionBeaconsConstraintFailedEvent> FailedRangingBeacons { get; }

/// <inheritdoc />
public IObservable<RegionBeaconsConstraintRangedEvent> RangedBeaconsSatisfyingConstraint { get; }

/// <inheritdoc />
public IObservable<RegionChangedEvent> StartedMonitoringForRegion { get; }

Expand Down
19 changes: 18 additions & 1 deletion src/Core/Geofence/GeoRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ namespace Rocket.Surgery.Airframe
/// </summary>
public class GeoRegion
{
/// <summary>
/// Initializes a new instance of the <see cref="GeoRegion"/> class.
/// </summary>
/// <param name="identifier">The unique identifier.</param>
/// <param name="center">The center point of the region.</param>
/// <param name="radius">The radius.</param>
/// <param name="notifyOnEntry">A value indicating whether to notify on entry.</param>
/// <param name="notifyOnExit">A value indicating whether to notify on exit.</param>
public GeoRegion(string identifier, GeoCoordinate center, double radius, bool notifyOnEntry, bool notifyOnExit)
{
Identifier = identifier;
Center = center;
NotifyOnEntry = notifyOnEntry;
NotifyOnExit = notifyOnExit;
Radius = radius;
}

/// <summary>
/// Gets or sets the region identifier.
/// </summary>
Expand All @@ -13,7 +30,7 @@ public class GeoRegion
/// <summary>
/// Gets or sets the center.
/// </summary>
public GeoLocation Center { get; set; }
public GeoCoordinate Center { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to notify on entry of the region.
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Locations/Events/ErrorEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ namespace Rocket.Surgery.Airframe
/// </summary>
public class ErrorEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ErrorEvent"/> class.
/// </summary>
/// <param name="exception">The exception.</param>
public ErrorEvent(Exception exception) => Exception = exception;

/// <summary>
/// Gets an exception.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Locations/Events/LocationUpdatedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class LocationUpdatedEvent
/// </summary>
/// <param name="previous">The previous.</param>
/// <param name="current">The current.</param>
public LocationUpdatedEvent(GeoLocation previous, GeoLocation current)
public LocationUpdatedEvent(IGpsLocation? previous, IGpsLocation current)
{
Previous = previous;
Current = current;
Expand All @@ -19,11 +19,11 @@ public LocationUpdatedEvent(GeoLocation previous, GeoLocation current)
/// <summary>
/// Gets the previous location.
/// </summary>
public GeoLocation Previous { get; }
public IGpsLocation? Previous { get; }

/// <summary>
/// Gets the current location.
/// </summary>
public GeoLocation Current { get; }
public IGpsLocation Current { get; }
}
}
4 changes: 2 additions & 2 deletions src/Core/Locations/Events/LocationsUpdatedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class LocationsUpdatedEvent
/// Initializes a new instance of the <see cref="LocationsUpdatedEvent"/> class.
/// </summary>
/// <param name="locations">The locations.</param>
public LocationsUpdatedEvent(IEnumerable<GeoLocation> locations) => Locations = locations;
public LocationsUpdatedEvent(IEnumerable<IGpsLocation> locations) => Locations = locations;

/// <summary>
/// Gets the locations.
/// </summary>
public IEnumerable<GeoLocation> Locations { get; }
public IEnumerable<IGpsLocation> Locations { get; }
}
}

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions src/Core/Locations/Events/RegionBeaconsFailedEvent.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Core/Locations/Events/RegionChangedEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RegionChangedEvent
/// </summary>
/// <param name="region">The region.</param>
/// <param name="state">The region state.</param>
public RegionChangedEvent(GeoRegion region, RegionState? state = default)
public RegionChangedEvent(GeoRegion region, RegionState? state = RegionState.Unknown)
{
Region = region;
State = state;
Expand Down
58 changes: 58 additions & 0 deletions src/Core/Locations/GeoCoordinate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;

namespace Rocket.Surgery.Airframe
{
/// <summary>
/// Represents a coordinate.
/// </summary>
/// <remarks>any of a set of numbers used in specifying the location of a point on a line, on a surface, or in space.</remarks>
public class GeoCoordinate : IEquatable<GeoCoordinate>
{
/// <summary>
/// Initializes a new instance of the <see cref="GeoCoordinate"/> class.
/// </summary>
/// <param name="latitude">The latitude value.</param>
/// <param name="longitude">The longitude value.</param>
public GeoCoordinate(double latitude, double longitude)
{
if (latitude is < -90 or > 90)
{
throw new ArgumentException($"Invalid latitude value - {latitude}");
}

if (longitude is < -180 or > 180)
{
throw new ArgumentException($"Invalid longitude value - {longitude}");
}

Latitude = latitude;
Longitude = longitude;
}

/// <summary>
/// Gets the latitude of the coordinate.
/// </summary>
public double Latitude { get; }

/// <summary>
/// Gets the longitude of the coordinate.
/// </summary>
public double Longitude { get; }

public static bool operator ==(GeoCoordinate? left, GeoCoordinate? right) => Equals(left, right);

public static bool operator !=(GeoCoordinate? left, GeoCoordinate? right) => !Equals(left, right);

/// <inheritdoc/>
public override string ToString() => $"Latitude: {Latitude} - Longitude: {Longitude}";

/// <inheritdoc/>
public bool Equals(GeoCoordinate? other) => other != null && (Latitude, Longitude).Equals((other.Latitude, other.Longitude));

/// <inheritdoc/>
public override bool Equals(object? obj) => obj is GeoCoordinate coordinate && Equals(coordinate);

/// <inheritdoc/>
public override int GetHashCode() => (Latitude, Longitude).GetHashCode();
}
}
Loading

0 comments on commit 7fb357b

Please sign in to comment.