From 68cee87521e7746a456342838e54e6a8274c9c88 Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Mon, 10 Jun 2024 18:02:16 -0700 Subject: [PATCH 1/9] Refactor basemap loading and add event handler --- .../BasemapGallery/BasemapGallery.cs | 25 ++++++++++++++++--- .../BasemapGallery/BasemapGallery.Windows.cs | 21 +++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs index a840f4bb7..b21c1eeca 100644 --- a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs +++ b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs @@ -41,9 +41,28 @@ public BasemapGallery() ListItemTemplate = DefaultListDataTemplate; GridItemTemplate = DefaultGridDataTemplate; ControlTemplate = DefaultControlTemplate; - _ = _controller.LoadFromDefaultPortal(); - } - + Loaded += BasemapGallery_Loaded; + } + + private void BasemapGallery_Loaded(object? sender, EventArgs e) + { + LoadBasemaps(); + } + + private void LoadBasemaps() + { + _controller.IsLoading = true; + if (AvailableBasemaps == null) + { + _ = _controller.LoadFromDefaultPortal(); + } + else + { + _controller.AvailableBasemaps = AvailableBasemaps; + } + _controller.IsLoading = false; + } + private void HandleControllerPropertyChanged(object? sender, PropertyChangedEventArgs e) { switch (e.PropertyName) diff --git a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs index 5dca3ef46..24a2e6808 100644 --- a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs +++ b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs @@ -45,7 +45,26 @@ public BasemapGallery() SizeChanged += BasemapGallerySizeChanged; AvailableBasemaps = new ObservableCollection(); _controller.PropertyChanged += HandleControllerPropertyChanged; - _ = _controller.LoadFromDefaultPortal(); + Loaded += BasemapGallery_Loaded; + } + + private void BasemapGallery_Loaded(object? sender, RoutedEventArgs e) + { + LoadBasemaps(); + } + + private void LoadBasemaps() + { + _controller.IsLoading = true; + if (AvailableBasemaps == null) + { + _ = _controller.LoadFromDefaultPortal(); + } + else + { + _controller.AvailableBasemaps = AvailableBasemaps; + } + _controller.IsLoading = false; } private void HandleControllerPropertyChanged(object? sender, PropertyChangedEventArgs e) From e06432b7b83b1127af80f54a3e210afdd2b826fe Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Tue, 11 Jun 2024 16:44:46 -0700 Subject: [PATCH 2/9] Update BasemapGallery and BasemapGalleryItem classes - In `BasemapGallery.Appearance.cs`, the visibility of `PART_LoadingScrim` Grid has been set to `False`. - The `LoadBasemaps` method in `BasemapGallery.cs` and `BasemapGallery.Windows.cs` has been removed and its functionality has been moved to the `BasemapGallery_Loaded` method. - In `BasemapGalleryItem.cs`, the `Equals` method has been updated to check for equality of `BaseLayers` count and existence of all layers in `Basemap` in the `other` object, in addition to the existing checks. --- .../BasemapGallery/BasemapGallery.Appearance.cs | 2 +- .../Toolkit.Maui/BasemapGallery/BasemapGallery.cs | 13 +------------ .../BasemapGallery/BasemapGallery.Windows.cs | 13 +------------ .../Controls/BasemapGallery/BasemapGalleryItem.cs | 3 ++- 4 files changed, 5 insertions(+), 26 deletions(-) diff --git a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.Appearance.cs b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.Appearance.cs index db4f84d3a..9f852809c 100644 --- a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.Appearance.cs +++ b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.Appearance.cs @@ -173,7 +173,7 @@ static BasemapGallery() - + diff --git a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs index b21c1eeca..626d59ca4 100644 --- a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs +++ b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs @@ -46,21 +46,10 @@ public BasemapGallery() private void BasemapGallery_Loaded(object? sender, EventArgs e) { - LoadBasemaps(); - } - - private void LoadBasemaps() - { - _controller.IsLoading = true; - if (AvailableBasemaps == null) + if (AvailableBasemaps is null) { _ = _controller.LoadFromDefaultPortal(); } - else - { - _controller.AvailableBasemaps = AvailableBasemaps; - } - _controller.IsLoading = false; } private void HandleControllerPropertyChanged(object? sender, PropertyChangedEventArgs e) diff --git a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs index 24a2e6808..2240b933c 100644 --- a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs +++ b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs @@ -50,21 +50,10 @@ public BasemapGallery() private void BasemapGallery_Loaded(object? sender, RoutedEventArgs e) { - LoadBasemaps(); - } - - private void LoadBasemaps() - { - _controller.IsLoading = true; - if (AvailableBasemaps == null) + if (AvailableBasemaps is null) { _ = _controller.LoadFromDefaultPortal(); } - else - { - _controller.AvailableBasemaps = AvailableBasemaps; - } - _controller.IsLoading = false; } private void HandleControllerPropertyChanged(object? sender, PropertyChangedEventArgs e) diff --git a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs index f95807278..a2f7f797c 100644 --- a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs +++ b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs @@ -246,7 +246,8 @@ internal bool EqualsBasemap(Basemap? other) return false; } - return other == Basemap || other.Name == Basemap?.Name + return other == Basemap || (Basemap.BaseLayers.Count == other.BaseLayers.Count + && Basemap.BaseLayers.All(layer => other?.BaseLayers.FirstOrDefault(l => l.Name == layer.Name) is not null)) || (other.Item?.ItemId != null && other.Item?.ItemId == Basemap?.Item?.ItemId) || (other.Uri != null && other.Uri == Basemap?.Uri); } From 63a48bdf5c85df973b8aa6503605983b8a2f81fa Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Wed, 12 Jun 2024 10:27:46 -0700 Subject: [PATCH 3/9] Refactor BasemapGalleryItem equality logic --- .../UI/Controls/BasemapGallery/BasemapGalleryItem.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs index a2f7f797c..5123b432d 100644 --- a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs +++ b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs @@ -246,10 +246,10 @@ internal bool EqualsBasemap(Basemap? other) return false; } - return other == Basemap || (Basemap.BaseLayers.Count == other.BaseLayers.Count - && Basemap.BaseLayers.All(layer => other?.BaseLayers.FirstOrDefault(l => l.Name == layer.Name) is not null)) - || (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) + || (Basemap?.BaseLayers.Count == other.BaseLayers.Count + && Basemap.BaseLayers.All(layer => other?.BaseLayers.FirstOrDefault(l => l.Name == layer.Name) is not null)); } /// From 7d2de1488e0abf87e6a9000fc6f9a9a1daba739b Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Wed, 12 Jun 2024 11:17:57 -0700 Subject: [PATCH 4/9] Add cancellation support to BasemapGallery --- .../BasemapGallery/BasemapGallery.cs | 19 ++++++++++++++++--- .../BasemapGallery/BasemapGallery.Windows.cs | 13 +++++++++++-- .../BasemapGalleryController.cs | 10 +++++----- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs index 626d59ca4..7597dea50 100644 --- a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs +++ b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs @@ -26,10 +26,13 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui; /// If connected to a GeoView, changing the basemap selection will change the connected Map or Scene's basemap. /// Only basemaps whose spatial reference matches the map or scene's spatial reference can be selected for display. /// +#pragma warning disable CA1001 // Types that own disposable fields should be disposable public partial class BasemapGallery +#pragma warning restore CA1001 // Types that own disposable fields should be disposable { private CollectionView? _listView; private readonly BasemapGalleryController _controller; + private CancellationTokenSource? _loadCancellationTokenSource; /// /// Initializes a new instance of the class. @@ -44,11 +47,17 @@ public BasemapGallery() Loaded += BasemapGallery_Loaded; } - private void BasemapGallery_Loaded(object? sender, EventArgs e) + private async void BasemapGallery_Loaded(object? sender, EventArgs e) { if (AvailableBasemaps is null) { - _ = _controller.LoadFromDefaultPortal(); + _loadCancellationTokenSource = new CancellationTokenSource(); + try + { + await _controller.LoadFromDefaultPortal(_loadCancellationTokenSource.Token); + } + catch (OperationCanceledException) + { } } } @@ -193,7 +202,11 @@ public GeoModel? GeoModel public IList? AvailableBasemaps { get => GetValue(AvailableBasemapsProperty) as IList; - set => SetValue(AvailableBasemapsProperty, value); + set + { + SetValue(AvailableBasemapsProperty, value); + _loadCancellationTokenSource?.Cancel(); + } } /// diff --git a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs index 2240b933c..dd6cc8258 100644 --- a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs +++ b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs @@ -31,9 +31,12 @@ namespace Esri.ArcGISRuntime.Toolkit.UI.Controls /// If connected to a GeoView, changing the basemap selection will change the connected Map or Scene's basemap. /// Only basemaps whose spatial reference matches the map or scene's spatial reference can be selected for display. /// +#pragma warning disable CA1001 // Types that own disposable fields should be disposable public partial class BasemapGallery : Control +#pragma warning restore CA1001 // Types that own disposable fields should be disposable { private readonly BasemapGalleryController _controller; + private CancellationTokenSource? _loadCancellationTokenSource; /// /// Initializes a new instance of the class. @@ -48,11 +51,17 @@ public BasemapGallery() Loaded += BasemapGallery_Loaded; } - private void BasemapGallery_Loaded(object? sender, RoutedEventArgs e) + private async void BasemapGallery_Loaded(object? sender, RoutedEventArgs e) { if (AvailableBasemaps is null) { - _ = _controller.LoadFromDefaultPortal(); + _loadCancellationTokenSource = new CancellationTokenSource(); + try + { + await _controller.LoadFromDefaultPortal(_loadCancellationTokenSource.Token); + } + catch (OperationCanceledException) + { } } } diff --git a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs index c367ece8a..3e8b0ab2d 100644 --- a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs +++ b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs @@ -224,12 +224,12 @@ private void HandleSelectedBasemapChanged() } } - public async Task LoadFromDefaultPortal() + public async Task LoadFromDefaultPortal(CancellationToken cancellationToken = default) { IsLoading = true; try { - AvailableBasemaps = await PopulateFromDefaultList(); + AvailableBasemaps = await PopulateFromDefaultList(cancellationToken); } finally { @@ -305,11 +305,11 @@ private static async Task BasemapIsActuallyNotABasemap(Basemap input) return listOfBasemaps; } - private static async Task> PopulateFromDefaultList() + private static async Task> 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(); From 4f0fa2da19fc8a6c255b5f9cabba4a0a09db2056 Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Wed, 12 Jun 2024 11:28:19 -0700 Subject: [PATCH 5/9] Unsubscribe from Loaded event in BasemapGallery --- src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs | 3 +++ .../BasemapGallery/BasemapGallery.Windows.cs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs index 7597dea50..3e28f8767 100644 --- a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs +++ b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs @@ -49,6 +49,9 @@ public BasemapGallery() 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) { _loadCancellationTokenSource = new CancellationTokenSource(); diff --git a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs index dd6cc8258..93edb92d2 100644 --- a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs +++ b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs @@ -53,6 +53,9 @@ public BasemapGallery() 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) { _loadCancellationTokenSource = new CancellationTokenSource(); From 5ce9d46a54f62746811edf396ac205a48cc2f194 Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Wed, 12 Jun 2024 17:02:46 -0700 Subject: [PATCH 6/9] Refactor basemap loading cancellation logic --- .../BasemapGallery/BasemapGallery.cs | 17 ++--------------- .../BasemapGallery/BasemapGallery.Windows.cs | 11 +---------- .../BasemapGallery/BasemapGalleryController.cs | 11 +++++++++-- 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs index 3e28f8767..6cd47dc83 100644 --- a/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs +++ b/src/Toolkit/Toolkit.Maui/BasemapGallery/BasemapGallery.cs @@ -26,13 +26,10 @@ namespace Esri.ArcGISRuntime.Toolkit.Maui; /// If connected to a GeoView, changing the basemap selection will change the connected Map or Scene's basemap. /// Only basemaps whose spatial reference matches the map or scene's spatial reference can be selected for display. /// -#pragma warning disable CA1001 // Types that own disposable fields should be disposable public partial class BasemapGallery -#pragma warning restore CA1001 // Types that own disposable fields should be disposable { private CollectionView? _listView; private readonly BasemapGalleryController _controller; - private CancellationTokenSource? _loadCancellationTokenSource; /// /// Initializes a new instance of the class. @@ -54,13 +51,7 @@ private async void BasemapGallery_Loaded(object? sender, EventArgs e) if (AvailableBasemaps is null) { - _loadCancellationTokenSource = new CancellationTokenSource(); - try - { - await _controller.LoadFromDefaultPortal(_loadCancellationTokenSource.Token); - } - catch (OperationCanceledException) - { } + await _controller.LoadFromDefaultPortal(); } } @@ -205,11 +196,7 @@ public GeoModel? GeoModel public IList? AvailableBasemaps { get => GetValue(AvailableBasemapsProperty) as IList; - set - { - SetValue(AvailableBasemapsProperty, value); - _loadCancellationTokenSource?.Cancel(); - } + set => SetValue(AvailableBasemapsProperty, value); } /// diff --git a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs index 93edb92d2..66ecfb5f8 100644 --- a/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs +++ b/src/Toolkit/Toolkit.UI.Controls/BasemapGallery/BasemapGallery.Windows.cs @@ -31,12 +31,9 @@ namespace Esri.ArcGISRuntime.Toolkit.UI.Controls /// If connected to a GeoView, changing the basemap selection will change the connected Map or Scene's basemap. /// Only basemaps whose spatial reference matches the map or scene's spatial reference can be selected for display. /// -#pragma warning disable CA1001 // Types that own disposable fields should be disposable public partial class BasemapGallery : Control -#pragma warning restore CA1001 // Types that own disposable fields should be disposable { private readonly BasemapGalleryController _controller; - private CancellationTokenSource? _loadCancellationTokenSource; /// /// Initializes a new instance of the class. @@ -58,13 +55,7 @@ private async void BasemapGallery_Loaded(object? sender, RoutedEventArgs e) if (AvailableBasemaps is null) { - _loadCancellationTokenSource = new CancellationTokenSource(); - try - { - await _controller.LoadFromDefaultPortal(_loadCancellationTokenSource.Token); - } - catch (OperationCanceledException) - { } + await _controller.LoadFromDefaultPortal(); } } diff --git a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs index 3e8b0ab2d..6d47a1e5b 100644 --- a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs +++ b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryController.cs @@ -29,7 +29,9 @@ 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; @@ -37,6 +39,7 @@ internal class BasemapGalleryController : INotifyPropertyChanged private GeoModel? _geoModel; private BasemapGalleryItem? _selectedBasemap; private bool _isLoading; + private CancellationTokenSource? _loadCancellationTokenSource; public bool IsLoading { @@ -72,6 +75,7 @@ public IList? AvailableBasemaps HandleAvailableBasemapsChanged(); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AvailableBasemaps))); + _loadCancellationTokenSource?.Cancel(); } } } @@ -224,13 +228,16 @@ private void HandleSelectedBasemapChanged() } } - public async Task LoadFromDefaultPortal(CancellationToken cancellationToken = default) + public async Task LoadFromDefaultPortal() { IsLoading = true; + _loadCancellationTokenSource = new CancellationTokenSource(); try { - AvailableBasemaps = await PopulateFromDefaultList(cancellationToken); + AvailableBasemaps = await PopulateFromDefaultList(_loadCancellationTokenSource.Token); } + catch (OperationCanceledException) + { } finally { IsLoading = false; From e074909380a0c033fec166ae13158963b832fb60 Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Thu, 13 Jun 2024 12:10:59 -0700 Subject: [PATCH 7/9] Refactor BasemapGalleryItem Equals logic --- .../BasemapGallery/BasemapGalleryItem.cs | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs index 5123b432d..aa7aba6dd 100644 --- a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs +++ b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs @@ -248,8 +248,36 @@ internal bool EqualsBasemap(Basemap? other) return other == Basemap || (other.Item?.ItemId != null && other.Item?.ItemId == Basemap?.Item?.ItemId) || (other.Uri != null && other.Uri == Basemap?.Uri) - || (Basemap?.BaseLayers.Count == other.BaseLayers.Count - && Basemap.BaseLayers.All(layer => other?.BaseLayers.FirstOrDefault(l => l.Name == layer.Name) is not null)); + || 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)); + } + + 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 + { + ArcGISSceneLayer sceneLayer => sceneLayer.Source == ((ArcGISSceneLayer)layer2).Source, + ArcGISTiledLayer tiledLayer => tiledLayer.Source == ((ArcGISTiledLayer)layer2).Source, + ArcGISVectorTiledLayer vectorTiledLayer => vectorTiledLayer.Source == ((ArcGISVectorTiledLayer)layer2).Source, + _ => false, + }; } /// From dd88cf8941b267ecba8c2cdb1f7dc801a9bdeea8 Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede Date: Fri, 14 Jun 2024 16:51:00 -0700 Subject: [PATCH 8/9] Enhance layer comparison in BasemapGalleryItem --- .../BasemapGallery/BasemapGalleryItem.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs index aa7aba6dd..c0fd3049d 100644 --- a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs +++ b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs @@ -273,9 +273,25 @@ private static bool LayerEquals(Layer layer1, Layer layer2) return layer1 switch { + AnnotationLayer annotationLayer => annotationLayer.Source == ((AnnotationLayer)layer2).Source, + ArcGISMapImageLayer arcGISMapImageLayer => arcGISMapImageLayer.Source == ((ArcGISMapImageLayer)layer2).Source, ArcGISSceneLayer sceneLayer => sceneLayer.Source == ((ArcGISSceneLayer)layer2).Source, ArcGISTiledLayer tiledLayer => tiledLayer.Source == ((ArcGISTiledLayer)layer2).Source, ArcGISVectorTiledLayer vectorTiledLayer => vectorTiledLayer.Source == ((ArcGISVectorTiledLayer)layer2).Source, + BingMapsLayer imageServiceLayer => imageServiceLayer.Portal?.Uri == ((BingMapsLayer)layer2).Portal?.Uri, + DimensionLayer dimensionLayer => dimensionLayer.Source == ((DimensionLayer)layer2).Source, + FeatureCollectionLayer featureCollectionLayer => featureCollectionLayer.FeatureCollection == ((FeatureCollectionLayer)layer2).FeatureCollection, + GroupLayer groupLayer => groupLayer.Layers.Count == ((GroupLayer)layer2).Layers.Count && LayersEqual(groupLayer.Layers, ((GroupLayer)layer2).Layers), + IntegratedMeshLayer integratedMeshLayer => integratedMeshLayer.Source == ((IntegratedMeshLayer)layer2).Source, + KmlLayer kmlLayer => kmlLayer.Dataset?.Source == ((KmlLayer)layer2).Dataset?.Source, + Ogc3DTilesLayer ogc3DTilesLayer => ogc3DTilesLayer.Source == ((Ogc3DTilesLayer)layer2).Source, + OpenStreetMapLayer openStreetMapLayer => true, // OpenStreetMap layers are considered equal if types match + PointCloudLayer pointCloudLayer => pointCloudLayer.Source == ((PointCloudLayer)layer2).Source, + WebTiledLayer webTiledLayer => webTiledLayer.TemplateUri == ((WebTiledLayer)layer2).TemplateUri, + ServiceImageTiledLayer serviceImageTiledLayer => serviceImageTiledLayer.TileInfo == ((ServiceImageTiledLayer)layer2).TileInfo && serviceImageTiledLayer.FullExtent == ((ServiceImageTiledLayer)layer2).FullExtent, + SubtypeFeatureLayer subtypeFeatureLayer => subtypeFeatureLayer.FeatureTable == ((SubtypeFeatureLayer)layer2).FeatureTable, + WmsLayer wmsLayer => wmsLayer.Source == ((WmsLayer)layer2).Source, + WmtsLayer wmtsLayer => wmtsLayer.Source == ((WmtsLayer)layer2).Source, _ => false, }; } From 94dbc0bdb9151cdf18239f3d1bd0c8391226bc08 Mon Sep 17 00:00:00 2001 From: Prathamesh Narkhede <55591622+prathameshnarkhede@users.noreply.github.com> Date: Mon, 17 Jun 2024 13:11:34 -0700 Subject: [PATCH 9/9] Update src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs --- .../Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs index c0fd3049d..d15258827 100644 --- a/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs +++ b/src/Toolkit/Toolkit/UI/Controls/BasemapGallery/BasemapGalleryItem.cs @@ -262,8 +262,7 @@ private static bool AreBasemapsEqualByLayers(Basemap? basemap1, Basemap? basemap 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)); + && layers1.Zip(layers2, LayerEquals).All(equal => equal); } private static bool LayerEquals(Layer layer1, Layer layer2)