|
| 1 | +namespace AzureMapsControl.Components.Drawing |
| 2 | +{ |
| 3 | + using System.Collections.Generic; |
| 4 | + using System.Linq; |
| 5 | + using System.Threading.Tasks; |
| 6 | + |
| 7 | + using AzureMapsControl.Components.Atlas; |
| 8 | + using AzureMapsControl.Components.Logger; |
| 9 | + using AzureMapsControl.Components.Runtime; |
| 10 | + |
| 11 | + using Microsoft.Extensions.Logging; |
| 12 | + |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// DrawingManager for the DrawingToolbar |
| 16 | + /// </summary> |
| 17 | + public sealed class DrawingManager |
| 18 | + { |
| 19 | + internal IMapJsRuntime JSRuntime { get; set; } |
| 20 | + internal ILogger Logger { get; set; } |
| 21 | + public bool Disposed { get; private set; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// List of shapes added to the data source |
| 25 | + /// </summary> |
| 26 | + private List<Shape> _sourceShapes; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Add shapes to the drawing manager data source |
| 30 | + /// </summary> |
| 31 | + /// <param name="shapes">Shapes to add</param> |
| 32 | + /// <returns></returns> |
| 33 | + /// <exception cref="Exceptions.ComponentNotAddedToMapException">The control has not been added to the map</exception> |
| 34 | + /// <exception cref="Exceptions.ComponentDisposedException">The control has already been disposed</exception> |
| 35 | + public async ValueTask AddShapesAsync(IEnumerable<Shape> shapes) |
| 36 | + { |
| 37 | + if (shapes == null || !shapes.Any()) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + EnsureJsRuntimeExists(); |
| 43 | + EnsureNotDisposed(); |
| 44 | + |
| 45 | + if (_sourceShapes == null) |
| 46 | + { |
| 47 | + _sourceShapes = new List<Shape>(); |
| 48 | + } |
| 49 | + |
| 50 | + var lineStrings = shapes.OfType<Shape<LineString>>(); |
| 51 | + if (lineStrings.Any()) |
| 52 | + { |
| 53 | + Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.Source_AddAsync, $"{lineStrings.Count()} linestrings will be added"); |
| 54 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.AddShapes.ToDrawingNamespace(), lineStrings); |
| 55 | + } |
| 56 | + |
| 57 | + var multiLineStrings = shapes.OfType<Shape<MultiLineString>>(); |
| 58 | + if (multiLineStrings.Any()) |
| 59 | + { |
| 60 | + Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.Source_AddAsync, $"{multiLineStrings.Count()} multilinestrings will be added"); |
| 61 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.AddShapes.ToDrawingNamespace(), multiLineStrings); |
| 62 | + } |
| 63 | + |
| 64 | + var multiPoints = shapes.OfType<Shape<MultiPoint>>(); |
| 65 | + if (multiPoints.Any()) |
| 66 | + { |
| 67 | + Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.Source_AddAsync, $"{multiPoints.Count()} multipoints will be added"); |
| 68 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.AddShapes.ToDrawingNamespace(), multiPoints); |
| 69 | + } |
| 70 | + |
| 71 | + var multiPolygons = shapes.OfType<Shape<MultiPolygon>>(); |
| 72 | + if (multiPolygons.Any()) |
| 73 | + { |
| 74 | + Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.Source_AddAsync, $"{multiPolygons.Count()} multipolygons will be added"); |
| 75 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.AddShapes.ToDrawingNamespace(), multiPolygons); |
| 76 | + } |
| 77 | + |
| 78 | + var points = shapes.OfType<Shape<Point>>(); |
| 79 | + if (points.Any()) |
| 80 | + { |
| 81 | + Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.Source_AddAsync, $"{points.Count()} points will be added"); |
| 82 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.AddShapes.ToDrawingNamespace(), points); |
| 83 | + } |
| 84 | + |
| 85 | + var polygons = shapes.OfType<Shape<Polygon>>(); |
| 86 | + if (polygons.Any()) |
| 87 | + { |
| 88 | + Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.Source_AddAsync, $"{polygons.Count()} polygons will be added"); |
| 89 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.AddShapes.ToDrawingNamespace(), polygons); |
| 90 | + } |
| 91 | + |
| 92 | + var routePoints = shapes.OfType<Shape<RoutePoint>>(); |
| 93 | + if (routePoints.Any()) |
| 94 | + { |
| 95 | + Logger?.LogAzureMapsControlDebug(AzureMapLogEvent.Source_AddAsync, $"{routePoints.Count()} route points will be added"); |
| 96 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.AddShapes.ToDrawingNamespace(), routePoints); |
| 97 | + } |
| 98 | + |
| 99 | + _sourceShapes.AddRange(shapes); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Clear the drawing manager source |
| 104 | + /// </summary> |
| 105 | + /// <returns></returns> |
| 106 | + /// <exception cref="Exceptions.ComponentNotAddedToMapException">The control has not been added to the map</exception> |
| 107 | + /// <exception cref="Exceptions.ComponentDisposedException">The control has already been disposed</exception> |
| 108 | + public async ValueTask ClearAsync() |
| 109 | + { |
| 110 | + Logger?.LogAzureMapsControlInfo(AzureMapLogEvent.Source_ClearAsync, "Clearing drawing manager source"); |
| 111 | + |
| 112 | + EnsureJsRuntimeExists(); |
| 113 | + EnsureNotDisposed(); |
| 114 | + |
| 115 | + _sourceShapes = null; |
| 116 | + await JSRuntime.InvokeVoidAsync(Constants.JsConstants.Methods.Source.Clear.ToDrawingNamespace()); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Mark the control as disposed |
| 121 | + /// </summary> |
| 122 | + /// <returns></returns> |
| 123 | + /// <exception cref="Exceptions.ComponentNotAddedToMapException">The control has not been added to the map</exception> |
| 124 | + /// <exception cref="Exceptions.ComponentDisposedException">The control has already been disposed</exception> |
| 125 | + internal void Dispose() |
| 126 | + { |
| 127 | + Logger?.LogAzureMapsControlInfo(AzureMapLogEvent.Source_DisposeAsync, "DrawingManager - Dispose"); |
| 128 | + |
| 129 | + EnsureJsRuntimeExists(); |
| 130 | + EnsureNotDisposed(); |
| 131 | + |
| 132 | + Disposed = true; |
| 133 | + } |
| 134 | + |
| 135 | + private void EnsureJsRuntimeExists() |
| 136 | + { |
| 137 | + if (JSRuntime is null) |
| 138 | + { |
| 139 | + throw new Exceptions.ComponentNotAddedToMapException(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private void EnsureNotDisposed() |
| 144 | + { |
| 145 | + if (Disposed) |
| 146 | + { |
| 147 | + throw new Exceptions.ComponentDisposedException(); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments