Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: BasemapGallery loading demo data in production release #579

Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static BasemapGallery()
</Style>
</Grid.Resources>
<CollectionView x:Name=""PART_InnerListView"" HorizontalOptions=""Fill"" VerticalOptions=""Fill"" SelectionMode=""Single"" BackgroundColor=""{{AppThemeBinding Light=#fff,Dark=#353535}}"" />
<Grid x:Name=""PART_LoadingScrim"">
<Grid x:Name=""PART_LoadingScrim"" IsVisible=""False"">
<Grid BackgroundColor=""{{AppThemeBinding Light=White, Dark=Black}}"" Opacity=""0.3"" />
<ActivityIndicator IsRunning=""True"" HorizontalOptions=""Center"" VerticalOptions=""Center"" />
</Grid>
Expand Down
19 changes: 15 additions & 4 deletions src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@ public BasemapGallery()
ListItemTemplate = DefaultListDataTemplate;
GridItemTemplate = DefaultGridDataTemplate;
ControlTemplate = DefaultControlTemplate;
_ = _controller.LoadFromDefaultPortal();
}

Loaded += BasemapGallery_Loaded;
}

private async void BasemapGallery_Loaded(object? sender, EventArgs e)
{
// Unsubscribe from the Loaded event to ensure this only runs once.
Loaded -= BasemapGallery_Loaded;

if (AvailableBasemaps is null)
{
await _controller.LoadFromDefaultPortal();
}
}

private void HandleControllerPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
Expand Down Expand Up @@ -185,7 +196,7 @@ public GeoModel? GeoModel
public IList<BasemapGalleryItem>? AvailableBasemaps
{
get => GetValue(AvailableBasemapsProperty) as IList<BasemapGalleryItem>;
set => SetValue(AvailableBasemapsProperty, value);
set => SetValue(AvailableBasemapsProperty, value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ public BasemapGallery()
SizeChanged += BasemapGallerySizeChanged;
AvailableBasemaps = new ObservableCollection<BasemapGalleryItem>();
_controller.PropertyChanged += HandleControllerPropertyChanged;
_ = _controller.LoadFromDefaultPortal();
Loaded += BasemapGallery_Loaded;
}

private async void BasemapGallery_Loaded(object? sender, RoutedEventArgs e)
{
// Unsubscribe from the Loaded event to ensure this only runs once.
Loaded -= BasemapGallery_Loaded;

if (AvailableBasemaps is null)
{
await _controller.LoadFromDefaultPortal();
}
}

private void HandleControllerPropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui
namespace Esri.ArcGISRuntime.Toolkit.UI
#endif
{
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
internal class BasemapGalleryController : INotifyPropertyChanged
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
{
private ArcGISPortal? _portal;
private bool _ignoreEventsFlag;
private IList<BasemapGalleryItem>? _availableBasemaps;
private GeoModel? _geoModel;
private BasemapGalleryItem? _selectedBasemap;
private bool _isLoading;
private CancellationTokenSource? _loadCancellationTokenSource;

public bool IsLoading
{
Expand Down Expand Up @@ -72,6 +75,7 @@ public IList<BasemapGalleryItem>? AvailableBasemaps

HandleAvailableBasemapsChanged();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AvailableBasemaps)));
_loadCancellationTokenSource?.Cancel();
}
}
}
Expand Down Expand Up @@ -227,10 +231,13 @@ private void HandleSelectedBasemapChanged()
public async Task LoadFromDefaultPortal()
{
IsLoading = true;
_loadCancellationTokenSource = new CancellationTokenSource();
try
{
AvailableBasemaps = await PopulateFromDefaultList();
AvailableBasemaps = await PopulateFromDefaultList(_loadCancellationTokenSource.Token);
}
catch (OperationCanceledException)
{ }
finally
{
IsLoading = false;
Expand Down Expand Up @@ -305,11 +312,11 @@ private static async Task<bool> BasemapIsActuallyNotABasemap(Basemap input)
return listOfBasemaps;
}

private static async Task<IList<BasemapGalleryItem>> PopulateFromDefaultList()
private static async Task<IList<BasemapGalleryItem>> PopulateFromDefaultList(CancellationToken cancellationToken = default)
{
ArcGISPortal defaultPortal = await ArcGISPortal.CreateAsync();
ArcGISPortal defaultPortal = await ArcGISPortal.CreateAsync(cancellationToken);

var results = await defaultPortal.GetDeveloperBasemapsAsync();
var results = await defaultPortal.GetDeveloperBasemapsAsync(cancellationToken);

var listOfBasemaps = new List<BasemapGalleryItem>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,38 @@ internal bool EqualsBasemap(Basemap? other)
return false;
}

return other == Basemap || other.Name == Basemap?.Name
|| (other.Item?.ItemId != null && other.Item?.ItemId == Basemap?.Item?.ItemId)
|| (other.Uri != null && other.Uri == Basemap?.Uri);
return other == Basemap || (other.Item?.ItemId != null && other.Item?.ItemId == Basemap?.Item?.ItemId)
|| (other.Uri != null && other.Uri == Basemap?.Uri)
|| AreBasemapsEqualByLayers(Basemap, other);
}

private static bool AreBasemapsEqualByLayers(Basemap? basemap1, Basemap? basemap2)
{
if (basemap1 == null || basemap2 == null) return false;

return LayersEqual(basemap1.BaseLayers, basemap2.BaseLayers)
&& LayersEqual(basemap1.ReferenceLayers, basemap2.ReferenceLayers);
}

private static bool LayersEqual(LayerCollection layers1, LayerCollection layers2)
{
return layers1.Count == layers2.Count
&& layers1.Zip(layers2, (layer1, layer2) => new { Layer1 = layer1, Layer2 = layer2 })
.All(pair => LayerEquals(pair.Layer1, pair.Layer2));
}
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved

private static bool LayerEquals(Layer layer1, Layer layer2)
{
// This method can be extended to handle more specific layer comparisons if needed
if (layer1.GetType() != layer2.GetType()) return false;

return layer1 switch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about DynamicEntityLayer, FeatureLayer, and RasterLayer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What could be the properties to compare in DynamicEntityLayer and RasterLayer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mstefarov those aren't valid basemap layers according to the official webmap spec.

{
ArcGISSceneLayer sceneLayer => sceneLayer.Source == ((ArcGISSceneLayer)layer2).Source,
ArcGISTiledLayer tiledLayer => tiledLayer.Source == ((ArcGISTiledLayer)layer2).Source,
ArcGISVectorTiledLayer vectorTiledLayer => vectorTiledLayer.Source == ((ArcGISVectorTiledLayer)layer2).Source,
_ => false,
prathameshnarkhede marked this conversation as resolved.
Show resolved Hide resolved
};
}

/// <inheritdoc />
Expand Down
Loading