diff --git a/BlazorDeviceInterop.Components/BlazorDeviceInterop.Components.csproj b/BlazorDeviceInterop.Components/BlazorDeviceInterop.Components.csproj deleted file mode 100644 index 59065df..0000000 --- a/BlazorDeviceInterop.Components/BlazorDeviceInterop.Components.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - netstandard2.0 - 3.0 - - - - - - - - - - - - - - diff --git a/BlazorDeviceInterop.Components/InteropObject.cs b/BlazorDeviceInterop.Components/InteropObject.cs deleted file mode 100644 index 24bd17f..0000000 --- a/BlazorDeviceInterop.Components/InteropObject.cs +++ /dev/null @@ -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 CreateJsObjectRef(IJSRuntime jsRuntime); - } -} diff --git a/BlazorDeviceInterop.Components/JsRuntimeObjectRef.cs b/BlazorDeviceInterop.Components/JsRuntimeObjectRef.cs deleted file mode 100644 index 8d452ef..0000000 --- a/BlazorDeviceInterop.Components/JsRuntimeObjectRef.cs +++ /dev/null @@ -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); - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/InteractiveLayer.cs b/BlazorDeviceInterop.Components/LeafletMap/InteractiveLayer.cs deleted file mode 100644 index f684870..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/InteractiveLayer.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public abstract class InteractiveLayer : Layer - { - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/LatLng.cs b/BlazorDeviceInterop.Components/LeafletMap/LatLng.cs deleted file mode 100644 index 8a99f50..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/LatLng.cs +++ /dev/null @@ -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; - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/Layer.cs b/BlazorDeviceInterop.Components/LeafletMap/Layer.cs deleted file mode 100644 index bf8a1cb..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/Layer.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.JSInterop; -using System.Threading.Tasks; - -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public abstract class Layer : InteropObject - { - public async Task AddTo(Map map) - { - await _jsObjRef.JSRuntime.InvokeVoidAsync("LeafletMap.Layer.addTo", this, map); - return this; - } - - public async Task Remove() - { - await _jsObjRef.JSRuntime.InvokeVoidAsync("LeafletMap.Layer.remove", this); - return this; - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/LayerOptions.cs b/BlazorDeviceInterop.Components/LeafletMap/LayerOptions.cs deleted file mode 100644 index 2620fc9..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/LayerOptions.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public class LayerOptions - { - public string Attribution { get; set; } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/LeafletMap.razor b/BlazorDeviceInterop.Components/LeafletMap/LeafletMap.razor deleted file mode 100644 index b3d2c7e..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/LeafletMap.razor +++ /dev/null @@ -1,3 +0,0 @@ -@inherits LeafletMapBase - -
\ No newline at end of file diff --git a/BlazorDeviceInterop.Components/LeafletMap/LeafletMap.razor.cs b/BlazorDeviceInterop.Components/LeafletMap/LeafletMap.razor.cs deleted file mode 100644 index 8fa407c..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/LeafletMap.razor.cs +++ /dev/null @@ -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); - } - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/Map.cs b/BlazorDeviceInterop.Components/LeafletMap/Map.cs deleted file mode 100644 index 4d418f3..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/Map.cs +++ /dev/null @@ -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 CreateJsObjectRef(IJSRuntime jsRuntime) - { - return await jsRuntime.InvokeAsync("LeafletMap.map", ElementId, Options); - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/MapOptions.cs b/BlazorDeviceInterop.Components/LeafletMap/MapOptions.cs deleted file mode 100644 index 2670d5f..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/MapOptions.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public class MapOptions - { - public LatLng Center { get; set; } - public int Zoom { get; set; } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/Marker.cs b/BlazorDeviceInterop.Components/LeafletMap/Marker.cs deleted file mode 100644 index 4fc563d..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/Marker.cs +++ /dev/null @@ -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 CreateJsObjectRef(IJSRuntime jsRuntime) - { - return await jsRuntime.InvokeAsync("LeafletMap.marker", LatLng, Options); - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/MarkerOptions.cs b/BlazorDeviceInterop.Components/LeafletMap/MarkerOptions.cs deleted file mode 100644 index 7b33702..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/MarkerOptions.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public class MarkerOptions - { - } -} \ No newline at end of file diff --git a/BlazorDeviceInterop.Components/LeafletMap/Path.cs b/BlazorDeviceInterop.Components/LeafletMap/Path.cs deleted file mode 100644 index 12a29d9..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/Path.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public abstract class Path : InteractiveLayer - { - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/Polyline.cs b/BlazorDeviceInterop.Components/LeafletMap/Polyline.cs deleted file mode 100644 index 0fbaa84..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/Polyline.cs +++ /dev/null @@ -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 LatLngs { get; } - [JsonIgnore] public PolylineOptions Options { get; } - - public Polyline(IEnumerable latLngs, PolylineOptions options) - { - LatLngs = latLngs; - Options = options; - } - - protected override async Task CreateJsObjectRef(IJSRuntime jsRuntime) - { - return await jsRuntime.InvokeAsync("LeafletMap.polyline", LatLngs.ToArray(), Options); - } - - public async Task AddLatLng(LatLng latLng) - { - await _jsObjRef.JSRuntime.InvokeVoidAsync("LeafletMap.Polyline.addLatLng", this, latLng); - return this; - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/PolylineOptions.cs b/BlazorDeviceInterop.Components/LeafletMap/PolylineOptions.cs deleted file mode 100644 index 4306e73..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/PolylineOptions.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public class PolylineOptions : LayerOptions - { - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/TileLayer.cs b/BlazorDeviceInterop.Components/LeafletMap/TileLayer.cs deleted file mode 100644 index 9c04a8d..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/TileLayer.cs +++ /dev/null @@ -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 CreateJsObjectRef(IJSRuntime jsRuntime) - { - return await jsRuntime.InvokeAsync("LeafletMap.tileLayer", UrlTemplate, Options); - } - } -} diff --git a/BlazorDeviceInterop.Components/LeafletMap/TileLayerOptions.cs b/BlazorDeviceInterop.Components/LeafletMap/TileLayerOptions.cs deleted file mode 100644 index c5616f9..0000000 --- a/BlazorDeviceInterop.Components/LeafletMap/TileLayerOptions.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace BlazorDeviceInterop.Components.LeafletMap -{ - public class TileLayerOptions : LayerOptions - { - } -} diff --git a/BlazorDeviceInterop.Components/wwwroot/device-interop.js b/BlazorDeviceInterop.Components/wwwroot/device-interop.js deleted file mode 100644 index 15323fd..0000000 --- a/BlazorDeviceInterop.Components/wwwroot/device-interop.js +++ /dev/null @@ -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; - } -}); \ No newline at end of file diff --git a/BlazorDeviceInterop.Components/wwwroot/leaflet-map/leaflet-map.js b/BlazorDeviceInterop.Components/wwwroot/leaflet-map/leaflet-map.js deleted file mode 100644 index bc94899..0000000 --- a/BlazorDeviceInterop.Components/wwwroot/leaflet-map/leaflet-map.js +++ /dev/null @@ -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); - } - - } - -} \ No newline at end of file diff --git a/BlazorDeviceInterop.sln b/BlazorDeviceInterop.sln index 3da3c89..730e360 100644 --- a/BlazorDeviceInterop.sln +++ b/BlazorDeviceInterop.sln @@ -3,12 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30503.244 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 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorDeviceTestRig", "BlazorDeviceTestRig\BlazorDeviceTestRig.csproj", "{47389794-07A5-4AC5-AA7A-69E9A4E23089}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorDeviceInterop.Components", "BlazorDeviceInterop.Components\BlazorDeviceInterop.Components.csproj", "{39BFE757-6826-4C20-9B23-737642140243}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BlazorDeviceInterop/BlazorDeviceInterop.csproj b/BlazorDeviceInterop/BlazorDeviceInterop.csproj deleted file mode 100644 index 17956c8..0000000 --- a/BlazorDeviceInterop/BlazorDeviceInterop.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - netstandard2.0 - 3.0 - - - - - - - - - diff --git a/BlazorDeviceInterop/Geolocation/GeolocationEventArgs.cs b/BlazorDeviceInterop/Geolocation/GeolocationEventArgs.cs deleted file mode 100644 index c87d8f7..0000000 --- a/BlazorDeviceInterop/Geolocation/GeolocationEventArgs.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace BlazorDeviceInterop.Geolocation -{ - public class GeolocationEventArgs : EventArgs - { - public GeolocationResult GeolocationResult { get; set; } - } -} diff --git a/BlazorDeviceInterop/Geolocation/GeolocationPositionErrorCode.cs b/BlazorDeviceInterop/Geolocation/GeolocationPositionErrorCode.cs deleted file mode 100644 index eab3eb5..0000000 --- a/BlazorDeviceInterop/Geolocation/GeolocationPositionErrorCode.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace BlazorDeviceInterop.Geolocation -{ - public enum GeolocationPositionErrorCode - { - DEVICE_NOT_SUPPORTED = 0, - PERMISSION_DENIED = 1, - POSITION_UNAVAILABLE = 2, - TIMEOUT = 3 - } -} \ No newline at end of file diff --git a/BlazorDeviceInterop/Geolocation/GeolocationResult.cs b/BlazorDeviceInterop/Geolocation/GeolocationResult.cs deleted file mode 100644 index 05231df..0000000 --- a/BlazorDeviceInterop/Geolocation/GeolocationResult.cs +++ /dev/null @@ -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); - } -} diff --git a/BlazorDeviceInterop/Geolocation/IGeolocationService.cs b/BlazorDeviceInterop/Geolocation/IGeolocationService.cs deleted file mode 100644 index b8082aa..0000000 --- a/BlazorDeviceInterop/Geolocation/IGeolocationService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace BlazorDeviceInterop.Geolocation -{ - public interface IGeolocationService - { - Task GetCurrentPosition(PositionOptions options = null); - Task WatchPosition(PositionOptions options = null); - void SetWatchPosition(GeolocationResult watchResult); - event EventHandler WatchPositionReceived; - Task ClearWatch(long watchId); - } -} diff --git a/BlazorDeviceInterop/_Imports.razor b/BlazorDeviceInterop/_Imports.razor deleted file mode 100644 index 7728512..0000000 --- a/BlazorDeviceInterop/_Imports.razor +++ /dev/null @@ -1 +0,0 @@ -@using Microsoft.AspNetCore.Components.Web diff --git a/BlazorDeviceTestRig/BlazorDeviceTestRig.csproj b/BlazorDeviceTestRig/BlazorDeviceTestRig.csproj index 2702995..048f3f9 100644 --- a/BlazorDeviceTestRig/BlazorDeviceTestRig.csproj +++ b/BlazorDeviceTestRig/BlazorDeviceTestRig.csproj @@ -6,6 +6,7 @@ + @@ -14,8 +15,7 @@ - - + diff --git a/BlazorDeviceTestRig/Geolocation/GeolocationPositionExtension.cs b/BlazorDeviceTestRig/Geolocation/GeolocationPositionExtension.cs index a2db540..c1bfed6 100644 --- a/BlazorDeviceTestRig/Geolocation/GeolocationPositionExtension.cs +++ b/BlazorDeviceTestRig/Geolocation/GeolocationPositionExtension.cs @@ -1,5 +1,5 @@ -using BlazorDeviceInterop.Components.LeafletMap; -using BlazorDeviceInterop.Geolocation; +using Darnton.Blazor.Leaflet.LeafletMap; +using Darnton.Blazor.DeviceInterop.Geolocation; namespace BlazorDeviceTestRig.Geolocation { diff --git a/BlazorDeviceTestRig/Pages/Geolocation.razor.cs b/BlazorDeviceTestRig/Pages/Geolocation.razor.cs index 45cd950..b8c9bf3 100644 --- a/BlazorDeviceTestRig/Pages/Geolocation.razor.cs +++ b/BlazorDeviceTestRig/Pages/Geolocation.razor.cs @@ -1,14 +1,16 @@ -using BlazorDeviceInterop.Components.LeafletMap; -using BlazorDeviceInterop.Geolocation; +using Darnton.Blazor.Leaflet.LeafletMap; +using Darnton.Blazor.DeviceInterop.Geolocation; using BlazorDeviceTestRig.Geolocation; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; +using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace BlazorDeviceTestRig.Pages { - public class GeolocationBase : ComponentBase + public class GeolocationBase : ComponentBase, IDisposable { [Inject] public IJSRuntime JSRuntime { get; set; } [Inject] public IGeolocationService GeolocationService { get; set; } @@ -31,7 +33,7 @@ namespace BlazorDeviceTestRig.Pages protected GeolocationResult LastWatchPositionResult { get; set; } protected string LastWatchLatitude => LastWatchPositionResult?.Position?.Coords?.Latitude.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"; public GeolocationBase() : base() @@ -77,8 +79,7 @@ namespace BlazorDeviceTestRig.Pages { if (isWatching) { - GeolocationService.WatchPositionReceived -= HandleWatchPositionReceived; - await GeolocationService.ClearWatch(WatchHandlerId.Value); + await StopWatching(); WatchHandlerId = null; foreach (var marker in WatchMarkers) { @@ -96,6 +97,12 @@ namespace BlazorDeviceTestRig.Pages StateHasChanged(); } + private async Task StopWatching() + { + GeolocationService.WatchPositionReceived -= HandleWatchPositionReceived; + await GeolocationService.ClearWatch(WatchHandlerId.Value); + } + private async void HandleWatchPositionReceived(object sender, GeolocationEventArgs e) { LastWatchPositionResult = e.GeolocationResult; @@ -120,5 +127,13 @@ namespace BlazorDeviceTestRig.Pages } StateHasChanged(); } + + public async void Dispose() + { + if (isWatching) + { + await StopWatching(); + } + } } } diff --git a/BlazorDeviceTestRig/Program.cs b/BlazorDeviceTestRig/Program.cs index 2e70972..9e16dad 100644 --- a/BlazorDeviceTestRig/Program.cs +++ b/BlazorDeviceTestRig/Program.cs @@ -1,13 +1,9 @@ using System; using System.Net.Http; -using System.Collections.Generic; using System.Threading.Tasks; -using System.Text; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using BlazorDeviceInterop.Geolocation; +using Darnton.Blazor.DeviceInterop.Geolocation; namespace BlazorDeviceTestRig { diff --git a/BlazorDeviceTestRig/_Imports.razor b/BlazorDeviceTestRig/_Imports.razor index 2645751..a890c37 100644 --- a/BlazorDeviceTestRig/_Imports.razor +++ b/BlazorDeviceTestRig/_Imports.razor @@ -6,6 +6,6 @@ @using Microsoft.AspNetCore.Components.Web @using Microsoft.AspNetCore.Components.WebAssembly.Http @using Microsoft.JSInterop -@using BlazorDeviceInterop.Components.LeafletMap +@using Darnton.Blazor.Leaflet.LeafletMap @using BlazorDeviceTestRig @using BlazorDeviceTestRig.Shared diff --git a/BlazorDeviceTestRig/wwwroot/index.html b/BlazorDeviceTestRig/wwwroot/index.html index b8b41ae..a1c444e 100644 --- a/BlazorDeviceTestRig/wwwroot/index.html +++ b/BlazorDeviceTestRig/wwwroot/index.html @@ -23,12 +23,12 @@ 🗙 - + - - + + diff --git a/Darnton.Blazor.DeviceInterop/Darnton.Blazor.DeviceInterop.csproj b/Darnton.Blazor.DeviceInterop/Darnton.Blazor.DeviceInterop.csproj new file mode 100644 index 0000000..c10ebd5 --- /dev/null +++ b/Darnton.Blazor.DeviceInterop/Darnton.Blazor.DeviceInterop.csproj @@ -0,0 +1,30 @@ + + + + netstandard2.0 + 3.0 + Darnton.Blazor.DeviceInterop + true + 0.1.0 + Bernard Darnton + Blazor device interop library + Blazor device interop library including wrappers for Geolocation API + 2020 Bernard Darnton + MIT + https://github.com/darnton/BlazorDeviceInterop + 0.0.1.0 + 0.0.1.0 + + + + C:\Users\user\source\repos\BlazorDeviceInterop\Darnton.Blazor.DeviceInterop\Darnton.Blazor.DeviceInterop.xml + + + + + + + + + + diff --git a/Darnton.Blazor.DeviceInterop/Darnton.Blazor.DeviceInterop.xml b/Darnton.Blazor.DeviceInterop/Darnton.Blazor.DeviceInterop.xml new file mode 100644 index 0000000..9114760 --- /dev/null +++ b/Darnton.Blazor.DeviceInterop/Darnton.Blazor.DeviceInterop.xml @@ -0,0 +1,231 @@ + + + + Darnton.Blazor.DeviceInterop + + + + + Geolocation Coordinates, based on . + + + + + Latitude in decimal degrees. + + + + + Longitude in decimal degrees. + + + + + Altitude in metres, relative to sea level. + + + + + Accuracy of the latitude and longitude properties, in metres. + + + + + Accuracy of the altitude, in metres. + + + + + The direction the device is travelling, in degrees clockwise from true north. + + + + + The velocity of the device, in metres per second. + + + + + Geolocation event data. Provides the with the position or error associated with the event. + + + + + The associated with the event. + + + + + Geolocation Position, based on . + + + + + The coordinates defining the current location + + + + + The time the coordinates were taken, in milliseconds since the Unix epoch. + + + + + The derived from the Timestamp, in UTC. + + + + + The reason for a Geolocation error, based on . + + + + + The for the error. + + + + + Details of the error. Intended for debugging rather than display to the user. + + + + + An enumeration of error codes used by . + . + + + + + Geolocation failoed because the device does not support geolocation. Not part of W3C spec. + + + + + Geolocation failed because permission to access location was denied. + + + + + Geolocation failed because of an internal error on the device. + + + + + Geolocation failed because no position was returned in time. + + + + + The result of a geolocation request. Contains either a or a . + + + + + The returned on successful geolocation. + + + + + The returned by a failed geolocation attempt. + + + + + Indicates whether the geolocation attempt was successful. + + + + + An implementation of that provides + an interop layer for the device's Geolocation API. + + + + + + + + Constructs a object. + + + + + + + + + + + + Invokes the event handler. + Invoked by the success and error callbacks of the JavaScript watchPosition() function. + + A passed back from JavaScript. + + + + + + + A wrapper around the device's Geolocation API services. + . + + + + + A wrapper around the function, + used to get the current position of the device. + + used to modify the request. + The result of the geolocation request. + + + + A wrapper around the function, + used to listen for position changes. If the service is listening, a event is fired + each time the device's position changes. + + used to modify the request. + A watch ID that refers to the handler. The ID can be used to unregister the handler with . + + + + Handles the receipt of new positions. Fired whenever a handler is registered and the device's position changes. + Invoked with the sender and the . + + + + + A wrapper around the function, + used to unregister a handler created with . + + The ID of the registered watch handler. + A task that represents the async clear operation. + + + + Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions. + + + + + Enable high accuracy mode for best possible results. + May be slower or increase power consumption. Defaults to false. + + + + + Maximum length of time allowed to return a position (in milliseconds). + Set to null for no timeout. Defaults to null. + + + + + 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. + + + + diff --git a/BlazorDeviceInterop/Geolocation/GeolocationCoordinates.cs b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationCoordinates.cs similarity index 86% rename from BlazorDeviceInterop/Geolocation/GeolocationCoordinates.cs rename to Darnton.Blazor.DeviceInterop/Geolocation/GeolocationCoordinates.cs index 386f6bf..79dfd33 100644 --- a/BlazorDeviceInterop/Geolocation/GeolocationCoordinates.cs +++ b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationCoordinates.cs @@ -1,10 +1,9 @@ using System; -namespace BlazorDeviceInterop.Geolocation +namespace Darnton.Blazor.DeviceInterop.Geolocation { - [Serializable] /// - /// Geolocation Coordinates, based on https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates. + /// Geolocation Coordinates, based on . /// public class GeolocationCoordinates { diff --git a/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationEventArgs.cs b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationEventArgs.cs new file mode 100644 index 0000000..210570a --- /dev/null +++ b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationEventArgs.cs @@ -0,0 +1,15 @@ +using System; + +namespace Darnton.Blazor.DeviceInterop.Geolocation +{ + /// + /// Geolocation event data. Provides the with the position or error associated with the event. + /// + public class GeolocationEventArgs : EventArgs + { + /// + /// The associated with the event. + /// + public GeolocationResult GeolocationResult { get; set; } + } +} diff --git a/BlazorDeviceInterop/Geolocation/GeolocationPosition.cs b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPosition.cs similarity index 52% rename from BlazorDeviceInterop/Geolocation/GeolocationPosition.cs rename to Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPosition.cs index cebbd2a..a5b29eb 100644 --- a/BlazorDeviceInterop/Geolocation/GeolocationPosition.cs +++ b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPosition.cs @@ -1,11 +1,11 @@ -using System; +using Darnton.Units; +using System; using System.Text.Json.Serialization; -namespace BlazorDeviceInterop.Geolocation +namespace Darnton.Blazor.DeviceInterop.Geolocation { - [Serializable] /// - /// Geolocation Position, based on https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition. + /// Geolocation Position, based on . /// public class GeolocationPosition { @@ -19,9 +19,10 @@ namespace BlazorDeviceInterop.Geolocation /// public long Timestamp { get; set; } - private const long UnixEpochTicks = 621355968000000000; - private const long TicksPerMillisecond = 10000; + /// + /// The derived from the Timestamp, in UTC. + /// [JsonIgnore] - public DateTime DateTime => new DateTime(Timestamp * TicksPerMillisecond + UnixEpochTicks); + public DateTimeOffset DateTimeOffset => (Timestamp / 1000).FromUnixTime().ToDateTimeOffset(); } } diff --git a/BlazorDeviceInterop/Geolocation/GeolocationPositionError.cs b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPositionError.cs similarity index 60% rename from BlazorDeviceInterop/Geolocation/GeolocationPositionError.cs rename to Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPositionError.cs index 1b4f67d..d9fbc4b 100644 --- a/BlazorDeviceInterop/Geolocation/GeolocationPositionError.cs +++ b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPositionError.cs @@ -1,15 +1,14 @@ using System; -namespace BlazorDeviceInterop.Geolocation +namespace Darnton.Blazor.DeviceInterop.Geolocation { - [Serializable] /// - /// 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 . /// public class GeolocationPositionError { /// - /// The code for the error + /// The for the error. /// public GeolocationPositionErrorCode Code { get; set; } diff --git a/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPositionErrorCode.cs b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPositionErrorCode.cs new file mode 100644 index 0000000..54387b9 --- /dev/null +++ b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationPositionErrorCode.cs @@ -0,0 +1,26 @@ +namespace Darnton.Blazor.DeviceInterop.Geolocation +{ + /// + /// An enumeration of error codes used by . + /// . + /// + public enum GeolocationPositionErrorCode + { + /// + /// Geolocation failoed because the device does not support geolocation. Not part of W3C spec. + /// + DEVICE_NOT_SUPPORTED = 0, + /// + /// Geolocation failed because permission to access location was denied. + /// + PERMISSION_DENIED = 1, + /// + /// Geolocation failed because of an internal error on the device. + /// + POSITION_UNAVAILABLE = 2, + /// + /// Geolocation failed because no position was returned in time. + /// + TIMEOUT = 3 + } +} \ No newline at end of file diff --git a/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationResult.cs b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationResult.cs new file mode 100644 index 0000000..0f484df --- /dev/null +++ b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationResult.cs @@ -0,0 +1,26 @@ +using System; +using System.Text.Json.Serialization; + +namespace Darnton.Blazor.DeviceInterop.Geolocation +{ + /// + /// The result of a geolocation request. Contains either a or a . + /// + public class GeolocationResult + { + /// + /// The returned on successful geolocation. + /// + public GeolocationPosition Position { get; set; } + /// + /// The returned by a failed geolocation attempt. + /// + public GeolocationPositionError Error { get; set; } + + /// + /// Indicates whether the geolocation attempt was successful. + /// + [JsonIgnore] + public bool IsSuccess => !(Position is null); + } +} diff --git a/BlazorDeviceInterop/Geolocation/GeolocationService.cs b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationService.cs similarity index 62% rename from BlazorDeviceInterop/Geolocation/GeolocationService.cs rename to Darnton.Blazor.DeviceInterop/Geolocation/GeolocationService.cs index dee729b..8285003 100644 --- a/BlazorDeviceInterop/Geolocation/GeolocationService.cs +++ b/Darnton.Blazor.DeviceInterop/Geolocation/GeolocationService.cs @@ -2,24 +2,35 @@ using System; using System.Threading.Tasks; -namespace BlazorDeviceInterop.Geolocation +namespace Darnton.Blazor.DeviceInterop.Geolocation { + /// + /// An implementation of that provides + /// an interop layer for the device's Geolocation API. + /// public class GeolocationService : IGeolocationService { private readonly IJSRuntime _jsRuntime; + /// public event EventHandler WatchPositionReceived; + /// + /// Constructs a object. + /// + /// public GeolocationService(IJSRuntime JSRuntime) { _jsRuntime = JSRuntime; } + /// public async Task GetCurrentPosition(PositionOptions options = null) { return await _jsRuntime.InvokeAsync("Geolocation.getCurrentPosition", options); } + /// public async Task WatchPosition(PositionOptions options = null) { var callbackObj = DotNetObjectReference.Create(this); @@ -27,6 +38,11 @@ namespace BlazorDeviceInterop.Geolocation callbackObj, nameof(SetWatchPosition), options); } + /// + /// Invokes the event handler. + /// Invoked by the success and error callbacks of the JavaScript watchPosition() function. + /// + /// A passed back from JavaScript. [JSInvokable] public void SetWatchPosition(GeolocationResult watchResult) { @@ -36,6 +52,7 @@ namespace BlazorDeviceInterop.Geolocation }); } + /// public async Task ClearWatch(long watchId) { await _jsRuntime.InvokeVoidAsync("Geolocation.clearWatch", watchId); diff --git a/Darnton.Blazor.DeviceInterop/Geolocation/IGeolocationService.cs b/Darnton.Blazor.DeviceInterop/Geolocation/IGeolocationService.cs new file mode 100644 index 0000000..d83d3c6 --- /dev/null +++ b/Darnton.Blazor.DeviceInterop/Geolocation/IGeolocationService.cs @@ -0,0 +1,40 @@ +using System; +using System.Threading.Tasks; + +namespace Darnton.Blazor.DeviceInterop.Geolocation +{ + /// + /// A wrapper around the device's Geolocation API services. + /// . + /// + public interface IGeolocationService + { + /// + /// A wrapper around the function, + /// used to get the current position of the device. + /// + /// used to modify the request. + /// The result of the geolocation request. + Task GetCurrentPosition(PositionOptions options = null); + /// + /// A wrapper around the function, + /// used to listen for position changes. If the service is listening, a event is fired + /// each time the device's position changes. + /// + /// used to modify the request. + /// A watch ID that refers to the handler. The ID can be used to unregister the handler with . + Task WatchPosition(PositionOptions options = null); + /// + /// Handles the receipt of new positions. Fired whenever a handler is registered and the device's position changes. + /// Invoked with the sender and the . + /// + event EventHandler WatchPositionReceived; + /// + /// A wrapper around the function, + /// used to unregister a handler created with . + /// + /// The ID of the registered watch handler. + /// A task that represents the async clear operation. + Task ClearWatch(long watchId); + } +} diff --git a/BlazorDeviceInterop/Geolocation/PositionOptions.cs b/Darnton.Blazor.DeviceInterop/Geolocation/PositionOptions.cs similarity index 94% rename from BlazorDeviceInterop/Geolocation/PositionOptions.cs rename to Darnton.Blazor.DeviceInterop/Geolocation/PositionOptions.cs index 3d112eb..5a0c7ad 100644 --- a/BlazorDeviceInterop/Geolocation/PositionOptions.cs +++ b/Darnton.Blazor.DeviceInterop/Geolocation/PositionOptions.cs @@ -1,4 +1,4 @@ -namespace BlazorDeviceInterop.Geolocation +namespace Darnton.Blazor.DeviceInterop.Geolocation { /// /// Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions. diff --git a/BlazorDeviceInterop.Components/_Imports.razor b/Darnton.Blazor.DeviceInterop/_Imports.razor similarity index 100% rename from BlazorDeviceInterop.Components/_Imports.razor rename to Darnton.Blazor.DeviceInterop/_Imports.razor diff --git a/BlazorDeviceInterop/wwwroot/js/Geolocation.js b/Darnton.Blazor.DeviceInterop/wwwroot/js/Geolocation.js similarity index 100% rename from BlazorDeviceInterop/wwwroot/js/Geolocation.js rename to Darnton.Blazor.DeviceInterop/wwwroot/js/Geolocation.js