Reorganise project structure and namespaces. Remove Leaflet project and replace with Darnton.Blazor.Leaflet package.

This commit is contained in:
Bernard Darnton
2020-10-28 10:25:04 +13:00
parent 9576c5aa5b
commit 7dc4cea8ae
46 changed files with 431 additions and 441 deletions
@@ -0,0 +1,45 @@
using System;
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// Geolocation Coordinates, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates"/>.
/// </summary>
public class GeolocationCoordinates
{
/// <summary>
/// Latitude in decimal degrees.
/// </summary>
public double Latitude { get; set; }
/// <summary>
/// Longitude in decimal degrees.
/// </summary>
public double Longitude { get; set; }
/// <summary>
/// Altitude in metres, relative to sea level.
/// </summary>
public double? Altitude { get; set; }
/// <summary>
/// Accuracy of the latitude and longitude properties, in metres.
/// </summary>
public double Accuracy { get; set; }
/// <summary>
/// Accuracy of the altitude, in metres.
/// </summary>
public double? AltitudeAccuracy { get; set; }
/// <summary>
/// The direction the device is travelling, in degrees clockwise from true north.
/// </summary>
public double? Heading { get; set; }
/// <summary>
/// The velocity of the device, in metres per second.
/// </summary>
public double? Speed { get; set; }
}
}
@@ -0,0 +1,15 @@
using System;
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// Geolocation event data. Provides the <see cref="GeolocationResult"/> with the position or error associated with the event.
/// </summary>
public class GeolocationEventArgs : EventArgs
{
/// <summary>
/// The <see cref="GeolocationResult"/> associated with the event.
/// </summary>
public GeolocationResult GeolocationResult { get; set; }
}
}
@@ -0,0 +1,28 @@
using Darnton.Units;
using System;
using System.Text.Json.Serialization;
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// Geolocation Position, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition"/>.
/// </summary>
public class GeolocationPosition
{
/// <summary>
/// The coordinates defining the current location
/// </summary>
public GeolocationCoordinates Coords { get; set; }
/// <summary>
/// The time the coordinates were taken, in milliseconds since the Unix epoch.
/// </summary>
public long Timestamp { get; set; }
/// <summary>
/// The <see cref="DateTimeOffset"/> derived from the Timestamp, in UTC.
/// </summary>
[JsonIgnore]
public DateTimeOffset DateTimeOffset => (Timestamp / 1000).FromUnixTime().ToDateTimeOffset();
}
}
@@ -0,0 +1,20 @@
using System;
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// The reason for a Geolocation error, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError"/>.
/// </summary>
public class GeolocationPositionError
{
/// <summary>
/// The <see cref="GeolocationPositionErrorCode"/> for the error.
/// </summary>
public GeolocationPositionErrorCode Code { get; set; }
/// <summary>
/// Details of the error. Intended for debugging rather than display to the user.
/// </summary>
public string Message { get; set; }
}
}
@@ -0,0 +1,26 @@
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// An enumeration of error codes used by <see cref="GeolocationPositionError"/>.
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError/code"/>.
/// </summary>
public enum GeolocationPositionErrorCode
{
/// <summary>
/// Geolocation failoed because the device does not support geolocation. Not part of W3C spec.
/// </summary>
DEVICE_NOT_SUPPORTED = 0,
/// <summary>
/// Geolocation failed because permission to access location was denied.
/// </summary>
PERMISSION_DENIED = 1,
/// <summary>
/// Geolocation failed because of an internal error on the device.
/// </summary>
POSITION_UNAVAILABLE = 2,
/// <summary>
/// Geolocation failed because no position was returned in time.
/// </summary>
TIMEOUT = 3
}
}
@@ -0,0 +1,26 @@
using System;
using System.Text.Json.Serialization;
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// The result of a geolocation request. Contains either a <see cref="GeolocationPosition"/> or a <see cref="GeolocationPositionError"/>.
/// </summary>
public class GeolocationResult
{
/// <summary>
/// The <see cref="GeolocationPosition"/> returned on successful geolocation.
/// </summary>
public GeolocationPosition Position { get; set; }
/// <summary>
/// The <see cref="GeolocationPositionError"/> returned by a failed geolocation attempt.
/// </summary>
public GeolocationPositionError Error { get; set; }
/// <summary>
/// Indicates whether the geolocation attempt was successful.
/// </summary>
[JsonIgnore]
public bool IsSuccess => !(Position is null);
}
}
@@ -0,0 +1,61 @@
using Microsoft.JSInterop;
using System;
using System.Threading.Tasks;
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// An implementation of <see cref="IGeolocationService"/> that provides
/// an interop layer for the device's Geolocation API.
/// </summary>
public class GeolocationService : IGeolocationService
{
private readonly IJSRuntime _jsRuntime;
/// <inheritdoc/>
public event EventHandler<GeolocationEventArgs> WatchPositionReceived;
/// <summary>
/// Constructs a <see cref="GeolocationService"/> object.
/// </summary>
/// <param name="JSRuntime"></param>
public GeolocationService(IJSRuntime JSRuntime)
{
_jsRuntime = JSRuntime;
}
/// <inheritdoc/>
public async Task<GeolocationResult> GetCurrentPosition(PositionOptions options = null)
{
return await _jsRuntime.InvokeAsync<GeolocationResult>("Geolocation.getCurrentPosition", options);
}
/// <inheritdoc/>
public async Task<long?> WatchPosition(PositionOptions options = null)
{
var callbackObj = DotNetObjectReference.Create(this);
return await _jsRuntime.InvokeAsync<int>("Geolocation.watchPosition",
callbackObj, nameof(SetWatchPosition), options);
}
/// <summary>
/// Invokes the <see cref="WatchPositionReceived"/> event handler.
/// Invoked by the success and error callbacks of the JavaScript watchPosition() function.
/// </summary>
/// <param name="watchResult">A <see cref="GeolocationResult"/> passed back from JavaScript.</param>
[JSInvokable]
public void SetWatchPosition(GeolocationResult watchResult)
{
WatchPositionReceived?.Invoke(this, new GeolocationEventArgs
{
GeolocationResult = watchResult
});
}
/// <inheritdoc/>
public async Task ClearWatch(long watchId)
{
await _jsRuntime.InvokeVoidAsync("Geolocation.clearWatch", watchId);
}
}
}
@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// A wrapper around the device's Geolocation API services.
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API"/>.
/// </summary>
public interface IGeolocationService
{
/// <summary>
/// A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition"/> function,
/// used to get the current position of the device.
/// </summary>
/// <param name="options"><see cref="PositionOptions"/> used to modify the request.</param>
/// <returns>The result of the geolocation request.</returns>
Task<GeolocationResult> GetCurrentPosition(PositionOptions options = null);
/// <summary>
/// A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition"/> function,
/// used to listen for position changes. If the service is listening, a <see cref="WatchPositionReceived"/> event is fired
/// each time the device's position changes.
/// </summary>
/// <param name="options"><see cref="PositionOptions"/> used to modify the request.</param>
/// <returns>A watch ID that refers to the handler. The ID can be used to unregister the handler with <see cref="ClearWatch(long)"/>.</returns>
Task<long?> WatchPosition(PositionOptions options = null);
/// <summary>
/// Handles the receipt of new positions. Fired whenever a handler is registered and the device's position changes.
/// Invoked with the sender and the <see cref="GeolocationEventArgs"/>.
/// </summary>
event EventHandler<GeolocationEventArgs> WatchPositionReceived;
/// <summary>
/// A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/clearWatch"/> function,
/// used to unregister a handler created with <see cref="WatchPosition(PositionOptions)"/>.
/// </summary>
/// <param name="watchId">The ID of the registered watch handler.</param>
/// <returns>A task that represents the async clear operation.</returns>
Task ClearWatch(long watchId);
}
}
@@ -0,0 +1,27 @@
namespace Darnton.Blazor.DeviceInterop.Geolocation
{
/// <summary>
/// Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions.
/// </summary>
public class PositionOptions
{
/// <summary>
/// Enable high accuracy mode for best possible results.
/// May be slower or increase power consumption. Defaults to false.
/// </summary>
public bool EnableHighAccuracy { get; set; } = false;
/// <summary>
/// Maximum length of time allowed to return a position (in milliseconds).
/// Set to null for no timeout. Defaults to null.
/// </summary>
public long? Timeout { get; set; } = null;
/// <summary>
/// Maximum allowed age for a cached result.
/// Set to null to disregard the age of cached results.
/// Set to 0 to skip the cache and attempt a fresh result every time. Defaults to 0.
/// </summary>
public long? MaximumAge { get; set; } = 0;
}
}