Skip to content

Commit

Permalink
Fix BasemapGallery for Forms UWP release (#384)
Browse files Browse the repository at this point in the history
* Fix BasemapGallery for Forms UWP release

* Code quality and warnings/messages

* Quality fixes from code review

* Add BasemapGallery to Toolbox

* Behavior improvements

* Revert unnecessary style changes

* Revert additional unnecessary changes
  • Loading branch information
nCastle1 authored Dec 1, 2021
1 parent 42b727f commit 0cad5cd
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 290 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ There are two ways to add Toolkit to your project:
> See [List of controls](https://esri.github.io/arcgis-toolkit-dotnet/controls.html) for a full list of controls with screenshots
- **[ARSceneView](https://esri.github.io/arcgis-toolkit-dotnet/ar.html)**: Part of the AR Toolkit, enables integration of GIS content and ARKit/ARCore.
- **[BasemapGallery](https://esri.github.io/arcgis-toolkit-dotnet/basemap-gallery.htm)**: Shows basemaps, either from a Portal or a custom collection; applies the selected basemap to the connected GeoModel.
- **[BasemapGallery](https://esri.github.io/arcgis-toolkit-dotnet/basemap-gallery.html)**: Shows basemaps, either from a Portal or a custom collection; applies the selected basemap to the connected GeoModel.
- **[Bookmarks](https://esri.github.io/arcgis-toolkit-dotnet/bookmarks-view.html)**: Shows bookmarks, from a map, scene, or a list; navigates the associated MapView/SceneView when a bookmark is selected.
- **Compass**: Shows a compass direction when the map is rotated. Auto-hides when the map points north up.
- **FeatureDataField**: Displays and optionally allows editing of a single field attribute of a feature.
Expand Down
112 changes: 35 additions & 77 deletions src/Toolkit.Forms/BasemapGallery/BasemapGallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
// ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.Toolkit.UI;
using Esri.ArcGISRuntime.Toolkit.UI.Controls;
using Xamarin.Forms;

namespace Esri.ArcGISRuntime.Toolkit.Xamarin.Forms
Expand All @@ -34,23 +31,46 @@ namespace Esri.ArcGISRuntime.Toolkit.Xamarin.Forms
/// 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.
/// </remarks>
public partial class BasemapGallery : IBasemapGallery
public partial class BasemapGallery
{
private CollectionView? _listView;
private BasemapGalleryController _controller;
private readonly BasemapGalleryController _controller;

/// <summary>
/// Initializes a new instance of the <see cref="BasemapGallery"/> class.
/// </summary>
public BasemapGallery()
{
_controller = new BasemapGalleryController(this);
_controller = new BasemapGalleryController();
_controller.PropertyChanged += HandleControllerPropertyChanged;
ListItemTemplate = DefaultListDataTemplate;
GridItemTemplate = DefaultGridDataTemplate;
ControlTemplate = DefaultControlTemplate;
_ = _controller.LoadFromDefaultPortal();
}

private void HandleControllerPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(BasemapGalleryController.AvailableBasemaps):
AvailableBasemaps = _controller.AvailableBasemaps;
break;
case nameof(BasemapGalleryController.IsLoading):
_loadingScrim?.SetValue(View.IsVisibleProperty, _controller.IsLoading);
break;
case nameof(BasemapGalleryController.SelectedBasemap):
SelectedBasemap = _controller.SelectedBasemap;
_listView?.SetValue(CollectionView.SelectedItemProperty, _controller.SelectedBasemap);
if (_controller.SelectedBasemap != null)
{
BasemapSelected?.Invoke(this, _controller.SelectedBasemap);
}

break;
}
}

private CollectionView? ListView
{
get => _listView;
Expand All @@ -67,7 +87,6 @@ private CollectionView? ListView

if (_listView != null)
{
_controller.HandleListViewChanged();
_listView.SelectionChanged += ListViewSelectionChanged;
HandleTemplateChange(Width);
}
Expand All @@ -79,7 +98,7 @@ private static void SelectedBasemapChanged(BindableObject sender, object oldValu
{
if (sender is BasemapGallery gallery)
{
gallery._controller.HandleSelectedBasemapChanged();
gallery._controller.SelectedBasemap = newValue as BasemapGalleryItem;
}
}

Expand All @@ -90,25 +109,10 @@ private static void GeoModelChanged(BindableObject sender, object oldValue, obje
{
if (sender is BasemapGallery gallery)
{
if (oldValue is GeoModel oldModel)
{
oldModel.PropertyChanged -= gallery.GeoModelPropertyChanged;
}

gallery._controller.HandleGeoModelChanged();

if (newValue is GeoModel newModel)
{
newModel.PropertyChanged += gallery.GeoModelPropertyChanged;
}
gallery._controller.GeoModel = newValue as GeoModel;
}
}

private void GeoModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
_controller.HandleGeoModelPropertyChanged(e.PropertyName);
}

private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ListView == null)
Expand All @@ -118,13 +122,13 @@ private void ListViewSelectionChanged(object sender, SelectionChangedEventArgs e

if (e.CurrentSelection.Count == 0)
{
_controller.HandleListViewSelectionChanged(null);
SelectedBasemap = null;
}
else if (e.CurrentSelection.FirstOrDefault() is BasemapGalleryItem selectedItem)
{
if (selectedItem.IsValid)
{
_controller.HandleListViewSelectionChanged(e.CurrentSelection.FirstOrDefault() as BasemapGalleryItem);
SelectedBasemap = selectedItem;
}
}
}
Expand All @@ -136,33 +140,22 @@ private static void PortalChanged(BindableObject sender, object oldValue, object
{
if (sender is BasemapGallery gallery)
{
_ = gallery._controller.HandlePortalChanged();
gallery._controller.Portal = newValue as ArcGISPortal;
}
}

private static void AvailableBasemapsChanged(BindableObject sender, object oldValue, object newValue)
{
if (sender is BasemapGallery gallery)
{
if (oldValue is ObservableCollection<BasemapGalleryItem> oldAvailableBasemaps)
gallery.ListView?.SetValue(CollectionView.ItemsSourceProperty, newValue);
if (newValue != gallery._controller.AvailableBasemaps)
{
oldAvailableBasemaps.CollectionChanged -= gallery.AvailableBasemapsCollectionChanged;
}

gallery._controller.HandleAvailableBasemapsChanged();

if (newValue is ObservableCollection<BasemapGalleryItem> newAvailableBasemaps)
{
newAvailableBasemaps.CollectionChanged += gallery.AvailableBasemapsCollectionChanged;
gallery._controller.AvailableBasemaps = newValue as IList<BasemapGalleryItem>;
}
}
}

private void AvailableBasemapsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
_controller.HandleAvailableBasemapsCollectionChanged(e);
}

/// <summary>
/// Event raised when a basemap is selected.
/// </summary>
Expand All @@ -175,7 +168,7 @@ private void AvailableBasemapsCollectionChanged(object sender, NotifyCollectionC
/// </summary>
public ArcGISPortal? Portal
{
get => (ArcGISPortal)GetValue(PortalProperty);
get => GetValue(PortalProperty) as ArcGISPortal;
set => SetValue(PortalProperty, value);
}

Expand Down Expand Up @@ -239,40 +232,5 @@ public BasemapGalleryItem? SelectedBasemap

#endregion Bindable Properties

#region Controller Callbacks

void IBasemapGallery.SetListViewSource(IList<BasemapGalleryItem>? newSource)
{
if (ListView != null)
{
ListView.ItemsSource = newSource;
}
}

void IBasemapGallery.SetListViewSelection(BasemapGalleryItem? item)
{
if (ListView != null)
{
ListView.SelectedItem = item;
}
}

void IBasemapGallery.NotifyBasemapSelected(BasemapGalleryItem item)
{
BasemapSelected?.Invoke(this, item);
}

void IBasemapGallery.SetIsLoading(bool isLoading)
{
if (isLoading && _loadingScrim != null)
{
_loadingScrim.IsVisible = true;
}
else if (_loadingScrim != null)
{
_loadingScrim.IsVisible = false;
}
}
#endregion Controller Callbacks
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class ByteArrayToImageSourceConverter : IValueConverter
/// <summary>
/// Converts a byte array to an image source for display in Xamarin.Forms.
/// </summary>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is byte[] rawBuffer)
{
Expand Down
1 change: 0 additions & 1 deletion src/Toolkit/Toolkit/Esri.ArcGISRuntime.Toolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<Compile Include="UI\ILayerContentItem.cs" />
<Compile Include="UI\Controls\BasemapGallery\BasemapGalleryViewStyle.cs" />
<Compile Include="UI\Controls\BasemapGallery\BasemapGalleryItem.cs" />
<Compile Include="UI\Controls\BasemapGallery\IBasemapGallery.cs" />
<Compile Include="UI\Controls\BasemapGallery\BasemapGalleryController.cs" />
<Compile Include="Internal\StringExtensions.cs" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 0cad5cd

Please sign in to comment.