Skip to content

Commit

Permalink
Code formating changes
Browse files Browse the repository at this point in the history
  • Loading branch information
valentasm committed Nov 17, 2024
1 parent 4c8e944 commit fa33d54
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 101 deletions.
35 changes: 23 additions & 12 deletions GoogleMapsComponents/AdvancedGoogleMap.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

internal Guid? MapId => MapRef?.InteropObject.Guid;

public int MarkerCount => MapComponents.Count;
public IEnumerable<MarkerComponent> Markers => MapComponents.Select(x => x.Value);
private readonly Dictionary<Guid, MarkerComponent> MapComponents = [];
internal DotNetObjectReference<AdvancedGoogleMap>? callbackRef;
public int MarkerCount => _mapComponents.Count;
public IEnumerable<MarkerComponent> Markers => _mapComponents.Select(x => x.Value);
private readonly Dictionary<Guid, MarkerComponent> _mapComponents = [];
internal DotNetObjectReference<AdvancedGoogleMap>? CallbackRef;

[Parameter]
public string? Id { get; set; }
Expand All @@ -48,7 +48,7 @@

protected override void OnInitialized()
{
callbackRef = DotNetObjectReference.Create(this);
CallbackRef = DotNetObjectReference.Create(this);
base.OnInitialized();
}

Expand All @@ -60,36 +60,47 @@
[JSInvokable]
public async Task OnMarkerClicked(Guid markerId)
{
if (MapComponents.TryGetValue(markerId, out var markerComponent))
if (_mapComponents.TryGetValue(markerId, out var markerComponent))
{
await markerComponent.MarkerClicked();
}
}

[JSInvokable]
public async Task OnMarkerDrag(Guid markerId, LatLngLiteral position)
{
if (MapComponents.TryGetValue(markerId, out var markerComponent))
if (_mapComponents.TryGetValue(markerId, out var markerComponent))
{
await markerComponent.MarkerDragged(position);
}
}

internal void AddMarker(MarkerComponent marker)
{
MapComponents.TryAdd(marker.Guid, marker);
_mapComponents.TryAdd(marker.Guid, marker);
OnMarkersChanged.InvokeAsync();
}

internal void RemoveMarker(MarkerComponent marker)
{
MapComponents.Remove(marker.Guid);
_mapComponents.Remove(marker.Guid);
OnMarkersChanged.InvokeAsync();
}

public async ValueTask DisposeAsync()
{
// Mark components as disposed, since they will be removed by disposing the MapRef.
foreach (var component in MapComponents)
foreach (var component in _mapComponents)
{
component.Value.IsDisposed = true;
if (MapRef != null) await MapRef.DisposeAsync();
callbackRef?.Dispose();
}

if (MapRef != null)
{
await MapRef.DisposeAsync();
}

CallbackRef?.Dispose();
}

}
10 changes: 5 additions & 5 deletions GoogleMapsComponents/EnumMemberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GoogleMapsComponents;

internal class EnumMemberConverter<T> : JsonConverter<T> where T : IComparable, IFormattable, IConvertible
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonValue = reader.GetString();

Expand All @@ -20,7 +20,7 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial
{
if (string.Equals(description.Value, jsonValue, StringComparison.OrdinalIgnoreCase))
{
return (T)fi.GetValue(null);
return (T?)fi.GetValue(null);
}
}
}
Expand All @@ -30,10 +30,10 @@ public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerial

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
var fi = value.GetType().GetField(value.ToString());
var fi = value.GetType().GetField(value.ToString() ?? string.Empty);

var description = (EnumMemberAttribute)fi.GetCustomAttribute(typeof(EnumMemberAttribute), false);
var description = (EnumMemberAttribute?)fi?.GetCustomAttribute(typeof(EnumMemberAttribute), false);

writer.WriteStringValue(description.Value);
writer.WriteStringValue(description?.Value);
}
}
3 changes: 1 addition & 2 deletions GoogleMapsComponents/GoogleMapsComponents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
</PropertyGroup>

<PropertyGroup>
<TargetFramework>net8.0;</TargetFramework>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<IsPackable>true</IsPackable>

<LangVersion>latest</LangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<PackageId>BlazorGoogleMaps</PackageId>
<Version>4.8.0</Version>
<Version>4.9.0</Version>
<Authors>Rungwiroon</Authors>
<Company>QueueStack Solution</Company>
<Product>BlazorGoogleMaps</Product>
Expand Down
76 changes: 38 additions & 38 deletions GoogleMapsComponents/Maps/MarkerComponent.razor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using GoogleMapsComponents.Maps.Extension;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using GoogleMapsComponents.Maps.Extension;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;

namespace GoogleMapsComponents.Maps;

Expand All @@ -16,41 +16,41 @@ public MarkerComponent()
_componentId = "marker_" + _guid.ToString("N");
}
private readonly string _componentId;
private bool hasRendered = false;
private bool _hasRendered = false;
internal bool IsDisposed = false;
private Guid _guid;

public Guid Guid => Id ?? _guid;

[Inject]
private IJSRuntime JS { get; set; } = default!;
[CascadingParameter(Name = "Map")]
[Inject]
private IJSRuntime Js { get; set; } = default!;

[CascadingParameter(Name = "Map")]
private AdvancedGoogleMap MapRef { get; set; } = default!;
[Parameter]

[Parameter]
[JsonIgnore]
public RenderFragment? ChildContent { get; set; }

[Parameter, JsonIgnore]
public Guid? Id { get; set; }

/// <summary>
/// Latitude in degrees. Values will be clamped to the range [-90, 90].
/// This means that if the value specified is less than -90, it will be set to -90.
/// And if the value is greater than 90, it will be set to 90.
/// </summary>
[Parameter, JsonIgnore]
[Parameter, JsonIgnore]
public double Lat { get; set; }

/// <summary>
/// Longitude in degrees. Values outside the range [-180, 180] will be wrapped so that they fall within the range.
/// For example, a value of -190 will be converted to 170. A value of 190 will be converted to -170.
/// This reflects the fact that longitudes wrap around the globe.
/// </summary>
[Parameter, JsonIgnore]
[Parameter, JsonIgnore]
public double Lng { get; set; }

/// <summary>
/// An enumeration specifying how an AdvancedMarkerElement should behave when it collides with another AdvancedMarkerElement or with the basemap labels on a vector map.
/// Note: AdvancedMarkerElement to AdvancedMarkerElement collision works on both raster and vector maps, however, AdvancedMarkerElement to base map's label collision only works on vector maps.
Expand All @@ -70,13 +70,13 @@ public MarkerComponent()
/// </summary>
[Parameter, JsonIgnore]
public EventCallback<LatLngLiteral> OnMove { get; set; }

/// <summary>
/// If true, the AdvancedMarkerElement will be clickable and trigger the gmp-click event, and will be interactive for accessibility purposes (e.g. allowing keyboard navigation via arrow keys).
/// </summary>
[Parameter, JsonIgnore]
public bool Clickable { get; set; }

/// <summary>
/// This event is fired when the marker is clicked.
/// </summary>
Expand All @@ -95,25 +95,25 @@ public MarkerComponent()
/// </summary>
[Parameter, JsonIgnore]
public int? ZIndex { get; set; }

/// <summary>
/// A possible override MapId, if this is unset, the markers will read their MapId from the AdvancedGoogleMap
/// </summary>
[Parameter, JsonIgnore]
public Guid? MapId { get; set; }

/// <summary>
/// Specifies additional custom attributes that will be rendered on the "root" component of the marker.
/// </summary>
/// <value>The attributes.</value>
[Parameter(CaptureUnmatchedValues = true), JsonIgnore]
public IReadOnlyDictionary<string, object> Attributes { get; set; } = default!;

internal async Task MarkerClicked()
{
await OnClick.InvokeAsync();
}

internal async Task MarkerDragged(LatLngLiteral position)
{
await OnMove.InvokeAsync(position);
Expand All @@ -124,24 +124,24 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
if (firstRender)
{
MapRef.AddMarker(this);
hasRendered = true;
_hasRendered = true;
await UpdateOptions();
}
await base.OnAfterRenderAsync(firstRender);
}

/// <summary>
/// Trigger a "update" of the component, by default the component will update automatically when parameters changes.
/// </summary>
public async Task ForceRender()
{
if (!hasRendered) return;
if (!_hasRendered) return;
await UpdateOptions();
}

private async Task UpdateOptions()
{
await JS.InvokeAsync<string>("blazorGoogleMaps.objectManager.updateAdvancedComponent", Guid, new AdvancedMarkerComponentOptions()
await Js.InvokeAsync<string>("blazorGoogleMaps.objectManager.updateAdvancedComponent", Guid, new AdvancedMarkerComponentOptions()
{
CollisionBehavior = CollisionBehavior,
Position = new LatLngLiteral(Lat, Lng),
Expand All @@ -151,28 +151,28 @@ private async Task UpdateOptions()
GmpDraggable = Draggable,
MapId = MapId ?? MapRef.MapId,
ZIndex = ZIndex
}, MapRef.callbackRef);
}, MapRef.CallbackRef);
}

public override async Task SetParametersAsync(ParameterView parameters)
{
if (!hasRendered)
if (!_hasRendered)
{
await base.SetParametersAsync(parameters);
return;
}
var optionsChanged = parameters.DidParameterChange(CollisionBehavior) ||
parameters.DidParameterChange(Lat) ||

var optionsChanged = parameters.DidParameterChange(CollisionBehavior) ||
parameters.DidParameterChange(Lat) ||
parameters.DidParameterChange(Lng) ||
parameters.DidParameterChange(ZIndex) ||
parameters.DidParameterChange(Title) ||
parameters.DidParameterChange(Clickable) ||
parameters.DidParameterChange(MapId) ||
parameters.DidParameterChange(Draggable);

await base.SetParametersAsync(parameters);

if (optionsChanged)
{
await UpdateOptions();
Expand All @@ -183,11 +183,11 @@ public async ValueTask DisposeAsync()
{
if (IsDisposed) return;
IsDisposed = true;
await JS.InvokeVoidAsync("blazorGoogleMaps.objectManager.disposeAdvancedMarkerComponent", Guid);
await Js.InvokeVoidAsync("blazorGoogleMaps.objectManager.disposeAdvancedMarkerComponent", Guid);
MapRef.RemoveMarker(this);
GC.SuppressFinalize(this);
}

internal readonly struct AdvancedMarkerComponentOptions
{
public LatLngLiteral? Position { get; init; }
Expand Down
Loading

0 comments on commit fa33d54

Please sign in to comment.