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
@@ -1,19 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.8" />
</ItemGroup>
<ItemGroup>
<None Include="wwwroot\leaflet-map\leaflet-map.js" />
</ItemGroup>
</Project>
@@ -1,22 +0,0 @@
using Microsoft.JSInterop;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components
{
public abstract class InteropObject
{
protected JsRuntimeObjectRef _jsObjRef;
[JsonPropertyName("__jsObjRefId")]
public int JsObjectRefId { get { return _jsObjRef.JsObjectRefId; } }
public async Task BindToJsRuntime(IJSRuntime jsRuntime)
{
_jsObjRef = await CreateJsObjectRef(jsRuntime);
_jsObjRef.JSRuntime = jsRuntime;
}
protected abstract Task<JsRuntimeObjectRef> CreateJsObjectRef(IJSRuntime jsRuntime);
}
}
@@ -1,20 +0,0 @@
using Microsoft.JSInterop;
using System;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components
{
public class JsRuntimeObjectRef : IAsyncDisposable
{
internal IJSRuntime JSRuntime { get; set; }
[JsonPropertyName("__jsObjRefId")]
public int JsObjectRefId { get; set; }
public async ValueTask DisposeAsync()
{
await JSRuntime.InvokeVoidAsync("deviceInterop.removeObjectRef", JsObjectRefId);
}
}
}
@@ -1,6 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public abstract class InteractiveLayer : Layer
{
}
}
@@ -1,13 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class LatLng
{
public double Lat { get; set; }
public double Lng { get; set; }
public LatLng(double lat, double lng)
{
Lat = lat;
Lng = lng;
}
}
}
@@ -1,20 +0,0 @@
using Microsoft.JSInterop;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components.LeafletMap
{
public abstract class Layer : InteropObject
{
public async Task<Layer> AddTo(Map map)
{
await _jsObjRef.JSRuntime.InvokeVoidAsync("LeafletMap.Layer.addTo", this, map);
return this;
}
public async Task<Layer> Remove()
{
await _jsObjRef.JSRuntime.InvokeVoidAsync("LeafletMap.Layer.remove", this);
return this;
}
}
}
@@ -1,7 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class LayerOptions
{
public string Attribution { get; set; }
}
}
@@ -1,3 +0,0 @@
@inherits LeafletMapBase
<div class="leafletMap" id="@Map.ElementId"></div>
@@ -1,23 +0,0 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class LeafletMapBase : ComponentBase
{
[Inject] public IJSRuntime JSRuntime { get; set; }
[Parameter] public Map Map { get; set; }
[Parameter] public TileLayer TileLayer { get; set; }
protected async override Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await Map.BindToJsRuntime(JSRuntime);
await TileLayer.BindToJsRuntime(JSRuntime);
await TileLayer.AddTo(Map);
}
}
}
}
@@ -1,23 +0,0 @@
using Microsoft.JSInterop;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class Map : InteropObject
{
[JsonIgnore] public string ElementId { get; }
[JsonIgnore] public MapOptions Options { get; }
public Map(string elementId, MapOptions options)
{
ElementId = elementId;
Options = options;
}
protected override async Task<JsRuntimeObjectRef> CreateJsObjectRef(IJSRuntime jsRuntime)
{
return await jsRuntime.InvokeAsync<JsRuntimeObjectRef>("LeafletMap.map", ElementId, Options);
}
}
}
@@ -1,8 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class MapOptions
{
public LatLng Center { get; set; }
public int Zoom { get; set; }
}
}
@@ -1,22 +0,0 @@
using Microsoft.JSInterop;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class Marker : InteractiveLayer
{
[JsonIgnore] public LatLng LatLng { get; }
[JsonIgnore] public MarkerOptions Options { get; }
public Marker(LatLng latlng, MarkerOptions options)
{
LatLng = latlng;
Options = options;
}
protected override async Task<JsRuntimeObjectRef> CreateJsObjectRef(IJSRuntime jsRuntime)
{
return await jsRuntime.InvokeAsync<JsRuntimeObjectRef>("LeafletMap.marker", LatLng, Options);
}
}
}
@@ -1,6 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class MarkerOptions
{
}
}
@@ -1,6 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public abstract class Path : InteractiveLayer
{
}
}
@@ -1,33 +0,0 @@
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class Polyline : Path
{
[JsonIgnore] public IEnumerable<LatLng> LatLngs { get; }
[JsonIgnore] public PolylineOptions Options { get; }
public Polyline(IEnumerable<LatLng> latLngs, PolylineOptions options)
{
LatLngs = latLngs;
Options = options;
}
protected override async Task<JsRuntimeObjectRef> CreateJsObjectRef(IJSRuntime jsRuntime)
{
return await jsRuntime.InvokeAsync<JsRuntimeObjectRef>("LeafletMap.polyline", LatLngs.ToArray(), Options);
}
public async Task<Polyline> AddLatLng(LatLng latLng)
{
await _jsObjRef.JSRuntime.InvokeVoidAsync("LeafletMap.Polyline.addLatLng", this, latLng);
return this;
}
}
}
@@ -1,6 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class PolylineOptions : LayerOptions
{
}
}
@@ -1,23 +0,0 @@
using Microsoft.JSInterop;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class TileLayer : Layer
{
[JsonIgnore] public string UrlTemplate { get; }
[JsonIgnore] public TileLayerOptions Options { get; }
public TileLayer(string urlTemplate, TileLayerOptions options)
{
UrlTemplate = urlTemplate;
Options = options;
}
protected override async Task<JsRuntimeObjectRef> CreateJsObjectRef(IJSRuntime jsRuntime)
{
return await jsRuntime.InvokeAsync<JsRuntimeObjectRef>("LeafletMap.tileLayer", UrlTemplate, Options);
}
}
}
@@ -1,6 +0,0 @@
namespace BlazorDeviceInterop.Components.LeafletMap
{
public class TileLayerOptions : LayerOptions
{
}
}
@@ -1,31 +0,0 @@
var deviceInterop = deviceInterop || {};
deviceInterop.objRefs = {};
deviceInterop.objRefId = 0;
deviceInterop.objRefKey = '__jsObjRefId';
deviceInterop.storeObjRef = function (obj) {
var id = deviceInterop.objRefId++;
deviceInterop.objRefs[id] = obj;
var objRef = {};
objRef[deviceInterop.objRefKey] = id;
return objRef;
}
deviceInterop.removeObjectRef = function (id) {
delete deviceInterop.objRefs[id];
}
DotNet.attachReviver(function (key, value) {
if (value &&
typeof value === 'object' &&
value.hasOwnProperty(deviceInterop.objRefKey) &&
typeof value[deviceInterop.objRefKey] === 'number') {
var id = value[deviceInterop.objRefKey];
if (!(id in deviceInterop.objRefs)) {
throw new Error("The JS object reference doesn't exist: " + id);
}
const instance = deviceInterop.objRefs[id];
return instance;
} else {
return value;
}
});
@@ -1,39 +0,0 @@
window.LeafletMap = {
map: function (id, options) {
return deviceInterop.storeObjRef(L.map(id, options));
},
marker: function (latlng, options) {
return deviceInterop.storeObjRef(L.marker(latlng, options));
},
polyline: function (latlngs, options) {
return deviceInterop.storeObjRef(L.polyline(latlngs, options));
},
tileLayer: function (urlTemplate, options) {
return deviceInterop.storeObjRef(L.tileLayer(urlTemplate, options));
},
Layer: {
addTo: function (layer, map) {
layer.addTo(map);
},
remove: function (layer) {
layer.remove();
}
},
Polyline: {
addLatLng: function (polyline, latlng, latlngs) {
polyline.addLatLng(latlng, latlngs);
}
}
}
+1 -7
View File
@@ -3,12 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 16
VisualStudioVersion = 16.0.30503.244 VisualStudioVersion = 16.0.30503.244
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorDeviceInterop", "BlazorDeviceInterop\BlazorDeviceInterop.csproj", "{A5E4A38E-AB58-44C0-A781-A2E414A1668E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Darnton.Blazor.DeviceInterop", "Darnton.Blazor.DeviceInterop\Darnton.Blazor.DeviceInterop.csproj", "{A5E4A38E-AB58-44C0-A781-A2E414A1668E}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorDeviceTestRig", "BlazorDeviceTestRig\BlazorDeviceTestRig.csproj", "{47389794-07A5-4AC5-AA7A-69E9A4E23089}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorDeviceTestRig", "BlazorDeviceTestRig\BlazorDeviceTestRig.csproj", "{47389794-07A5-4AC5-AA7A-69E9A4E23089}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorDeviceInterop.Components", "BlazorDeviceInterop.Components\BlazorDeviceInterop.Components.csproj", "{39BFE757-6826-4C20-9B23-737642140243}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -23,10 +21,6 @@ Global
{47389794-07A5-4AC5-AA7A-69E9A4E23089}.Debug|Any CPU.Build.0 = Debug|Any CPU {47389794-07A5-4AC5-AA7A-69E9A4E23089}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47389794-07A5-4AC5-AA7A-69E9A4E23089}.Release|Any CPU.ActiveCfg = Release|Any CPU {47389794-07A5-4AC5-AA7A-69E9A4E23089}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47389794-07A5-4AC5-AA7A-69E9A4E23089}.Release|Any CPU.Build.0 = Release|Any CPU {47389794-07A5-4AC5-AA7A-69E9A4E23089}.Release|Any CPU.Build.0 = Release|Any CPU
{39BFE757-6826-4C20-9B23-737642140243}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39BFE757-6826-4C20-9B23-737642140243}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39BFE757-6826-4C20-9B23-737642140243}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39BFE757-6826-4C20-9B23-737642140243}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -1,14 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.8" />
</ItemGroup>
</Project>
@@ -1,9 +0,0 @@
using System;
namespace BlazorDeviceInterop.Geolocation
{
public class GeolocationEventArgs : EventArgs
{
public GeolocationResult GeolocationResult { get; set; }
}
}
@@ -1,10 +0,0 @@
namespace BlazorDeviceInterop.Geolocation
{
public enum GeolocationPositionErrorCode
{
DEVICE_NOT_SUPPORTED = 0,
PERMISSION_DENIED = 1,
POSITION_UNAVAILABLE = 2,
TIMEOUT = 3
}
}
@@ -1,15 +0,0 @@
using System;
using System.Text.Json.Serialization;
namespace BlazorDeviceInterop.Geolocation
{
[Serializable]
public class GeolocationResult
{
public GeolocationPosition Position { get; set; }
public GeolocationPositionError Error { get; set; }
[JsonIgnore]
public bool IsSuccess => !(Position is null);
}
}
@@ -1,14 +0,0 @@
using System;
using System.Threading.Tasks;
namespace BlazorDeviceInterop.Geolocation
{
public interface IGeolocationService
{
Task<GeolocationResult> GetCurrentPosition(PositionOptions options = null);
Task<long?> WatchPosition(PositionOptions options = null);
void SetWatchPosition(GeolocationResult watchResult);
event EventHandler<GeolocationEventArgs> WatchPositionReceived;
Task ClearWatch(long watchId);
}
}
-1
View File
@@ -1 +0,0 @@
@using Microsoft.AspNetCore.Components.Web
@@ -6,6 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Darnton.Blazor.Leaflet" Version="0.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" /> <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
@@ -14,8 +15,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BlazorDeviceInterop.Components\BlazorDeviceInterop.Components.csproj" /> <ProjectReference Include="..\Darnton.Blazor.DeviceInterop\Darnton.Blazor.DeviceInterop.csproj" />
<ProjectReference Include="..\BlazorDeviceInterop\BlazorDeviceInterop.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
@@ -1,5 +1,5 @@
using BlazorDeviceInterop.Components.LeafletMap; using Darnton.Blazor.Leaflet.LeafletMap;
using BlazorDeviceInterop.Geolocation; using Darnton.Blazor.DeviceInterop.Geolocation;
namespace BlazorDeviceTestRig.Geolocation namespace BlazorDeviceTestRig.Geolocation
{ {
+21 -6
View File
@@ -1,14 +1,16 @@
using BlazorDeviceInterop.Components.LeafletMap; using Darnton.Blazor.Leaflet.LeafletMap;
using BlazorDeviceInterop.Geolocation; using Darnton.Blazor.DeviceInterop.Geolocation;
using BlazorDeviceTestRig.Geolocation; using BlazorDeviceTestRig.Geolocation;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
namespace BlazorDeviceTestRig.Pages namespace BlazorDeviceTestRig.Pages
{ {
public class GeolocationBase : ComponentBase public class GeolocationBase : ComponentBase, IDisposable
{ {
[Inject] public IJSRuntime JSRuntime { get; set; } [Inject] public IJSRuntime JSRuntime { get; set; }
[Inject] public IGeolocationService GeolocationService { get; set; } [Inject] public IGeolocationService GeolocationService { get; set; }
@@ -31,7 +33,7 @@ namespace BlazorDeviceTestRig.Pages
protected GeolocationResult LastWatchPositionResult { get; set; } protected GeolocationResult LastWatchPositionResult { get; set; }
protected string LastWatchLatitude => LastWatchPositionResult?.Position?.Coords?.Latitude.ToString("F2"); protected string LastWatchLatitude => LastWatchPositionResult?.Position?.Coords?.Latitude.ToString("F2");
protected string LastWatchLongitude => LastWatchPositionResult?.Position?.Coords?.Longitude.ToString("F2"); protected string LastWatchLongitude => LastWatchPositionResult?.Position?.Coords?.Longitude.ToString("F2");
protected string LastWatchTimestamp => LastWatchPositionResult?.Position?.DateTime.ToString(); protected string LastWatchTimestamp => LastWatchPositionResult?.Position?.DateTimeOffset.ToString();
protected string ToggleWatchCommand => isWatching ? "Stop watching" : "Start watching"; protected string ToggleWatchCommand => isWatching ? "Stop watching" : "Start watching";
public GeolocationBase() : base() public GeolocationBase() : base()
@@ -77,8 +79,7 @@ namespace BlazorDeviceTestRig.Pages
{ {
if (isWatching) if (isWatching)
{ {
GeolocationService.WatchPositionReceived -= HandleWatchPositionReceived; await StopWatching();
await GeolocationService.ClearWatch(WatchHandlerId.Value);
WatchHandlerId = null; WatchHandlerId = null;
foreach (var marker in WatchMarkers) foreach (var marker in WatchMarkers)
{ {
@@ -96,6 +97,12 @@ namespace BlazorDeviceTestRig.Pages
StateHasChanged(); StateHasChanged();
} }
private async Task StopWatching()
{
GeolocationService.WatchPositionReceived -= HandleWatchPositionReceived;
await GeolocationService.ClearWatch(WatchHandlerId.Value);
}
private async void HandleWatchPositionReceived(object sender, GeolocationEventArgs e) private async void HandleWatchPositionReceived(object sender, GeolocationEventArgs e)
{ {
LastWatchPositionResult = e.GeolocationResult; LastWatchPositionResult = e.GeolocationResult;
@@ -120,5 +127,13 @@ namespace BlazorDeviceTestRig.Pages
} }
StateHasChanged(); StateHasChanged();
} }
public async void Dispose()
{
if (isWatching)
{
await StopWatching();
}
}
} }
} }
+1 -5
View File
@@ -1,13 +1,9 @@
using System; using System;
using System.Net.Http; using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Darnton.Blazor.DeviceInterop.Geolocation;
using BlazorDeviceInterop.Geolocation;
namespace BlazorDeviceTestRig namespace BlazorDeviceTestRig
{ {
+1 -1
View File
@@ -6,6 +6,6 @@
@using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Http @using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop @using Microsoft.JSInterop
@using BlazorDeviceInterop.Components.LeafletMap @using Darnton.Blazor.Leaflet.LeafletMap
@using BlazorDeviceTestRig @using BlazorDeviceTestRig
@using BlazorDeviceTestRig.Shared @using BlazorDeviceTestRig.Shared
+3 -3
View File
@@ -23,12 +23,12 @@
<a class="dismiss">🗙</a> <a class="dismiss">🗙</a>
</div> </div>
<script src="_framework/blazor.webassembly.js"></script> <script src="_framework/blazor.webassembly.js"></script>
<script src="_content/BlazorDeviceInterop/js/Geolocation.js"></script> <script src="_content/Darnton.Blazor.DeviceInterop/js/Geolocation.js"></script>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js" <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script> crossorigin=""></script>
<script src="_content/BlazorDeviceInterop.Components/device-interop.js"></script> <script src="_content/Darnton.Blazor.Leaflet/device-interop.js"></script>
<script src="_content/BlazorDeviceInterop.Components/leaflet-map/leaflet-map.js"></script> <script src="_content/Darnton.Blazor.Leaflet/leaflet-map/leaflet-map.js"></script>
</body> </body>
</html> </html>
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RazorLangVersion>3.0</RazorLangVersion>
<RootNamespace>Darnton.Blazor.DeviceInterop</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.0</Version>
<Authors>Bernard Darnton</Authors>
<Product>Blazor device interop library</Product>
<Description>Blazor device interop library including wrappers for Geolocation API</Description>
<Copyright>2020 Bernard Darnton</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/darnton/BlazorDeviceInterop</RepositoryUrl>
<AssemblyVersion>0.0.1.0</AssemblyVersion>
<FileVersion>0.0.1.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>C:\Users\user\source\repos\BlazorDeviceInterop\Darnton.Blazor.DeviceInterop\Darnton.Blazor.DeviceInterop.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Darnton.Units" Version="0.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.8" />
</ItemGroup>
</Project>
@@ -0,0 +1,231 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>Darnton.Blazor.DeviceInterop</name>
</assembly>
<members>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates">
<summary>
Geolocation Coordinates, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates"/>.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Latitude">
<summary>
Latitude in decimal degrees.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Longitude">
<summary>
Longitude in decimal degrees.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Altitude">
<summary>
Altitude in metres, relative to sea level.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Accuracy">
<summary>
Accuracy of the latitude and longitude properties, in metres.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.AltitudeAccuracy">
<summary>
Accuracy of the altitude, in metres.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Heading">
<summary>
The direction the device is travelling, in degrees clockwise from true north.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Speed">
<summary>
The velocity of the device, in metres per second.
</summary>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs">
<summary>
Geolocation event data. Provides the <see cref="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs.GeolocationResult"/> with the position or error associated with the event.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs.GeolocationResult">
<summary>
The <see cref="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs.GeolocationResult"/> associated with the event.
</summary>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition">
<summary>
Geolocation Position, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition"/>.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.Coords">
<summary>
The coordinates defining the current location
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.Timestamp">
<summary>
The time the coordinates were taken, in milliseconds since the Unix epoch.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.DateTimeOffset">
<summary>
The <see cref="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.DateTimeOffset"/> derived from the Timestamp, in UTC.
</summary>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError">
<summary>
The reason for a Geolocation error, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError"/>.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError.Code">
<summary>
The <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode"/> for the error.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError.Message">
<summary>
Details of the error. Intended for debugging rather than display to the user.
</summary>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode">
<summary>
An enumeration of error codes used by <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError"/>.
<see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError/code"/>.
</summary>
</member>
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.DEVICE_NOT_SUPPORTED">
<summary>
Geolocation failoed because the device does not support geolocation. Not part of W3C spec.
</summary>
</member>
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.PERMISSION_DENIED">
<summary>
Geolocation failed because permission to access location was denied.
</summary>
</member>
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.POSITION_UNAVAILABLE">
<summary>
Geolocation failed because of an internal error on the device.
</summary>
</member>
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.TIMEOUT">
<summary>
Geolocation failed because no position was returned in time.
</summary>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult">
<summary>
The result of a geolocation request. Contains either a <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition"/> or a <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError"/>.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult.Position">
<summary>
The <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition"/> returned on successful geolocation.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult.Error">
<summary>
The <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError"/> returned by a failed geolocation attempt.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult.IsSuccess">
<summary>
Indicates whether the geolocation attempt was successful.
</summary>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService">
<summary>
An implementation of <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService"/> that provides
an interop layer for the device's Geolocation API.
</summary>
</member>
<member name="E:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.WatchPositionReceived">
<inheritdoc/>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.#ctor(Microsoft.JSInterop.IJSRuntime)">
<summary>
Constructs a <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService"/> object.
</summary>
<param name="JSRuntime"></param>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.GetCurrentPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
<inheritdoc/>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.WatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
<inheritdoc/>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.SetWatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult)">
<summary>
Invokes the <see cref="E:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.WatchPositionReceived"/> event handler.
Invoked by the success and error callbacks of the JavaScript watchPosition() function.
</summary>
<param name="watchResult">A <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult"/> passed back from JavaScript.</param>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.ClearWatch(System.Int64)">
<inheritdoc/>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService">
<summary>
A wrapper around the device's Geolocation API services.
<see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API"/>.
</summary>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.GetCurrentPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
<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="T:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions"/> used to modify the request.</param>
<returns>The result of the geolocation request.</returns>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
<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="E:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPositionReceived"/> event is fired
each time the device's position changes.
</summary>
<param name="options"><see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.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="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.ClearWatch(System.Int64)"/>.</returns>
</member>
<member name="E:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPositionReceived">
<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="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs"/>.
</summary>
</member>
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.ClearWatch(System.Int64)">
<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="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)"/>.
</summary>
<param name="watchId">The ID of the registered watch handler.</param>
<returns>A task that represents the async clear operation.</returns>
</member>
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions">
<summary>
Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions.EnableHighAccuracy">
<summary>
Enable high accuracy mode for best possible results.
May be slower or increase power consumption. Defaults to false.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions.Timeout">
<summary>
Maximum length of time allowed to return a position (in milliseconds).
Set to null for no timeout. Defaults to null.
</summary>
</member>
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions.MaximumAge">
<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>
</member>
</members>
</doc>
@@ -1,10 +1,9 @@
using System; using System;
namespace BlazorDeviceInterop.Geolocation namespace Darnton.Blazor.DeviceInterop.Geolocation
{ {
[Serializable]
/// <summary> /// <summary>
/// Geolocation Coordinates, based on https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates. /// Geolocation Coordinates, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates"/>.
/// </summary> /// </summary>
public class GeolocationCoordinates public class GeolocationCoordinates
{ {
@@ -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; }
}
}
@@ -1,11 +1,11 @@
using System; using Darnton.Units;
using System;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
namespace BlazorDeviceInterop.Geolocation namespace Darnton.Blazor.DeviceInterop.Geolocation
{ {
[Serializable]
/// <summary> /// <summary>
/// Geolocation Position, based on https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition. /// Geolocation Position, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition"/>.
/// </summary> /// </summary>
public class GeolocationPosition public class GeolocationPosition
{ {
@@ -19,9 +19,10 @@ namespace BlazorDeviceInterop.Geolocation
/// </summary> /// </summary>
public long Timestamp { get; set; } public long Timestamp { get; set; }
private const long UnixEpochTicks = 621355968000000000; /// <summary>
private const long TicksPerMillisecond = 10000; /// The <see cref="DateTimeOffset"/> derived from the Timestamp, in UTC.
/// </summary>
[JsonIgnore] [JsonIgnore]
public DateTime DateTime => new DateTime(Timestamp * TicksPerMillisecond + UnixEpochTicks); public DateTimeOffset DateTimeOffset => (Timestamp / 1000).FromUnixTime().ToDateTimeOffset();
} }
} }
@@ -1,15 +1,14 @@
using System; using System;
namespace BlazorDeviceInterop.Geolocation namespace Darnton.Blazor.DeviceInterop.Geolocation
{ {
[Serializable]
/// <summary> /// <summary>
/// The reason for a Geolocation error, based on https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError. /// The reason for a Geolocation error, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError"/>.
/// </summary> /// </summary>
public class GeolocationPositionError public class GeolocationPositionError
{ {
/// <summary> /// <summary>
/// The code for the error /// The <see cref="GeolocationPositionErrorCode"/> for the error.
/// </summary> /// </summary>
public GeolocationPositionErrorCode Code { get; set; } public GeolocationPositionErrorCode Code { 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);
}
}
@@ -2,24 +2,35 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace BlazorDeviceInterop.Geolocation 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 public class GeolocationService : IGeolocationService
{ {
private readonly IJSRuntime _jsRuntime; private readonly IJSRuntime _jsRuntime;
/// <inheritdoc/>
public event EventHandler<GeolocationEventArgs> WatchPositionReceived; public event EventHandler<GeolocationEventArgs> WatchPositionReceived;
/// <summary>
/// Constructs a <see cref="GeolocationService"/> object.
/// </summary>
/// <param name="JSRuntime"></param>
public GeolocationService(IJSRuntime JSRuntime) public GeolocationService(IJSRuntime JSRuntime)
{ {
_jsRuntime = JSRuntime; _jsRuntime = JSRuntime;
} }
/// <inheritdoc/>
public async Task<GeolocationResult> GetCurrentPosition(PositionOptions options = null) public async Task<GeolocationResult> GetCurrentPosition(PositionOptions options = null)
{ {
return await _jsRuntime.InvokeAsync<GeolocationResult>("Geolocation.getCurrentPosition", options); return await _jsRuntime.InvokeAsync<GeolocationResult>("Geolocation.getCurrentPosition", options);
} }
/// <inheritdoc/>
public async Task<long?> WatchPosition(PositionOptions options = null) public async Task<long?> WatchPosition(PositionOptions options = null)
{ {
var callbackObj = DotNetObjectReference.Create(this); var callbackObj = DotNetObjectReference.Create(this);
@@ -27,6 +38,11 @@ namespace BlazorDeviceInterop.Geolocation
callbackObj, nameof(SetWatchPosition), options); 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] [JSInvokable]
public void SetWatchPosition(GeolocationResult watchResult) public void SetWatchPosition(GeolocationResult watchResult)
{ {
@@ -36,6 +52,7 @@ namespace BlazorDeviceInterop.Geolocation
}); });
} }
/// <inheritdoc/>
public async Task ClearWatch(long watchId) public async Task ClearWatch(long watchId)
{ {
await _jsRuntime.InvokeVoidAsync("Geolocation.clearWatch", 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);
}
}
@@ -1,4 +1,4 @@
namespace BlazorDeviceInterop.Geolocation namespace Darnton.Blazor.DeviceInterop.Geolocation
{ {
/// <summary> /// <summary>
/// Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions. /// Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions.